File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.372: download - view: text, annotated - select for diffs
Thu Sep 17 00:35:04 2020 UTC (3 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6935

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.372 2020/09/17 00:35:04 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: #
   29: ###############################################################
   30: ###############################################################
   31: 
   32: =pod
   33: 
   34: =head1 NAME
   35: 
   36: Apache::domainprefs.pm
   37: 
   38: =head1 SYNOPSIS
   39: 
   40: Handles configuration of a LON-CAPA domain.  
   41: 
   42: This is part of the LearningOnline Network with CAPA project
   43: described at http://www.lon-capa.org.
   44: 
   45: 
   46: =head1 OVERVIEW
   47: 
   48: Each institution using LON-CAPA will typically have a single domain designated 
   49: for use by individuals affiliated with the institution.  Accordingly, each domain
   50: may define a default set of logos and a color scheme which can be used to "brand"
   51: the LON-CAPA instance. In addition, an institution will typically have a language
   52: and timezone which are used for the majority of courses.
   53: 
   54: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   55: host of other domain-wide settings which determine the types of functionality
   56: available to users and courses in the domain.
   57: 
   58: There is also a mechanism to configure cataloging of courses in the domain, and
   59: controls on the operation of automated processes which govern such things as
   60: roster updates, user directory updates and processing of course requests.
   61: 
   62: The domain coordination manual which is built dynamically on install/update of 
   63: LON-CAPA from the relevant help items provides more information about domain 
   64: configuration.
   65: 
   66: Most of the domain settings are stored in the configuration.db GDBM file which is
   67: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   68: where $dom is the domain.  The configuration.db stores settings in a number of 
   69: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   70: the domain as files (e.g., image files for logos etc., or plain text files for
   71: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   72: session hosted on the primary library server in the domain, as these files are 
   73: stored in author space belonging to a special $dom-domainconfig user.   
   74: 
   75: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   76: the current settings, and provides an interface to make modifications.
   77: 
   78: =head1 SUBROUTINES
   79: 
   80: =over
   81: 
   82: =item print_quotas()
   83: 
   84: Inputs: 4 
   85: 
   86: $dom,$settings,$rowtotal,$action.
   87: 
   88: $dom is the domain, $settings is a reference to a hash of current settings for
   89: the current context, $rowtotal is a reference to the scalar used to record the 
   90: number of rows displayed on the page, and $action is the context (quotas, 
   91: requestcourses or requestauthor).
   92: 
   93: The print_quotas routine was orginally created to display/store information
   94: about default quota sizes for portfolio spaces for the different types of 
   95: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   96: but is now also used to manage availability of user tools: 
   97: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   98: used by course owners to request creation of a course, and to display/store
   99: default quota sizes for Authoring Spaces.
  100: 
  101: Outputs: 1
  102: 
  103: $datatable  - HTML containing form elements which allow settings to be changed. 
  104: 
  105: In the case of course requests, radio buttons are displayed for each institutional
  106: affiliate type (and also default, and _LC_adv) for each of the course types 
  107: (official, unofficial, community, textbook, placement, and lti).  
  108: In each case the radio buttons allow the selection of one of four values:  
  109: 
  110: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  111: which have the following effects:
  112: 
  113: 0
  114: 
  115: =over
  116: 
  117: - course requests are not allowed for this course types/affiliation
  118: 
  119: =back
  120: 
  121: approval 
  122: 
  123: =over 
  124: 
  125: - course requests must be approved by a Doman Coordinator in the 
  126: course's domain
  127: 
  128: =back
  129: 
  130: validate 
  131: 
  132: =over
  133: 
  134: - an institutional validation (e.g., check requestor is instructor
  135: of record) needs to be passed before the course will be created.  The required
  136: validation is in localenroll.pm on the primary library server for the course 
  137: domain.
  138: 
  139: =back
  140: 
  141: autolimit 
  142: 
  143: =over
  144:  
  145: - course requests will be processed automatically up to a limit of
  146: N requests for the course type for the particular requestor.
  147: If N is undefined, there is no limit to the number of course requests
  148: which a course owner may submit and have processed automatically. 
  149: 
  150: =back
  151: 
  152: =item modify_quotas() 
  153: 
  154: =back
  155: 
  156: =cut
  157: 
  158: package Apache::domainprefs;
  159: 
  160: use strict;
  161: use Apache::Constants qw(:common :http);
  162: use Apache::lonnet;
  163: use Apache::loncommon();
  164: use Apache::lonhtmlcommon();
  165: use Apache::lonlocal;
  166: use Apache::lonmsg();
  167: use Apache::lonconfigsettings;
  168: use Apache::lonuserutils();
  169: use Apache::loncoursequeueadmin();
  170: use LONCAPA qw(:DEFAULT :match);
  171: use LONCAPA::Enrollment;
  172: use LONCAPA::lonauthcgi();
  173: use LONCAPA::SSL;
  174: use File::Copy;
  175: use Locale::Language;
  176: use DateTime::TimeZone;
  177: use DateTime::Locale;
  178: use Time::HiRes qw( sleep );
  179: 
  180: my $registered_cleanup;
  181: my $modified_urls;
  182: 
  183: sub handler {
  184:     my $r=shift;
  185:     if ($r->header_only) {
  186:         &Apache::loncommon::content_type($r,'text/html');
  187:         $r->send_http_header;
  188:         return OK;
  189:     }
  190: 
  191:     my $context = 'domain';
  192:     my $dom = $env{'request.role.domain'};
  193:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  194:     if (&Apache::lonnet::allowed('mau',$dom)) {
  195:         &Apache::loncommon::content_type($r,'text/html');
  196:         $r->send_http_header;
  197:     } else {
  198:         $env{'user.error.msg'}=
  199:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  200:         return HTTP_NOT_ACCEPTABLE;
  201:     }
  202: 
  203:     $registered_cleanup=0;
  204:     @{$modified_urls}=();
  205: 
  206:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  207:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  208:                                             ['phase','actions']);
  209:     my $phase = 'pickactions';
  210:     if ( exists($env{'form.phase'}) ) {
  211:         $phase = $env{'form.phase'};
  212:     }
  213:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  214:     my %domconfig =
  215:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  216:                 'quotas','autoenroll','autoupdate','autocreate',
  217:                 'directorysrch','usercreation','usermodification',
  218:                 'contacts','defaults','scantron','coursecategories',
  219:                 'serverstatuses','requestcourses','helpsettings',
  220:                 'coursedefaults','usersessions','loadbalancing',
  221:                 'requestauthor','selfenrollment','inststatus',
  222:                 'ltitools','ssl','trust','lti','privacy','passwords',
  223:                 'proctoring'],$dom);
  224:     my %encconfig =
  225:         &Apache::lonnet::get_dom('encconfig',['ltitools','lti','proctoring'],$dom);
  226:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
  227:         if (ref($encconfig{'ltitools'}) eq 'HASH') {
  228:             foreach my $id (keys(%{$domconfig{'ltitools'}})) {
  229:                 if ((ref($domconfig{'ltitools'}{$id}) eq 'HASH') &&
  230:                     (ref($encconfig{'ltitools'}{$id}) eq 'HASH')) {
  231:                     foreach my $item ('key','secret') {
  232:                         $domconfig{'ltitools'}{$id}{$item} = $encconfig{'ltitools'}{$id}{$item};
  233:                     }
  234:                 }
  235:             }
  236:         }
  237:     }
  238:     if (ref($domconfig{'lti'}) eq 'HASH') {
  239:         if (ref($encconfig{'lti'}) eq 'HASH') {
  240:             foreach my $id (keys(%{$domconfig{'lti'}})) {
  241:                 if ((ref($domconfig{'lti'}{$id}) eq 'HASH') &&
  242:                     (ref($encconfig{'lti'}{$id}) eq 'HASH')) {
  243:                     foreach my $item ('key','secret') {
  244:                         $domconfig{'lti'}{$id}{$item} = $encconfig{'lti'}{$id}{$item};
  245:                     }
  246:                 }
  247:             }
  248:         }
  249:     }
  250:     if (ref($domconfig{'proctoring'}) eq 'HASH') {
  251:         if (ref($encconfig{'proctoring'}) eq 'HASH') {
  252:             foreach my $provider (keys(%{$domconfig{'proctoring'}})) {
  253:                 if ((ref($domconfig{'proctoring'}{$provider}) eq 'HASH') &&
  254:                     (ref($encconfig{'proctoring'}{$provider}) eq 'HASH')) {
  255:                     foreach my $item ('key','secret') {
  256:                         $domconfig{'proctoring'}{$provider}{$item} = $encconfig{'proctoring'}{$provider}{$item};
  257:                     }
  258:                 }
  259:             }
  260:         }
  261:     }
  262:     my @prefs_order = ('rolecolors','login','defaults','passwords','quotas','autoenroll',
  263:                        'autoupdate','autocreate','directorysrch','contacts','privacy',
  264:                        'usercreation','selfcreation','usermodification','scantron',
  265:                        'requestcourses','requestauthor','coursecategories',
  266:                        'serverstatuses','helpsettings','coursedefaults',
  267:                        'ltitools','proctoring','selfenrollment','usersessions','ssl',
  268:                        'trust','lti');
  269:     my %existing;
  270:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  271:         %existing = %{$domconfig{'loadbalancing'}};
  272:     }
  273:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  274:         push(@prefs_order,'loadbalancing');
  275:     }
  276:     my %prefs = (
  277:         'rolecolors' =>
  278:                    { text => 'Default color schemes',
  279:                      help => 'Domain_Configuration_Color_Schemes',
  280:                      header => [{col1 => 'Student Settings',
  281:                                  col2 => '',},
  282:                                 {col1 => 'Coordinator Settings',
  283:                                  col2 => '',},
  284:                                 {col1 => 'Author Settings',
  285:                                  col2 => '',},
  286:                                 {col1 => 'Administrator Settings',
  287:                                  col2 => '',}],
  288:                       print => \&print_rolecolors,
  289:                       modify => \&modify_rolecolors,
  290:                     },
  291:         'login' =>
  292:                     { text => 'Log-in page options',
  293:                       help => 'Domain_Configuration_Login_Page',
  294:                       header => [{col1 => 'Log-in Page Items',
  295:                                   col2 => '',},
  296:                                  {col1 => 'Log-in Help',
  297:                                   col2 => 'Value'},
  298:                                  {col1 => 'Custom HTML in document head',
  299:                                   col2 => 'Value'}],
  300:                       print => \&print_login,
  301:                       modify => \&modify_login,
  302:                     },
  303:         'defaults' => 
  304:                     { text => 'Default authentication/language/timezone/portal/types',
  305:                       help => 'Domain_Configuration_LangTZAuth',
  306:                       header => [{col1 => 'Setting',
  307:                                   col2 => 'Value'},
  308:                                  {col1 => 'Institutional user types',
  309:                                   col2 => 'Name displayed'}],
  310:                       print => \&print_defaults,
  311:                       modify => \&modify_defaults,
  312:                     },
  313:         'passwords' =>
  314:                     { text => 'Passwords (Internal authentication)',
  315:                       help => 'Domain_Configuration_Passwords',
  316:                       header => [{col1 => 'Resetting Forgotten Password',
  317:                                   col2 => 'Settings'},
  318:                                  {col1 => 'Encryption of Stored Passwords (Internal Auth)',
  319:                                   col2 => 'Settings'},
  320:                                  {col1 => 'Rules for LON-CAPA Passwords',
  321:                                   col2 => 'Settings'},
  322:                                  {col1 => 'Course Owner Changing Student Passwords',
  323:                                   col2 => 'Settings'}],
  324:                       print => \&print_passwords,
  325:                       modify => \&modify_passwords,
  326:                     },
  327:         'quotas' => 
  328:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  329:                       help => 'Domain_Configuration_Quotas',
  330:                       header => [{col1 => 'User affiliation',
  331:                                   col2 => 'Available tools',
  332:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  333:                       print => \&print_quotas,
  334:                       modify => \&modify_quotas,
  335:                     },
  336:         'autoenroll' =>
  337:                    { text => 'Auto-enrollment settings',
  338:                      help => 'Domain_Configuration_Auto_Enrollment',
  339:                      header => [{col1 => 'Configuration setting',
  340:                                  col2 => 'Value(s)'}],
  341:                      print => \&print_autoenroll,
  342:                      modify => \&modify_autoenroll,
  343:                    },
  344:         'autoupdate' => 
  345:                    { text => 'Auto-update settings',
  346:                      help => 'Domain_Configuration_Auto_Updates',
  347:                      header => [{col1 => 'Setting',
  348:                                  col2 => 'Value',},
  349:                                 {col1 => 'Setting',
  350:                                  col2 => 'Affiliation'},
  351:                                 {col1 => 'User population',
  352:                                  col2 => 'Updatable user data'}],
  353:                      print => \&print_autoupdate,
  354:                      modify => \&modify_autoupdate,
  355:                   },
  356:         'autocreate' => 
  357:                   { text => 'Auto-course creation settings',
  358:                      help => 'Domain_Configuration_Auto_Creation',
  359:                      header => [{col1 => 'Configuration Setting',
  360:                                  col2 => 'Value',}],
  361:                      print => \&print_autocreate,
  362:                      modify => \&modify_autocreate,
  363:                   },
  364:         'directorysrch' => 
  365:                   { text => 'Directory searches',
  366:                     help => 'Domain_Configuration_InstDirectory_Search',
  367:                     header => [{col1 => 'Institutional Directory Setting',
  368:                                 col2 => 'Value',},
  369:                                {col1 => 'LON-CAPA Directory Setting',
  370:                                 col2 => 'Value',}],
  371:                     print => \&print_directorysrch,
  372:                     modify => \&modify_directorysrch,
  373:                   },
  374:         'contacts' =>
  375:                   { text => 'E-mail addresses and helpform',
  376:                     help => 'Domain_Configuration_Contact_Info',
  377:                     header => [{col1 => 'Default e-mail addresses',
  378:                                 col2 => 'Value',},
  379:                                {col1 => 'Recipient(s) for notifications',
  380:                                 col2 => 'Value',},
  381:                                {col1 => 'Nightly status check e-mail',
  382:                                 col2 => 'Settings',},
  383:                                {col1 => 'Ask helpdesk form settings',
  384:                                 col2 => 'Value',},],
  385:                     print => \&print_contacts,
  386:                     modify => \&modify_contacts,
  387:                   },
  388:         'usercreation' => 
  389:                   { text => 'User creation',
  390:                     help => 'Domain_Configuration_User_Creation',
  391:                     header => [{col1 => 'Format rule type',
  392:                                 col2 => 'Format rules in force'},
  393:                                {col1 => 'User account creation',
  394:                                 col2 => 'Usernames which may be created',},
  395:                                {col1 => 'Context',
  396:                                 col2 => 'Assignable authentication types'}],
  397:                     print => \&print_usercreation,
  398:                     modify => \&modify_usercreation,
  399:                   },
  400:         'selfcreation' => 
  401:                   { text => 'Users self-creating accounts',
  402:                     help => 'Domain_Configuration_Self_Creation', 
  403:                     header => [{col1 => 'Self-creation with institutional username',
  404:                                 col2 => 'Enabled?'},
  405:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  406:                                 col2 => 'Information user can enter'},
  407:                                {col1 => 'Self-creation with e-mail verification',
  408:                                 col2 => 'Settings'}],
  409:                     print => \&print_selfcreation,
  410:                     modify => \&modify_selfcreation,
  411:                   },
  412:         'usermodification' =>
  413:                   { text => 'User modification',
  414:                     help => 'Domain_Configuration_User_Modification',
  415:                     header => [{col1 => 'Target user has role',
  416:                                 col2 => 'User information updatable in author context'},
  417:                                {col1 => 'Target user has role',
  418:                                 col2 => 'User information updatable in course context'}],
  419:                     print => \&print_usermodification,
  420:                     modify => \&modify_usermodification,
  421:                   },
  422:         'scantron' =>
  423:                   { text => 'Bubblesheet format',
  424:                     help => 'Domain_Configuration_Scantron_Format',
  425:                     header => [ {col1 => 'Bubblesheet format file',
  426:                                  col2 => ''},
  427:                                 {col1 => 'Bubblesheet data upload formats',
  428:                                  col2 => 'Settings'}],
  429:                     print => \&print_scantron,
  430:                     modify => \&modify_scantron,
  431:                   },
  432:         'requestcourses' => 
  433:                  {text => 'Request creation of courses',
  434:                   help => 'Domain_Configuration_Request_Courses',
  435:                   header => [{col1 => 'User affiliation',
  436:                               col2 => 'Availability/Processing of requests',},
  437:                              {col1 => 'Setting',
  438:                               col2 => 'Value'},
  439:                              {col1 => 'Available textbooks',
  440:                               col2 => ''},
  441:                              {col1 => 'Available templates',
  442:                               col2 => ''},
  443:                              {col1 => 'Validation (not official courses)',
  444:                               col2 => 'Value'},],
  445:                   print => \&print_quotas,
  446:                   modify => \&modify_quotas,
  447:                  },
  448:         'requestauthor' =>
  449:                  {text => 'Request Authoring Space',
  450:                   help => 'Domain_Configuration_Request_Author',
  451:                   header => [{col1 => 'User affiliation',
  452:                               col2 => 'Availability/Processing of requests',},
  453:                              {col1 => 'Setting',
  454:                               col2 => 'Value'}],
  455:                   print => \&print_quotas,
  456:                   modify => \&modify_quotas,
  457:                  },
  458:         'coursecategories' =>
  459:                   { text => 'Cataloging of courses/communities',
  460:                     help => 'Domain_Configuration_Cataloging_Courses',
  461:                     header => [{col1 => 'Catalog type/availability',
  462:                                 col2 => '',},
  463:                                {col1 => 'Category settings for standard catalog',
  464:                                 col2 => '',},
  465:                                {col1 => 'Categories',
  466:                                 col2 => '',
  467:                                }],
  468:                     print => \&print_coursecategories,
  469:                     modify => \&modify_coursecategories,
  470:                   },
  471:         'serverstatuses' =>
  472:                  {text   => 'Access to server status pages',
  473:                   help   => 'Domain_Configuration_Server_Status',
  474:                   header => [{col1 => 'Status Page',
  475:                               col2 => 'Other named users',
  476:                               col3 => 'Specific IPs',
  477:                             }],
  478:                   print => \&print_serverstatuses,
  479:                   modify => \&modify_serverstatuses,
  480:                  },
  481:         'helpsettings' =>
  482:                  {text   => 'Support settings',
  483:                   help   => 'Domain_Configuration_Help_Settings',
  484:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  485:                               col2 => 'Value'},
  486:                              {col1 => 'Helpdesk Roles',
  487:                               col2 => 'Settings'},],
  488:                   print  => \&print_helpsettings,
  489:                   modify => \&modify_helpsettings,
  490:                  },
  491:         'coursedefaults' => 
  492:                  {text => 'Course/Community defaults',
  493:                   help => 'Domain_Configuration_Course_Defaults',
  494:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  495:                               col2 => 'Value',},
  496:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  497:                               col2 => 'Value',},],
  498:                   print => \&print_coursedefaults,
  499:                   modify => \&modify_coursedefaults,
  500:                  },
  501:         'selfenrollment' => 
  502:                  {text   => 'Self-enrollment in Course/Community',
  503:                   help   => 'Domain_Configuration_Selfenrollment',
  504:                   header => [{col1 => 'Configuration Rights',
  505:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  506:                              {col1 => 'Defaults',
  507:                               col2 => 'Value'},
  508:                              {col1 => 'Self-enrollment validation (optional)',
  509:                               col2 => 'Value'},],
  510:                   print => \&print_selfenrollment,
  511:                   modify => \&modify_selfenrollment,
  512:                  },
  513:         'privacy' => 
  514:                  {text   => 'Availability of User Information',
  515:                   help   => 'Domain_Configuration_User_Privacy',
  516:                   header => [{col1 => 'Role assigned in different domain',
  517:                               col2 => 'Approval options'},
  518:                              {col1 => 'Role assigned in different domain to user of type',
  519:                               col2 => 'User information available in that domain'},
  520:                              {col1 => "Role assigned in user's domain",
  521:                               col2 => 'Information viewable by privileged user'},
  522:                              {col1 => "Role assigned in user's domain",
  523:                               col2 => 'Information viewable by unprivileged user'}],
  524:                   print => \&print_privacy,
  525:                   modify => \&modify_privacy,
  526:                  },
  527:         'usersessions' =>
  528:                  {text  => 'User session hosting/offloading',
  529:                   help  => 'Domain_Configuration_User_Sessions',
  530:                   header => [{col1 => 'Domain server',
  531:                               col2 => 'Servers to offload sessions to when busy'},
  532:                              {col1 => 'Hosting of users from other domains',
  533:                               col2 => 'Rules'},
  534:                              {col1 => "Hosting domain's own users elsewhere",
  535:                               col2 => 'Rules'}],
  536:                   print => \&print_usersessions,
  537:                   modify => \&modify_usersessions,
  538:                  },
  539:         'loadbalancing' =>
  540:                  {text  => 'Dedicated Load Balancer(s)',
  541:                   help  => 'Domain_Configuration_Load_Balancing',
  542:                   header => [{col1 => 'Balancers',
  543:                               col2 => 'Default destinations',
  544:                               col3 => 'User affiliation',
  545:                               col4 => 'Overrides'},
  546:                             ],
  547:                   print => \&print_loadbalancing,
  548:                   modify => \&modify_loadbalancing,
  549:                  },
  550:         'ltitools' => 
  551:                  {text => 'External Tools (LTI)',
  552:                   help => 'Domain_Configuration_LTI_Tools',
  553:                   header => [{col1 => 'Setting',
  554:                               col2 => 'Value',}],
  555:                   print => \&print_ltitools,
  556:                   modify => \&modify_ltitools,
  557:                  },
  558:         'proctoring' =>
  559:                  {text => 'Remote Proctoring Integration',
  560:                   help => 'Domain_Configuration_Proctoring',
  561:                   header => [{col1 => 'Name',
  562:                               col2 => 'Configuration'}],
  563:                   print => \&print_proctoring,
  564:                   modify => \&modify_proctoring,
  565:                  },
  566:         'ssl' =>
  567:                  {text  => 'LON-CAPA Network (SSL)',
  568:                   help  => 'Domain_Configuration_Network_SSL',
  569:                   header => [{col1 => 'Server',
  570:                               col2 => 'Certificate Status'},
  571:                              {col1 => 'Connections to other servers',
  572:                               col2 => 'Rules'},
  573:                              {col1 => 'Connections from other servers',
  574:                               col2 => 'Rules'},
  575:                              {col1 => "Replicating domain's published content",
  576:                               col2 => 'Rules'}],
  577:                   print => \&print_ssl,
  578:                   modify => \&modify_ssl,
  579:                  },
  580:         'trust' =>
  581:                  {text   => 'Trust Settings',
  582:                   help   => 'Domain_Configuration_Trust',
  583:                   header => [{col1 => "Access to this domain's content by others",
  584:                               col2 => 'Rules'},
  585:                              {col1 => "Access to other domain's content by this domain",
  586:                               col2 => 'Rules'},
  587:                              {col1 => "Enrollment in this domain's courses by others",
  588:                               col2 => 'Rules',},
  589:                              {col1 => "Co-author roles in this domain for others",
  590:                               col2 => 'Rules',},
  591:                              {col1 => "Co-author roles for this domain's users elsewhere",
  592:                               col2 => 'Rules',},
  593:                              {col1 => "Domain roles in this domain assignable to others",
  594:                               col2 => 'Rules'},
  595:                              {col1 => "Course catalog for this domain displayed elsewhere",
  596:                               col2 => 'Rules'},
  597:                              {col1 => "Requests for creation of courses in this domain by others",
  598:                               col2 => 'Rules'},
  599:                              {col1 => "Users in other domains can send messages to this domain",
  600:                               col2 => 'Rules'},],
  601:                   print => \&print_trust,
  602:                   modify => \&modify_trust,
  603:                  },
  604:           'lti' =>
  605:                  {text => 'LTI Provider',
  606:                   help => 'Domain_Configuration_LTI_Provider',
  607:                   header => [{col1 => 'Setting',
  608:                               col2 => 'Value',}],
  609:                   print => \&print_lti,
  610:                   modify => \&modify_lti,
  611:                  },
  612:     );
  613:     if (keys(%servers) > 1) {
  614:         $prefs{'login'}  = { text   => 'Log-in page options',
  615:                              help   => 'Domain_Configuration_Login_Page',
  616:                             header => [{col1 => 'Log-in Service',
  617:                                         col2 => 'Server Setting',},
  618:                                        {col1 => 'Log-in Page Items',
  619:                                         col2 => ''},
  620:                                        {col1 => 'Log-in Help',
  621:                                         col2 => 'Value'},
  622:                                        {col1 => 'Custom HTML in document head',
  623:                                         col2 => 'Value'}],
  624:                             print => \&print_login,
  625:                             modify => \&modify_login,
  626:                            };
  627:     }
  628: 
  629:     my @roles = ('student','coordinator','author','admin');
  630:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  631:     &Apache::lonhtmlcommon::add_breadcrumb
  632:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  633:       text=>"Settings to display/modify"});
  634:     my $confname = $dom.'-domainconfig';
  635: 
  636:     if ($phase eq 'process') {
  637:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  638:                                                               \%prefs,\%domconfig,$confname,\@roles);
  639:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  640:             $r->rflush();
  641:             &devalidate_remote_domconfs($dom,$result);
  642:         }
  643:     } elsif ($phase eq 'display') {
  644:         my $js = &recaptcha_js().
  645:                  &toggle_display_js();
  646:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  647:             my ($othertitle,$usertypes,$types) =
  648:                 &Apache::loncommon::sorted_inst_types($dom);
  649:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  650:                                           $domconfig{'loadbalancing'}).
  651:                    &new_spares_js().
  652:                    &common_domprefs_js().
  653:                    &Apache::loncommon::javascript_array_indexof();
  654:         }
  655:         if (grep(/^requestcourses$/,@actions)) {
  656:             my $javascript_validations;
  657:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  658:             $js .= <<END;
  659: <script type="text/javascript">
  660: $javascript_validations
  661: </script>
  662: $coursebrowserjs
  663: END
  664:         }
  665:         if (grep(/^selfcreation$/,@actions)) {
  666:             $js .= &selfcreate_javascript();
  667:         }
  668:         if (grep(/^contacts$/,@actions)) {
  669:             $js .= &contacts_javascript();
  670:         }
  671:         if (grep(/^scantron$/,@actions)) {
  672:             $js .= &scantron_javascript();
  673:         }
  674:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  675:     } else {
  676: # check if domconfig user exists for the domain.
  677:         my $servadm = $r->dir_config('lonAdmEMail');
  678:         my ($configuserok,$author_ok,$switchserver) =
  679:             &config_check($dom,$confname,$servadm);
  680:         unless ($configuserok eq 'ok') {
  681:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  682:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  683:                           $confname).
  684:                       '<br />'
  685:             );
  686:             if ($switchserver) {
  687:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  688:                           '<br />'.
  689:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  690:                           '<br />'.
  691:                           &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).
  692:                           '<br />'.
  693:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  694:                 );
  695:             } else {
  696:                 $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.').
  697:                           '<br />'.
  698:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  699:                 );
  700:             }
  701:             $r->print(&Apache::loncommon::end_page());
  702:             return OK;
  703:         }
  704:         if (keys(%domconfig) == 0) {
  705:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  706:             my @ids=&Apache::lonnet::current_machine_ids();
  707:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  708:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  709:                 my @loginimages = ('img','logo','domlogo','login');
  710:                 my $custom_img_count = 0;
  711:                 foreach my $img (@loginimages) {
  712:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  713:                         $custom_img_count ++;
  714:                     }
  715:                 }
  716:                 foreach my $role (@roles) {
  717:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  718:                         $custom_img_count ++;
  719:                     }
  720:                 }
  721:                 if ($custom_img_count > 0) {
  722:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  723:                     my $switch_server = &check_switchserver($dom,$confname);
  724:                     $r->print(
  725:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  726:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  727:     &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 />'.
  728:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  729:                     if ($switch_server) {
  730:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  731:                     }
  732:                     $r->print(&Apache::loncommon::end_page());
  733:                     return OK;
  734:                 }
  735:             }
  736:         }
  737:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  738:     }
  739:     return OK;
  740: }
  741: 
  742: sub process_changes {
  743:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  744:     my %domconfig;
  745:     if (ref($values) eq 'HASH') {
  746:         %domconfig = %{$values};
  747:     }
  748:     my $output;
  749:     if ($action eq 'login') {
  750:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  751:     } elsif ($action eq 'rolecolors') {
  752:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  753:                                      $lastactref,%domconfig);
  754:     } elsif ($action eq 'quotas') {
  755:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  756:     } elsif ($action eq 'autoenroll') {
  757:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  758:     } elsif ($action eq 'autoupdate') {
  759:         $output = &modify_autoupdate($dom,%domconfig);
  760:     } elsif ($action eq 'autocreate') {
  761:         $output = &modify_autocreate($dom,%domconfig);
  762:     } elsif ($action eq 'directorysrch') {
  763:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  764:     } elsif ($action eq 'usercreation') {
  765:         $output = &modify_usercreation($dom,%domconfig);
  766:     } elsif ($action eq 'selfcreation') {
  767:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  768:     } elsif ($action eq 'usermodification') {
  769:         $output = &modify_usermodification($dom,%domconfig);
  770:     } elsif ($action eq 'contacts') {
  771:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  772:     } elsif ($action eq 'defaults') {
  773:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  774:     } elsif ($action eq 'scantron') {
  775:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  776:     } elsif ($action eq 'coursecategories') {
  777:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  778:     } elsif ($action eq 'serverstatuses') {
  779:         $output = &modify_serverstatuses($dom,%domconfig);
  780:     } elsif ($action eq 'requestcourses') {
  781:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  782:     } elsif ($action eq 'requestauthor') {
  783:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  784:     } elsif ($action eq 'helpsettings') {
  785:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  786:     } elsif ($action eq 'coursedefaults') {
  787:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  788:     } elsif ($action eq 'selfenrollment') {
  789:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  790:     } elsif ($action eq 'usersessions') {
  791:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  792:     } elsif ($action eq 'loadbalancing') {
  793:         $output = &modify_loadbalancing($dom,%domconfig);
  794:     } elsif ($action eq 'ltitools') {
  795:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  796:     } elsif ($action eq 'proctoring') {
  797:         $output = &modify_proctoring($r,$dom,$action,$lastactref,%domconfig);
  798:     } elsif ($action eq 'ssl') {
  799:         $output = &modify_ssl($dom,$lastactref,%domconfig);
  800:     } elsif ($action eq 'trust') {
  801:         $output = &modify_trust($dom,$lastactref,%domconfig);
  802:     } elsif ($action eq 'lti') {
  803:         $output = &modify_lti($r,$dom,$action,$lastactref,%domconfig);
  804:     } elsif ($action eq 'privacy') {
  805:         $output = &modify_privacy($dom,%domconfig);
  806:     } elsif ($action eq 'passwords') {
  807:         $output = &modify_passwords($r,$dom,$confname,$lastactref,%domconfig);
  808:     }
  809:     return $output;
  810: }
  811: 
  812: sub print_config_box {
  813:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  814:     my $rowtotal = 0;
  815:     my $output;
  816:     if ($action eq 'coursecategories') {
  817:         $output = &coursecategories_javascript($settings);
  818:     } elsif ($action eq 'defaults') {
  819:         $output = &defaults_javascript($settings); 
  820:     } elsif ($action eq 'passwords') {
  821:         $output = &passwords_javascript();
  822:     } elsif ($action eq 'helpsettings') {
  823:         my (%privs,%levelscurrent);
  824:         my %full=();
  825:         my %levels=(
  826:                      course => {},
  827:                      domain => {},
  828:                      system => {},
  829:                    );
  830:         my $context = 'domain';
  831:         my $crstype = 'Course';
  832:         my $formname = 'display';
  833:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  834:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  835:         $output =
  836:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full, 
  837:                                                       \@templateroles);
  838:     } elsif ($action eq 'ltitools') {
  839:         $output .= &ltitools_javascript($settings);
  840:     } elsif ($action eq 'lti') {
  841:         $output .= &lti_javascript($settings);
  842:     } elsif ($action eq 'proctoring') {
  843:         $output .= &proctoring_javascript($settings);
  844:     }
  845:     $output .=
  846:          '<table class="LC_nested_outer">
  847:           <tr>
  848:            <th class="LC_left_item LC_middle"><span class="LC_nobreak">'.
  849:            &mt($item->{text}).'&nbsp;'.
  850:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  851:           '</tr>';
  852:     $rowtotal ++;
  853:     my $numheaders = 1;
  854:     if (ref($item->{'header'}) eq 'ARRAY') {
  855:         $numheaders = scalar(@{$item->{'header'}});
  856:     }
  857:     if ($numheaders > 1) {
  858:         my $colspan = '';
  859:         my $rightcolspan = '';
  860:         my $leftnobr = '';
  861:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  862:             ($action eq 'directorysrch') ||
  863:             (($action eq 'login') && ($numheaders < 4))) {
  864:             $colspan = ' colspan="2"';
  865:         }
  866:         if ($action eq 'usersessions') {
  867:             $rightcolspan = ' colspan="3"'; 
  868:         }
  869:         if ($action eq 'passwords') {
  870:             $leftnobr = ' LC_nobreak';
  871:         }
  872:         $output .= '
  873:           <tr>
  874:            <td>
  875:             <table class="LC_nested">
  876:              <tr class="LC_info_row">
  877:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  878:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  879:              </tr>';
  880:         $rowtotal ++;
  881:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  882:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  883:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'ssl') ||
  884:             ($action eq 'directorysrch') || ($action eq 'trust') || ($action eq 'helpsettings') ||
  885:             ($action eq 'contacts') || ($action eq 'privacy')) {
  886:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  887:         } elsif ($action eq 'passwords') {
  888:             $output .= $item->{'print'}->('top',$dom,$confname,$settings,\$rowtotal);
  889:         } elsif ($action eq 'coursecategories') {
  890:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  891:         } elsif ($action eq 'scantron') {
  892:             $output .= $item->{'print'}->($r,'top',$dom,$confname,$settings,\$rowtotal);
  893:         } elsif ($action eq 'login') {
  894:             if ($numheaders == 4) {
  895:                 $colspan = ' colspan="2"';
  896:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  897:             } else {
  898:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  899:             }
  900:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  901:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  902:         } elsif ($action eq 'rolecolors') {
  903:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  904:         }
  905:         $output .= '
  906:            </table>
  907:           </td>
  908:          </tr>
  909:          <tr>
  910:            <td>
  911:             <table class="LC_nested">
  912:              <tr class="LC_info_row">
  913:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  914:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  915:              </tr>';
  916:             $rowtotal ++;
  917:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  918:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  919:             ($action eq 'usersessions') || ($action eq 'coursecategories') || 
  920:             ($action eq 'trust') || ($action eq 'contacts') ||
  921:             ($action eq 'privacy') || ($action eq 'passwords')) {
  922:             if ($action eq 'coursecategories') {
  923:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  924:                 $colspan = ' colspan="2"';
  925:             } elsif ($action eq 'trust') {
  926:                 $output .= $item->{'print'}->('shared',$dom,$settings,\$rowtotal);
  927:             } elsif ($action eq 'passwords') {
  928:                 $output .= $item->{'print'}->('middle',$dom,$confname,$settings,\$rowtotal);
  929:             } else {
  930:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  931:             }
  932:             if ($action eq 'trust') {
  933:                 $output .= '
  934:             </table>
  935:           </td>
  936:          </tr>';
  937:                 my @trusthdrs = qw(2 3 4 5 6 7);
  938:                 my @prefixes = qw(enroll othcoau coaurem domroles catalog reqcrs);
  939:                 for (my $i=0; $i<@trusthdrs; $i++) {
  940:                     $output .= '
  941:          <tr>
  942:            <td>
  943:             <table class="LC_nested">
  944:              <tr class="LC_info_row">
  945:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col1'}).'</td>
  946:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col2'}).'</td></tr>'.
  947:                            $item->{'print'}->($prefixes[$i],$dom,$settings,\$rowtotal).'
  948:             </table>
  949:           </td>
  950:          </tr>';
  951:                 }
  952:                 $output .= '
  953:          <tr>
  954:            <td>
  955:             <table class="LC_nested">
  956:              <tr class="LC_info_row">
  957:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col1'}).'</td>
  958:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col2'}).'</td></tr>'.
  959:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  960:             } else {
  961:                 $output .= '
  962:            </table>
  963:           </td>
  964:          </tr>
  965:          <tr>
  966:            <td>
  967:             <table class="LC_nested">
  968:              <tr class="LC_info_row">
  969:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  970:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  971:              </tr>'."\n";
  972:                 if ($action eq 'coursecategories') {
  973:                     $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  974:                 } elsif (($action eq 'contacts') || ($action eq 'privacy') || ($action eq 'passwords')) {
  975:                     if ($action eq 'passwords') {
  976:                         $output .= $item->{'print'}->('lower',$dom,$confname,$settings,\$rowtotal);
  977:                     } else {
  978:                         $output .= $item->{'print'}->('lower',$dom,$settings,\$rowtotal);
  979:                     }
  980:                     $output .= '
  981:              </tr>
  982:             </table>
  983:            </td>
  984:           </tr>
  985:           <tr>
  986:            <td>
  987:             <table class="LC_nested">
  988:              <tr class="LC_info_row">
  989:               <td class="LC_left_item'.$leftnobr.'"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  990:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'."\n";
  991:                     if ($action eq 'passwords') {
  992:                         $output .= $item->{'print'}->('bottom',$dom,$confname,$settings,\$rowtotal);
  993:                     } else {
  994:                         $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  995:                     }
  996:                     $output .= '
  997:             </table>
  998:           </td>
  999:          </tr>
 1000:          <tr>';
 1001:                 } else {
 1002:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1003:                 }
 1004:             }
 1005:             $rowtotal ++;
 1006:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
 1007:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
 1008:                  ($action eq 'helpsettings')) {
 1009:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1010:         } elsif ($action eq 'scantron') {
 1011:             $output .= $item->{'print'}->($r,'bottom',$dom,$confname,$settings,\$rowtotal);
 1012:         } elsif ($action eq 'ssl') {
 1013:             $output .= $item->{'print'}->('connto',$dom,$settings,\$rowtotal).'
 1014:             </table>
 1015:           </td>
 1016:          </tr>
 1017:          <tr>
 1018:            <td>
 1019:             <table class="LC_nested">
 1020:              <tr class="LC_info_row">
 1021:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1022:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1023:                            $item->{'print'}->('connfrom',$dom,$settings,\$rowtotal).'
 1024:             </table>
 1025:           </td>
 1026:          </tr>
 1027:          <tr>
 1028:            <td>
 1029:             <table class="LC_nested">
 1030:              <tr class="LC_info_row">
 1031:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1032:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'.
 1033:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
 1034:         } elsif ($action eq 'login') {
 1035:             if ($numheaders == 4) {
 1036:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
 1037:            </table>
 1038:           </td>
 1039:          </tr>
 1040:          <tr>
 1041:            <td>
 1042:             <table class="LC_nested">
 1043:              <tr class="LC_info_row">
 1044:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1045:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
 1046:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1047:                 $rowtotal ++;
 1048:             } else {
 1049:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
 1050:             }
 1051:             $output .= '
 1052:            </table>
 1053:           </td>
 1054:          </tr>
 1055:          <tr>
 1056:            <td>
 1057:             <table class="LC_nested">
 1058:              <tr class="LC_info_row">';
 1059:             if ($numheaders == 4) {
 1060:                 $output .= '
 1061:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1062:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1063:              </tr>';
 1064:             } else {
 1065:                 $output .= '
 1066:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1067:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
 1068:              </tr>';
 1069:             }
 1070:             $rowtotal ++;
 1071:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
 1072:         } elsif ($action eq 'requestcourses') {
 1073:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1074:             $rowtotal ++;
 1075:             $output .= &print_studentcode($settings,\$rowtotal).'
 1076:            </table>
 1077:           </td>
 1078:          </tr>
 1079:          <tr>
 1080:            <td>
 1081:             <table class="LC_nested">
 1082:              <tr class="LC_info_row">
 1083:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
 1084:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
 1085:                        &textbookcourses_javascript($settings).
 1086:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
 1087:             </table>
 1088:            </td>
 1089:           </tr>
 1090:          <tr>
 1091:            <td>
 1092:             <table class="LC_nested">
 1093:              <tr class="LC_info_row">
 1094:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1095:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
 1096:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
 1097:             </table>
 1098:            </td>
 1099:           </tr>
 1100:           <tr>
 1101:            <td>
 1102:             <table class="LC_nested">
 1103:              <tr class="LC_info_row">
 1104:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
 1105:               <td class="LC_right_item" style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
 1106:              </tr>'.
 1107:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
 1108:         } elsif ($action eq 'requestauthor') {
 1109:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
 1110:             $rowtotal ++;
 1111:         } elsif ($action eq 'rolecolors') {
 1112:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
 1113:            </table>
 1114:           </td>
 1115:          </tr>
 1116:          <tr>
 1117:            <td>
 1118:             <table class="LC_nested">
 1119:              <tr class="LC_info_row">
 1120:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.
 1121:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
 1122:               <td class="LC_right_item" style="vertical-align: top">'.
 1123:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
 1124:              </tr>'.
 1125:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
 1126:            </table>
 1127:           </td>
 1128:          </tr>
 1129:          <tr>
 1130:            <td>
 1131:             <table class="LC_nested">
 1132:              <tr class="LC_info_row">
 1133:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1134:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1135:              </tr>'.
 1136:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
 1137:             $rowtotal += 2;
 1138:         }
 1139:     } else {
 1140:         $output .= '
 1141:           <tr>
 1142:            <td>
 1143:             <table class="LC_nested">
 1144:              <tr class="LC_info_row">';
 1145:         if ($action eq 'login') {
 1146:             $output .= '  
 1147:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1148:         } elsif ($action eq 'serverstatuses') {
 1149:             $output .= '
 1150:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).
 1151:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
 1152: 
 1153:         } else {
 1154:             $output .= '
 1155:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1156:         }
 1157:         if (defined($item->{'header'}->[0]->{'col3'})) {
 1158:             $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1159:                        &mt($item->{'header'}->[0]->{'col2'});
 1160:             if ($action eq 'serverstatuses') {
 1161:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
 1162:             } 
 1163:         } else {
 1164:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1165:                        &mt($item->{'header'}->[0]->{'col2'});
 1166:         }
 1167:         $output .= '</td>';
 1168:         if ($item->{'header'}->[0]->{'col3'}) {
 1169:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1170:                 $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1171:                             &mt($item->{'header'}->[0]->{'col3'});
 1172:             } else {
 1173:                 $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1174:                            &mt($item->{'header'}->[0]->{'col3'});
 1175:             }
 1176:             if ($action eq 'serverstatuses') {
 1177:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1178:             }
 1179:             $output .= '</td>';
 1180:         }
 1181:         if ($item->{'header'}->[0]->{'col4'}) {
 1182:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1183:                        &mt($item->{'header'}->[0]->{'col4'});
 1184:         }
 1185:         $output .= '</tr>';
 1186:         $rowtotal ++;
 1187:         if ($action eq 'quotas') {
 1188:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1189:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1190:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') || 
 1191:                  ($action eq 'ltitools') || ($action eq 'lti') ||
 1192:                  ($action eq 'proctoring')) {
 1193:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1194:         }
 1195:     }
 1196:     $output .= '
 1197:    </table>
 1198:   </td>
 1199:  </tr>
 1200: </table><br />';
 1201:     return ($output,$rowtotal);
 1202: }
 1203: 
 1204: sub print_login {
 1205:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1206:     my ($css_class,$datatable);
 1207:     my %choices = &login_choices();
 1208: 
 1209:     if ($caller eq 'service') {
 1210:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1211:         my $choice = $choices{'disallowlogin'};
 1212:         $css_class = ' class="LC_odd_row"';
 1213:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1214:                       '<td style="text-align: right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1215:                       '<th>'.$choices{'server'}.'</th>'.
 1216:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1217:                       '<th>'.$choices{'custompath'}.'</th>'.
 1218:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1219:         my %disallowed;
 1220:         if (ref($settings) eq 'HASH') {
 1221:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1222:                %disallowed = %{$settings->{'loginvia'}};
 1223:             }
 1224:         }
 1225:         foreach my $lonhost (sort(keys(%servers))) {
 1226:             my $direct = 'selected="selected"';
 1227:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1228:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1229:                     $direct = '';
 1230:                 }
 1231:             }
 1232:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1233:                           '<td><select name="'.$lonhost.'_server">'.
 1234:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1235:                           '</option>';
 1236:             foreach my $hostid (sort(keys(%servers))) {
 1237:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1238:                 my $selected = '';
 1239:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1240:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1241:                         $selected = 'selected="selected"';
 1242:                     }
 1243:                 }
 1244:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1245:                               $servers{$hostid}.'</option>';
 1246:             }
 1247:             $datatable .= '</select></td>'.
 1248:                           '<td><select name="'.$lonhost.'_serverpath">';
 1249:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1250:                 my $pathname = $path;
 1251:                 if ($path eq 'custom') {
 1252:                     $pathname = &mt('Custom Path').' ->';
 1253:                 }
 1254:                 my $selected = '';
 1255:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1256:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1257:                         $selected = 'selected="selected"';
 1258:                     }
 1259:                 } elsif ($path eq '') {
 1260:                     $selected = 'selected="selected"';
 1261:                 }
 1262:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1263:             }
 1264:             $datatable .= '</select></td>';
 1265:             my ($custom,$exempt);
 1266:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1267:                 $custom = $disallowed{$lonhost}{'custompath'};
 1268:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1269:             }
 1270:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1271:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1272:                           '</tr>';
 1273:         }
 1274:         $datatable .= '</table></td></tr>';
 1275:         return $datatable;
 1276:     } elsif ($caller eq 'page') {
 1277:         my %defaultchecked = ( 
 1278:                                'coursecatalog' => 'on',
 1279:                                'helpdesk'      => 'on',
 1280:                                'adminmail'     => 'off',
 1281:                                'newuser'       => 'off',
 1282:                              );
 1283:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1284:         my (%checkedon,%checkedoff);
 1285:         foreach my $item (@toggles) {
 1286:             if ($defaultchecked{$item} eq 'on') { 
 1287:                 $checkedon{$item} = ' checked="checked" ';
 1288:                 $checkedoff{$item} = ' ';
 1289:             } elsif ($defaultchecked{$item} eq 'off') {
 1290:                 $checkedoff{$item} = ' checked="checked" ';
 1291:                 $checkedon{$item} = ' ';
 1292:             }
 1293:         }
 1294:         my @images = ('img','logo','domlogo','login');
 1295:         my @logintext = ('textcol','bgcol');
 1296:         my @bgs = ('pgbg','mainbg','sidebg');
 1297:         my @links = ('link','alink','vlink');
 1298:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1299:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1300:         my (%is_custom,%designs);
 1301:         my %defaults = (
 1302:                        font => $defaultdesign{'login.font'},
 1303:                        );
 1304:         foreach my $item (@images) {
 1305:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1306:             $defaults{'showlogo'}{$item} = 1;
 1307:         }
 1308:         foreach my $item (@bgs) {
 1309:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1310:         }
 1311:         foreach my $item (@logintext) {
 1312:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1313:         }
 1314:         foreach my $item (@links) {
 1315:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1316:         }
 1317:         if (ref($settings) eq 'HASH') {
 1318:             foreach my $item (@toggles) {
 1319:                 if ($settings->{$item} eq '1') {
 1320:                     $checkedon{$item} =  ' checked="checked" ';
 1321:                     $checkedoff{$item} = ' ';
 1322:                 } elsif ($settings->{$item} eq '0') {
 1323:                     $checkedoff{$item} =  ' checked="checked" ';
 1324:                     $checkedon{$item} = ' ';
 1325:                 }
 1326:             }
 1327:             foreach my $item (@images) {
 1328:                 if (defined($settings->{$item})) {
 1329:                     $designs{$item} = $settings->{$item};
 1330:                     $is_custom{$item} = 1;
 1331:                 }
 1332:                 if (defined($settings->{'showlogo'}{$item})) {
 1333:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1334:                 }
 1335:             }
 1336:             foreach my $item (@logintext) {
 1337:                 if ($settings->{$item} ne '') {
 1338:                     $designs{'logintext'}{$item} = $settings->{$item};
 1339:                     $is_custom{$item} = 1;
 1340:                 }
 1341:             }
 1342:             if ($settings->{'font'} ne '') {
 1343:                 $designs{'font'} = $settings->{'font'};
 1344:                 $is_custom{'font'} = 1;
 1345:             }
 1346:             foreach my $item (@bgs) {
 1347:                 if ($settings->{$item} ne '') {
 1348:                     $designs{'bgs'}{$item} = $settings->{$item};
 1349:                     $is_custom{$item} = 1;
 1350:                 }
 1351:             }
 1352:             foreach my $item (@links) {
 1353:                 if ($settings->{$item} ne '') {
 1354:                     $designs{'links'}{$item} = $settings->{$item};
 1355:                     $is_custom{$item} = 1;
 1356:                 }
 1357:             }
 1358:         } else {
 1359:             if ($designhash{$dom.'.login.font'} ne '') {
 1360:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1361:                 $is_custom{'font'} = 1;
 1362:             }
 1363:             foreach my $item (@images) {
 1364:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1365:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1366:                     $is_custom{$item} = 1;
 1367:                 }
 1368:             }
 1369:             foreach my $item (@bgs) {
 1370:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1371:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1372:                     $is_custom{$item} = 1;
 1373:                 }
 1374:             }
 1375:             foreach my $item (@links) {
 1376:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1377:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1378:                     $is_custom{$item} = 1;
 1379:                 }
 1380:             }
 1381:         }
 1382:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1383:                                                       logo => 'Institution Logo',
 1384:                                                       domlogo => 'Domain Logo',
 1385:                                                       login => 'Login box');
 1386:         my $itemcount = 1;
 1387:         foreach my $item (@toggles) {
 1388:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1389:             $datatable .=  
 1390:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1391:                 '</td><td>'.
 1392:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1393:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1394:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1395:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1396:                 '</tr>';
 1397:             $itemcount ++;
 1398:         }
 1399:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1400:         $datatable .= '</tr></table></td></tr>';
 1401:     } elsif ($caller eq 'help') {
 1402:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1403:         my $switchserver = &check_switchserver($dom,$confname);
 1404:         my $itemcount = 1;
 1405:         $defaulturl = '/adm/loginproblems.html';
 1406:         $defaulttype = 'default';
 1407:         %lt = &Apache::lonlocal::texthash (
 1408:                      del     => 'Delete?',
 1409:                      rep     => 'Replace:',
 1410:                      upl     => 'Upload:',
 1411:                      default => 'Default',
 1412:                      custom  => 'Custom',
 1413:                                              );
 1414:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1415:         my @currlangs;
 1416:         if (ref($settings) eq 'HASH') {
 1417:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1418:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1419:                     next if ($settings->{'helpurl'}{$key} eq '');
 1420:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1421:                     $type{$key} = 'custom';
 1422:                     unless ($key eq 'nolang') {
 1423:                         push(@currlangs,$key);
 1424:                     }
 1425:                 }
 1426:             } elsif ($settings->{'helpurl'} ne '') {
 1427:                 $type{'nolang'} = 'custom';
 1428:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1429:             }
 1430:         }
 1431:         foreach my $lang ('nolang',sort(@currlangs)) {
 1432:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1433:             $datatable .= '<tr'.$css_class.'>';
 1434:             if ($url{$lang} eq '') {
 1435:                 $url{$lang} = $defaulturl;
 1436:             }
 1437:             if ($type{$lang} eq '') {
 1438:                 $type{$lang} = $defaulttype;
 1439:             }
 1440:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1441:             if ($lang eq 'nolang') {
 1442:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1443:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1444:             } else {
 1445:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1446:                                   $langchoices{$lang},
 1447:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1448:             }
 1449:             $datatable .= '</span></td>'."\n".
 1450:                           '<td class="LC_left_item">';
 1451:             if ($type{$lang} eq 'custom') {
 1452:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1453:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1454:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1455:             } else {
 1456:                 $datatable .= $lt{'upl'};
 1457:             }
 1458:             $datatable .='<br />';
 1459:             if ($switchserver) {
 1460:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1461:             } else {
 1462:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1463:             }
 1464:             $datatable .= '</td></tr>';
 1465:             $itemcount ++;
 1466:         }
 1467:         my @addlangs;
 1468:         foreach my $lang (sort(keys(%langchoices))) {
 1469:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1470:             push(@addlangs,$lang);
 1471:         }
 1472:         if (@addlangs > 0) {
 1473:             my %toadd;
 1474:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1475:             $toadd{''} = &mt('Select');
 1476:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1477:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1478:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1479:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1480:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1481:             if ($switchserver) {
 1482:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1483:             } else {
 1484:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1485:             }
 1486:             $datatable .= '</td></tr>';
 1487:             $itemcount ++;
 1488:         }
 1489:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1490:     } elsif ($caller eq 'headtag') {
 1491:         my %domservers = &Apache::lonnet::get_servers($dom);
 1492:         my $choice = $choices{'headtag'};
 1493:         $css_class = ' class="LC_odd_row"';
 1494:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1495:                       '<td style="text-align: left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1496:                       '<th>'.$choices{'current'}.'</th>'.
 1497:                       '<th>'.$choices{'action'}.'</th>'.
 1498:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1499:         my (%currurls,%currexempt);
 1500:         if (ref($settings) eq 'HASH') {
 1501:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1502:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1503:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1504:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1505:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1506:                     }
 1507:                 }
 1508:             }
 1509:         }
 1510:         my %lt = &Apache::lonlocal::texthash(
 1511:                                                del  => 'Delete?',
 1512:                                                rep  => 'Replace:',
 1513:                                                upl  => 'Upload:',
 1514:                                                curr => 'View contents',
 1515:                                                none => 'None',
 1516:         );
 1517:         my $switchserver = &check_switchserver($dom,$confname);
 1518:         foreach my $lonhost (sort(keys(%domservers))) {
 1519:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1520:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1521:             if ($currurls{$lonhost}) {
 1522:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1523:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1524:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1525:                               '">'.$lt{'curr'}.'</a></td>'.
 1526:                               '<td><span class="LC_nobreak"><label>'.
 1527:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1528:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1529:             } else {
 1530:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1531:             }
 1532:             $datatable .='<br />';
 1533:             if ($switchserver) {
 1534:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1535:             } else {
 1536:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1537:             }
 1538:             $datatable .= '</td><td><input type="text" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1539:         }
 1540:         $datatable .= '</table></td></tr>';
 1541:     }
 1542:     return $datatable;
 1543: }
 1544: 
 1545: sub login_choices {
 1546:     my %choices =
 1547:         &Apache::lonlocal::texthash (
 1548:             coursecatalog => 'Display Course/Community Catalog link?',
 1549:             adminmail     => "Display Administrator's E-mail Address?",
 1550:             helpdesk      => 'Display "Contact Helpdesk" link',
 1551:             disallowlogin => "Login page requests redirected",
 1552:             hostid        => "Server",
 1553:             server        => "Redirect to:",
 1554:             serverpath    => "Path",
 1555:             custompath    => "Custom", 
 1556:             exempt        => "Exempt IP(s)",
 1557:             directlogin   => "No redirect",
 1558:             newuser       => "Link to create a user account",
 1559:             img           => "Header",
 1560:             logo          => "Main Logo",
 1561:             domlogo       => "Domain Logo",
 1562:             login         => "Log-in Header", 
 1563:             textcol       => "Text color",
 1564:             bgcol         => "Box color",
 1565:             bgs           => "Background colors",
 1566:             links         => "Link colors",
 1567:             font          => "Font color",
 1568:             pgbg          => "Header",
 1569:             mainbg        => "Page",
 1570:             sidebg        => "Login box",
 1571:             link          => "Link",
 1572:             alink         => "Active link",
 1573:             vlink         => "Visited link",
 1574:             headtag       => "Custom markup",
 1575:             action        => "Action",
 1576:             current       => "Current",
 1577:         );
 1578:     return %choices;
 1579: }
 1580: 
 1581: sub print_rolecolors {
 1582:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1583:     my %choices = &color_font_choices();
 1584:     my @bgs = ('pgbg','tabbg','sidebg');
 1585:     my @links = ('link','alink','vlink');
 1586:     my @images = ('img');
 1587:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1588:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1589:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1590:     my (%is_custom,%designs);
 1591:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1592:     if (ref($settings) eq 'HASH') {
 1593:         if (ref($settings->{$role}) eq 'HASH') {
 1594:             if ($settings->{$role}->{'img'} ne '') {
 1595:                 $designs{'img'} = $settings->{$role}->{'img'};
 1596:                 $is_custom{'img'} = 1;
 1597:             }
 1598:             if ($settings->{$role}->{'font'} ne '') {
 1599:                 $designs{'font'} = $settings->{$role}->{'font'};
 1600:                 $is_custom{'font'} = 1;
 1601:             }
 1602:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1603:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1604:                 $is_custom{'fontmenu'} = 1;
 1605:             }
 1606:             foreach my $item (@bgs) {
 1607:                 if ($settings->{$role}->{$item} ne '') {
 1608:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1609:                     $is_custom{$item} = 1;
 1610:                 }
 1611:             }
 1612:             foreach my $item (@links) {
 1613:                 if ($settings->{$role}->{$item} ne '') {
 1614:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1615:                     $is_custom{$item} = 1;
 1616:                 }
 1617:             }
 1618:         }
 1619:     } else {
 1620:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1621:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1622:             $is_custom{'img'} = 1;
 1623:         }
 1624:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1625:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1626:             $is_custom{'fontmenu'} = 1; 
 1627:         }
 1628:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1629:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1630:             $is_custom{'font'} = 1;
 1631:         }
 1632:         foreach my $item (@bgs) {
 1633:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1634:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1635:                 $is_custom{$item} = 1;
 1636:             
 1637:             }
 1638:         }
 1639:         foreach my $item (@links) {
 1640:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1641:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1642:                 $is_custom{$item} = 1;
 1643:             }
 1644:         }
 1645:     }
 1646:     my $itemcount = 1;
 1647:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1648:     $datatable .= '</tr></table></td></tr>';
 1649:     return $datatable;
 1650: }
 1651: 
 1652: sub role_defaults {
 1653:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1654:     my %defaults;
 1655:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1656:         return %defaults;
 1657:     }
 1658:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1659:     if ($role eq 'login') {
 1660:         %defaults = (
 1661:                        font => $defaultdesign{$role.'.font'},
 1662:                     );
 1663:         if (ref($logintext) eq 'ARRAY') {
 1664:             foreach my $item (@{$logintext}) {
 1665:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1666:             }
 1667:         }
 1668:         foreach my $item (@{$images}) {
 1669:             $defaults{'showlogo'}{$item} = 1;
 1670:         }
 1671:     } else {
 1672:         %defaults = (
 1673:                        img => $defaultdesign{$role.'.img'},
 1674:                        font => $defaultdesign{$role.'.font'},
 1675:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1676:                     );
 1677:     }
 1678:     foreach my $item (@{$bgs}) {
 1679:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1680:     }
 1681:     foreach my $item (@{$links}) {
 1682:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1683:     }
 1684:     foreach my $item (@{$images}) {
 1685:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1686:     }
 1687:     return %defaults;
 1688: }
 1689: 
 1690: sub display_color_options {
 1691:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1692:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1693:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1694:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1695:     my $datatable = '<tr'.$css_class.'>'.
 1696:         '<td>'.$choices->{'font'}.'</td>';
 1697:     if (!$is_custom->{'font'}) {
 1698:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1699:     } else {
 1700:         $datatable .= '<td>&nbsp;</td>';
 1701:     }
 1702:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1703: 
 1704:     $datatable .= '<td><span class="LC_nobreak">'.
 1705:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1706:                   ' value="'.$current_color.'" />&nbsp;'.
 1707:                   '&nbsp;</span></td></tr>';
 1708:     unless ($role eq 'login') { 
 1709:         $datatable .= '<tr'.$css_class.'>'.
 1710:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1711:         if (!$is_custom->{'fontmenu'}) {
 1712:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span class="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1713:         } else {
 1714:             $datatable .= '<td>&nbsp;</td>';
 1715:         }
 1716: 	$current_color = $designs->{'fontmenu'} ?
 1717: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1718:         $datatable .= '<td><span class="LC_nobreak">'.
 1719:                       '<input class="colorchooser" type="text" size="10" name="'
 1720: 		      .$role.'_fontmenu"'.
 1721:                       ' value="'.$current_color.'" />&nbsp;'.
 1722:                       '&nbsp;</span></td></tr>';
 1723:     }
 1724:     my $switchserver = &check_switchserver($dom,$confname);
 1725:     foreach my $img (@{$images}) {
 1726: 	$itemcount ++;
 1727:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1728:         $datatable .= '<tr'.$css_class.'>'.
 1729:                       '<td>'.$choices->{$img};
 1730:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1731:         if ($role eq 'login') {
 1732:             if ($img eq 'login') {
 1733:                 $login_hdr_pick =
 1734:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1735:                 $logincolors =
 1736:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1737:                                        $designs,$defaults);
 1738:             } elsif ($img ne 'domlogo') {
 1739:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1740:             }
 1741:         }
 1742:         $datatable .= '</td>';
 1743:         if ($designs->{$img} ne '') {
 1744:             $imgfile = $designs->{$img};
 1745: 	    $img_import = ($imgfile =~ m{^/adm/});
 1746:         } else {
 1747:             $imgfile = $defaults->{$img};
 1748:         }
 1749:         if ($imgfile) {
 1750:             my ($showfile,$fullsize);
 1751:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1752:                 my $urldir = $1;
 1753:                 my $filename = $2;
 1754:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1755:                 if (@info) {
 1756:                     my $thumbfile = 'tn-'.$filename;
 1757:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1758:                     if (@thumb) {
 1759:                         $showfile = $urldir.'/'.$thumbfile;
 1760:                     } else {
 1761:                         $showfile = $imgfile;
 1762:                     }
 1763:                 } else {
 1764:                     $showfile = '';
 1765:                 }
 1766:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1767:                 $showfile = $imgfile;
 1768:                 my $imgdir = $1;
 1769:                 my $filename = $2;
 1770:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1771:                     $showfile = "/$imgdir/tn-".$filename;
 1772:                 } else {
 1773:                     my $input = $londocroot.$imgfile;
 1774:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1775:                     if (!-e $output) {
 1776:                         my ($width,$height) = &thumb_dimensions();
 1777:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1778:                         if ($fullwidth ne '' && $fullheight ne '') {
 1779:                             if ($fullwidth > $width && $fullheight > $height) { 
 1780:                                 my $size = $width.'x'.$height;
 1781:                                 my @args = ('convert','-sample',$size,$input,$output);
 1782:                                 system({$args[0]} @args);
 1783:                                 $showfile = "/$imgdir/tn-".$filename;
 1784:                             }
 1785:                         }
 1786:                     }
 1787:                 }
 1788:             }
 1789:             if ($showfile) {
 1790:                 if ($showfile =~ m{^/(adm|res)/}) {
 1791:                     if ($showfile =~ m{^/res/}) {
 1792:                         my $local_showfile =
 1793:                             &Apache::lonnet::filelocation('',$showfile);
 1794:                         &Apache::lonnet::repcopy($local_showfile);
 1795:                     }
 1796:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1797:                 }
 1798:                 if ($imgfile) {
 1799:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1800:                         if ($imgfile =~ m{^/res/}) {
 1801:                             my $local_imgfile =
 1802:                                 &Apache::lonnet::filelocation('',$imgfile);
 1803:                             &Apache::lonnet::repcopy($local_imgfile);
 1804:                         }
 1805:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1806:                     } else {
 1807:                         $fullsize = $imgfile;
 1808:                     }
 1809:                 }
 1810:                 $datatable .= '<td>';
 1811:                 if ($img eq 'login') {
 1812:                     $datatable .= $login_hdr_pick;
 1813:                 } 
 1814:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1815:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1816:             } else {
 1817:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1818:                               &mt('Upload:').'<br />';
 1819:             }
 1820:         } else {
 1821:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1822:                           &mt('Upload:').'<br />';
 1823:         }
 1824:         if ($switchserver) {
 1825:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1826:         } else {
 1827:             if ($img ne 'login') { # suppress file selection for Log-in header
 1828:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1829:             }
 1830:         }
 1831:         $datatable .= '</td></tr>';
 1832:     }
 1833:     $itemcount ++;
 1834:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1835:     $datatable .= '<tr'.$css_class.'>'.
 1836:                   '<td>'.$choices->{'bgs'}.'</td>';
 1837:     my $bgs_def;
 1838:     foreach my $item (@{$bgs}) {
 1839:         if (!$is_custom->{$item}) {
 1840:             $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>';
 1841:         }
 1842:     }
 1843:     if ($bgs_def) {
 1844:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1845:     } else {
 1846:         $datatable .= '<td>&nbsp;</td>';
 1847:     }
 1848:     $datatable .= '<td class="LC_right_item">'.
 1849:                   '<table border="0"><tr>';
 1850: 
 1851:     foreach my $item (@{$bgs}) {
 1852:         $datatable .= '<td style="text-align: center">'.$choices->{$item};
 1853: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1854:         if ($designs->{'bgs'}{$item}) {
 1855:             $datatable .= '&nbsp;';
 1856:         }
 1857:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1858:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1859:     }
 1860:     $datatable .= '</tr></table></td></tr>';
 1861:     $itemcount ++;
 1862:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1863:     $datatable .= '<tr'.$css_class.'>'.
 1864:                   '<td>'.$choices->{'links'}.'</td>';
 1865:     my $links_def;
 1866:     foreach my $item (@{$links}) {
 1867:         if (!$is_custom->{$item}) {
 1868:             $links_def .= '<td>'.$choices->{$item}.'<br /><span class="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1869:         }
 1870:     }
 1871:     if ($links_def) {
 1872:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1873:     } else {
 1874:         $datatable .= '<td>&nbsp;</td>';
 1875:     }
 1876:     $datatable .= '<td class="LC_right_item">'.
 1877:                   '<table border="0"><tr>';
 1878:     foreach my $item (@{$links}) {
 1879: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1880:         $datatable .= '<td style="text-align: center">'.$choices->{$item}."\n";
 1881:         if ($designs->{'links'}{$item}) {
 1882:             $datatable.='&nbsp;';
 1883:         }
 1884:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1885:                       '" /></td>';
 1886:     }
 1887:     $$rowtotal += $itemcount;
 1888:     return $datatable;
 1889: }
 1890: 
 1891: sub logo_display_options {
 1892:     my ($img,$defaults,$designs) = @_;
 1893:     my $checkedon;
 1894:     if (ref($defaults) eq 'HASH') {
 1895:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1896:             if ($defaults->{'showlogo'}{$img}) {
 1897:                 $checkedon = 'checked="checked" ';     
 1898:             }
 1899:         } 
 1900:     }
 1901:     if (ref($designs) eq 'HASH') {
 1902:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1903:             if (defined($designs->{'showlogo'}{$img})) {
 1904:                 if ($designs->{'showlogo'}{$img} == 0) {
 1905:                     $checkedon = '';
 1906:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1907:                     $checkedon = 'checked="checked" ';
 1908:                 }
 1909:             }
 1910:         }
 1911:     }
 1912:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1913:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1914:            &mt('show').'</label>'."\n";
 1915: }
 1916: 
 1917: sub login_header_options  {
 1918:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1919:     my $output = '';
 1920:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1921:         $output .= &mt('Text default(s):').'<br />';
 1922:         if (!$is_custom->{'textcol'}) {
 1923:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1924:                        '&nbsp;&nbsp;&nbsp;';
 1925:         }
 1926:         if (!$is_custom->{'bgcol'}) {
 1927:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1928:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1929:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1930:         }
 1931:         $output .= '<br />';
 1932:     }
 1933:     $output .='<br />';
 1934:     return $output;
 1935: }
 1936: 
 1937: sub login_text_colors {
 1938:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1939:     my $color_menu = '<table border="0"><tr>';
 1940:     foreach my $item (@{$logintext}) {
 1941:         $color_menu .= '<td style="text-align: center">'.$choices->{$item};
 1942:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1943:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1944:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1945:     }
 1946:     $color_menu .= '</tr></table><br />';
 1947:     return $color_menu;
 1948: }
 1949: 
 1950: sub image_changes {
 1951:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1952:     my $output;
 1953:     if ($img eq 'login') {
 1954:         $output = '</td><td>'.$logincolors; # suppress image for Log-in header
 1955:     } elsif (!$is_custom) {
 1956:         if ($img ne 'domlogo') {
 1957:             $output = &mt('Default image:').'<br />';
 1958:         } else {
 1959:             $output = &mt('Default in use:').'<br />';
 1960:         }
 1961:     }
 1962:     if ($img ne 'login') {
 1963:         if ($img_import) {
 1964:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1965:         }
 1966:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1967:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1968:         if ($is_custom) {
 1969:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1970:                        '<input type="checkbox" name="'.
 1971:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1972:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1973:         } else {
 1974:             $output .= '<td class="LC_middle">'.$logincolors.&mt('Upload:').'<br />';
 1975:         }
 1976:     }
 1977:     return $output;
 1978: }
 1979: 
 1980: sub print_quotas {
 1981:     my ($dom,$settings,$rowtotal,$action) = @_;
 1982:     my $context;
 1983:     if ($action eq 'quotas') {
 1984:         $context = 'tools';
 1985:     } else {
 1986:         $context = $action;
 1987:     }
 1988:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1989:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1990:     my $typecount = 0;
 1991:     my ($css_class,%titles);
 1992:     if ($context eq 'requestcourses') {
 1993:         @usertools = ('official','unofficial','community','textbook','placement','lti');
 1994:         @options =('norequest','approval','validate','autolimit');
 1995:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1996:         %titles = &courserequest_titles();
 1997:     } elsif ($context eq 'requestauthor') {
 1998:         @usertools = ('author');
 1999:         @options = ('norequest','approval','automatic');
 2000:         %titles = &authorrequest_titles();
 2001:     } else {
 2002:         @usertools = ('aboutme','blog','webdav','portfolio');
 2003:         %titles = &tool_titles();
 2004:     }
 2005:     if (ref($types) eq 'ARRAY') {
 2006:         foreach my $type (@{$types}) {
 2007:             my ($currdefquota,$currauthorquota);
 2008:             unless (($context eq 'requestcourses') ||
 2009:                     ($context eq 'requestauthor')) {
 2010:                 if (ref($settings) eq 'HASH') {
 2011:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 2012:                         $currdefquota = $settings->{defaultquota}->{$type};
 2013:                     } else {
 2014:                         $currdefquota = $settings->{$type};
 2015:                     }
 2016:                     if (ref($settings->{authorquota}) eq 'HASH') {
 2017:                         $currauthorquota = $settings->{authorquota}->{$type};
 2018:                     }
 2019:                 }
 2020:             }
 2021:             if (defined($usertypes->{$type})) {
 2022:                 $typecount ++;
 2023:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 2024:                 $datatable .= '<tr'.$css_class.'>'.
 2025:                               '<td>'.$usertypes->{$type}.'</td>'.
 2026:                               '<td class="LC_left_item">';
 2027:                 if ($context eq 'requestcourses') {
 2028:                     $datatable .= '<table><tr>';
 2029:                 }
 2030:                 my %cell;  
 2031:                 foreach my $item (@usertools) {
 2032:                     if ($context eq 'requestcourses') {
 2033:                         my ($curroption,$currlimit);
 2034:                         if (ref($settings) eq 'HASH') {
 2035:                             if (ref($settings->{$item}) eq 'HASH') {
 2036:                                 $curroption = $settings->{$item}->{$type};
 2037:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 2038:                                     $currlimit = $1; 
 2039:                                 }
 2040:                             }
 2041:                         }
 2042:                         if (!$curroption) {
 2043:                             $curroption = 'norequest';
 2044:                         }
 2045:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 2046:                         foreach my $option (@options) {
 2047:                             my $val = $option;
 2048:                             if ($option eq 'norequest') {
 2049:                                 $val = 0;  
 2050:                             }
 2051:                             if ($option eq 'validate') {
 2052:                                 my $canvalidate = 0;
 2053:                                 if (ref($validations{$item}) eq 'HASH') { 
 2054:                                     if ($validations{$item}{$type}) {
 2055:                                         $canvalidate = 1;
 2056:                                     }
 2057:                                 }
 2058:                                 next if (!$canvalidate);
 2059:                             }
 2060:                             my $checked = '';
 2061:                             if ($option eq $curroption) {
 2062:                                 $checked = ' checked="checked"';
 2063:                             } elsif ($option eq 'autolimit') {
 2064:                                 if ($curroption =~ /^autolimit/) {
 2065:                                     $checked = ' checked="checked"';
 2066:                                 }                       
 2067:                             } 
 2068:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 2069:                                   '<input type="radio" name="crsreq_'.$item.
 2070:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 2071:                                   $titles{$option}.'</label>';
 2072:                             if ($option eq 'autolimit') {
 2073:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2074:                                                 $item.'_limit_'.$type.'" size="1" '.
 2075:                                                 'value="'.$currlimit.'" />';
 2076:                             }
 2077:                             $cell{$item} .= '</span> ';
 2078:                             if ($option eq 'autolimit') {
 2079:                                 $cell{$item} .= $titles{'unlimited'};
 2080:                             }
 2081:                         }
 2082:                     } elsif ($context eq 'requestauthor') {
 2083:                         my $curroption;
 2084:                         if (ref($settings) eq 'HASH') {
 2085:                             $curroption = $settings->{$type};
 2086:                         }
 2087:                         if (!$curroption) {
 2088:                             $curroption = 'norequest';
 2089:                         }
 2090:                         foreach my $option (@options) {
 2091:                             my $val = $option;
 2092:                             if ($option eq 'norequest') {
 2093:                                 $val = 0;
 2094:                             }
 2095:                             my $checked = '';
 2096:                             if ($option eq $curroption) {
 2097:                                 $checked = ' checked="checked"';
 2098:                             }
 2099:                             $datatable .= '<span class="LC_nobreak"><label>'.
 2100:                                   '<input type="radio" name="authorreq_'.$type.
 2101:                                   '" value="'.$val.'"'.$checked.' />'.
 2102:                                   $titles{$option}.'</label></span>&nbsp; ';
 2103:                         }
 2104:                     } else {
 2105:                         my $checked = 'checked="checked" ';
 2106:                         if (ref($settings) eq 'HASH') {
 2107:                             if (ref($settings->{$item}) eq 'HASH') {
 2108:                                 if ($settings->{$item}->{$type} == 0) {
 2109:                                     $checked = '';
 2110:                                 } elsif ($settings->{$item}->{$type} == 1) {
 2111:                                     $checked =  'checked="checked" ';
 2112:                                 }
 2113:                             }
 2114:                         }
 2115:                         $datatable .= '<span class="LC_nobreak"><label>'.
 2116:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 2117:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 2118:                                       '</label></span>&nbsp; ';
 2119:                     }
 2120:                 }
 2121:                 if ($context eq 'requestcourses') {
 2122:                     $datatable .= '</tr><tr>';
 2123:                     foreach my $item (@usertools) {
 2124:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 2125:                     }
 2126:                     $datatable .= '</tr></table>';
 2127:                 }
 2128:                 $datatable .= '</td>';
 2129:                 unless (($context eq 'requestcourses') ||
 2130:                         ($context eq 'requestauthor')) {
 2131:                     $datatable .= 
 2132:                               '<td class="LC_right_item">'.
 2133:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2134:                               '<input type="text" name="quota_'.$type.
 2135:                               '" value="'.$currdefquota.
 2136:                               '" size="5" /></span>'.('&nbsp;' x 2).
 2137:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2138:                               '<input type="text" name="authorquota_'.$type.
 2139:                               '" value="'.$currauthorquota.
 2140:                               '" size="5" /></span></td>';
 2141:                 }
 2142:                 $datatable .= '</tr>';
 2143:             }
 2144:         }
 2145:     }
 2146:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2147:         $defaultquota = '20';
 2148:         $authorquota = '500';
 2149:         if (ref($settings) eq 'HASH') {
 2150:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2151:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2152:             } elsif (defined($settings->{'default'})) {
 2153:                 $defaultquota = $settings->{'default'};
 2154:             }
 2155:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 2156:                 $authorquota = $settings->{'authorquota'}->{'default'};
 2157:             }
 2158:         }
 2159:     }
 2160:     $typecount ++;
 2161:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2162:     $datatable .= '<tr'.$css_class.'>'.
 2163:                   '<td>'.$othertitle.'</td>'.
 2164:                   '<td class="LC_left_item">';
 2165:     if ($context eq 'requestcourses') {
 2166:         $datatable .= '<table><tr>';
 2167:     }
 2168:     my %defcell;
 2169:     foreach my $item (@usertools) {
 2170:         if ($context eq 'requestcourses') {
 2171:             my ($curroption,$currlimit);
 2172:             if (ref($settings) eq 'HASH') {
 2173:                 if (ref($settings->{$item}) eq 'HASH') {
 2174:                     $curroption = $settings->{$item}->{'default'};
 2175:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2176:                         $currlimit = $1;
 2177:                     }
 2178:                 }
 2179:             }
 2180:             if (!$curroption) {
 2181:                 $curroption = 'norequest';
 2182:             }
 2183:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2184:             foreach my $option (@options) {
 2185:                 my $val = $option;
 2186:                 if ($option eq 'norequest') {
 2187:                     $val = 0;
 2188:                 }
 2189:                 if ($option eq 'validate') {
 2190:                     my $canvalidate = 0;
 2191:                     if (ref($validations{$item}) eq 'HASH') {
 2192:                         if ($validations{$item}{'default'}) {
 2193:                             $canvalidate = 1;
 2194:                         }
 2195:                     }
 2196:                     next if (!$canvalidate);
 2197:                 }
 2198:                 my $checked = '';
 2199:                 if ($option eq $curroption) {
 2200:                     $checked = ' checked="checked"';
 2201:                 } elsif ($option eq 'autolimit') {
 2202:                     if ($curroption =~ /^autolimit/) {
 2203:                         $checked = ' checked="checked"';
 2204:                     }
 2205:                 }
 2206:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2207:                                   '<input type="radio" name="crsreq_'.$item.
 2208:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2209:                                   $titles{$option}.'</label>';
 2210:                 if ($option eq 'autolimit') {
 2211:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2212:                                        $item.'_limit_default" size="1" '.
 2213:                                        'value="'.$currlimit.'" />';
 2214:                 }
 2215:                 $defcell{$item} .= '</span> ';
 2216:                 if ($option eq 'autolimit') {
 2217:                     $defcell{$item} .= $titles{'unlimited'};
 2218:                 }
 2219:             }
 2220:         } elsif ($context eq 'requestauthor') {
 2221:             my $curroption;
 2222:             if (ref($settings) eq 'HASH') {
 2223:                 $curroption = $settings->{'default'};
 2224:             }
 2225:             if (!$curroption) {
 2226:                 $curroption = 'norequest';
 2227:             }
 2228:             foreach my $option (@options) {
 2229:                 my $val = $option;
 2230:                 if ($option eq 'norequest') {
 2231:                     $val = 0;
 2232:                 }
 2233:                 my $checked = '';
 2234:                 if ($option eq $curroption) {
 2235:                     $checked = ' checked="checked"';
 2236:                 }
 2237:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2238:                               '<input type="radio" name="authorreq_default"'.
 2239:                               ' value="'.$val.'"'.$checked.' />'.
 2240:                               $titles{$option}.'</label></span>&nbsp; ';
 2241:             }
 2242:         } else {
 2243:             my $checked = 'checked="checked" ';
 2244:             if (ref($settings) eq 'HASH') {
 2245:                 if (ref($settings->{$item}) eq 'HASH') {
 2246:                     if ($settings->{$item}->{'default'} == 0) {
 2247:                         $checked = '';
 2248:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2249:                         $checked = 'checked="checked" ';
 2250:                     }
 2251:                 }
 2252:             }
 2253:             $datatable .= '<span class="LC_nobreak"><label>'.
 2254:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2255:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2256:                           '</label></span>&nbsp; ';
 2257:         }
 2258:     }
 2259:     if ($context eq 'requestcourses') {
 2260:         $datatable .= '</tr><tr>';
 2261:         foreach my $item (@usertools) {
 2262:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2263:         }
 2264:         $datatable .= '</tr></table>';
 2265:     }
 2266:     $datatable .= '</td>';
 2267:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2268:         $datatable .= '<td class="LC_right_item">'.
 2269:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2270:                       '<input type="text" name="defaultquota" value="'.
 2271:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2272:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2273:                       '<input type="text" name="authorquota" value="'.
 2274:                       $authorquota.'" size="5" /></span></td>';
 2275:     }
 2276:     $datatable .= '</tr>';
 2277:     $typecount ++;
 2278:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2279:     $datatable .= '<tr'.$css_class.'>'.
 2280:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2281:     if ($context eq 'requestcourses') {
 2282:         $datatable .= &mt('(overrides affiliation, if set)').
 2283:                       '</td>'.
 2284:                       '<td class="LC_left_item">'.
 2285:                       '<table><tr>';
 2286:     } else {
 2287:         $datatable .= &mt('(overrides affiliation, if checked)').
 2288:                       '</td>'.
 2289:                       '<td class="LC_left_item" colspan="2">'.
 2290:                       '<br />';
 2291:     }
 2292:     my %advcell;
 2293:     foreach my $item (@usertools) {
 2294:         if ($context eq 'requestcourses') {
 2295:             my ($curroption,$currlimit);
 2296:             if (ref($settings) eq 'HASH') {
 2297:                 if (ref($settings->{$item}) eq 'HASH') {
 2298:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2299:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2300:                         $currlimit = $1;
 2301:                     }
 2302:                 }
 2303:             }
 2304:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2305:             my $checked = '';
 2306:             if ($curroption eq '') {
 2307:                 $checked = ' checked="checked"';
 2308:             }
 2309:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2310:                                '<input type="radio" name="crsreq_'.$item.
 2311:                                '__LC_adv" value=""'.$checked.' />'.
 2312:                                &mt('No override set').'</label></span>&nbsp; ';
 2313:             foreach my $option (@options) {
 2314:                 my $val = $option;
 2315:                 if ($option eq 'norequest') {
 2316:                     $val = 0;
 2317:                 }
 2318:                 if ($option eq 'validate') {
 2319:                     my $canvalidate = 0;
 2320:                     if (ref($validations{$item}) eq 'HASH') {
 2321:                         if ($validations{$item}{'_LC_adv'}) {
 2322:                             $canvalidate = 1;
 2323:                         }
 2324:                     }
 2325:                     next if (!$canvalidate);
 2326:                 }
 2327:                 my $checked = '';
 2328:                 if ($val eq $curroption) {
 2329:                     $checked = ' checked="checked"';
 2330:                 } elsif ($option eq 'autolimit') {
 2331:                     if ($curroption =~ /^autolimit/) {
 2332:                         $checked = ' checked="checked"';
 2333:                     }
 2334:                 }
 2335:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2336:                                   '<input type="radio" name="crsreq_'.$item.
 2337:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2338:                                   $titles{$option}.'</label>';
 2339:                 if ($option eq 'autolimit') {
 2340:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2341:                                        $item.'_limit__LC_adv" size="1" '.
 2342:                                        'value="'.$currlimit.'" />';
 2343:                 }
 2344:                 $advcell{$item} .= '</span> ';
 2345:                 if ($option eq 'autolimit') {
 2346:                     $advcell{$item} .= $titles{'unlimited'};
 2347:                 }
 2348:             }
 2349:         } elsif ($context eq 'requestauthor') {
 2350:             my $curroption;
 2351:             if (ref($settings) eq 'HASH') {
 2352:                 $curroption = $settings->{'_LC_adv'};
 2353:             }
 2354:             my $checked = '';
 2355:             if ($curroption eq '') {
 2356:                 $checked = ' checked="checked"';
 2357:             }
 2358:             $datatable .= '<span class="LC_nobreak"><label>'.
 2359:                           '<input type="radio" name="authorreq__LC_adv"'.
 2360:                           ' value=""'.$checked.' />'.
 2361:                           &mt('No override set').'</label></span>&nbsp; ';
 2362:             foreach my $option (@options) {
 2363:                 my $val = $option;
 2364:                 if ($option eq 'norequest') {
 2365:                     $val = 0;
 2366:                 }
 2367:                 my $checked = '';
 2368:                 if ($val eq $curroption) {
 2369:                     $checked = ' checked="checked"';
 2370:                 }
 2371:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2372:                               '<input type="radio" name="authorreq__LC_adv"'.
 2373:                               ' value="'.$val.'"'.$checked.' />'.
 2374:                               $titles{$option}.'</label></span>&nbsp; ';
 2375:             }
 2376:         } else {
 2377:             my $checked = 'checked="checked" ';
 2378:             if (ref($settings) eq 'HASH') {
 2379:                 if (ref($settings->{$item}) eq 'HASH') {
 2380:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2381:                         $checked = '';
 2382:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2383:                         $checked = 'checked="checked" ';
 2384:                     }
 2385:                 }
 2386:             }
 2387:             $datatable .= '<span class="LC_nobreak"><label>'.
 2388:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2389:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2390:                           '</label></span>&nbsp; ';
 2391:         }
 2392:     }
 2393:     if ($context eq 'requestcourses') {
 2394:         $datatable .= '</tr><tr>';
 2395:         foreach my $item (@usertools) {
 2396:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2397:         }
 2398:         $datatable .= '</tr></table>';
 2399:     }
 2400:     $datatable .= '</td></tr>';
 2401:     $$rowtotal += $typecount;
 2402:     return $datatable;
 2403: }
 2404: 
 2405: sub print_requestmail {
 2406:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2407:     my ($now,$datatable,%currapp);
 2408:     $now = time;
 2409:     if (ref($settings) eq 'HASH') {
 2410:         if (ref($settings->{'notify'}) eq 'HASH') {
 2411:             if ($settings->{'notify'}{'approval'} ne '') {
 2412:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2413:             }
 2414:         }
 2415:     }
 2416:     my $numinrow = 2;
 2417:     my $css_class;
 2418:     if ($$rowtotal%2) {
 2419:         $css_class = 'LC_odd_row';
 2420:     }
 2421:     if ($customcss) {
 2422:         $css_class .= " $customcss";
 2423:     }
 2424:     $css_class =~ s/^\s+//;
 2425:     if ($css_class) {
 2426:         $css_class = ' class="'.$css_class.'"';
 2427:     }
 2428:     if ($rowstyle) {
 2429:         $css_class .= ' style="'.$rowstyle.'"';
 2430:     }
 2431:     my $text;
 2432:     if ($action eq 'requestcourses') {
 2433:         $text = &mt('Receive notification of course requests requiring approval');
 2434:     } elsif ($action eq 'requestauthor') {
 2435:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2436:     } else {
 2437:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2438:     }
 2439:     $datatable = '<tr'.$css_class.'>'.
 2440:                  ' <td>'.$text.'</td>'.
 2441:                  ' <td class="LC_left_item">';
 2442:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2443:                                                  $action.'notifyapproval',%currapp);
 2444:     if ($numdc > 0) {
 2445:         $datatable .= $table;
 2446:     } else {
 2447:         $datatable .= &mt('There are no active Domain Coordinators');
 2448:     }
 2449:     $datatable .='</td></tr>';
 2450:     return $datatable;
 2451: }
 2452: 
 2453: sub print_studentcode {
 2454:     my ($settings,$rowtotal) = @_;
 2455:     my $rownum = 0; 
 2456:     my ($output,%current);
 2457:     my @crstypes = ('official','unofficial','community','textbook','placement','lti');
 2458:     if (ref($settings) eq 'HASH') {
 2459:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2460:             foreach my $type (@crstypes) {
 2461:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2462:             }
 2463:         }
 2464:     }
 2465:     $output .= '<tr>'.
 2466:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2467:                '<td class="LC_left_item">';
 2468:     foreach my $type (@crstypes) {
 2469:         my $check = ' ';
 2470:         if ($current{$type}) {
 2471:             $check = ' checked="checked" ';
 2472:         }
 2473:         $output .= '<span class="LC_nobreak"><label>'.
 2474:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2475:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2476:     }
 2477:     $output .= '</td></tr>';
 2478:     $$rowtotal ++;
 2479:     return $output;
 2480: }
 2481: 
 2482: sub print_textbookcourses {
 2483:     my ($dom,$type,$settings,$rowtotal) = @_;
 2484:     my $rownum = 0;
 2485:     my $css_class;
 2486:     my $itemcount = 1;
 2487:     my $maxnum = 0;
 2488:     my $bookshash;
 2489:     if (ref($settings) eq 'HASH') {
 2490:         $bookshash = $settings->{$type};
 2491:     }
 2492:     my %ordered;
 2493:     if (ref($bookshash) eq 'HASH') {
 2494:         foreach my $item (keys(%{$bookshash})) {
 2495:             if (ref($bookshash->{$item}) eq 'HASH') {
 2496:                 my $num = $bookshash->{$item}{'order'};
 2497:                 $ordered{$num} = $item;
 2498:             }
 2499:         }
 2500:     }
 2501:     my $confname = $dom.'-domainconfig';
 2502:     my $switchserver = &check_switchserver($dom,$confname);
 2503:     my $maxnum = scalar(keys(%ordered));
 2504:     my $datatable;
 2505:     if (keys(%ordered)) {
 2506:         my @items = sort { $a <=> $b } keys(%ordered);
 2507:         for (my $i=0; $i<@items; $i++) {
 2508:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2509:             my $key = $ordered{$items[$i]};
 2510:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2511:             my $coursetitle = $coursehash{'description'};
 2512:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2513:             if (ref($bookshash->{$key}) eq 'HASH') {
 2514:                 $subject = $bookshash->{$key}->{'subject'};
 2515:                 $title = $bookshash->{$key}->{'title'};
 2516:                 if ($type eq 'textbooks') {
 2517:                     $publisher = $bookshash->{$key}->{'publisher'};
 2518:                     $author = $bookshash->{$key}->{'author'};
 2519:                     $image = $bookshash->{$key}->{'image'};
 2520:                     if ($image ne '') {
 2521:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2522:                         my $imagethumb = "$path/tn-".$imagefile;
 2523:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2524:                     }
 2525:                 }
 2526:             }
 2527:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2528:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2529:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2530:             for (my $k=0; $k<=$maxnum; $k++) {
 2531:                 my $vpos = $k+1;
 2532:                 my $selstr;
 2533:                 if ($k == $i) {
 2534:                     $selstr = ' selected="selected" ';
 2535:                 }
 2536:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2537:             }
 2538:             $datatable .= '</select>'.('&nbsp;'x2).
 2539:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2540:                 &mt('Delete?').'</label></span></td>'.
 2541:                 '<td colspan="2">'.
 2542:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2543:                 ('&nbsp;'x2).
 2544:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2545:             if ($type eq 'textbooks') {
 2546:                 $datatable .= ('&nbsp;'x2).
 2547:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2548:                               ('&nbsp;'x2).
 2549:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2550:                               ('&nbsp;'x2).
 2551:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2552:                 if ($image) {
 2553:                     $datatable .= $imgsrc.
 2554:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2555:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2556:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2557:                 }
 2558:                 if ($switchserver) {
 2559:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2560:                 } else {
 2561:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2562:                 }
 2563:             }
 2564:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2565:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2566:                           $coursetitle.'</span></td></tr>'."\n";
 2567:             $itemcount ++;
 2568:         }
 2569:     }
 2570:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2571:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2572:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2573:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2574:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2575:     for (my $k=0; $k<$maxnum+1; $k++) {
 2576:         my $vpos = $k+1;
 2577:         my $selstr;
 2578:         if ($k == $maxnum) {
 2579:             $selstr = ' selected="selected" ';
 2580:         }
 2581:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2582:     }
 2583:     $datatable .= '</select>&nbsp;'."\n".
 2584:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</span></td>'."\n".
 2585:                   '<td colspan="2">'.
 2586:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2587:                   ('&nbsp;'x2).
 2588:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2589:                   ('&nbsp;'x2);
 2590:     if ($type eq 'textbooks') {
 2591:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2592:                       ('&nbsp;'x2).
 2593:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2594:                       ('&nbsp;'x2).
 2595:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2596:         if ($switchserver) {
 2597:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2598:         } else {
 2599:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2600:         }
 2601:         $datatable .= '</span>'."\n";
 2602:     }
 2603:     $datatable .= '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2604:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2605:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2606:                   &Apache::loncommon::selectcourse_link
 2607:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course').
 2608:                   '</span></td>'."\n".
 2609:                   '</tr>'."\n";
 2610:     $itemcount ++;
 2611:     return $datatable;
 2612: }
 2613: 
 2614: sub textbookcourses_javascript {
 2615:     my ($settings) = @_;
 2616:     return unless(ref($settings) eq 'HASH');
 2617:     my (%ordered,%total,%jstext);
 2618:     foreach my $type ('textbooks','templates') {
 2619:         $total{$type} = 0;
 2620:         if (ref($settings->{$type}) eq 'HASH') {
 2621:             foreach my $item (keys(%{$settings->{$type}})) {
 2622:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2623:                     my $num = $settings->{$type}->{$item}{'order'};
 2624:                     $ordered{$type}{$num} = $item;
 2625:                 }
 2626:             }
 2627:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2628:         }
 2629:         my @jsarray = ();
 2630:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2631:             push(@jsarray,$ordered{$type}{$item});
 2632:         }
 2633:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2634:     }
 2635:     return <<"ENDSCRIPT";
 2636: <script type="text/javascript">
 2637: // <![CDATA[
 2638: function reorderBooks(form,item,caller) {
 2639:     var changedVal;
 2640: $jstext{'textbooks'};
 2641: $jstext{'templates'};
 2642:     var newpos;
 2643:     var maxh;
 2644:     if (caller == 'textbooks') {  
 2645:         newpos = 'textbooks_addbook_pos';
 2646:         maxh = 1 + $total{'textbooks'};
 2647:     } else {
 2648:         newpos = 'templates_addbook_pos';
 2649:         maxh = 1 + $total{'templates'};
 2650:     }
 2651:     var current = new Array;
 2652:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2653:     if (item == newpos) {
 2654:         changedVal = newitemVal;
 2655:     } else {
 2656:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2657:         current[newitemVal] = newpos;
 2658:     }
 2659:     if (caller == 'textbooks') {
 2660:         for (var i=0; i<textbooks.length; i++) {
 2661:             var elementName = 'textbooks_'+textbooks[i];
 2662:             if (elementName != item) {
 2663:                 if (form.elements[elementName]) {
 2664:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2665:                     current[currVal] = elementName;
 2666:                 }
 2667:             }
 2668:         }
 2669:     }
 2670:     if (caller == 'templates') {
 2671:         for (var i=0; i<templates.length; i++) {
 2672:             var elementName = 'templates_'+templates[i];
 2673:             if (elementName != item) {
 2674:                 if (form.elements[elementName]) {
 2675:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2676:                     current[currVal] = elementName;
 2677:                 }
 2678:             }
 2679:         }
 2680:     }
 2681:     var oldVal;
 2682:     for (var j=0; j<maxh; j++) {
 2683:         if (current[j] == undefined) {
 2684:             oldVal = j;
 2685:         }
 2686:     }
 2687:     if (oldVal < changedVal) {
 2688:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2689:            var elementName = current[k];
 2690:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2691:         }
 2692:     } else {
 2693:         for (var k=changedVal; k<oldVal; k++) {
 2694:             var elementName = current[k];
 2695:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2696:         }
 2697:     }
 2698:     return;
 2699: }
 2700: 
 2701: // ]]>
 2702: </script>
 2703: 
 2704: ENDSCRIPT
 2705: }
 2706: 
 2707: sub ltitools_javascript {
 2708:     my ($settings) = @_;
 2709:     my $togglejs = &ltitools_toggle_js();
 2710:     unless (ref($settings) eq 'HASH') {
 2711:         return $togglejs;
 2712:     }
 2713:     my (%ordered,$total,%jstext);
 2714:     $total = 0;
 2715:     foreach my $item (keys(%{$settings})) {
 2716:         if (ref($settings->{$item}) eq 'HASH') {
 2717:             my $num = $settings->{$item}{'order'};
 2718:             $ordered{$num} = $item;
 2719:         }
 2720:     }
 2721:     $total = scalar(keys(%{$settings}));
 2722:     my @jsarray = ();
 2723:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2724:         push(@jsarray,$ordered{$item});
 2725:     }
 2726:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 2727:     return <<"ENDSCRIPT";
 2728: <script type="text/javascript">
 2729: // <![CDATA[
 2730: function reorderLTITools(form,item) {
 2731:     var changedVal;
 2732: $jstext
 2733:     var newpos = 'ltitools_add_pos';
 2734:     var maxh = 1 + $total;
 2735:     var current = new Array;
 2736:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2737:     if (item == newpos) {
 2738:         changedVal = newitemVal;
 2739:     } else {
 2740:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2741:         current[newitemVal] = newpos;
 2742:     }
 2743:     for (var i=0; i<ltitools.length; i++) {
 2744:         var elementName = 'ltitools_'+ltitools[i];
 2745:         if (elementName != item) {
 2746:             if (form.elements[elementName]) {
 2747:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2748:                 current[currVal] = elementName;
 2749:             }
 2750:         }
 2751:     }
 2752:     var oldVal;
 2753:     for (var j=0; j<maxh; j++) {
 2754:         if (current[j] == undefined) {
 2755:             oldVal = j;
 2756:         }
 2757:     }
 2758:     if (oldVal < changedVal) {
 2759:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2760:            var elementName = current[k];
 2761:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2762:         }
 2763:     } else {
 2764:         for (var k=changedVal; k<oldVal; k++) {
 2765:             var elementName = current[k];
 2766:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2767:         }
 2768:     }
 2769:     return;
 2770: }
 2771: 
 2772: // ]]>
 2773: </script>
 2774: 
 2775: $togglejs
 2776: 
 2777: ENDSCRIPT
 2778: }
 2779: 
 2780: sub ltitools_toggle_js {
 2781:     return <<"ENDSCRIPT";
 2782: <script type="text/javascript">
 2783: // <![CDATA[
 2784: 
 2785: function toggleLTITools(form,setting,item) {
 2786:     var radioname = '';
 2787:     var divid = '';
 2788:     if ((setting == 'passback') || (setting == 'roster')) {
 2789:         radioname = 'ltitools_'+setting+'_'+item;
 2790:         divid = 'ltitools_'+setting+'time_'+item;
 2791:         var num = form.elements[radioname].length;
 2792:         if (num) {
 2793:             var setvis = '';
 2794:             for (var i=0; i<num; i++) {
 2795:                 if (form.elements[radioname][i].checked) {
 2796:                     if (form.elements[radioname][i].value == '1') {
 2797:                         if (document.getElementById(divid)) {
 2798:                             document.getElementById(divid).style.display = 'inline-block';
 2799:                         }
 2800:                         setvis = 1;
 2801:                     }
 2802:                     break;
 2803:                 }
 2804:             }
 2805:         }
 2806:         if (!setvis) {
 2807:             if (document.getElementById(divid)) {
 2808:                 document.getElementById(divid).style.display = 'none';
 2809:             }
 2810:         }
 2811:     }
 2812:     if (setting == 'user') {
 2813:         divid = 'ltitools_'+setting+'_div_'+item;
 2814:         var checkid = 'ltitools_'+setting+'_field_'+item;
 2815:         if (document.getElementById(divid)) {
 2816:             if (document.getElementById(checkid)) {
 2817:                 if (document.getElementById(checkid).checked) {
 2818:                     document.getElementById(divid).style.display = 'inline-block';
 2819:                 } else {
 2820:                     document.getElementById(divid).style.display = 'none';
 2821:                 }
 2822:             }
 2823:         }
 2824:     }
 2825:     return;
 2826: }
 2827: // ]]>
 2828: </script>
 2829: 
 2830: ENDSCRIPT
 2831: }
 2832: 
 2833: sub proctoring_javascript {
 2834:     my ($settings) = @_;
 2835:     my (%ordered,$total,%jstext);
 2836:     $total = 0;
 2837:     if (ref($settings) eq 'HASH') {
 2838:         foreach my $item (keys(%{$settings})) {
 2839:             if (ref($settings->{$item}) eq 'HASH') {
 2840:                 my $num = $settings->{$item}{'order'};
 2841:                 $ordered{$num} = $item;
 2842:             }
 2843:         }
 2844:         $total = scalar(keys(%{$settings}));
 2845:     } else {
 2846:         %ordered = (
 2847:                        0 => 'proctorio',
 2848:                        1 => 'examity',
 2849:                    );
 2850:         $total = 2; 
 2851:     }
 2852:     my @jsarray = ();
 2853:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2854:         push(@jsarray,$ordered{$item});
 2855:     }
 2856:     my $jstext = '    var proctors = Array('."'".join("','",@jsarray)."'".');'."\n";
 2857:     return <<"ENDSCRIPT";
 2858: <script type="text/javascript">
 2859: // <![CDATA[
 2860: function reorderProctoring(form,item) {
 2861:     var changedVal;
 2862: $jstext
 2863:     var maxh = $total;
 2864:     var current = new Array;
 2865:     var changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2866:     for (var i=0; i<proctors.length; i++) {
 2867:         var elementName = 'proctoring_pos_'+proctors[i];
 2868:         if (elementName != item) {
 2869:             if (form.elements[elementName]) {
 2870:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2871:                 current[currVal] = elementName;
 2872:             }
 2873:         }
 2874:     }
 2875:     var oldVal;
 2876:     for (var j=0; j<maxh; j++) {
 2877:         if (current[j] == undefined) {
 2878:             oldVal = j;
 2879:         }
 2880:     }
 2881:     if (oldVal < changedVal) {
 2882:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2883:            var elementName = current[k];
 2884:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2885:         }
 2886:     } else {
 2887:         for (var k=changedVal; k<oldVal; k++) {
 2888:             var elementName = current[k];
 2889:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2890:         }
 2891:     }
 2892:     return;
 2893: }
 2894: 
 2895: function toggleProctoring(form,item) {
 2896:     var fieldsets = document.getElementsByClassName('proctoring_'+item);
 2897:     if (fieldsets.length) {
 2898:         var radioname = 'proctoring_available_'+item;
 2899:         var num = form.elements[radioname].length;
 2900:         if (num) {
 2901:             var setvis = '';
 2902:             for (var i=0; i<num; i++) {
 2903:                 if (form.elements[radioname][i].checked) {
 2904:                     if (form.elements[radioname][i].value == '1') {
 2905:                        setvis = 1;
 2906:                        break;
 2907:                     }
 2908:                 }
 2909:             }
 2910:             for (var j=0; j<fieldsets.length; j++) {
 2911:                 if (setvis) {
 2912:                     fieldsets[j].style.display = 'block';
 2913:                 } else {
 2914:                     fieldsets[j].style.display = 'none';
 2915:                 }
 2916:             }
 2917:         }
 2918:     }
 2919:     return;
 2920: }
 2921: 
 2922: // ]]>
 2923: </script>
 2924: 
 2925: ENDSCRIPT
 2926: }
 2927: 
 2928: 
 2929: sub lti_javascript {
 2930:     my ($settings) = @_;
 2931:     my $togglejs = &lti_toggle_js();
 2932:     unless (ref($settings) eq 'HASH') {
 2933:         return $togglejs;
 2934:     }
 2935:     my (%ordered,$total,%jstext);
 2936:     $total = 0;
 2937:     foreach my $item (keys(%{$settings})) {
 2938:         if (ref($settings->{$item}) eq 'HASH') {
 2939:             my $num = $settings->{$item}{'order'};
 2940:             $ordered{$num} = $item;
 2941:         }
 2942:     }
 2943:     $total = scalar(keys(%{$settings}));
 2944:     my @jsarray = ();
 2945:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2946:         push(@jsarray,$ordered{$item});
 2947:     }
 2948:     my $jstext = '    var lti = Array('."'".join("','",@jsarray)."'".');'."\n";
 2949:     return <<"ENDSCRIPT";
 2950: <script type="text/javascript">
 2951: // <![CDATA[
 2952: function reorderLTI(form,item) {
 2953:     var changedVal;
 2954: $jstext
 2955:     var newpos = 'lti_pos_add';
 2956:     var maxh = 1 + $total;
 2957:     var current = new Array;
 2958:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2959:     if (item == newpos) {
 2960:         changedVal = newitemVal;
 2961:     } else {
 2962:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2963:         current[newitemVal] = newpos;
 2964:     }
 2965:     for (var i=0; i<lti.length; i++) {
 2966:         var elementName = 'lti_pos_'+lti[i];
 2967:         if (elementName != item) {
 2968:             if (form.elements[elementName]) {
 2969:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2970:                 current[currVal] = elementName;
 2971:             }
 2972:         }
 2973:     }
 2974:     var oldVal;
 2975:     for (var j=0; j<maxh; j++) {
 2976:         if (current[j] == undefined) {
 2977:             oldVal = j;
 2978:         }
 2979:     }
 2980:     if (oldVal < changedVal) {
 2981:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2982:            var elementName = current[k];
 2983:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2984:         }
 2985:     } else {
 2986:         for (var k=changedVal; k<oldVal; k++) {
 2987:             var elementName = current[k];
 2988:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2989:         }
 2990:     }
 2991:     return;
 2992: }
 2993: // ]]>
 2994: </script>
 2995: 
 2996: $togglejs
 2997: 
 2998: ENDSCRIPT
 2999: }
 3000: 
 3001: sub lti_toggle_js {
 3002:     my %lcauthparmtext = &Apache::lonlocal::texthash (
 3003:                             localauth => 'Local auth argument',
 3004:                             krb       => 'Kerberos domain',
 3005:                          );
 3006:     return <<"ENDSCRIPT";
 3007: <script type="text/javascript">
 3008: // <![CDATA[
 3009: 
 3010: function toggleLTI(form,setting,item) {
 3011:     if (setting == 'requser') {
 3012:         var fieldsets = document.getElementsByClassName('ltioption_'+item);
 3013:         if (fieldsets.length) {
 3014:             var radioname = 'lti_'+setting+'_'+item;
 3015:             var num = form.elements[radioname].length;
 3016:             if (num) {
 3017:                 var setvis = '';
 3018:                 for (var i=0; i<num; i++) {
 3019:                     if (form.elements[radioname][i].checked) {
 3020:                         if (form.elements[radioname][i].value == '1') {
 3021:                            setvis = 1;
 3022:                            break;
 3023:                         }
 3024:                     }
 3025:                 }
 3026:                 for (var j=0; j<fieldsets.length; j++) {
 3027:                     if (setvis) {
 3028:                         fieldsets[j].style.display = 'block';
 3029:                     } else {
 3030:                         fieldsets[j].style.display = 'none';
 3031:                     }
 3032:                 }
 3033:             }
 3034:         }
 3035:     } else if ((setting == 'user') || (setting == 'crs') || (setting == 'passback') || (setting == 'callback')) {
 3036:         var radioname = '';
 3037:         var divid = '';
 3038:         if (setting == 'user') {
 3039:             radioname = 'lti_mapuser_'+item;
 3040:             divid = 'lti_userfield_'+item;
 3041:         } else if (setting == 'crs') {
 3042:             radioname = 'lti_mapcrs_'+item;
 3043:             divid = 'lti_crsfield_'+item;
 3044:         } else if (setting == 'callback') {
 3045:             radioname = 'lti_callback_'+item;
 3046:             divid = 'lti_callbackfield_'+item;
 3047:         } else {
 3048:             radioname = 'lti_passback_'+item;
 3049:             divid =  'lti_passback_'+item;
 3050:         }
 3051:         var num = form.elements[radioname].length;
 3052:         if (num) {
 3053:             var setvis = '';
 3054:             for (var i=0; i<num; i++) {
 3055:                if (form.elements[radioname][i].checked) {
 3056:                    if ((setting == 'passback') || (setting == 'callback')) {
 3057:                        if (form.elements[radioname][i].value == '1') {
 3058:                            if (document.getElementById(divid)) {
 3059:                                document.getElementById(divid).style.display = 'inline-block';
 3060:                            }
 3061:                            setvis = 1;
 3062:                            break;
 3063:                        }
 3064:                    } else {
 3065:                        if (form.elements[radioname][i].value == 'other') {
 3066:                            if (document.getElementById(divid)) {
 3067:                                document.getElementById(divid).style.display = 'inline-block';
 3068:                            }
 3069:                            setvis = 1;
 3070:                            break;
 3071:                        }
 3072:                    }
 3073:                } 
 3074:             }
 3075:             if (!setvis) {
 3076:                 if (document.getElementById(divid)) {
 3077:                     document.getElementById(divid).style.display = 'none';
 3078:                 }
 3079:             }
 3080:         }
 3081:     } else if ((setting == 'sec') || (setting == 'secsrc')) {
 3082:         var numsec = form.elements['lti_crssec_'+item].length;
 3083:         if (numsec) {
 3084:             var setvis = '';
 3085:             for (var i=0; i<numsec; i++) {
 3086:                 if (form.elements['lti_crssec_'+item][i].checked) {
 3087:                     if (form.elements['lti_crssec_'+item][i].value == '1') {
 3088:                         if (document.getElementById('lti_crssecfield_'+item)) {
 3089:                             document.getElementById('lti_crssecfield_'+item).style.display = 'inline-block';
 3090:                             setvis = 1;
 3091:                             var numsrcsec = form.elements['lti_crssecsrc_'+item].length;
 3092:                             if (numsrcsec) {
 3093:                                 var setsrcvis = '';
 3094:                                 for (var j=0; j<numsrcsec; j++) {
 3095:                                     if (form.elements['lti_crssecsrc_'+item][j].checked) {
 3096:                                         if (form.elements['lti_crssecsrc_'+item][j].value == 'other') {
 3097:                                             if (document.getElementById('lti_secsrcfield_'+item)) {
 3098:                                                 document.getElementById('lti_secsrcfield_'+item).style.display = 'inline-block';
 3099:                                                 setsrcvis = 1;
 3100:                                             }
 3101:                                         }
 3102:                                     }
 3103:                                 }
 3104:                                 if (!setsrcvis) {
 3105:                                     if (document.getElementById('lti_secsrcfield_'+item)) {
 3106:                                         document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3107:                                     }
 3108:                                 }
 3109:                             }
 3110:                         }
 3111:                     }
 3112:                 }
 3113:             }
 3114:             if (!setvis) {
 3115:                 if (document.getElementById('lti_crssecfield_'+item)) {
 3116:                     document.getElementById('lti_crssecfield_'+item).style.display = 'none';
 3117:                 }
 3118:                 if (document.getElementById('lti_secsrcfield_'+item)) {
 3119:                     document.getElementById('lti_secsrcfield_'+item).style.display = 'none';
 3120:                 }
 3121:             }
 3122:         }
 3123:     } else if (setting == 'lcauth') {
 3124:         var numauth = form.elements['lti_lcauth_'+item].length;
 3125:         if (numauth) {
 3126:             for (var i=0; i<numauth; i++) {
 3127:                 if (form.elements['lti_lcauth_'+item][i].checked) {
 3128:                     if (document.getElementById('lti_'+setting+'_parmrow_'+item)) {
 3129:                         if ((form.elements['lti_'+setting+'_'+item][i].value == 'internal') || (form.elements['lti_'+setting+'_'+item][i].value == 'lti')) {
 3130:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'none';
 3131:                         } else {
 3132:                             document.getElementById('lti_'+setting+'_parmrow_'+item).style.display = 'table-row';
 3133:                             if (document.getElementById('lti_'+setting+'_parmtext_'+item)) {
 3134:                                 if (form.elements['lti_'+setting+'_'+item][i].value == 'localauth') {
 3135:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'localauth'}";
 3136:                                 } else {
 3137:                                     document.getElementById('lti_'+setting+'_parmtext_'+item).innerHTML = "$lcauthparmtext{'krb'}";
 3138:                                 }
 3139:                             }
 3140:                         }
 3141:                     }
 3142:                 }
 3143:             }
 3144:         }
 3145:     } else if (setting == 'lcmenu') {
 3146:         var menus = new Array('lti_topmenu_'+item,'lti_inlinemenu_'+item);
 3147:         var divid = 'lti_menufield_'+item;
 3148:         var setvis = '';
 3149:         for (var i=0; i<menus.length; i++) {
 3150:             var radioname = menus[i];  
 3151:             var num = form.elements[radioname].length;
 3152:             if (num) {
 3153:                 for (var j=0; j<num; j++) {
 3154:                     if (form.elements[radioname][j].checked) {
 3155:                         if (form.elements[radioname][j].value == '1') {
 3156:                             if (document.getElementById(divid)) {
 3157:                                 document.getElementById(divid).style.display = 'inline-block';
 3158:                             }
 3159:                             setvis = 1;
 3160:                             break;
 3161:                         }
 3162:                     }
 3163:                 }
 3164:             }
 3165:             if (setvis == 1) {
 3166:                 break;
 3167:             }
 3168:         }
 3169:         if (!setvis) {
 3170:             if (document.getElementById(divid)) {
 3171:                 document.getElementById(divid).style.display = 'none';
 3172:             }
 3173:         }
 3174:     }
 3175:     return;
 3176: }
 3177: // ]]>
 3178: </script>
 3179: 
 3180: ENDSCRIPT
 3181: }
 3182: 
 3183: sub print_autoenroll {
 3184:     my ($dom,$settings,$rowtotal) = @_;
 3185:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 3186:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 3187:     if (ref($settings) eq 'HASH') {
 3188:         if (exists($settings->{'run'})) {
 3189:             if ($settings->{'run'} eq '0') {
 3190:                 $runoff = ' checked="checked" ';
 3191:                 $runon = ' ';
 3192:             } else {
 3193:                 $runon = ' checked="checked" ';
 3194:                 $runoff = ' ';
 3195:             }
 3196:         } else {
 3197:             if ($autorun) {
 3198:                 $runon = ' checked="checked" ';
 3199:                 $runoff = ' ';
 3200:             } else {
 3201:                 $runoff = ' checked="checked" ';
 3202:                 $runon = ' ';
 3203:             }
 3204:         }
 3205:         if (exists($settings->{'co-owners'})) {
 3206:             if ($settings->{'co-owners'} eq '0') {
 3207:                 $coownersoff = ' checked="checked" ';
 3208:                 $coownerson = ' ';
 3209:             } else {
 3210:                 $coownerson = ' checked="checked" ';
 3211:                 $coownersoff = ' ';
 3212:             }
 3213:         } else {
 3214:             $coownersoff = ' checked="checked" ';
 3215:             $coownerson = ' ';
 3216:         }
 3217:         if (exists($settings->{'sender_domain'})) {
 3218:             $defdom = $settings->{'sender_domain'};
 3219:         }
 3220:         if (exists($settings->{'autofailsafe'})) {
 3221:             $failsafe = $settings->{'autofailsafe'};
 3222:         }
 3223:     } else {
 3224:         if ($autorun) {
 3225:             $runon = ' checked="checked" ';
 3226:             $runoff = ' ';
 3227:         } else {
 3228:             $runoff = ' checked="checked" ';
 3229:             $runon = ' ';
 3230:         }
 3231:     }
 3232:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 3233:     my $notif_sender;
 3234:     if (ref($settings) eq 'HASH') {
 3235:         $notif_sender = $settings->{'sender_uname'};
 3236:     }
 3237:     my $datatable='<tr class="LC_odd_row">'.
 3238:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 3239:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3240:                   '<input type="radio" name="autoenroll_run"'.
 3241:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3242:                   '<label><input type="radio" name="autoenroll_run"'.
 3243:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3244:                   '</tr><tr>'.
 3245:                   '<td>'.&mt('Notification messages - sender').
 3246:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 3247:                   &mt('username').':&nbsp;'.
 3248:                   '<input type="text" name="sender_uname" value="'.
 3249:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 3250:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 3251:                   '<tr class="LC_odd_row">'.
 3252:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 3253:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3254:                   '<input type="radio" name="autoassign_coowners"'.
 3255:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3256:                   '<label><input type="radio" name="autoassign_coowners"'.
 3257:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3258:                   '</tr><tr>'.
 3259:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 3260:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3261:                   '<input type="text" name="autoenroll_failsafe"'.
 3262:                   ' value="'.$failsafe.'" size="4" /></span></td></tr>';
 3263:     $$rowtotal += 4;
 3264:     return $datatable;
 3265: }
 3266: 
 3267: sub print_autoupdate {
 3268:     my ($position,$dom,$settings,$rowtotal) = @_;
 3269:     my $datatable;
 3270:     if ($position eq 'top') {
 3271:         my $updateon = ' ';
 3272:         my $updateoff = ' checked="checked" ';
 3273:         my $classlistson = ' ';
 3274:         my $classlistsoff = ' checked="checked" ';
 3275:         if (ref($settings) eq 'HASH') {
 3276:             if ($settings->{'run'} eq '1') {
 3277:                 $updateon = $updateoff;
 3278:                 $updateoff = ' ';
 3279:             }
 3280:             if ($settings->{'classlists'} eq '1') {
 3281:                 $classlistson = $classlistsoff;
 3282:                 $classlistsoff = ' ';
 3283:             }
 3284:         }
 3285:         my %title = (
 3286:                    run => 'Auto-update active?',
 3287:                    classlists => 'Update information in classlists?',
 3288:                     );
 3289:         $datatable = '<tr class="LC_odd_row">'. 
 3290:                   '<td>'.&mt($title{'run'}).'</td>'.
 3291:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3292:                   '<input type="radio" name="autoupdate_run"'.
 3293:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3294:                   '<label><input type="radio" name="autoupdate_run"'.
 3295:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 3296:                   '</tr><tr>'.
 3297:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 3298:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 3299:                   '<label><input type="radio" name="classlists"'.
 3300:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3301:                   '<label><input type="radio" name="classlists"'.
 3302:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 3303:                   '</tr>';
 3304:         $$rowtotal += 2;
 3305:     } elsif ($position eq 'middle') {
 3306:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3307:         my $numinrow = 3;
 3308:         my $locknamesettings;
 3309:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 3310:                                      $dom,$numinrow,$othertitle,
 3311:                                     'lockablenames',$rowtotal);
 3312:         $$rowtotal ++;
 3313:     } else {
 3314:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3315:         my @fields = ('lastname','firstname','middlename','generation',
 3316:                       'permanentemail','id');
 3317:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 3318:         my $numrows = 0;
 3319:         if (ref($types) eq 'ARRAY') {
 3320:             if (@{$types} > 0) {
 3321:                 $datatable = 
 3322:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 3323:                                          \@fields,$types,\$numrows);
 3324:                     $$rowtotal += @{$types}; 
 3325:             }
 3326:         }
 3327:         $datatable .= 
 3328:             &usertype_update_row($settings,{'default' => $othertitle},
 3329:                                  \%fieldtitles,\@fields,['default'],
 3330:                                  \$numrows);
 3331:         $$rowtotal ++;     
 3332:     }
 3333:     return $datatable;
 3334: }
 3335: 
 3336: sub print_autocreate {
 3337:     my ($dom,$settings,$rowtotal) = @_;
 3338:     my (%createon,%createoff,%currhash);
 3339:     my @types = ('xml','req');
 3340:     if (ref($settings) eq 'HASH') {
 3341:         foreach my $item (@types) {
 3342:             $createoff{$item} = ' checked="checked" ';
 3343:             $createon{$item} = ' ';
 3344:             if (exists($settings->{$item})) {
 3345:                 if ($settings->{$item}) {
 3346:                     $createon{$item} = ' checked="checked" ';
 3347:                     $createoff{$item} = ' ';
 3348:                 }
 3349:             }
 3350:         }
 3351:         if ($settings->{'xmldc'} ne '') {
 3352:             $currhash{$settings->{'xmldc'}} = 1;
 3353:         }
 3354:     } else {
 3355:         foreach my $item (@types) {
 3356:             $createoff{$item} = ' checked="checked" ';
 3357:             $createon{$item} = ' ';
 3358:         }
 3359:     }
 3360:     $$rowtotal += 2;
 3361:     my $numinrow = 2;
 3362:     my $datatable='<tr class="LC_odd_row">'.
 3363:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 3364:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3365:                   '<input type="radio" name="autocreate_xml"'.
 3366:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3367:                   '<label><input type="radio" name="autocreate_xml"'.
 3368:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 3369:                   '</td></tr><tr>'.
 3370:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 3371:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3372:                   '<input type="radio" name="autocreate_req"'.
 3373:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3374:                   '<label><input type="radio" name="autocreate_req"'.
 3375:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 3376:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 3377:                                                    'autocreate_xmldc',%currhash);
 3378:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 3379:     if ($numdc > 1) {
 3380:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 3381:                       '</td><td class="LC_left_item">';
 3382:     } else {
 3383:         $datatable .= &mt('Course creation processed as:').
 3384:                       '</td><td class="LC_right_item">';
 3385:     }
 3386:     $datatable .= $dctable.'</td></tr>';
 3387:     $$rowtotal += $rows;
 3388:     return $datatable;
 3389: }
 3390: 
 3391: sub print_directorysrch {
 3392:     my ($position,$dom,$settings,$rowtotal) = @_;
 3393:     my $datatable;
 3394:     if ($position eq 'top') {
 3395:         my $instsrchon = ' ';
 3396:         my $instsrchoff = ' checked="checked" ';
 3397:         my ($exacton,$containson,$beginson);
 3398:         my $instlocalon = ' ';
 3399:         my $instlocaloff = ' checked="checked" ';
 3400:         if (ref($settings) eq 'HASH') {
 3401:             if ($settings->{'available'} eq '1') {
 3402:                 $instsrchon = $instsrchoff;
 3403:                 $instsrchoff = ' ';
 3404:             }
 3405:             if ($settings->{'localonly'} eq '1') {
 3406:                 $instlocalon = $instlocaloff;
 3407:                 $instlocaloff = ' ';
 3408:             }
 3409:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 3410:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 3411:                     if ($type eq 'exact') {
 3412:                         $exacton = ' checked="checked" ';
 3413:                     } elsif ($type eq 'contains') {
 3414:                         $containson = ' checked="checked" ';
 3415:                     } elsif ($type eq 'begins') {
 3416:                         $beginson = ' checked="checked" ';
 3417:                     }
 3418:                 }
 3419:             } else {
 3420:                 if ($settings->{'searchtypes'} eq 'exact') {
 3421:                     $exacton = ' checked="checked" ';
 3422:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 3423:                     $containson = ' checked="checked" ';
 3424:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 3425:                     $exacton = ' checked="checked" ';
 3426:                     $containson = ' checked="checked" ';
 3427:                 }
 3428:             }
 3429:         }
 3430:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 3431:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3432: 
 3433:         my $numinrow = 4;
 3434:         my $cansrchrow = 0;
 3435:         $datatable='<tr class="LC_odd_row">'.
 3436:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 3437:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3438:                    '<input type="radio" name="dirsrch_available"'.
 3439:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3440:                    '<label><input type="radio" name="dirsrch_available"'.
 3441:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3442:                    '</tr><tr>'.
 3443:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 3444:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3445:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 3446:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 3447:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 3448:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 3449:                    '</tr>';
 3450:         $$rowtotal += 2;
 3451:         if (ref($usertypes) eq 'HASH') {
 3452:             if (keys(%{$usertypes}) > 0) {
 3453:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3454:                                              $numinrow,$othertitle,'cansearch',
 3455:                                              $rowtotal);
 3456:                 $cansrchrow = 1;
 3457:             }
 3458:         }
 3459:         if ($cansrchrow) {
 3460:             $$rowtotal ++;
 3461:             $datatable .= '<tr>';
 3462:         } else {
 3463:             $datatable .= '<tr class="LC_odd_row">';
 3464:         }
 3465:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 3466:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 3467:         foreach my $title (@{$titleorder}) {
 3468:             if (defined($searchtitles->{$title})) {
 3469:                 my $check = ' ';
 3470:                 if (ref($settings) eq 'HASH') {
 3471:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 3472:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 3473:                             $check = ' checked="checked" ';
 3474:                         }
 3475:                     }
 3476:                 }
 3477:                 $datatable .= '<td class="LC_left_item">'.
 3478:                               '<span class="LC_nobreak"><label>'.
 3479:                               '<input type="checkbox" name="searchby" '.
 3480:                               'value="'.$title.'"'.$check.'/>'.
 3481:                               $searchtitles->{$title}.'</label></span></td>';
 3482:             }
 3483:         }
 3484:         $datatable .= '</tr></table></td></tr>';
 3485:         $$rowtotal ++;
 3486:         if ($cansrchrow) {
 3487:             $datatable .= '<tr class="LC_odd_row">';
 3488:         } else {
 3489:             $datatable .= '<tr>';
 3490:         }
 3491:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 3492:                       '<td class="LC_left_item" colspan="2">'.
 3493:                       '<span class="LC_nobreak"><label>'.
 3494:                       '<input type="checkbox" name="searchtypes" '.
 3495:                       $exacton.' value="exact" />'.&mt('Exact match').
 3496:                       '</label>&nbsp;'.
 3497:                       '<label><input type="checkbox" name="searchtypes" '.
 3498:                       $beginson.' value="begins" />'.&mt('Begins with').
 3499:                       '</label>&nbsp;'.
 3500:                       '<label><input type="checkbox" name="searchtypes" '.
 3501:                       $containson.' value="contains" />'.&mt('Contains').
 3502:                       '</label></span></td></tr>';
 3503:         $$rowtotal ++;
 3504:     } else {
 3505:         my $domsrchon = ' checked="checked" ';
 3506:         my $domsrchoff = ' ';
 3507:         my $domlocalon = ' ';
 3508:         my $domlocaloff = ' checked="checked" ';
 3509:         if (ref($settings) eq 'HASH') {
 3510:             if ($settings->{'lclocalonly'} eq '1') {
 3511:                 $domlocalon = $domlocaloff;
 3512:                 $domlocaloff = ' ';
 3513:             }
 3514:             if ($settings->{'lcavailable'} eq '0') {
 3515:                 $domsrchoff = $domsrchon;
 3516:                 $domsrchon = ' ';
 3517:             }
 3518:         }
 3519:         $datatable='<tr class="LC_odd_row">'.
 3520:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 3521:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3522:                       '<input type="radio" name="dirsrch_domavailable"'.
 3523:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 3524:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 3525:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 3526:                       '</tr><tr>'.
 3527:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 3528:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3529:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 3530:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 3531:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 3532:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 3533:                       '</tr>';
 3534:         $$rowtotal += 2;
 3535:     }
 3536:     return $datatable;
 3537: }
 3538: 
 3539: sub print_contacts {
 3540:     my ($position,$dom,$settings,$rowtotal) = @_;
 3541:     my $datatable;
 3542:     my @contacts = ('adminemail','supportemail');
 3543:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 3544:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings,%lonstatus);
 3545:     if ($position eq 'top') {
 3546:         if (ref($settings) eq 'HASH') {
 3547:             foreach my $item (@contacts) {
 3548:                 if (exists($settings->{$item})) {
 3549:                     $to{$item} = $settings->{$item};
 3550:                 }
 3551:             }
 3552:         }
 3553:     } elsif ($position eq 'middle') {
 3554:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 3555:                      'updatesmail','idconflictsmail','hostipmail');
 3556:         foreach my $type (@mailings) {
 3557:             $otheremails{$type} = '';
 3558:         }
 3559:     } elsif ($position eq 'lower') {
 3560:         if (ref($settings) eq 'HASH') {
 3561:             if (ref($settings->{'lonstatus'}) eq 'HASH') {
 3562:                 %lonstatus = %{$settings->{'lonstatus'}};
 3563:             }
 3564:         }
 3565:     } else {
 3566:         @mailings = ('helpdeskmail','otherdomsmail');
 3567:         foreach my $type (@mailings) {
 3568:             $otheremails{$type} = '';
 3569:         }
 3570:         $bccemails{'helpdeskmail'} = '';
 3571:         $bccemails{'otherdomsmail'} = '';
 3572:         $includestr{'helpdeskmail'} = '';
 3573:         $includestr{'otherdomsmail'} = '';
 3574:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 3575:     }
 3576:     if (ref($settings) eq 'HASH') {
 3577:         unless (($position eq 'top') || ($position eq 'lower')) {
 3578:             foreach my $type (@mailings) {
 3579:                 if (exists($settings->{$type})) {
 3580:                     if (ref($settings->{$type}) eq 'HASH') {
 3581:                         foreach my $item (@contacts) {
 3582:                             if ($settings->{$type}{$item}) {
 3583:                                 $checked{$type}{$item} = ' checked="checked" ';
 3584:                             }
 3585:                         }
 3586:                         $otheremails{$type} = $settings->{$type}{'others'};
 3587:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3588:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 3589:                             if ($settings->{$type}{'include'} ne '') {
 3590:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3591:                                 $includestr{$type} = &unescape($includestr{$type});
 3592:                             }
 3593:                         }
 3594:                     }
 3595:                 } elsif ($type eq 'lonstatusmail') {
 3596:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3597:                 }
 3598:             }
 3599:         }
 3600:         if ($position eq 'bottom') {
 3601:             foreach my $type (@mailings) {
 3602:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 3603:                 if ($settings->{$type}{'include'} ne '') {
 3604:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3605:                     $includestr{$type} = &unescape($includestr{$type});
 3606:                 }
 3607:             }
 3608:             if (ref($settings->{'helpform'}) eq 'HASH') {
 3609:                 if (ref($fields) eq 'ARRAY') {
 3610:                     foreach my $field (@{$fields}) {
 3611:                         $currfield{$field} = $settings->{'helpform'}{$field};
 3612:                     }
 3613:                 }
 3614:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 3615:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 3616:                 } else {
 3617:                     $maxsize = '1.0';
 3618:                 }
 3619:             } else {
 3620:                 if (ref($fields) eq 'ARRAY') {
 3621:                     foreach my $field (@{$fields}) {
 3622:                         $currfield{$field} = 'yes';
 3623:                     }
 3624:                 }
 3625:                 $maxsize = '1.0';
 3626:             }
 3627:         }
 3628:     } else {
 3629:         if ($position eq 'top') {
 3630:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 3631:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 3632:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 3633:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 3634:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3635:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 3636:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 3637:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 3638:             $checked{'hostipmail'}{'adminemail'} = ' checked="checked" ';
 3639:         } elsif ($position eq 'bottom') {
 3640:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 3641:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 3642:             if (ref($fields) eq 'ARRAY') {
 3643:                 foreach my $field (@{$fields}) {
 3644:                     $currfield{$field} = 'yes';
 3645:                 }
 3646:             }
 3647:             $maxsize = '1.0';
 3648:         }
 3649:     }
 3650:     my ($titles,$short_titles) = &contact_titles();
 3651:     my $rownum = 0;
 3652:     my $css_class;
 3653:     if ($position eq 'top') {
 3654:         foreach my $item (@contacts) {
 3655:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3656:             $datatable .= '<tr'.$css_class.'>'. 
 3657:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 3658:                           '</span></td><td class="LC_right_item">'.
 3659:                           '<input type="text" name="'.$item.'" value="'.
 3660:                           $to{$item}.'" /></td></tr>';
 3661:             $rownum ++;
 3662:         }
 3663:     } elsif ($position eq 'bottom') {
 3664:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3665:         $datatable .= '<tr'.$css_class.'>'.
 3666:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 3667:                       &mt('(e-mail, subject, and description always shown)').
 3668:                       '</td><td class="LC_left_item">';
 3669:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 3670:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 3671:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 3672:             foreach my $field (@{$fields}) {
 3673:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 3674:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 3675:                     $datatable .= ' '.&mt('(logged-in users)');
 3676:                 }
 3677:                 $datatable .='</td><td>';
 3678:                 my $clickaction;
 3679:                 if ($field eq 'screenshot') {
 3680:                     $clickaction = ' onclick="screenshotSize(this);"';
 3681:                 }
 3682:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 3683:                     foreach my $option (@{$possoptions->{$field}}) {
 3684:                         my $checked;
 3685:                         if ($currfield{$field} eq $option) {
 3686:                             $checked = ' checked="checked"';
 3687:                         }
 3688:                         $datatable .= '<span class="LC_nobreak"><label>'.
 3689:                                       '<input type="radio" name="helpform_'.$field.'" '.
 3690:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 3691:                                       '</label></span>'.('&nbsp;'x2);
 3692:                     }
 3693:                 }
 3694:                 if ($field eq 'screenshot') {
 3695:                     my $display;
 3696:                     if ($currfield{$field} eq 'no') {
 3697:                         $display = ' style="display:none"';
 3698:                     }
 3699:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.'>'.
 3700:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3701:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3702:                 }
 3703:                 $datatable .= '</td></tr>';
 3704:             }
 3705:             $datatable .= '</table>';
 3706:         }
 3707:         $datatable .= '</td></tr>'."\n";
 3708:         $rownum ++;
 3709:     }
 3710:     unless (($position eq 'top') || ($position eq 'lower')) {
 3711:         foreach my $type (@mailings) {
 3712:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3713:             $datatable .= '<tr'.$css_class.'>'.
 3714:                           '<td><span class="LC_nobreak">'.
 3715:                           $titles->{$type}.': </span></td>'.
 3716:                           '<td class="LC_left_item">';
 3717:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3718:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 3719:             }
 3720:             $datatable .= '<span class="LC_nobreak">';
 3721:             foreach my $item (@contacts) {
 3722:                 $datatable .= '<label>'.
 3723:                               '<input type="checkbox" name="'.$type.'"'.
 3724:                               $checked{$type}{$item}.
 3725:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 3726:                               '</label>&nbsp;';
 3727:             }
 3728:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3729:                           '<input type="text" name="'.$type.'_others" '.
 3730:                           'value="'.$otheremails{$type}.'"  />';
 3731:             my %locchecked;
 3732:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3733:                 foreach my $loc ('s','b') {
 3734:                     if ($includeloc{$type} eq $loc) {
 3735:                         $locchecked{$loc} = ' checked="checked"';
 3736:                         last;
 3737:                     }
 3738:                 }
 3739:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3740:                               '<input type="text" name="'.$type.'_bcc" '.
 3741:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 3742:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3743:                               &mt('Text automatically added to e-mail:').' '.
 3744:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br />'.
 3745:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3746:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3747:                               ('&nbsp;'x2).
 3748:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3749:                               '</span></fieldset>';
 3750:             }
 3751:             $datatable .= '</td></tr>'."\n";
 3752:             $rownum ++;
 3753:         }
 3754:     }
 3755:     if ($position eq 'middle') {
 3756:         my %choices;
 3757:         my $corelink = &core_link_msu();
 3758:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',$corelink);
 3759:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3760:                                         $corelink);
 3761:         $choices{'reportstatus'} = &mt('E-mail status if errors above threshold to [_1]',$corelink);
 3762:         my @toggles = ('reporterrors','reportupdates','reportstatus');
 3763:         my %defaultchecked = ('reporterrors'  => 'on',
 3764:                               'reportupdates' => 'on',
 3765:                               'reportstatus'  => 'on');
 3766:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3767:                                                    \%choices,$rownum);
 3768:         $datatable .= $reports;
 3769:     } elsif ($position eq 'lower') {
 3770:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3771:         my ($threshold,$sysmail,%excluded,%weights);
 3772:         my ($defaults,$names) = &Apache::loncommon::lon_status_items();
 3773:         if ($lonstatus{'threshold'} =~ /^\d+$/) {
 3774:             $threshold = $lonstatus{'threshold'};
 3775:         } else {
 3776:             $threshold = $defaults->{'threshold'};
 3777:         }
 3778:         if ($lonstatus{'sysmail'} =~ /^\d+$/) {
 3779:             $sysmail = $lonstatus{'sysmail'};
 3780:         } else {
 3781:             $sysmail = $defaults->{'sysmail'};
 3782:         }
 3783:         if (ref($lonstatus{'weights'}) eq 'HASH') {
 3784:             foreach my $type ('E','W','N','U') {
 3785:                 if ($lonstatus{'weights'}{$type} =~ /^\d+$/) {
 3786:                     $weights{$type} = $lonstatus{'weights'}{$type};
 3787:                 } else {
 3788:                     $weights{$type} = $defaults->{$type};
 3789:                 }
 3790:             }
 3791:         } else {
 3792:             foreach my $type ('E','W','N','U') {
 3793:                 $weights{$type} = $defaults->{$type};
 3794:             }
 3795:         }
 3796:         if (ref($lonstatus{'excluded'}) eq 'ARRAY') {
 3797:             if (@{$lonstatus{'excluded'}} > 0) {
 3798:                 map {$excluded{$_} = 1; } @{$lonstatus{'excluded'}};
 3799:             }
 3800:         }
 3801:         $datatable .= '<tr'.$css_class.'>'.
 3802:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 3803:                       $titles->{'errorthreshold'}.
 3804:                       '</span></td><td class="LC_left_item">'.
 3805:                       '<input type="text" name="errorthreshold" value="'.
 3806:                       $threshold.'" size="5" /></td></tr>';
 3807:         $rownum ++;
 3808:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3809:         $datatable .= '<tr'.$css_class.'>'.
 3810:                       '<td class="LC_left_item">'.
 3811:                       '<span class="LC_nobreak">'.$titles->{'errorweights'}.
 3812:                       '</span></td><td class="LC_left_item"><table><tr>';
 3813:         foreach my $type ('E','W','N','U') {
 3814:             $datatable .= '<td>'.$names->{$type}.'<br />'.
 3815:                           '<input type="text" name="errorweights_'.$type.'" value="'.
 3816:                           $weights{$type}.'" size="5" /></td>';
 3817:         }
 3818:         $datatable .= '</tr></table></tr>';
 3819:         $rownum ++;
 3820:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3821:         $datatable .= '<tr'.$css_class.'><td class="LC_left_item">'.
 3822:                       $titles->{'errorexcluded'}.'</td>'.
 3823:                       '<td class="LC_left_item"><table>';
 3824:         my $numinrow = 4;
 3825:         my @ids = sort(values(%Apache::lonnet::serverhomeIDs));
 3826:         for (my $i=0; $i<@ids; $i++) {
 3827:             my $rem = $i%($numinrow);
 3828:             if ($rem == 0) {
 3829:                 if ($i > 0) {
 3830:                     $datatable .= '</tr>';
 3831:                 }
 3832:                 $datatable .= '<tr>';
 3833:             }
 3834:             my $check;
 3835:             if ($excluded{$ids[$i]}) {
 3836:                 $check = ' checked="checked" ';
 3837:             }
 3838:             $datatable .= '<td class="LC_left_item">'.
 3839:                           '<span class="LC_nobreak"><label>'.
 3840:                           '<input type="checkbox" name="errorexcluded" '.
 3841:                           'value="'.$ids[$i].'"'.$check.' />'.
 3842:                           $ids[$i].'</label></span></td>';
 3843:         }
 3844:         my $colsleft = $numinrow - @ids%($numinrow);
 3845:         if ($colsleft > 1 ) {
 3846:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 3847:                           '&nbsp;</td>';
 3848:         } elsif ($colsleft == 1) {
 3849:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 3850:         }
 3851:         $datatable .= '</tr></table></td></tr>';
 3852:         $rownum ++;
 3853:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3854:         $datatable .= '<tr'.$css_class.'>'.
 3855:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 3856:                       $titles->{'errorsysmail'}.
 3857:                       '</span></td><td class="LC_left_item">'.
 3858:                       '<input type="text" name="errorsysmail" value="'.
 3859:                       $sysmail.'" size="5" /></td></tr>';
 3860:         $rownum ++;
 3861:     } elsif ($position eq 'bottom') {
 3862:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3863:         my (@posstypes,%usertypeshash);
 3864:         if (ref($types) eq 'ARRAY') {
 3865:             @posstypes = @{$types};
 3866:         }
 3867:         if (@posstypes) {
 3868:             if (ref($usertypes) eq 'HASH') {
 3869:                 %usertypeshash = %{$usertypes};
 3870:             }
 3871:             my @overridden;
 3872:             my $numinrow = 4;
 3873:             if (ref($settings) eq 'HASH') {
 3874:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 3875:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 3876:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 3877:                             push(@overridden,$key);
 3878:                             foreach my $item (@contacts) {
 3879:                                 if ($settings->{'overrides'}{$key}{$item}) {
 3880:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 3881:                                 }
 3882:                             }
 3883:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 3884:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 3885:                             $includeloc{'override_'.$key} = '';
 3886:                             $includestr{'override_'.$key} = '';
 3887:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 3888:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) = 
 3889:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 3890:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 3891:                             }
 3892:                         }
 3893:                     }
 3894:                 }
 3895:             }
 3896:             my $customclass = 'LC_helpdesk_override';
 3897:             my $optionsprefix = 'LC_options_helpdesk_';
 3898: 
 3899:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 3900:  
 3901:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3902:                                          $numinrow,$othertitle,'overrides',
 3903:                                          \$rownum,$onclicktypes,$customclass);
 3904:             $rownum ++;
 3905:             $usertypeshash{'default'} = $othertitle;
 3906:             foreach my $status (@posstypes) {
 3907:                 my $css_class;
 3908:                 if ($rownum%2) {
 3909:                     $css_class = 'LC_odd_row ';
 3910:                 }
 3911:                 $css_class .= $customclass;
 3912:                 my $rowid = $optionsprefix.$status;
 3913:                 my $hidden = 1;
 3914:                 my $currstyle = 'display:none';
 3915:                 if (grep(/^\Q$status\E$/,@overridden)) {
 3916:                     $currstyle = 'display:table-row';
 3917:                     $hidden = 0;
 3918:                 }
 3919:                 my $key = 'override_'.$status;
 3920:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 3921:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 3922:                                                   $usertypeshash{$status},$css_class,$currstyle,
 3923:                                                   \@contacts,$short_titles);
 3924:                 unless ($hidden) {
 3925:                     $rownum ++;
 3926:                 }
 3927:             }
 3928:         }
 3929:     }
 3930:     $$rowtotal += $rownum;
 3931:     return $datatable;
 3932: }
 3933: 
 3934: sub core_link_msu {
 3935:     return &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3936:                                           &mt('LON-CAPA core group - MSU'),600,500);
 3937: }
 3938: 
 3939: sub overridden_helpdesk {
 3940:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 3941:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 3942:     my $class = 'LC_left_item';
 3943:     if ($css_class) {
 3944:         $css_class = ' class="'.$css_class.'"';
 3945:     }
 3946:     if ($rowid) {
 3947:         $rowid = ' id="'.$rowid.'"';
 3948:     }
 3949:     if ($rowstyle) {
 3950:         $rowstyle = ' style="'.$rowstyle.'"';
 3951:     }
 3952:     my ($output,$description);
 3953:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 3954:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 3955:               "<td>$description</td>\n".
 3956:               '<td class="'.$class.'" colspan="2">'.
 3957:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 3958:               '<span class="LC_nobreak">';
 3959:     if (ref($contacts) eq 'ARRAY') {
 3960:         foreach my $item (@{$contacts}) {
 3961:             my $check;
 3962:             if (ref($checked) eq 'HASH') {
 3963:                $check = $checked->{$item};
 3964:             }
 3965:             my $title;
 3966:             if (ref($short_titles) eq 'HASH') {
 3967:                 $title = $short_titles->{$item}; 
 3968:             }
 3969:             $output .= '<label>'.
 3970:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 3971:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 3972:         }
 3973:     }
 3974:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3975:                '<input type="text" name="override_'.$type.'_others" '.
 3976:                'value="'.$otheremails.'"  />';
 3977:     my %locchecked;
 3978:     foreach my $loc ('s','b') {
 3979:         if ($includeloc eq $loc) {
 3980:             $locchecked{$loc} = ' checked="checked"';
 3981:             last;
 3982:         }
 3983:     }
 3984:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3985:                '<input type="text" name="override_'.$type.'_bcc" '.
 3986:                'value="'.$bccemails.'"  /></fieldset>'.
 3987:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3988:                &mt('Text automatically added to e-mail:').' '.
 3989:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br />'.
 3990:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3991:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3992:                ('&nbsp;'x2).
 3993:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3994:                '</span></fieldset>'.
 3995:                '</td></tr>'."\n";
 3996:     return $output;
 3997: }
 3998: 
 3999: sub contacts_javascript {
 4000:     return <<"ENDSCRIPT";
 4001: 
 4002: <script type="text/javascript">
 4003: // <![CDATA[
 4004: 
 4005: function screenshotSize(field) {
 4006:     if (document.getElementById('help_screenshotsize')) {
 4007:         if (field.value == 'no') {
 4008:             document.getElementById('help_screenshotsize').style.display="none";
 4009:         } else {
 4010:             document.getElementById('help_screenshotsize').style.display="";
 4011:         }
 4012:     }
 4013:     return;
 4014: }
 4015: 
 4016: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 4017:     if (form.elements[checkbox].length != undefined) {
 4018:         var count = 0;
 4019:         if (docount) {
 4020:             for (var i=0; i<form.elements[checkbox].length; i++) {
 4021:                 if (form.elements[checkbox][i].checked) {
 4022:                     count ++;
 4023:                 }
 4024:             }
 4025:         }
 4026:         for (var i=0; i<form.elements[checkbox].length; i++) {
 4027:             var type = form.elements[checkbox][i].value;
 4028:             if (document.getElementById(prefix+type)) {
 4029:                 if (form.elements[checkbox][i].checked) {
 4030:                     document.getElementById(prefix+type).style.display = 'table-row';
 4031:                     if (count % 2 == 1) {
 4032:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 4033:                     } else {
 4034:                         document.getElementById(prefix+type).className = target;
 4035:                     }
 4036:                     count ++;
 4037:                 } else {
 4038:                     document.getElementById(prefix+type).style.display = 'none';
 4039:                 }
 4040:             }
 4041:         }
 4042:     }
 4043:     return;
 4044: }
 4045: 
 4046: 
 4047: // ]]>
 4048: </script>
 4049: 
 4050: ENDSCRIPT
 4051: }
 4052: 
 4053: sub print_helpsettings {
 4054:     my ($position,$dom,$settings,$rowtotal) = @_;
 4055:     my $confname = $dom.'-domainconfig';
 4056:     my $formname = 'display';
 4057:     my ($datatable,$itemcount);
 4058:     if ($position eq 'top') {
 4059:         $itemcount = 1;
 4060:         my (%choices,%defaultchecked,@toggles);
 4061:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 4062:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 4063:                                      &mt('LON-CAPA bug tracker'),600,500));
 4064:         %defaultchecked = ('submitbugs' => 'on');
 4065:         @toggles = ('submitbugs');
 4066:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4067:                                                      \%choices,$itemcount);
 4068:         $$rowtotal ++;
 4069:     } else {
 4070:         my $css_class;
 4071:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 4072:         my (%customroles,%ordered,%current);
 4073:         if (ref($settings) eq 'HASH') {
 4074:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 4075:                 %current = %{$settings->{'adhoc'}};
 4076:             }
 4077:         }
 4078:         my $count = 0;
 4079:         foreach my $key (sort(keys(%existing))) {
 4080:             if ($key=~/^rolesdef\_(\w+)$/) {
 4081:                 my $rolename = $1;
 4082:                 my (%privs,$order);
 4083:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 4084:                 $customroles{$rolename} = \%privs;
 4085:                 if (ref($current{$rolename}) eq 'HASH') {
 4086:                     $order = $current{$rolename}{'order'};
 4087:                 }
 4088:                 if ($order eq '') {
 4089:                     $order = $count;
 4090:                 }
 4091:                 $ordered{$order} = $rolename;
 4092:                 $count++;
 4093:             }
 4094:         }
 4095:         my $maxnum = scalar(keys(%ordered));
 4096:         my @roles_by_num = ();
 4097:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 4098:             push(@roles_by_num,$item);
 4099:         }
 4100:         my $context = 'domprefs';
 4101:         my $crstype = 'Course';
 4102:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 4103:         my @accesstypes = ('all','dh','da','none');
 4104:         my ($numstatustypes,@jsarray);
 4105:         if (ref($types) eq 'ARRAY') {
 4106:             if (@{$types} > 0) {
 4107:                 $numstatustypes = scalar(@{$types});
 4108:                 push(@accesstypes,'status');
 4109:                 @jsarray = ('bystatus');
 4110:             }
 4111:         }
 4112:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 4113:         if (keys(%domhelpdesk)) {
 4114:             push(@accesstypes,('inc','exc'));
 4115:             push(@jsarray,('notinc','notexc'));
 4116:         }
 4117:         my $hiddenstr = join("','",@jsarray);
 4118:         my $context = 'domprefs';
 4119:         my $crstype = 'Course';
 4120:         my $prefix = 'helproles_';
 4121:         my $add_class = 'LC_hidden';
 4122:         foreach my $num (@roles_by_num) {
 4123:             my $role = $ordered{$num};
 4124:             my ($desc,$access,@statuses);
 4125:             if (ref($current{$role}) eq 'HASH') {
 4126:                 $desc = $current{$role}{'desc'};
 4127:                 $access = $current{$role}{'access'};
 4128:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 4129:                     @statuses = @{$current{$role}{'insttypes'}};
 4130:                 }
 4131:             }
 4132:             if ($desc eq '') {
 4133:                 $desc = $role;
 4134:             }
 4135:             my $identifier = 'custhelp'.$num;
 4136:             my %full=();
 4137:             my %levels= (
 4138:                          course => {},
 4139:                          domain => {},
 4140:                          system => {},
 4141:                         );
 4142:             my %levelscurrent=(
 4143:                                course => {},
 4144:                                domain => {},
 4145:                                system => {},
 4146:                               );
 4147:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 4148:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4149:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4150:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 4151:             $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><b>'.$role.'</b><br />'.
 4152:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 4153:             for (my $k=0; $k<=$maxnum; $k++) {
 4154:                 my $vpos = $k+1;
 4155:                 my $selstr;
 4156:                 if ($k == $num) {
 4157:                     $selstr = ' selected="selected" ';
 4158:                 }
 4159:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4160:             }
 4161:             $datatable .= '</select>'.('&nbsp;'x2).
 4162:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 4163:                           '</td>'.
 4164:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4165:                           &mt('Name shown to users:').
 4166:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 4167:                           '</fieldset>'.
 4168:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 4169:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 4170:                           '<fieldset>'.
 4171:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 4172:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 4173:                                                                    \%levelscurrent,$identifier,
 4174:                                                                    'LC_hidden',$prefix.$num.'_privs').
 4175:                           '</fieldset></td>';
 4176:             $itemcount ++;
 4177:         }
 4178:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4179:         my $newcust = 'custhelp'.$count;
 4180:         my (%privs,%levelscurrent);
 4181:         my %full=();
 4182:         my %levels= (
 4183:                      course => {},
 4184:                      domain => {},
 4185:                      system => {},
 4186:                     );
 4187:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 4188:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 4189:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 4190:         $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><span class="LC_nobreak"><label>'.
 4191:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 4192:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 4193:         for (my $k=0; $k<$maxnum+1; $k++) {
 4194:             my $vpos = $k+1;
 4195:             my $selstr;
 4196:             if ($k == $maxnum) {
 4197:                 $selstr = ' selected="selected" ';
 4198:             }
 4199:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4200:         }
 4201:         $datatable .= '</select>&nbsp;'."\n".
 4202:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 4203:                       '</label></span></td>'.
 4204:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 4205:                       '<span class="LC_nobreak">'.
 4206:                       &mt('Internal name:').
 4207:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 4208:                       '</span>'.('&nbsp;'x4).
 4209:                       '<span class="LC_nobreak">'.
 4210:                       &mt('Name shown to users:').
 4211:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 4212:                       '</span></fieldset>'.
 4213:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 4214:                                              $usertypes,$types,\%domhelpdesk).
 4215:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 4216:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 4217:                                                                 \@templateroles,$newcust).
 4218:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 4219:                                                                \%levelscurrent,$newcust).
 4220:                       '</fieldset>'.
 4221:                       &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname).
 4222:                       '</td></tr>';
 4223:         $count ++;
 4224:         $$rowtotal += $count;
 4225:     }
 4226:     return $datatable;
 4227: }
 4228: 
 4229: sub adhocbutton {
 4230:     my ($prefix,$num,$field,$visibility) = @_;
 4231:     my %lt = &Apache::lonlocal::texthash(
 4232:                                           show => 'Show details',
 4233:                                           hide => 'Hide details',
 4234:                                         );
 4235:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 4236:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 4237:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 4238:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 4239: }
 4240: 
 4241: sub helpsettings_javascript {
 4242:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 4243:     return unless(ref($roles_by_num) eq 'ARRAY');
 4244:     my %html_js_lt = &Apache::lonlocal::texthash(
 4245:                                           show => 'Show details',
 4246:                                           hide => 'Hide details',
 4247:                                         );
 4248:     &html_escape(\%html_js_lt);
 4249:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 4250:     return <<"ENDSCRIPT";
 4251: <script type="text/javascript">
 4252: // <![CDATA[
 4253: 
 4254: function reorderHelpRoles(form,item) {
 4255:     var changedVal;
 4256: $jstext
 4257:     var newpos = 'helproles_${total}_pos';
 4258:     var maxh = 1 + $total;
 4259:     var current = new Array();
 4260:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 4261:     if (item == newpos) {
 4262:         changedVal = newitemVal;
 4263:     } else {
 4264:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 4265:         current[newitemVal] = newpos;
 4266:     }
 4267:     for (var i=0; i<helproles.length; i++) {
 4268:         var elementName = 'helproles_'+helproles[i]+'_pos';
 4269:         if (elementName != item) {
 4270:             if (form.elements[elementName]) {
 4271:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 4272:                 current[currVal] = elementName;
 4273:             }
 4274:         }
 4275:     }
 4276:     var oldVal;
 4277:     for (var j=0; j<maxh; j++) {
 4278:         if (current[j] == undefined) {
 4279:             oldVal = j;
 4280:         }
 4281:     }
 4282:     if (oldVal < changedVal) {
 4283:         for (var k=oldVal+1; k<=changedVal ; k++) {
 4284:            var elementName = current[k];
 4285:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 4286:         }
 4287:     } else {
 4288:         for (var k=changedVal; k<oldVal; k++) {
 4289:             var elementName = current[k];
 4290:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 4291:         }
 4292:     }
 4293:     return;
 4294: }
 4295: 
 4296: function helpdeskAccess(num) {
 4297:     var curraccess = null;
 4298:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 4299:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 4300:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 4301:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 4302:             }
 4303:         }
 4304:     }
 4305:     var shown = Array();
 4306:     var hidden = Array();
 4307:     if (curraccess == 'none') {
 4308:         hidden = Array('$hiddenstr');
 4309:     } else {
 4310:         if (curraccess == 'status') {
 4311:             shown = Array('bystatus');
 4312:             hidden = Array('notinc','notexc');
 4313:         } else {
 4314:             if (curraccess == 'exc') {
 4315:                 shown = Array('notexc');
 4316:                 hidden = Array('notinc','bystatus');
 4317:             }
 4318:             if (curraccess == 'inc') {
 4319:                 shown = Array('notinc');
 4320:                 hidden = Array('notexc','bystatus');
 4321:             }
 4322:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 4323:                 hidden = Array('notinc','notexc','bystatus');
 4324:             }
 4325:         }
 4326:     }
 4327:     if (hidden.length > 0) {
 4328:         for (var i=0; i<hidden.length; i++) {
 4329:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 4330:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 4331:             }
 4332:         }
 4333:     }
 4334:     if (shown.length > 0) {
 4335:         for (var i=0; i<shown.length; i++) {
 4336:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 4337:                 if (shown[i] == 'privs') {
 4338:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 4339:                 } else {
 4340:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 4341:                 }
 4342:             }
 4343:         }
 4344:     }
 4345:     return;
 4346: }
 4347: 
 4348: function toggleHelpdeskItem(num,field) {
 4349:     if (document.getElementById('helproles_'+num+'_'+field)) {
 4350:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 4351:             document.getElementById('helproles_'+num+'_'+field).className =
 4352:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 4353:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4354:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 4355:             }
 4356:         } else {
 4357:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 4358:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 4359:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 4360:             }
 4361:         }
 4362:     }
 4363:     return;
 4364: }
 4365: 
 4366: // ]]>
 4367: </script>
 4368: 
 4369: ENDSCRIPT
 4370: }
 4371: 
 4372: sub helpdeskroles_access {
 4373:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 4374:         $usertypes,$types,$domhelpdesk) = @_;
 4375:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 4376:     my %lt = &Apache::lonlocal::texthash(
 4377:                     'rou'    => 'Role usage',
 4378:                     'whi'    => 'Which helpdesk personnel may use this role?',
 4379:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 4380:                     'dh'     => 'All with domain helpdesk role',
 4381:                     'da'     => 'All with domain helpdesk assistant role',
 4382:                     'none'   => 'None',
 4383:                     'status' => 'Determined based on institutional status',
 4384:                     'inc'    => 'Include all, but exclude specific personnel',
 4385:                     'exc'    => 'Exclude all, but include specific personnel',
 4386:                   );
 4387:     my %usecheck = (
 4388:                      all => ' checked="checked"',
 4389:                    );
 4390:     my %displaydiv = (
 4391:                       status => 'none',
 4392:                       inc    => 'none',
 4393:                       exc    => 'none',
 4394:                       priv   => 'block',
 4395:                      );
 4396:     my $output;
 4397:     if (ref($current) eq 'HASH') {
 4398:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 4399:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 4400:                 $usecheck{$current->{access}} = $usecheck{'all'};
 4401:                 delete($usecheck{'all'});
 4402:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 4403:                     my $access = $1;
 4404:                     $displaydiv{$access} = 'inline';
 4405:                 } elsif ($current->{access} eq 'none') {
 4406:                     $displaydiv{'priv'} = 'none';
 4407:                 }
 4408:             }
 4409:         }
 4410:     }
 4411:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 4412:               '<p>'.$lt{'whi'}.'</p>';
 4413:     foreach my $access (@{$accesstypes}) {
 4414:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 4415:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 4416:                    $lt{$access}.'</label>';
 4417:         if ($access eq 'status') {
 4418:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 4419:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 4420:                                                                  $othertitle,$usertypes,$types).
 4421:                        '</div>';
 4422:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 4423:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 4424:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 4425:                        '</div>';
 4426:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 4427:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 4428:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 4429:                        '</div>';
 4430:         }
 4431:         $output .= '</p>';
 4432:     }
 4433:     $output .= '</fieldset>';
 4434:     return $output;
 4435: }
 4436: 
 4437: sub radiobutton_prefs {
 4438:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 4439:         $additional,$align) = @_;
 4440:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 4441:                    (ref($choices) eq 'HASH'));
 4442: 
 4443:     my (%checkedon,%checkedoff,$datatable,$css_class);
 4444: 
 4445:     foreach my $item (@{$toggles}) {
 4446:         if ($defaultchecked->{$item} eq 'on') {
 4447:             $checkedon{$item} = ' checked="checked" ';
 4448:             $checkedoff{$item} = ' ';
 4449:         } elsif ($defaultchecked->{$item} eq 'off') {
 4450:             $checkedoff{$item} = ' checked="checked" ';
 4451:             $checkedon{$item} = ' ';
 4452:         }
 4453:     }
 4454:     if (ref($settings) eq 'HASH') {
 4455:         foreach my $item (@{$toggles}) {
 4456:             if ($settings->{$item} eq '1') {
 4457:                 $checkedon{$item} =  ' checked="checked" ';
 4458:                 $checkedoff{$item} = ' ';
 4459:             } elsif ($settings->{$item} eq '0') {
 4460:                 $checkedoff{$item} =  ' checked="checked" ';
 4461:                 $checkedon{$item} = ' ';
 4462:             }
 4463:         }
 4464:     }
 4465:     if ($onclick) {
 4466:         $onclick = ' onclick="'.$onclick.'"';
 4467:     }
 4468:     foreach my $item (@{$toggles}) {
 4469:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4470:         $datatable .=
 4471:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 4472:             '<span class="LC_nobreak">'.$choices->{$item}.
 4473:             '</span></td>';
 4474:         if ($align eq 'left') {
 4475:             $datatable .= '<td class="LC_left_item">';
 4476:         } else {
 4477:             $datatable .= '<td class="LC_right_item">';
 4478:         }
 4479:         $datatable .=
 4480:             '<span class="LC_nobreak">'.
 4481:             '<label><input type="radio" name="'.
 4482:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 4483:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 4484:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 4485:             '</span>'.$additional.
 4486:             '</td>'.
 4487:             '</tr>';
 4488:         $itemcount ++;
 4489:     }
 4490:     return ($datatable,$itemcount);
 4491: }
 4492: 
 4493: sub print_ltitools {
 4494:     my ($dom,$settings,$rowtotal) = @_;
 4495:     my $rownum = 0;
 4496:     my $css_class;
 4497:     my $itemcount = 1;
 4498:     my $maxnum = 0;
 4499:     my %ordered;
 4500:     if (ref($settings) eq 'HASH') {
 4501:         foreach my $item (keys(%{$settings})) {
 4502:             if (ref($settings->{$item}) eq 'HASH') {
 4503:                 my $num = $settings->{$item}{'order'};
 4504:                 $ordered{$num} = $item;
 4505:             }
 4506:         }
 4507:     }
 4508:     my $confname = $dom.'-domainconfig';
 4509:     my $switchserver = &check_switchserver($dom,$confname);
 4510:     my $maxnum = scalar(keys(%ordered));
 4511:     my $datatable;
 4512:     my %lt = &ltitools_names();
 4513:     my @courseroles = ('cc','in','ta','ep','st');
 4514:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 4515:     my @fields = ('fullname','firstname','lastname','email','roles','user');
 4516:     if (keys(%ordered)) {
 4517:         my @items = sort { $a <=> $b } keys(%ordered);
 4518:         for (my $i=0; $i<@items; $i++) {
 4519:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4520:             my $item = $ordered{$items[$i]};
 4521:             my ($title,$key,$secret,$url,$lifetime,$imgsrc,%sigsel);
 4522:             if (ref($settings->{$item}) eq 'HASH') {
 4523:                 $title = $settings->{$item}->{'title'};
 4524:                 $url = $settings->{$item}->{'url'};
 4525:                 $key = $settings->{$item}->{'key'};
 4526:                 $secret = $settings->{$item}->{'secret'};
 4527:                 $lifetime = $settings->{$item}->{'lifetime'};
 4528:                 my $image = $settings->{$item}->{'image'};
 4529:                 if ($image ne '') {
 4530:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 4531:                 }
 4532:                 if ($settings->{$item}->{'sigmethod'} eq 'HMAC-256') {
 4533:                     $sigsel{'HMAC-256'} = ' selected="selected"';
 4534:                 } else {
 4535:                     $sigsel{'HMAC-SHA1'} = ' selected="selected"';
 4536:                 }
 4537:             }
 4538:             my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_".$item."'".');"';
 4539:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 4540:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 4541:             for (my $k=0; $k<=$maxnum; $k++) {
 4542:                 my $vpos = $k+1;
 4543:                 my $selstr;
 4544:                 if ($k == $i) {
 4545:                     $selstr = ' selected="selected" ';
 4546:                 }
 4547:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4548:             }
 4549:             $datatable .= '</select>'.('&nbsp;'x2).
 4550:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 4551:                 &mt('Delete?').'</label></span></td>'.
 4552:                 '<td colspan="2">'.
 4553:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4554:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 4555:                 ('&nbsp;'x2).
 4556:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 4557:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 4558:                 ('&nbsp;'x2).
 4559:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 4560:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 4561:                 ('&nbsp;'x2).
 4562:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_sigmethod_'.$i.'">'.
 4563:                 '<option value="HMAC-SHA1"'.$sigsel{'HMAC-SHA1'}.'>HMAC-SHA1</option>'.
 4564:                 '<option value="HMAC-SHA256"'.$sigsel{'HMAC-SHA256'}.'>HMAC-SHA256</option></select></span>'.
 4565:                 '<br /><br />'.
 4566:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_url_'.$i.'"'.
 4567:                 ' value="'.$url.'" /></span>'.
 4568:                 ('&nbsp;'x2).
 4569:                 '<span class="LC_nobreak">'.$lt{'key'}.':'.
 4570:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 4571:                 ('&nbsp;'x2).
 4572:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':'.
 4573:                 '<input type="text" size="5" name="ltitools_lifetime_'.$i.'" value="'.$lifetime.'" /></span> '.
 4574:                 ('&nbsp;'x2).
 4575:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 4576:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 4577:                 '<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>'.
 4578:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 4579:                 '</fieldset>'.
 4580:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 4581:                 '<span class="LC_nobreak">'.&mt('Display target:');
 4582:             my %currdisp;
 4583:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 4584:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 4585:                     $currdisp{'window'} = ' checked="checked"';
 4586:                 } elsif ($settings->{$item}->{'display'}->{'target'} eq 'tab') {
 4587:                     $currdisp{'tab'} = ' checked="checked"';
 4588:                 } else {
 4589:                     $currdisp{'iframe'} = ' checked="checked"';
 4590:                 }
 4591:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 4592:                     $currdisp{'width'} = $1;
 4593:                 }
 4594:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 4595:                      $currdisp{'height'} = $1;
 4596:                 }
 4597:                 $currdisp{'linktext'} = $settings->{$item}->{'display'}->{'linktext'};
 4598:                 $currdisp{'explanation'} = $settings->{$item}->{'display'}->{'explanation'};
 4599:             } else {
 4600:                 $currdisp{'iframe'} = ' checked="checked"';
 4601:             }
 4602:             foreach my $disp ('iframe','tab','window') {
 4603:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 4604:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 4605:             }
 4606:             $datatable .= ('&nbsp;'x4);
 4607:             foreach my $dimen ('width','height') {
 4608:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 4609:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 4610:                               ('&nbsp;'x2);
 4611:             }
 4612:             $datatable .= '</span><br />'.
 4613:                           '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 4614:                           '<input type="text" name="ltitools_linktext_'.$i.'" size="25" value="'.$currdisp{'linktext'}.'" /></div>'.
 4615:                           '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 4616:                           '<textarea name="ltitools_explanation_'.$i.'" rows="5" cols="40">'.$currdisp{'explanation'}.
 4617:                           '</textarea></div><div style=""></div><br />';
 4618:             my %units = (
 4619:                           'passback' => 'days',
 4620:                           'roster'   => 'seconds',
 4621:                         );
 4622:             foreach my $extra ('passback','roster') {
 4623:                 my $validsty = 'none';
 4624:                 my $currvalid;
 4625:                 my $checkedon = '';
 4626:                 my $checkedoff = ' checked="checked"';
 4627:                 if ($settings->{$item}->{$extra}) {
 4628:                     $checkedon = $checkedoff;
 4629:                     $checkedoff = '';
 4630:                     $validsty = 'inline-block';
 4631:                     if ($settings->{$item}->{$extra.'valid'} =~ /^\d+\.?\d*$/) {
 4632:                         $currvalid = $settings->{$item}->{$extra.'valid'};
 4633:                     }
 4634:                 }
 4635:                 my $onclick = ' onclick="toggleLTITools(this.form,'."'$extra','$i'".');"';
 4636:                 $datatable .= '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{$extra}.'&nbsp;'.
 4637:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.$onclick.' />'.
 4638:                               &mt('No').'</label>'.('&nbsp;'x2).
 4639:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.$onclick.' />'.
 4640:                               &mt('Yes').'</label></span></div>'.
 4641:                               '<div class="LC_floatleft" style="display:'.$validsty.';" id="ltitools_'.$extra.'time_'.$i.'">'.
 4642:                               '<span class="LC_nobreak">'.
 4643:                               &mt("at least [_1] $units{$extra} after launch",
 4644:                                   '<input type="text" name="ltitools_'.$extra.'valid_'.$i.'" value="'.$currvalid.'" />').
 4645:                               '</span></div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 4646:             }
 4647:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 4648:             if ($imgsrc) {
 4649:                 $datatable .= $imgsrc.
 4650:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 4651:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 4652:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 4653:             } else {
 4654:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 4655:             }
 4656:             if ($switchserver) {
 4657:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4658:             } else {
 4659:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 4660:             }
 4661:             $datatable .= '</span></fieldset>';
 4662:             my (%checkedfields,%rolemaps,$userincdom);
 4663:             if (ref($settings->{$item}) eq 'HASH') {
 4664:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 4665:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 4666:                 }
 4667:                 $userincdom = $settings->{$item}->{'incdom'};
 4668:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 4669:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 4670:                     $checkedfields{'roles'} = 1;
 4671:                 }
 4672:             }
 4673:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 4674:                           '<span class="LC_nobreak">';
 4675:             my $userfieldstyle = 'display:none;';
 4676:             my $seluserdom = '';
 4677:             my $unseluserdom = ' selected="selected"';
 4678:             foreach my $field (@fields) {
 4679:                 my ($checked,$onclick,$id,$spacer);
 4680:                 if ($checkedfields{$field}) {
 4681:                     $checked = ' checked="checked"';
 4682:                 }
 4683:                 if ($field eq 'user') {
 4684:                     $id = ' id="ltitools_user_field_'.$i.'"';
 4685:                     $onclick = ' onclick="toggleLTITools(this.form,'."'$field','$i'".')"';
 4686:                     if ($checked) {
 4687:                         $userfieldstyle = 'display:inline-block';
 4688:                         if ($userincdom) {
 4689:                             $seluserdom = $unseluserdom;
 4690:                             $unseluserdom = '';
 4691:                         }
 4692:                     }
 4693:                 } else {
 4694:                     $spacer = ('&nbsp;' x2);
 4695:                 }
 4696:                 $datatable .= '<label>'.
 4697:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$id.$checked.$onclick.' />'.
 4698:                               $lt{$field}.'</label>'.$spacer;
 4699:             }
 4700:             $datatable .= '</span>';
 4701:             $datatable .= '<div style="'.$userfieldstyle.'" id="ltitools_user_div_'.$i.'">'.
 4702:                           '<span class="LC_nobreak"> : '.
 4703:                           '<select name="ltitools_userincdom_'.$i.'">'.
 4704:                           '<option value="">'.&mt('Select').'</option>'.
 4705:                           '<option value="0"'.$unseluserdom.'>'.&mt('username').'</option>'.
 4706:                           '<option value="1"'.$seluserdom.'>'.&mt('username:domain').'</option>'.
 4707:                           '</select></span></div>';
 4708:             $datatable .= '</fieldset>'.
 4709:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4710:             foreach my $role (@courseroles) {
 4711:                 my ($selected,$selectnone);
 4712:                 if (!$rolemaps{$role}) {
 4713:                     $selectnone = ' selected="selected"';
 4714:                 }
 4715:                 $datatable .= '<td style="text-align: center">'. 
 4716:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4717:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 4718:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 4719:                 foreach my $ltirole (@ltiroles) {
 4720:                     unless ($selectnone) {
 4721:                         if ($rolemaps{$role} eq $ltirole) {
 4722:                             $selected = ' selected="selected"';
 4723:                         } else {
 4724:                             $selected = '';
 4725:                         }
 4726:                     }
 4727:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 4728:                 }
 4729:                 $datatable .= '</select></td>';
 4730:             }
 4731:             $datatable .= '</tr></table></fieldset>';
 4732:             my %courseconfig;
 4733:             if (ref($settings->{$item}) eq 'HASH') {
 4734:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 4735:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 4736:                 }
 4737:             }
 4738:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4739:             foreach my $item ('label','title','target','linktext','explanation','append') {
 4740:                 my $checked;
 4741:                 if ($courseconfig{$item}) {
 4742:                     $checked = ' checked="checked"';
 4743:                 }
 4744:                 $datatable .= '<label>'.
 4745:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 4746:                        $lt{'crs'.$item}.'</label>&nbsp; '."\n";
 4747:             }
 4748:             $datatable .= '</span></fieldset>'.
 4749:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4750:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 4751:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 4752:                 my %custom = %{$settings->{$item}->{'custom'}};
 4753:                 if (keys(%custom) > 0) {
 4754:                     foreach my $key (sort(keys(%custom))) {
 4755:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 4756:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 4757:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 4758:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 4759:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 4760:                     }
 4761:                 }
 4762:             }
 4763:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 4764:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 4765:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 4766:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 4767:             $datatable .= '</table></fieldset></td></tr>'."\n";
 4768:             $itemcount ++;
 4769:         }
 4770:     }
 4771:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4772:     my $chgstr = ' onchange="javascript:reorderLTITools(this.form,'."'ltitools_add_pos'".');"';
 4773:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 4774:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 4775:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 4776:     for (my $k=0; $k<$maxnum+1; $k++) {
 4777:         my $vpos = $k+1;
 4778:         my $selstr;
 4779:         if ($k == $maxnum) {
 4780:             $selstr = ' selected="selected" ';
 4781:         }
 4782:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4783:     }
 4784:     $datatable .= '</select>&nbsp;'."\n".
 4785:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 4786:                   '<td colspan="2">'.
 4787:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4788:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="20" name="ltitools_add_title" value="" /></span> '."\n".
 4789:                   ('&nbsp;'x2).
 4790:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 4791:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 4792:                   ('&nbsp;'x2).
 4793:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 4794:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 4795:                   '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="ltitools_add_sigmethod">'.
 4796:                   '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 4797:                   '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 4798:                   '<br />'.
 4799:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="ltitools_add_url" value="" /></span> '."\n".
 4800:                   ('&nbsp;'x2).
 4801:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 4802:                   ('&nbsp;'x2).
 4803:                   '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="ltitools_add_lifetime" value="300" /></span> '."\n".
 4804:                   ('&nbsp;'x2).
 4805:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 4806:                   '<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".
 4807:                   '</fieldset>'.
 4808:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 4809:                   '<span class="LC_nobreak">'.&mt('Display target:');
 4810:     my %defaultdisp;
 4811:     $defaultdisp{'iframe'} = ' checked="checked"';
 4812:     foreach my $disp ('iframe','tab','window') {
 4813:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 4814:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 4815:     }
 4816:     $datatable .= ('&nbsp;'x4);
 4817:     foreach my $dimen ('width','height') {
 4818:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 4819:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 4820:                       ('&nbsp;'x2);
 4821:     }
 4822:     $datatable .= '</span><br />'.
 4823:                   '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 4824:                   '<input type="text" name="ltitools_add_linktext" size="5" /></div>'.
 4825:                   '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 4826:                   '<textarea name="ltitools_add_explanation" rows="5" cols="40"></textarea>'.
 4827:                   '</div><div style=""></div><br />';
 4828:     my %units = (
 4829:                   'passback' => 'days',
 4830:                   'roster'   => 'seconds',
 4831:                 );
 4832:     my %defaulttimes = (
 4833:                      'passback' => '7',
 4834:                      'roster'   => '300',
 4835:                    );
 4836:     foreach my $extra ('passback','roster') {
 4837:         my $onclick = ' onclick="toggleLTITools(this.form,'."'$extra','add'".');"';
 4838:         $datatable .= '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{$extra}.'&nbsp;'.
 4839:                       '<label><input type="radio" name="ltitools_'.$extra.'_add" value="0" checked="checked"'.$onclick.' />'.
 4840:                       &mt('No').'</label></span>'.('&nbsp;'x2).'<span class="LC_nobreak">'.
 4841:                       '<label><input type="radio" name="ltitools_'.$extra.'_add" value="1"'.$onclick.' />'.
 4842:                       &mt('Yes').'</label></span></div>'.
 4843:                       '<div class="LC_floatleft" style="display:none;" id="ltitools_'.$extra.'time_add">'.
 4844:                       '<span class="LC_nobreak">'.
 4845:                       &mt("at least [_1] $units{$extra} after launch",
 4846:                           '<input type="text" name="ltitools_'.$extra.'valid_add" value="'.$defaulttimes{$extra}.'" />').
 4847:                       '</span></div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 4848:     }
 4849:     $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 4850:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 4851:     if ($switchserver) {
 4852:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4853:     } else {
 4854:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 4855:     }
 4856:     $datatable .= '</span></fieldset>'.
 4857:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 4858:                   '<span class="LC_nobreak">';
 4859:     foreach my $field (@fields) {
 4860:         my ($id,$onclick,$spacer);
 4861:         if ($field eq 'user') {
 4862:             $id = ' id="ltitools_user_field_add"';
 4863:             $onclick = ' onclick="toggleLTITools(this.form,'."'$field','add'".')"';
 4864:         } else {
 4865:             $spacer = ('&nbsp;' x2);
 4866:         }
 4867:         $datatable .= '<label>'.
 4868:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'"'.$id.$onclick.' />'.
 4869:                       $lt{$field}.'</label>'.$spacer;
 4870:     }
 4871:     $datatable .= '</span>'.
 4872:                   '<div style="display:none;" id="ltitools_user_div_add">'.
 4873:                   '<span class="LC_nobreak"> : '.
 4874:                   '<select name="ltitools_userincdom_add">'.
 4875:                   '<option value="" selected="selected">'.&mt('Select').'</option>'.
 4876:                   '<option value="0">'.&mt('username').'</option>'.
 4877:                   '<option value="1">'.&mt('username:domain').'</option>'.
 4878:                   '</select></span></div></fieldset>';
 4879:     $datatable .= '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4880:     foreach my $role (@courseroles) {
 4881:         my ($checked,$checkednone);
 4882:         $datatable .= '<td style="text-align: center">'.
 4883:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4884:                       '<select name="ltitools_add_roles_'.$role.'">'.
 4885:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 4886:         foreach my $ltirole (@ltiroles) {
 4887:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 4888:         }
 4889:         $datatable .= '</select></td>';
 4890:     }
 4891:     $datatable .= '</tr></table></fieldset>'.
 4892:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4893:     foreach my $item ('label','title','target','linktext','explanation','append') {
 4894:         $datatable .= '<label>'.
 4895:                       '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 4896:                       $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4897:     }
 4898:     $datatable .= '</span></fieldset>'.
 4899:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4900:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 4901:                   '<tr><td><span class="LC_nobreak">'.
 4902:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 4903:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 4904:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 4905:                   '</table></fieldset>'."\n".
 4906:                   '</td>'."\n".
 4907:                   '</tr>'."\n";
 4908:     $itemcount ++;
 4909:     return $datatable;
 4910: }
 4911: 
 4912: sub ltitools_names {
 4913:     my %lt = &Apache::lonlocal::texthash(
 4914:                                           'title'          => 'Title',
 4915:                                           'version'        => 'Version',
 4916:                                           'msgtype'        => 'Message Type',
 4917:                                           'sigmethod'      => 'Signature Method',
 4918:                                           'url'            => 'URL',
 4919:                                           'key'            => 'Key',
 4920:                                           'lifetime'       => 'Nonce lifetime (s)',
 4921:                                           'secret'         => 'Secret',
 4922:                                           'icon'           => 'Icon',   
 4923:                                           'user'           => 'User',
 4924:                                           'fullname'       => 'Full Name',
 4925:                                           'firstname'      => 'First Name',
 4926:                                           'lastname'       => 'Last Name',
 4927:                                           'email'          => 'E-mail',
 4928:                                           'roles'          => 'Role',
 4929:                                           'window'         => 'Window',
 4930:                                           'tab'            => 'Tab',
 4931:                                           'iframe'         => 'iFrame',
 4932:                                           'height'         => 'Height',
 4933:                                           'width'          => 'Width',
 4934:                                           'linktext'       => 'Default Link Text',
 4935:                                           'explanation'    => 'Default Explanation',
 4936:                                           'passback'       => 'Tool can return grades:',
 4937:                                           'roster'         => 'Tool can retrieve roster:',
 4938:                                           'crstarget'      => 'Display target',
 4939:                                           'crslabel'       => 'Course label',
 4940:                                           'crstitle'       => 'Course title', 
 4941:                                           'crslinktext'    => 'Link Text',
 4942:                                           'crsexplanation' => 'Explanation',
 4943:                                           'crsappend'      => 'Provider URL',
 4944:                                         );
 4945:     return %lt;
 4946: }
 4947: 
 4948: sub print_proctoring {
 4949:     my ($dom,$settings,$rowtotal) = @_;
 4950:     my $itemcount = 1;
 4951:     my (%ordered,%providernames,%current,%currentdef);
 4952:     my $confname = $dom.'-domainconfig';
 4953:     my $switchserver = &check_switchserver($dom,$confname);
 4954:     if (ref($settings) eq 'HASH') {
 4955:         foreach my $item (keys(%{$settings})) {
 4956:             if (ref($settings->{$item}) eq 'HASH') {
 4957:                 my $num = $settings->{$item}{'order'};
 4958:                 $ordered{$num} = $item;
 4959:             }
 4960:         }
 4961:     } else {
 4962:         %ordered = (
 4963:                      1 => 'proctorio',
 4964:                      2 => 'examity',
 4965:                    );
 4966:     }
 4967:     %providernames = &proctoring_providernames();
 4968:     my $maxnum = scalar(keys(%ordered));
 4969:     my (%requserfields,%optuserfields,%defaults,%extended,%crsconf,@courseroles,@ltiroles);
 4970:     my ($requref,$opturef,$defref,$extref,$crsref,$rolesref,$ltiref) = &proctoring_data();
 4971:     if (ref($requref) eq 'HASH') {
 4972:         %requserfields = %{$requref};
 4973:     }
 4974:     if (ref($opturef) eq 'HASH') {
 4975:         %optuserfields = %{$opturef};
 4976:     }
 4977:     if (ref($defref) eq 'HASH') {
 4978:         %defaults = %{$defref};
 4979:     }
 4980:     if (ref($extref) eq 'HASH') {
 4981:         %extended = %{$extref};
 4982:     }
 4983:     if (ref($crsref) eq 'HASH') {
 4984:         %crsconf = %{$crsref};
 4985:     }
 4986:     if (ref($rolesref) eq 'ARRAY') {
 4987:         @courseroles = @{$rolesref};
 4988:     }
 4989:     if (ref($ltiref) eq 'ARRAY') {
 4990:         @ltiroles = @{$ltiref};
 4991:     }
 4992:     my $datatable;
 4993:     my $css_class;
 4994:     if (keys(%ordered)) {
 4995:         my @items = sort { $a <=> $b } keys(%ordered);
 4996:         for (my $i=0; $i<@items; $i++) {
 4997:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4998:             my $provider = $ordered{$items[$i]};
 4999:             my $optionsty = 'none';
 5000:             my ($available,$version,$lifetime,$imgsrc,$userincdom,$showroles,
 5001:                 %checkedfields,%rolemaps,%inuse,%crsconfig,%current);
 5002:             if (ref($settings) eq 'HASH') {
 5003:                 if (ref($settings->{$provider}) eq 'HASH') {
 5004:                     %current = %{$settings->{$provider}};
 5005:                     if ($current{'available'}) {
 5006:                         $optionsty = 'block';
 5007:                         $available = 1;
 5008:                     }
 5009:                     if ($current{'lifetime'} =~ /^\d+$/) {
 5010:                         $lifetime = $current{'lifetime'};
 5011:                     }
 5012:                     if ($current{'version'} =~ /^\d+\.\d+$/) {
 5013:                         $version = $current{'version'};
 5014:                     }
 5015:                     if ($current{'image'} ne '') {
 5016:                         $imgsrc = '<img src="'.$current{'image'}.'" alt="'.&mt('Proctoring service icon').'" />';
 5017:                     }
 5018:                     if (ref($current{'fields'}) eq 'ARRAY') {
 5019:                         map { $checkedfields{$_} = 1; } @{$current{'fields'}};
 5020:                     }
 5021:                     $userincdom = $current{'incdom'};
 5022:                     if (ref($current{'roles'}) eq 'HASH') {
 5023:                         %rolemaps = %{$current{'roles'}};
 5024:                         $checkedfields{'roles'} = 1;
 5025:                     }
 5026:                     if (ref($current{'defaults'}) eq 'ARRAY') {
 5027:                         foreach my $val (@{$current{'defaults'}}) {
 5028:                             if (grep(/^\Q$val\E$/,@{$defaults{$provider}})) {
 5029:                                 $inuse{$val} = 1;
 5030:                             } else {
 5031:                                 foreach my $poss (keys(%{$extended{$provider}})) {
 5032:                                     if (ref($extended{$provider}{$poss}) eq 'ARRAY') {
 5033:                                         if (grep(/^\Q$val\E$/,@{$extended{$provider}{$poss}})) {
 5034:                                             $inuse{$poss} = $val;
 5035:                                             last;
 5036:                                         }
 5037:                                     }
 5038:                                 }
 5039:                             }
 5040:                         }
 5041:                     } elsif (ref($current{'defaults'}) eq 'HASH') {
 5042:                         foreach my $key (keys(%{$current{'defaults'}})) {
 5043:                             my $currval = $current{'defaults'}{$key}; 
 5044:                             if (grep(/^\Q$key\E$/,@{$defaults{$provider}})) {
 5045:                                 $inuse{$key} = 1;
 5046:                             } else {
 5047:                                 my $match;
 5048:                                 foreach my $poss (keys(%{$extended{$provider}})) {
 5049:                                     if (ref($extended{$provider}{$poss}) eq 'ARRAY') {
 5050:                                         if (grep(/^\Q$key\E$/,@{$extended{$provider}{$poss}})) {
 5051:                                             $inuse{$poss} = $key;
 5052:                                             last;
 5053:                                         }
 5054:                                     } elsif (ref($extended{$provider}{$poss}) eq 'HASH') {
 5055:                                         foreach my $inner (sort(keys(%{$extended{$provider}{$poss}}))) {
 5056:                                             if (ref($extended{$provider}{$poss}{$inner}) eq 'ARRAY') {
 5057:                                                 if (grep(/^\Q$currval\E$/,@{$extended{$provider}{$poss}{$inner}})) {
 5058:                                                     $currentdef{$inner} = $currval;
 5059:                                                     $match = 1;
 5060:                                                     last;
 5061:                                                 }
 5062:                                             } elsif ($inner eq $key) {
 5063:                                                 $currentdef{$key} = $currval;
 5064:                                                 $match = 1;
 5065:                                                 last;
 5066:                                             }
 5067:                                         }
 5068:                                     }
 5069:                                     last if ($match);
 5070:                                 }
 5071:                             }
 5072:                         }
 5073:                     }
 5074:                     if (ref($current{'crsconf'}) eq 'ARRAY') {
 5075:                         map { $crsconfig{$_} = 1; } @{$current{'crsconf'}};
 5076:                     }
 5077:                 }
 5078:             }
 5079:             my %lt = &proctoring_titles($provider);
 5080:             my %fieldtitles = &proctoring_fieldtitles($provider);
 5081:             my $onclickavailable = ' onclick="toggleProctoring(this.form,'."'$provider'".');"';
 5082:             my %checkedavailable = (
 5083:                                    yes => '',
 5084:                                    no  => ' checked="checked"',
 5085:                                  );
 5086:             if ($available) {
 5087:                 $checkedavailable{'yes'} = $checkedavailable{'no'};
 5088:                 $checkedavailable{'no'} = '';
 5089:             }
 5090:             my $chgstr = ' onchange="javascript:reorderProctoring(this.form,'."'proctoring_pos_".$provider."'".');"';
 5091:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5092:                          .'<select name="proctoring_pos_'.$provider.'"'.$chgstr.'>';
 5093:             for (my $k=0; $k<$maxnum; $k++) {
 5094:                 my $vpos = $k+1;
 5095:                 my $selstr;
 5096:                 if ($k == $i) {
 5097:                     $selstr = ' selected="selected" ';
 5098:                 }
 5099:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5100:             }
 5101:             if ($version eq '') {
 5102:                 if ($provider eq 'proctorio') {
 5103:                     $version = '1.0';
 5104:                 } elsif ($provider eq 'examity') {
 5105:                     $version = '1.1';
 5106:                 }
 5107:             }
 5108:             if ($lifetime eq '') {
 5109:                 $lifetime = '300';
 5110:             }
 5111:             $datatable .=
 5112:                 '</select>'.('&nbsp;'x2).'<b>'.$providernames{$provider}.'</b></span><br />'.
 5113:                 '<span class="LC_nobreak">'.$lt{'avai'}.'&nbsp;'.
 5114:                 '<label><input type="radio" name="proctoring_available_'.$provider.'" value="1"'.$onclickavailable.$checkedavailable{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 5115:                 '<label><input type="radio" name="proctoring_available_'.$provider.'" value="0"'.$onclickavailable.$checkedavailable{no}.' />'.&mt('No').'</label></span>'."\n".
 5116:                 '</td>'.
 5117:                 '<td colspan="2">'.
 5118:                 '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'base'}.'</legend>'.
 5119:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="proctoring_'.$provider.'_version">'.
 5120:                 '<option value="'.$version.'" selected="selected">'.$version.'</option></select></span> '."\n".
 5121:                 ('&nbsp;'x2).
 5122:                 '<span class="LC_nobreak">'.$lt{'sigmethod'}.':<select name="proctoring_'.$provider.'_sigmethod">'.
 5123:                 '<option value="HMAC-SHA1" selected="selected">HMAC-SHA1</option>'.
 5124:                 '<option value="HMAC-SHA256">HMAC-SHA256</option></select></span>'.
 5125:                 ('&nbsp;'x2).
 5126:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="5" name="proctoring_'.$provider.'_lifetime" value="'.$lifetime.'" /></span> '."\n".
 5127:                 '<br />'.
 5128:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="40" name="proctoring_'.$provider.'_url" value="'.$current{'url'}.'" /></span> '."\n".
 5129:                 '<br />'.
 5130:                 '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="proctoring_'.$provider.'_key" value="'.$current{'key'}.'" /></span> '."\n".
 5131:                 ('&nbsp;'x2).
 5132:                 '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="proctoring_'.$provider.'_secret" value="'.$current{'secret'}.'" />'.
 5133:                 '<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";
 5134:             $datatable .= '<span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 5135:             if ($imgsrc) {
 5136:                 $datatable .= $imgsrc.
 5137:                               '<label><input type="checkbox" name="proctoring_image_del"'.
 5138:                               ' value="'.$provider.'" />'.&mt('Delete?').'</label></span> '.
 5139:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:');
 5140:             }
 5141:             $datatable .= '&nbsp;';
 5142:             if ($switchserver) {
 5143:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 5144:             } else {
 5145:                 $datatable .= '<input type="file" name="proctoring_image_'.$provider.'" value="" />';
 5146:             }
 5147:             unless ($imgsrc) {
 5148:                 $datatable .= '<br />('.&mt('if larger than 21x21 pixels, image will be scaled').')';
 5149:             }
 5150:             $datatable .= '</fieldset>'."\n";
 5151:             if (ref($requserfields{$provider}) eq 'ARRAY') {
 5152:                 if (@{$requserfields{$provider}} > 0) {
 5153:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'requ'}.'</legend>';
 5154:                     foreach my $field (@{$requserfields{$provider}}) {
 5155:                         $datatable .= '<span class="LC_nobreak">'.
 5156:                                       '<label><input type="checkbox" name="proctoring_reqd_'.$provider.'" value="'.$field.'" checked="checked" disabled="disabled" />'.
 5157:                                       $lt{$field}.'</label>';
 5158:                         if ($field eq 'user') {
 5159:                             my $seluserdom = '';
 5160:                             my $unseluserdom = ' selected="selected"';
 5161:                             if ($userincdom) {
 5162:                                 $seluserdom = $unseluserdom;
 5163:                                 $unseluserdom = '';
 5164:                             }
 5165:                             $datatable .= ':&nbsp;'.
 5166:                                 '<select name="proctoring_userincdom_'.$provider.'">'.
 5167:                                 '<option value="0"'.$unseluserdom.'>'.$lt{'username'}.'</option>'.
 5168:                                 '<option value="1"'.$seluserdom.'>'.$lt{'uname:dom'}.'</option>'.
 5169:                                 '</select>&nbsp;';
 5170:                         } else {
 5171:                             $datatable .= '&nbsp;';
 5172:                             if ($field eq 'roles') {
 5173:                                 $showroles = 1; 
 5174:                             } 
 5175:                         }
 5176:                         $datatable .= '</span> ';
 5177:                     }
 5178:                 }
 5179:                 $datatable .= '</fieldset>'."\n";
 5180:             }
 5181:             if (ref($optuserfields{$provider}) eq 'ARRAY') {
 5182:                 if (@{$optuserfields{$provider}} > 0) {
 5183:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'optu'}.'</legend>'; 
 5184:                     foreach my $field (@{$optuserfields{$provider}}) {
 5185:                         my $checked;
 5186:                         if ($checkedfields{$field}) {
 5187:                             $checked = ' checked="checked"';
 5188:                         }
 5189:                         $datatable .= '<span class="LC_nobreak">'.
 5190:                                       '<label><input type="checkbox" name="proctoring_optional_'.$provider.'" value="'.$field.'"'.$checked.' />'.$lt{$field}.'</label></span>&nbsp; ';
 5191:                     }
 5192:                     $datatable .= '</fieldset>'."\n";
 5193:                 }
 5194:             }
 5195:             if (ref($defaults{$provider}) eq 'ARRAY') {
 5196:                 if (@{$defaults{$provider}}) {
 5197:                     my (%options,@selectboxes);
 5198:                     if (ref($extended{$provider}) eq 'HASH') {
 5199:                         %options = %{$extended{$provider}};
 5200:                     }
 5201:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'"><legend>'.$lt{'defa'}.'</legend>';
 5202:                     my ($rem,$numinrow,$dropdowns);
 5203:                     if ($provider eq 'proctorio') { 
 5204:                         $datatable .= '<table>';
 5205:                         $numinrow = 4;
 5206:                     }
 5207:                     my $i = 0;
 5208:                     foreach my $field (@{$defaults{$provider}}) {
 5209:                         my $checked;
 5210:                         if ($inuse{$field}) {
 5211:                             $checked = ' checked="checked"';
 5212:                         }
 5213:                         if ($provider eq 'examity') {
 5214:                             if ($field eq 'display') {
 5215:                                 $datatable .= '<span class="LC_nobreak">'.&mt('Display target:');
 5216:                                 foreach my $option ('iframe','tab','window') {
 5217:                                     my $checkdisp;
 5218:                                     if ($currentdef{'target'} eq $option) {
 5219:                                         $checkdisp = ' checked="checked"';
 5220:                                     }
 5221:                                     $datatable .= '<label><input type="radio" name="proctoring_target_'.$provider.'" value="'.$option.'"'.$checkdisp.' />'.
 5222:                                     $fieldtitles{$option}.'</label>'.('&nbsp;'x2);
 5223:                                 }
 5224:                                 $datatable .= ('&nbsp;'x4);
 5225:                                 foreach my $dimen ('width','height') {
 5226:                                     $datatable .= '<label>'.$fieldtitles{$dimen}.'&nbsp;'.
 5227:                                                   '<input type="text" name="proctoring_'.$dimen.'_'.$provider.'" size="5" '.
 5228:                                                   'value="'.$currentdef{$dimen}.'" /></label>'.
 5229:                                                   ('&nbsp;'x2);
 5230:                                 }
 5231:                                 $datatable .= '</span><br />'.
 5232:                                               '<div class="LC_left_float">'.$fieldtitles{'linktext'}.'<br />'.
 5233:                                               '<input type="text" name="proctoring_linktext_'.$provider.'" '.
 5234:                                               'size="25" value="'.$currentdef{'linktext'}.'" /></div>'.
 5235:                                               '<div class="LC_left_float">'.$fieldtitles{'explanation'}.'<br />'.
 5236:                                               '<textarea name="proctoring_explanation_'.$provider.'" rows="5" cols="40">'.
 5237:                                               $currentdef{'explanation'}.
 5238:                                               '</textarea></div><div style=""></div><br />';
 5239:                             }
 5240:                         } else {
 5241:                             if ((exists($options{$field})) && (ref($options{$field}) eq 'ARRAY')) {
 5242:                                 my ($output,$selnone);
 5243:                                 unless ($checked) {
 5244:                                     $selnone = ' selected="selected"';
 5245:                                 }
 5246:                                 $output .= '<span class="LC_nobreak">'.$fieldtitles{$field}.': '.
 5247:                                            '<select name="proctoring_defaults_'.$field.'_'.$provider.'">'.
 5248:                                            '<option value=""'.$selnone.'>'.&mt('Not in use').'</option>'; 
 5249:                                 foreach my $option (@{$options{$field}}) {
 5250:                                     my $sel; 
 5251:                                     if ($inuse{$field} eq $option) {
 5252:                                         $sel = ' selected="selected"';
 5253:                                     }
 5254:                                     $output .= '<option value="'.$option.'"'.$sel.'>'.$fieldtitles{$option}.'</option>';
 5255:                                 }
 5256:                                 $output .= '</select></span>';
 5257:                                 push(@selectboxes,$output);
 5258:                             } else {
 5259:                                 $rem = $i%($numinrow);
 5260:                                 if ($rem == 0) {
 5261:                                     if ($i > 0) {
 5262:                                         $datatable .= '</tr>';
 5263:                                     }
 5264:                                     $datatable .= '<tr>';
 5265:                                 }
 5266:                                 $datatable .= '<td class="LC_left_item">'.
 5267:                                               '<span class="LC_nobreak">'.
 5268:                                               '<label><input type="checkbox" name="proctoring_defaults_'.$provider.'" value="'.$field.'"'.$checked.' />'.
 5269:                                               $fieldtitles{$field}.'</label></span></td>';
 5270:                                 $i++;
 5271:                             }
 5272:                         }
 5273:                     }
 5274:                     if ($provider eq 'proctorio') {
 5275:                         if ($numinrow) {
 5276:                             $rem = $i%$numinrow;
 5277:                         }
 5278:                         my $colsleft = $numinrow - $rem;
 5279:                         if ($colsleft > 1) {
 5280:                             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5281:                         } else {
 5282:                             $datatable .= '<td class="LC_left_item">';
 5283:                         }
 5284:                         $datatable .= '&nbsp;'.
 5285:                                       '</td></tr></table>';
 5286:                         if (@selectboxes) {
 5287:                             $datatable .= '<hr /><table>';
 5288:                             $numinrow = 2;
 5289:                             for (my $i=0; $i<@selectboxes; $i++) {
 5290:                                 $rem = $i%($numinrow);
 5291:                                 if ($rem == 0) {
 5292:                                     if ($i > 0) {
 5293:                                         $datatable .= '</tr>';
 5294:                                     }
 5295:                                     $datatable .= '<tr>';
 5296:                                 }
 5297:                                 $datatable .= '<td class="LC_left_item">'.
 5298:                                               $selectboxes[$i].'</td>';
 5299:                             }
 5300:                             if ($numinrow) {
 5301:                                 $rem = $i%$numinrow;
 5302:                             }
 5303:                             $colsleft = $numinrow - $rem;
 5304:                             if ($colsleft > 1) {
 5305:                                 $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5306:                             } else {
 5307:                                 $datatable .= '<td class="LC_left_item">';
 5308:                             }
 5309:                             $datatable .= '&nbsp;'.
 5310:                                           '</td></tr></table>';
 5311:                         }
 5312:                     }
 5313:                     $datatable .= '</fieldset>';
 5314:                 }
 5315:                 if (ref($crsconf{$provider}) eq 'ARRAY') {
 5316:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5317:                                   '<legend>'.&mt('Configurable in course').'</legend>';
 5318:                     my ($rem,$numinrow);
 5319:                     if ($provider eq 'proctorio') {
 5320:                         $datatable .= '<table>';
 5321:                         $numinrow = 4;
 5322:                     }
 5323:                     my $i = 0;
 5324:                     foreach my $item (@{$crsconf{$provider}}) {
 5325:                         my $name;
 5326:                         if ($provider eq 'examity') {
 5327:                             $name = $lt{'crs'.$item};
 5328:                         } elsif ($provider eq 'proctorio') {
 5329:                             $name = $fieldtitles{$item};
 5330:                             $rem = $i%($numinrow);
 5331:                             if ($rem == 0) {
 5332:                                 if ($i > 0) {
 5333:                                     $datatable .= '</tr>';
 5334:                                 }
 5335:                                 $datatable .= '<tr>';
 5336:                             }
 5337:                             $datatable .= '<td class="LC_left_item>';
 5338:                         }
 5339:                         my $checked;
 5340:                         if ($crsconfig{$item}) {
 5341:                             $checked = ' checked="checked"';
 5342:                         }
 5343:                         $datatable .= '<span class="LC_nobreak"><label>'.
 5344:                                       '<input type="checkbox" name="proctoring_crsconf_'.$provider.'" value="'.$item.'"'.$checked.' />'.
 5345:                                       $name.'</label></span>';
 5346:                         if ($provider eq 'examity') {
 5347:                             $datatable .= '&nbsp; ';
 5348:                         }
 5349:                         $datatable .= "\n";
 5350:                         $i++;
 5351:                     }
 5352:                     if ($provider eq 'proctorio') {
 5353:                         if ($numinrow) {
 5354:                             $rem = $i%$numinrow;
 5355:                         }
 5356:                         my $colsleft = $numinrow - $rem;
 5357:                         if ($colsleft > 1) {
 5358:                             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 5359:                         } else {
 5360:                             $datatable .= '<td class="LC_left_item">';
 5361:                         }
 5362:                         $datatable .= '&nbsp;'.
 5363:                                       '</td></tr></table>';
 5364:                     }
 5365:                     $datatable .= '</fieldset>';
 5366:                 }
 5367:                 if ($showroles) {
 5368:                     $datatable .= '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5369:                                   '<legend>'.&mt('Role mapping').'</legend><table><tr>';
 5370:                     foreach my $role (@courseroles) {
 5371:                         my ($selected,$selectnone);
 5372:                         if (!$rolemaps{$role}) {
 5373:                             $selectnone = ' selected="selected"';
 5374:                         }
 5375:                         $datatable .= '<td style="text-align: center">'.
 5376:                                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 5377:                                       '<select name="proctoring_roles_'.$role.'_'.$provider.'">'.
 5378:                                       '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 5379:                         foreach my $ltirole (@ltiroles) {
 5380:                             unless ($selectnone) {
 5381:                                 if ($rolemaps{$role} eq $ltirole) {
 5382:                                     $selected = ' selected="selected"';
 5383:                                 } else {
 5384:                                     $selected = '';
 5385:                                 }
 5386:                             }
 5387:                             $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 5388:                         }
 5389:                         $datatable .= '</select></td>';
 5390:                     }
 5391:                     $datatable .= '</tr></table></fieldset>'.
 5392:                                   '<fieldset class="proctoring_'.$provider.'" style="display:'.$optionsty.'">'.
 5393:                                   '<legend>'.&mt('Custom items sent on launch').'</legend>'.
 5394:                                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 5395:                                   '<tr><td></td><td>lms</td>'.
 5396:                                   '<td><input type="text" name="proctoring_customval_lms_'.$provider.'"'.
 5397:                                   ' value="Loncapa" disabled="disabled"/></td></tr>';
 5398:                     if ((ref($settings) eq 'HASH') && (ref($settings->{$provider}) eq 'HASH') &&
 5399:                         (ref($settings->{$provider}->{'custom'}) eq 'HASH')) {
 5400:                         my %custom = %{$settings->{$provider}->{'custom'}};
 5401:                         if (keys(%custom) > 0) {
 5402:                             foreach my $key (sort(keys(%custom))) {
 5403:                                 next if ($key eq 'lms');
 5404:                                 $datatable .= '<tr><td><span class="LC_nobreak">'.
 5405:                                               '<label><input type="checkbox" name="proctoring_customdel_'.$provider.'" value="'.
 5406:                                               $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 5407:                                               '<td><input type="text" name="proctoring_customval_'.$key.'_'.$provider.'"'.
 5408:                                               ' value="'.$custom{$key}.'" /></td></tr>';
 5409:                             }
 5410:                         }
 5411:                     }
 5412:                     $datatable .= '<tr><td><span class="LC_nobreak">'.
 5413:                                   '<label><input type="checkbox" name="proctoring_customadd" value="'.$provider.'" />'.
 5414:                                   &mt('Add more').'</label></span></td><td><input type="text" name="proctoring_custom_name_'.$provider.'" />'.
 5415:                                   '</td><td><input type="text" name="proctoring_custom_value_'.$provider.'" /></td></tr>'.
 5416:                                   '</table></fieldset></td></tr>'."\n";
 5417:                 }
 5418:                 $datatable .= '</td></tr>';
 5419:             }
 5420:             $itemcount ++;
 5421:         }
 5422:     }
 5423:     return $datatable;
 5424: }
 5425: 
 5426: sub proctoring_data {
 5427:     my $requserfields = {
 5428:                       proctorio => ['user'],
 5429:                       examity   => ['roles','user'],
 5430:                      };
 5431:     my $optuserfields = {
 5432:                         proctorio => ['fullname'],
 5433:                         examity   => ['fullname','firstname','lastname','email'],
 5434:                      };
 5435:     my $defaults = {
 5436:                   proctorio => ['recordvideo','recordaudio','recordscreen','recordwebtraffic',
 5437:                                 'recordroomstart','verifyvideo','verifyaudio','verifydesktop',
 5438:                                 'verifyid','verifysignature','fullscreen','clipboard','tabslinks',
 5439:                                 'closetabs','onescreen','print','downloads','cache','rightclick',
 5440:                                 'reentry','calculator','whiteboard'],
 5441:                   examity   => ['display'],
 5442:                 };
 5443:     my $extended = { 
 5444:                   proctorio => {
 5445:                                  verifyid => ['verifyidauto','verifyidlive'],
 5446:                                  fullscreen => ['fullscreenlenient','fullscreenmoderate','fullscreensever'],
 5447:                                  tabslinks => ['notabs','linksonly'],
 5448:                                  reentry => ['noreentry','agentreentry'],
 5449:                                  calculator => ['calculatorbasic','calculatorsci'],
 5450:                                },
 5451:                   examity => {
 5452:                                display => {
 5453:                                            target      => ['iframe','tab','window'],
 5454:                                            width       => '',
 5455:                                            height      => '',
 5456:                                            linktext    => '',
 5457:                                            explanation => '',
 5458:                                           },
 5459:                              },
 5460:                 };
 5461:     my $crsconf = {
 5462:                  proctorio => ['recordvideo','recordaudio','recordscreen','recordwebtraffic',
 5463:                                'recordroomstart','verifyvideo','verifyaudio','verifydesktop',
 5464:                                'verifyid','verifysignature','fullscreen','clipboard','tabslinks',
 5465:                                'closetabs','onescreen','print','downloads','cache','rightclick',
 5466:                                'reentry','calculator','whiteboard'],
 5467:                  examity => ['label','title','target','linktext','explanation','append'],
 5468:                };
 5469:     my $courseroles = ['cc','in','ta','ep','st'];
 5470:     my $ltiroles = ['Instructor','ContentDeveloper','TeachingAssistant','Learner'];
 5471:     return ($requserfields,$optuserfields,$defaults,$extended,$crsconf,$courseroles,$ltiroles);
 5472: }
 5473: 
 5474: sub proctoring_titles {
 5475:     my ($item) = @_;
 5476:     my (%common_lt,%custom_lt);
 5477:     %common_lt = &Apache::lonlocal::texthash (
 5478:                      'avai'      => 'Available?',
 5479:                      'base'      => 'Basic Settings',
 5480:                      'requ'      => 'User data required to be sent on launch',
 5481:                      'optu'      => 'User data optionally sent on launch',
 5482:                      'udsl'      => 'User data sent on launch',
 5483:                      'defa'      => 'Defaults for items configurable in course',
 5484:                      'sigmethod' => 'Signature Method',
 5485:                      'key'       => 'Key',
 5486:                      'lifetime'  => 'Nonce lifetime (s)',
 5487:                      'secret'    => 'Secret',
 5488:                      'icon'      => 'Icon',
 5489:                      'fullname'  => 'Full Name',
 5490:                      'visible'   => 'Visible input',
 5491:                      'username'  => 'username',
 5492:                      'user'      => 'User',
 5493:                  );
 5494:     if ($item eq 'proctorio') {
 5495:         %custom_lt = &Apache::lonlocal::texthash (
 5496:                          'version'        => 'OAuth version',
 5497:                          'url'            => 'API URL',
 5498:                          'uname:dom'      => 'username-domain',
 5499:         );
 5500:     } elsif ($item eq 'examity') {
 5501:         %custom_lt = &Apache::lonlocal::texthash (
 5502:                          'version'        => 'LTI Version',
 5503:                          'url'            => 'URL',
 5504:                          'uname:dom'      => 'username:domain',
 5505:                          'msgtype'        => 'Message Type',
 5506:                          'firstname'      => 'First Name',
 5507:                          'lastname'       => 'Last Name',
 5508:                          'email'          => 'E-mail',
 5509:                          'roles'          => 'Role',
 5510:                          'crstarget'      => 'Display target',
 5511:                          'crslabel'       => 'Course label',
 5512:                          'crstitle'       => 'Course title', 
 5513:                          'crslinktext'    => 'Link Text',
 5514:                          'crsexplanation' => 'Explanation',
 5515:                          'crsappend'      => 'Provider URL',
 5516:         );
 5517:     }
 5518:     my %lt = (%common_lt,%custom_lt);
 5519:     return %lt;
 5520: }
 5521: 
 5522: sub proctoring_fieldtitles {
 5523:     my ($item) = @_;
 5524:     if ($item eq 'proctorio') {
 5525:         return &Apache::lonlocal::texthash (
 5526:                   'recordvideo' => 'Record video',
 5527:                   'recordaudio' => 'Record audio',
 5528:                   'recordscreen' => 'Record screen',
 5529:                   'recordwebtraffic' => 'Record web traffic',
 5530:                   'recordroomstart' => 'Record room scan',
 5531:                   'verifyvideo' => 'Verify webcam',
 5532:                   'verifyaudio' => 'Verify microphone',
 5533:                   'verifydesktop' => 'Verify desktop recording',
 5534:                   'verifyid' => 'Photo ID verification',
 5535:                   'verifysignature' => 'Require signature',
 5536:                   'fullscreen' => 'Fullscreen',
 5537:                   'clipboard' => 'Disable copy/paste',
 5538:                   'tabslinks' => 'New tabs/windows',
 5539:                   'closetabs' => 'Close other tabs',
 5540:                   'onescreen' => 'Limit to single screen',
 5541:                   'print' => 'Disable Printing',
 5542:                   'downloads' => 'Disable Downloads',
 5543:                   'cache' => 'Empty cache after exam',
 5544:                   'rightclick' => 'Disable right click',
 5545:                   'reentry' => 'Re-entry to exam',
 5546:                   'calculator' => 'Onscreen calculator',
 5547:                   'whiteboard' => 'Onscreen whiteboard',
 5548:                   'verifyidauto' => 'Automated verification',
 5549:                   'verifyidlive' => 'Live agent verification',
 5550:                   'fullscreenlenient' => 'Forced, but can navigate away for up to 30s',
 5551:                   'fullscreenmoderate' => 'Forced, but can navigate away for up to 15s',
 5552:                   'fullscreensever' => 'Forced, navigation away ends exam',
 5553:                   'notabs' => 'Disaallowed',
 5554:                   'linksonly' => 'Allowed from links in exam',
 5555:                   'noreentry' => 'Disallowed',
 5556:                   'agentreentry' => 'Agent required for re-entry',
 5557:                   'calculatorbasic' => 'Basic',
 5558:                   'calculatorsci' => 'Scientific',
 5559:         );
 5560:     } elsif ($item eq 'examity') {
 5561:         return &Apache::lonlocal::texthash (
 5562:                 'target'         => 'Display target',   
 5563:                 'window'         => 'Window',
 5564:                 'tab'            => 'Tab',
 5565:                 'iframe'         => 'iFrame',
 5566:                 'height'         => 'Height (pixels)',
 5567:                 'width'          => 'Width (pixels)',
 5568:                 'linktext'       => 'Default Link Text',
 5569:                 'explanation'    => 'Default Explanation',
 5570:                 'append'         => 'Provider URL',
 5571:         );
 5572:     }
 5573: }
 5574: 
 5575: sub proctoring_providernames {
 5576:     return (
 5577:              proctorio => 'Proctorio',
 5578:              examity   => 'Examity',
 5579:            );
 5580: }
 5581: 
 5582: sub print_lti {
 5583:     my ($dom,$settings,$rowtotal) = @_;
 5584:     my $itemcount = 1;
 5585:     my $maxnum = 0;
 5586:     my $css_class;
 5587:     my %ordered;
 5588:     if (ref($settings) eq 'HASH') {
 5589:         foreach my $item (keys(%{$settings})) {
 5590:             if (ref($settings->{$item}) eq 'HASH') {
 5591:                 my $num = $settings->{$item}{'order'};
 5592:                 $ordered{$num} = $item;
 5593:             }
 5594:         }
 5595:     }
 5596:     my $maxnum = scalar(keys(%ordered));
 5597:     my $datatable;
 5598:     my %lt = &lti_names();
 5599:     if (keys(%ordered)) {
 5600:         my @items = sort { $a <=> $b } keys(%ordered);
 5601:         for (my $i=0; $i<@items; $i++) {
 5602:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5603:             my $item = $ordered{$items[$i]};
 5604:             my ($key,$secret,$lifetime,$consumer,$requser,$current);
 5605:             if (ref($settings->{$item}) eq 'HASH') {
 5606:                 $key = $settings->{$item}->{'key'};
 5607:                 $secret = $settings->{$item}->{'secret'};
 5608:                 $lifetime = $settings->{$item}->{'lifetime'};
 5609:                 $consumer = $settings->{$item}->{'consumer'};
 5610:                 $requser = $settings->{$item}->{'requser'};
 5611:                 $current = $settings->{$item};
 5612:             }
 5613:             my $onclickrequser = ' onclick="toggleLTI(this.form,'."'requser','$i'".');"';
 5614:             my %checkedrequser = (
 5615:                                    yes => ' checked="checked"',
 5616:                                    no  => '',
 5617:                                  );
 5618:             if (!$requser) {
 5619:                 $checkedrequser{'no'} = $checkedrequser{'yes'};
 5620:                 $checkedrequser{'yes'} = '';
 5621:             }
 5622:             my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_".$item."'".');"';
 5623:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 5624:                          .'<select name="lti_pos_'.$item.'"'.$chgstr.'>';
 5625:             for (my $k=0; $k<=$maxnum; $k++) {
 5626:                 my $vpos = $k+1;
 5627:                 my $selstr;
 5628:                 if ($k == $i) {
 5629:                     $selstr = ' selected="selected" ';
 5630:                 }
 5631:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5632:             }
 5633:             $datatable .= '</select>'.('&nbsp;'x2).
 5634:                 '<label><input type="checkbox" name="lti_del" value="'.$item.'" />'.
 5635:                 &mt('Delete?').'</label></span></td>'.
 5636:                 '<td colspan="2">'.
 5637:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 5638:                 '<span class="LC_nobreak">'.$lt{'consumer'}.
 5639:                 ':<input type="text" size="15" name="lti_consumer_'.$i.'" value="'.$consumer.'" /></span> '.
 5640:                 ('&nbsp;'x2).
 5641:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_'.$i.'">'.
 5642:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 5643:                 ('&nbsp;'x2).
 5644:                 '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" name="lti_lifetime_'.$i.'"'.
 5645:                 'value="'.$lifetime.'" size="3" /></span>'.
 5646:                 ('&nbsp;'x2).
 5647:                  '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 5648:                  '<label><input type="radio" name="lti_requser_'.$i.'" value="1"'.$onclickrequser.$checkedrequser{yes}.' />'.&mt('Yes').'</label>&nbsp;'."\n".
 5649:                  '<label><input type="radio" name="lti_requser_'.$i.'" value="0"'.$onclickrequser.$checkedrequser{no}.' />'.&mt('No').'</label></span>'."\n".
 5650:                 '<br /><br />'.
 5651:                 '<span class="LC_nobreak">'.$lt{'key'}.
 5652:                 ':<input type="text" size="25" name="lti_key_'.$i.'" value="'.$key.'" /></span> '.
 5653:                 ('&nbsp;'x2).
 5654:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 5655:                 '<input type="password" size="20" name="lti_secret_'.$i.'" value="'.$secret.'" />'.
 5656:                 '<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>'.
 5657:                 '<input type="hidden" name="lti_id_'.$i.'" value="'.$item.'" /></span>'.
 5658:                 '</fieldset>'.&lti_options($i,$current,$itemcount,%lt).'</td></tr>';
 5659:             $itemcount ++;
 5660:         }
 5661:     }
 5662:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 5663:     my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'lti_pos_add'".');"';
 5664:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 5665:                   '<input type="hidden" name="lti_maxnum" value="'.$maxnum.'" />'."\n".
 5666:                   '<select name="lti_pos_add"'.$chgstr.'>';
 5667:     for (my $k=0; $k<$maxnum+1; $k++) {
 5668:         my $vpos = $k+1;
 5669:         my $selstr;
 5670:         if ($k == $maxnum) {
 5671:             $selstr = ' selected="selected" ';
 5672:         }
 5673:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 5674:     }
 5675:     $datatable .= '</select>&nbsp;'."\n".
 5676:                   '<input type="checkbox" name="lti_add" value="1" />'.&mt('Add').'</span></td>'."\n".
 5677:                   '<td colspan="2">'.
 5678:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 5679:                   '<span class="LC_nobreak">'.$lt{'consumer'}.
 5680:                   ':<input type="text" size="15" name="lti_consumer_add" value="" /></span> '."\n".
 5681:                   ('&nbsp;'x2).
 5682:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="lti_version_add">'.
 5683:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 5684:                   ('&nbsp;'x2).
 5685:                   '<span class="LC_nobreak">'.$lt{'lifetime'}.':<input type="text" size="3" name="lti_lifetime_add" value="300" /></span> '."\n".
 5686:                   ('&nbsp;'x2).
 5687:                   '<span class="LC_nobreak">'.$lt{'requser'}.':'.
 5688:                   '<label><input type="radio" name="lti_requser_add" value="1" onclick="toggleLTI(this.form,'."'requser','add'".');" checked="checked" />'.&mt('Yes').'</label>&nbsp;'."\n".
 5689:                   '<label><input type="radio" name="lti_requser_add" value="0" onclick="toggleLTI(this.form,'."'requser','add'".');" />'.&mt('No').'</label></span>'."\n".
 5690:                   '<br /><br />'.
 5691:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="lti_key_add" value="" /></span> '."\n".
 5692:                   ('&nbsp;'x2).
 5693:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="lti_secret_add" value="" />'.
 5694:                   '<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".
 5695:                   '</fieldset>'.&lti_options('add',undef,$itemcount,%lt).
 5696:                   '</td>'."\n".
 5697:                   '</tr>'."\n";
 5698:     $$rowtotal ++;
 5699:     return $datatable;;
 5700: }
 5701: 
 5702: sub lti_names {
 5703:     my %lt = &Apache::lonlocal::texthash(
 5704:                                           'version'   => 'LTI Version',
 5705:                                           'url'       => 'URL',
 5706:                                           'key'       => 'Key',
 5707:                                           'lifetime'  => 'Nonce lifetime (s)',
 5708:                                           'consumer'  => 'Consumer',
 5709:                                           'secret'    => 'Secret',
 5710:                                           'requser'   => "User's identity sent",
 5711:                                           'email'     => 'Email address',
 5712:                                           'sourcedid' => 'User ID',
 5713:                                           'other'     => 'Other',
 5714:                                           'passback'  => 'Can return grades to Consumer:',
 5715:                                           'roster'    => 'Can retrieve roster from Consumer:',
 5716:                                           'topmenu'   => 'Display LON-CAPA page header',
 5717:                                           'inlinemenu'=> 'Display LON-CAPA inline menu',
 5718:                                         );
 5719:     return %lt;
 5720: }
 5721: 
 5722: sub lti_options {
 5723:     my ($num,$current,$itemcount,%lt) = @_;
 5724:     my (%checked,%rolemaps,$crssecsrc,$userfield,$cidfield,$callback);
 5725:     $checked{'mapuser'}{'sourcedid'} = ' checked="checked"';
 5726:     $checked{'mapcrs'}{'course_offering_sourcedid'} = ' checked="checked"';
 5727:     $checked{'makecrs'}{'N'} = '  checked="checked"';
 5728:     $checked{'mapcrstype'} = {};
 5729:     $checked{'makeuser'} = {};
 5730:     $checked{'selfenroll'} = {};
 5731:     $checked{'crssec'} = {};
 5732:     $checked{'crssecsrc'} = {};
 5733:     $checked{'lcauth'} = {};
 5734:     $checked{'menuitem'} = {};
 5735:     if ($num eq 'add') {
 5736:         $checked{'lcauth'}{'lti'} = ' checked="checked"';
 5737:     }
 5738:     my $userfieldsty = 'none';
 5739:     my $crsfieldsty = 'none';
 5740:     my $crssecfieldsty = 'none';
 5741:     my $secsrcfieldsty = 'none';
 5742:     my $callbacksty = 'none';
 5743:     my $passbacksty = 'none';
 5744:     my $optionsty = 'block';
 5745:     my $lcauthparm;
 5746:     my $lcauthparmstyle = 'display:none';
 5747:     my $lcauthparmtext;
 5748:     my $menusty;
 5749:     my $numinrow = 4;
 5750:     my %menutitles = &ltimenu_titles();
 5751: 
 5752:     if (ref($current) eq 'HASH') {
 5753:         if (!$current->{'requser'}) {
 5754:             $optionsty = 'none';
 5755:         }
 5756:         if (($current->{'mapuser'} ne '') && ($current->{'mapuser'} ne 'lis_person_sourcedid')) {
 5757:             $checked{'mapuser'}{'sourcedid'} = '';
 5758:             if ($current->{'mapuser'} eq 'lis_person_contact_email_primary') {
 5759:                 $checked{'mapuser'}{'email'} = ' checked="checked"'; 
 5760:             } else {
 5761:                 $checked{'mapuser'}{'other'} = ' checked="checked"';
 5762:                 $userfield = $current->{'mapuser'};
 5763:                 $userfieldsty = 'inline-block';
 5764:             }
 5765:         }
 5766:         if (($current->{'mapcrs'} ne '') && ($current->{'mapcrs'} ne 'course_offering_sourcedid')) {
 5767:             $checked{'mapcrs'}{'course_offering_sourcedid'} = '';
 5768:             if ($current->{'mapcrs'} eq 'context_id') {
 5769:                 $checked{'mapcrs'}{'context_id'} = ' checked="checked"'; 
 5770:             } else {
 5771:                 $checked{'mapcrs'}{'other'} = ' checked="checked"';
 5772:                 $cidfield = $current->{'mapcrs'};
 5773:                 $crsfieldsty = 'inline-block';
 5774:             }
 5775:         }
 5776:         if (ref($current->{'mapcrstype'}) eq 'ARRAY') {
 5777:             foreach my $type (@{$current->{'mapcrstype'}}) {
 5778:                 $checked{'mapcrstype'}{$type} = ' checked="checked"';
 5779:             }
 5780:         }
 5781:         if ($current->{'makecrs'}) {
 5782:             $checked{'makecrs'}{'Y'} = '  checked="checked"';
 5783:         }
 5784:         if (ref($current->{'makeuser'}) eq 'ARRAY') {
 5785:             foreach my $role (@{$current->{'makeuser'}}) {
 5786:                 $checked{'makeuser'}{$role} = ' checked="checked"';
 5787:             }
 5788:         }
 5789:         if ($current->{'lcauth'} =~ /^(internal|localauth|krb4|krb5|lti)$/) {
 5790:             $checked{'lcauth'}{$1} = ' checked="checked"';
 5791:             unless (($current->{'lcauth'} eq 'lti') || ($current->{'lcauth'} eq 'internal')) {
 5792:                 $lcauthparm = $current->{'lcauthparm'};
 5793:                 $lcauthparmstyle = 'display:table-row'; 
 5794:                 if ($current->{'lcauth'} eq 'localauth') {
 5795:                     $lcauthparmtext = &mt('Local auth argument');
 5796:                 } else {
 5797:                     $lcauthparmtext = &mt('Kerberos domain');
 5798:                 }
 5799:             }
 5800:         }
 5801:         if (ref($current->{'selfenroll'}) eq 'ARRAY') {
 5802:             foreach my $role (@{$current->{'selfenroll'}}) {
 5803:                 $checked{'selfenroll'}{$role} = ' checked="checked"';
 5804:             }
 5805:         }
 5806:         if (ref($current->{'maproles'}) eq 'HASH') {
 5807:             %rolemaps = %{$current->{'maproles'}};
 5808:         }
 5809:         if ($current->{'section'} ne '') {
 5810:             $checked{'crssec'}{'Y'} = '  checked="checked"'; 
 5811:             $crssecfieldsty = 'inline-block';
 5812:             if ($current->{'section'} eq 'course_section_sourcedid') {
 5813:                 $checked{'crssecsrc'}{'sourcedid'} = ' checked="checked"';
 5814:             } else {
 5815:                 $checked{'crssecsrc'}{'other'} = ' checked="checked"';
 5816:                 $crssecsrc = $current->{'section'};
 5817:                 $secsrcfieldsty = 'inline-block';
 5818:             }
 5819:         } else {
 5820:             $checked{'crssec'}{'N'} = ' checked="checked"';
 5821:         }
 5822:         if ($current->{'callback'} ne '') {
 5823:             $callback = $current->{'callback'};
 5824:             $checked{'callback'}{'Y'} = ' checked="checked"';
 5825:             $callbacksty = 'inline-block';
 5826:         } else {
 5827:             $checked{'callback'}{'N'} = ' checked="checked"';
 5828:         }
 5829:         if ($current->{'topmenu'}) {
 5830:             $checked{'topmenu'}{'Y'} = ' checked="checked"';
 5831:         } else {
 5832:             $checked{'topmenu'}{'N'} = ' checked="checked"';
 5833:         }
 5834:         if ($current->{'inlinemenu'}) {
 5835:             $checked{'inlinemenu'}{'Y'} = ' checked="checked"';
 5836:         } else {
 5837:             $checked{'inlinemenu'}{'N'} = ' checked="checked"';
 5838:         }
 5839:         if (($current->{'topmenu'}) || ($current->{'inlinemenu'})) {
 5840:             $menusty = 'inline-block';
 5841:             if (ref($current->{'lcmenu'}) eq 'ARRAY') {
 5842:                 foreach my $item (@{$current->{'lcmenu'}}) {
 5843:                     if (exists($menutitles{$item})) {
 5844:                         $checked{'menuitem'}{$item} = ' checked="checked"';
 5845:                     }
 5846:                 }
 5847:             }
 5848:         } else {
 5849:             $menusty = 'none';
 5850:         }
 5851:     } else {
 5852:         $checked{'makecrs'}{'N'} = ' checked="checked"';
 5853:         $checked{'crssec'}{'N'} = ' checked="checked"';
 5854:         $checked{'callback'}{'N'} = ' checked="checked"';
 5855:         $checked{'topmenu'}{'N'} = ' checked="checked"';
 5856:         $checked{'inlinemenu'}{'Y'} = ' checked="checked"'; 
 5857:         $checked{'menuitem'}{'grades'} = ' checked="checked"';
 5858:         $menusty = 'inline-block'; 
 5859:     }
 5860:     my @coursetypes = ('official','unofficial','community','textbook','placement','lti');
 5861:     my %coursetypetitles = &Apache::lonlocal::texthash (
 5862:                                official   => 'Official',
 5863:                                unofficial => 'Unofficial',
 5864:                                community  => 'Community',
 5865:                                textbook   => 'Textbook',
 5866:                                placement  => 'Placement Test',
 5867:                                lti        => 'LTI Provider',
 5868:     );
 5869:     my @authtypes = ('internal','krb4','krb5','localauth');
 5870:     my %shortauth = (
 5871:                      internal => 'int',
 5872:                      krb4 => 'krb4',
 5873:                      krb5 => 'krb5',
 5874:                      localauth  => 'loc'
 5875:                     );
 5876:     my %authnames = &authtype_names();
 5877:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
 5878:     my @lticourseroles = qw(Learner Instructor TeachingAssistant Mentor);
 5879:     my @courseroles = ('cc','in','ta','ep','st');
 5880:     my $onclickuser = ' onclick="toggleLTI(this.form,'."'user','$num'".');"';
 5881:     my $onclickcrs = ' onclick="toggleLTI(this.form,'."'crs','$num'".');"';
 5882:     my $onclicksec = ' onclick="toggleLTI(this.form,'."'sec','$num'".');"';
 5883:     my $onclickcallback = ' onclick="toggleLTI(this.form,'."'callback','$num'".');"';
 5884:     my $onclicksecsrc = ' onclick="toggleLTI(this.form,'."'secsrc','$num'".')"';
 5885:     my $onclicklcauth = ' onclick="toggleLTI(this.form,'."'lcauth','$num'".')"';
 5886:     my $onclickmenu = ' onclick="toggleLTI(this.form,'."'lcmenu','$num'".');"';
 5887:     my $output = '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping users').'</legend>'.
 5888:                  '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('LON-CAPA username').':&nbsp;';
 5889:     foreach my $option ('sourcedid','email','other') {
 5890:         $output .= '<label><input type="radio" name="lti_mapuser_'.$num.'" value="'.$option.'"'.
 5891:                    $checked{'mapuser'}{$option}.$onclickuser.' />'.$lt{$option}.'</label>'.
 5892:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 5893:     }
 5894:     $output .= '</span></div>'.
 5895:                '<div class="LC_floatleft" style="display:'.$userfieldsty.';" id="lti_userfield_'.$num.'">'.
 5896:                '<input type="text" name="lti_customuser_'.$num.'" '.
 5897:                'value="'.$userfield.'" /></div></fieldset>'.
 5898:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping course roles').'</legend><table><tr>';
 5899:     foreach my $ltirole (@lticourseroles) {
 5900:         my ($selected,$selectnone);
 5901:         if ($rolemaps{$ltirole} eq '') {
 5902:             $selectnone = ' selected="selected"';
 5903:         }
 5904:         $output .= '<td style="text-align: center">'.$ltirole.'<br />'.
 5905:                    '<select name="lti_maprole_'.$ltirole.'_'.$num.'">'.
 5906:                    '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 5907:         foreach my $role (@courseroles) {
 5908:             unless ($selectnone) {
 5909:                 if ($rolemaps{$ltirole} eq $role) {
 5910:                     $selected = ' selected="selected"';
 5911:                 } else {
 5912:                     $selected = '';
 5913:                 }
 5914:             }
 5915:             $output .= '<option value="'.$role.'"'.$selected.'>'.
 5916:                        &Apache::lonnet::plaintext($role,'Course').
 5917:                        '</option>';
 5918:         }
 5919:         $output .= '</select></td>';
 5920:     }
 5921:     $output .= '</tr></table></fieldset>'.
 5922:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may create user accounts').'</legend>';
 5923:     foreach my $ltirole (@ltiroles) {
 5924:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_makeuser_'.$num.'" value="'.$ltirole.'"'.
 5925:                    $checked{'makeuser'}{$ltirole}.' />'.$ltirole.'</label>&nbsp;</span> ';     
 5926:     }
 5927:     $output .= '</fieldset>'.
 5928:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('New user accounts created for LTI users').'</legend>'.
 5929:                '<table>'.
 5930:                &modifiable_userdata_row('lti','instdata_'.$num,$current,$numinrow,$itemcount).
 5931:                '</table>'.
 5932:                '<table class="LC_nested"><tr><td class="LC_left_item">LON-CAPA Authentication</td>'.
 5933:                '<td class="LC_left_item">';
 5934:     foreach my $auth ('lti',@authtypes) {
 5935:         my $authtext;
 5936:         if ($auth eq 'lti') {
 5937:             $authtext = &mt('None');
 5938:         } else {
 5939:             $authtext = $authnames{$shortauth{$auth}};
 5940:         }
 5941:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="lti_lcauth_'.$num.
 5942:                    '" value="'.$auth.'"'.$checked{'lcauth'}{$auth}.$onclicklcauth.' />'.
 5943:                    $authtext.'</label></span> &nbsp;';
 5944:     }
 5945:     $output .= '</td></tr>'.
 5946:                '<tr id="lti_lcauth_parmrow_'.$num.'" style="'.$lcauthparmstyle.'">'.
 5947:                '<td class="LC_right_item" colspan="2"><span class="LC_nobreak">'.
 5948:                '<span id="lti_lcauth_parmtext_'.$num.'">'.$lcauthparmtext.'</span>'.
 5949:                '<input type="text" name="lti_lcauthparm_'.$num.'" value="" /></span></td></tr>'.
 5950:                '</table></fieldset>'.
 5951:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Mapping courses').'</legend>'.
 5952:                '<div class="LC_floatleft"><span class="LC_nobreak">'.
 5953:                &mt('Unique course identifier').':&nbsp;';
 5954:     foreach my $option ('course_offering_sourcedid','context_id','other') {
 5955:         $output .= '<label><input type="radio" name="lti_mapcrs_'.$num.'" value="'.$option.'"'.
 5956:                    $checked{'mapcrs'}{$option}.$onclickcrs.' />'.$option.'</label>'.
 5957:                    ($option eq 'other' ? '' : ('&nbsp;'x2) );
 5958:     }
 5959:     $output .= '</span></div><div class="LC_floatleft" style="display:'.$crsfieldsty.';" id="lti_crsfield_'.$num.'">'.
 5960:                '<input type="text" name="lti_mapcrsfield_'.$num.'" value="'.$cidfield.'" />'.
 5961:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>'.
 5962:                '<span class="LC_nobreak">'.&mt('LON-CAPA course type(s)').':&nbsp;';
 5963:     foreach my $type (@coursetypes) {
 5964:         $output .= '<label><input type="checkbox" name="lti_mapcrstype_'.$num.'" value="'.$type.'"'.
 5965:                    $checked{'mapcrstype'}{$type}.' />'.$coursetypetitles{$type}.'</label>'.
 5966:                    ('&nbsp;'x2);
 5967:     }
 5968:     $output .= '</span></fieldset>'.
 5969:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Creating courses').'</legend>'.
 5970:                '<span class="LC_nobreak">'.&mt('Course created (if absent) on Instructor access').':&nbsp;'.
 5971:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="0"'.
 5972:                $checked{'makecrs'}{'N'}.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5973:                '<label><input type="radio" name="lti_makecrs_'.$num.'" value="1"'.
 5974:                $checked{'makecrs'}{'Y'}.' />'.&mt('Yes').'</label></span>'.
 5975:                '</fieldset>'.
 5976:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Roles which may self-enroll').'</legend>';
 5977:     foreach my $lticrsrole (@lticourseroles) {
 5978:         $output .= '<span class="LC_nobreak"><label><input type="checkbox" name="lti_selfenroll_'.$num.'" value="'.$lticrsrole.'"'.
 5979:                    $checked{'selfenroll'}{$lticrsrole}.' />'.$lticrsrole.'</label>&nbsp;</span> ';
 5980:     }
 5981:     $output .= '</fieldset>'.
 5982:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Course options').'</legend>'.
 5983:                '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Assign users to sections').':&nbsp;'.
 5984:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="0"'.
 5985:                $checked{'crssec'}{'N'}.$onclicksec.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 5986:                '<label><input type="radio" name="lti_crssec_'.$num.'" value="1"'.
 5987:                $checked{'crssec'}{'Y'}.$onclicksec.' />'.&mt('Yes').'</label></span></div>'.
 5988:                '<div class="LC_floatleft" style="display:'.$crssecfieldsty.';" id="lti_crssecfield_'.$num.'">'.
 5989:                '<span class="LC_nobreak">'.&mt('From').':<label>'.
 5990:                '<input type="radio" name="lti_crssecsrc_'.$num.'" value="course_section_sourcedid"'.
 5991:                $checked{'crssecsrc'}{'sourcedid'}.$onclicksecsrc.' />'.
 5992:                &mt('Standard field').'</label>'.('&nbsp;'x2).
 5993:                '<label><input type="radio" name="lti_crssecsrc_'.$num.'" value="other"'.
 5994:                $checked{'crssecsrc'}{'other'}.$onclicksecsrc.' />'.&mt('Other').
 5995:                '</label></span></div><div class="LC_floatleft" style="display:'.$secsrcfieldsty.';" id="lti_secsrcfield_'.$num.'">'.
 5996:                '<input type="text" name="lti_customsection_'.$num.'" value="'.$crssecsrc.'" />'.
 5997:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div>';
 5998:     my ($pb1p1chk,$pb1p0chk,$onclickpb);
 5999:     foreach my $extra ('roster','passback') {
 6000:         my $checkedon = '';
 6001:         my $checkedoff = ' checked="checked"';
 6002:         if ($extra eq 'passback') {
 6003:             $pb1p1chk = ' checked="checked"';
 6004:             $pb1p0chk = '';
 6005:             $onclickpb = ' onclick="toggleLTI(this.form,'."'passback','$num'".');"'; 
 6006:         } else {
 6007:             $onclickpb = ''; 
 6008:         }
 6009:         if (ref($current) eq 'HASH') {
 6010:             if (($current->{$extra})) {
 6011:                 $checkedon = $checkedoff;
 6012:                 $checkedoff = '';
 6013:                 if ($extra eq 'passback') {
 6014:                     $passbacksty = 'inline-block';
 6015:                 }
 6016:                 if ($current->{'passbackformat'} eq '1.0') {
 6017:                     $pb1p0chk =  ' checked="checked"';
 6018:                     $pb1p1chk = '';
 6019:                 }
 6020:             }
 6021:         }
 6022:         $output .= $lt{$extra}.'&nbsp;'.
 6023:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="0"'.$checkedoff.$onclickpb.' />'.
 6024:                    &mt('No').'</label>'.('&nbsp;'x2).
 6025:                    '<label><input type="radio" name="lti_'.$extra.'_'.$num.'" value="1"'.$checkedon.$onclickpb.' />'.
 6026:                    &mt('Yes').'</label><br />';
 6027:     }
 6028:     $output .= '<div class="LC_floatleft" style="display:'.$passbacksty.';" id="lti_passback_'.$num.'">'.
 6029:                '<span class="LC_nobreak">'.&mt('Grade format').
 6030:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.1"'.$pb1p1chk.' />'.
 6031:                &mt('Outcomes Service (1.1)').'</label>'.('&nbsp;'x2).
 6032:                '<label><input type="radio" name="lti_passbackformat_'.$num.'" value="1.0"'.$pb1p0chk.'/>'.
 6033:                &mt('Outcomes Extension (1.0)').'</label></span></div>'.
 6034:                '<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6035:                '<div class="LC_floatleft"><span class="LC_nobreak">'.&mt('Callback on logout').':&nbsp;'.
 6036:                '<label><input type="radio" name="lti_callback_'.$num.'" value="0"'.
 6037:                $checked{'callback'}{'N'}.$onclickcallback.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6038:                '<label><input type="radio" name="lti_callback_'.$num.'" value="1"'.
 6039:                $checked{'callback'}{'Y'}.$onclickcallback.' />'.&mt('Yes').'</label></span></div>'.
 6040:                '<div class="LC_floatleft" style="display:'.$callbacksty.';" id="lti_callbackfield_'.$num.'">'.
 6041:                '<span class="LC_nobreak">'.&mt('Parameter').': '.
 6042:                '<input type="text" name="lti_callbackparam_'.$num.'" value="'.$callback.'" /></span>'.
 6043:                '</div><div style="padding:0;clear:both;margin:0;border:0"></div></fieldset>'.
 6044:                '<fieldset class="ltioption_'.$num.'" style="display:'.$optionsty.'"><legend>'.&mt('Course defaults (Course Coordinator can override)').'</legend>'.
 6045:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'topmenu'}.':&nbsp;'.
 6046:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="0"'.
 6047:                $checked{'topmenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6048:                '<label><input type="radio" name="lti_topmenu_'.$num.'" value="1"'.
 6049:                $checked{'topmenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>'.
 6050:                '<div style="padding:0;clear:both;margin:0;border:0"></div>'.
 6051:                '<div class="LC_floatleft"><span class="LC_nobreak">'.$lt{'inlinemenu'}.':&nbsp;'.
 6052:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="0"'.
 6053:                $checked{'inlinemenu'}{'N'}.$onclickmenu.' />'.&mt('No').'</label>'.('&nbsp;'x2).
 6054:                '<label><input type="radio" name="lti_inlinemenu_'.$num.'" value="1"'.
 6055:                $checked{'inlinemenu'}{'Y'}.$onclickmenu.' />'.&mt('Yes').'</label></span></div>';
 6056:      $output .='<div style="padding:0;clear:both;margin:0;border:0"></div>'. 
 6057:                '<div class="LC_floatleft" style="display:'.$menusty.';" id="lti_menufield_'.$num.'">'.
 6058:                '<span class="LC_nobreak">'.&mt('Menu items').':&nbsp;';
 6059:     foreach my $type ('fullname','coursetitle','role','logout','grades') {
 6060:         $output .= '<label><input type="checkbox" name="lti_menuitem_'.$num.'" value="'.$type.'"'.
 6061:                    $checked{'menuitem'}{$type}.' />'.$menutitles{$type}.'</label>'.
 6062:                    ('&nbsp;'x2);
 6063:     }
 6064:     $output .= '</span></div></fieldset>';
 6065: #        '<fieldset><legend>'.&mt('Assigning author roles').'</legend>';
 6066: #
 6067: #    $output .= '</fieldset>'.
 6068: #        '<fieldset><legend>'.&mt('Assigning domain roles').'</legend>';
 6069:     return $output;
 6070: }
 6071: 
 6072: sub ltimenu_titles {
 6073:     return &Apache::lonlocal::texthash(
 6074:                                         fullname    => 'Full name',
 6075:                                         coursetitle => 'Course title',
 6076:                                         role        => 'Role',
 6077:                                         logout      => 'Logout',
 6078:                                         grades      => 'Grades',
 6079:     );
 6080: }
 6081: 
 6082: sub print_coursedefaults {
 6083:     my ($position,$dom,$settings,$rowtotal) = @_;
 6084:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 6085:     my $itemcount = 1;
 6086:     my %choices =  &Apache::lonlocal::texthash (
 6087:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 6088:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 6089:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 6090:         coursecredits        => 'Credits can be specified for courses',
 6091:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 6092:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 6093:         texengine            => 'Default method to display mathematics',
 6094:         postsubmit           => 'Disable submit button/keypress following student submission',
 6095:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 6096:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 6097:     );
 6098:     my %staticdefaults = (
 6099:                            anonsurvey_threshold => 10,
 6100:                            uploadquota          => 500,
 6101:                            postsubmit           => 60,
 6102:                            mysqltables          => 172800,
 6103:                          );
 6104:     if ($position eq 'top') {
 6105:         %defaultchecked = (
 6106:                             'canuse_pdfforms' => 'off',
 6107:                             'uselcmath'       => 'on',
 6108:                             'usejsme'         => 'on',
 6109:                             'canclone'        => 'none',
 6110:                           );
 6111:         @toggles = ('canuse_pdfforms','uselcmath','usejsme');
 6112:         my $deftex = $Apache::lonnet::deftex;
 6113:         if (ref($settings) eq 'HASH') {
 6114:             if ($settings->{'texengine'}) {
 6115:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 6116:                     $deftex = $settings->{'texengine'};
 6117:                 }
 6118:             }
 6119:         }
 6120:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6121:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 6122:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 6123:                        '</span></td><td class="LC_right_item">'.
 6124:                        '<select name="texengine">'."\n";
 6125:         my %texoptions = (
 6126:                             MathJax  => 'MathJax',
 6127:                             mimetex  => &mt('Convert to Images'),
 6128:                             tth      => &mt('TeX to HTML'),
 6129:                          );
 6130:         foreach my $renderer ('MathJax','mimetex','tth') {
 6131:             my $selected = '';
 6132:             if ($renderer eq $deftex) {
 6133:                 $selected = ' selected="selected"';
 6134:             }
 6135:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 6136:         }
 6137:         $mathdisp .= '</select></td></tr>'."\n";
 6138:         $itemcount ++;
 6139:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 6140:                                                      \%choices,$itemcount);
 6141:         $datatable = $mathdisp.$datatable;
 6142:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6143:         $datatable .=
 6144:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 6145:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 6146:             '</span></td><td class="LC_left_item">';
 6147:         my $currcanclone = 'none';
 6148:         my $onclick;
 6149:         my @cloneoptions = ('none','domain');
 6150:         my %clonetitles = (
 6151:                              none     => 'No additional course requesters',
 6152:                              domain   => "Any course requester in course's domain",
 6153:                              instcode => 'Course requests for official courses ...',
 6154:                           );
 6155:         my (%codedefaults,@code_order,@posscodes);
 6156:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 6157:                                                     \@code_order) eq 'ok') {
 6158:             if (@code_order > 0) {
 6159:                 push(@cloneoptions,'instcode');
 6160:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 6161:             }
 6162:         }
 6163:         if (ref($settings) eq 'HASH') {
 6164:             if ($settings->{'canclone'}) {
 6165:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 6166:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 6167:                         if (@code_order > 0) {
 6168:                             $currcanclone = 'instcode';
 6169:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 6170:                         }
 6171:                     }
 6172:                 } elsif ($settings->{'canclone'} eq 'domain') {
 6173:                     $currcanclone = $settings->{'canclone'};
 6174:                 }
 6175:             }
 6176:         }
 6177:         foreach my $option (@cloneoptions) {
 6178:             my ($checked,$additional);
 6179:             if ($currcanclone eq $option) {
 6180:                 $checked = ' checked="checked"';
 6181:             }
 6182:             if ($option eq 'instcode') {
 6183:                 if (@code_order) {
 6184:                     my $show = 'none';
 6185:                     if ($checked) {
 6186:                         $show = 'block';
 6187:                     }
 6188:                     $additional = '<div id="cloneinstcode" style="display:'.$show.';" />'.
 6189:                                   &mt('Institutional codes for new and cloned course have identical:').
 6190:                                   '<br />';
 6191:                     foreach my $item (@code_order) {
 6192:                         my $codechk;
 6193:                         if ($checked) {
 6194:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 6195:                                 $codechk = ' checked="checked"';
 6196:                             }
 6197:                         }
 6198:                         $additional .= '<label>'.
 6199:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 6200:                                        $item.'</label>';
 6201:                     }
 6202:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 6203:                 }
 6204:             }
 6205:             $datatable .=
 6206:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 6207:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 6208:                 '</label>&nbsp;'.$additional.'</span><br />';
 6209:         }
 6210:         $datatable .= '</td>'.
 6211:                       '</tr>';
 6212:         $itemcount ++;
 6213:     } else {
 6214:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6215:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 6216:         my $currusecredits = 0;
 6217:         my $postsubmitclient = 1;
 6218:         my @types = ('official','unofficial','community','textbook','placement');
 6219:         if (ref($settings) eq 'HASH') {
 6220:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 6221:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 6222:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 6223:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 6224:                 }
 6225:             }
 6226:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 6227:                 foreach my $type (@types) {
 6228:                     next if ($type eq 'community');
 6229:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 6230:                     if ($defcredits{$type} ne '') {
 6231:                         $currusecredits = 1;
 6232:                     }
 6233:                 }
 6234:             }
 6235:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 6236:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 6237:                     $postsubmitclient = 0;
 6238:                     foreach my $type (@types) {
 6239:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6240:                     }
 6241:                 } else {
 6242:                     foreach my $type (@types) {
 6243:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 6244:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 6245:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 6246:                             } else {
 6247:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6248:                             }
 6249:                         } else {
 6250:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6251:                         }
 6252:                     }
 6253:                 }
 6254:             } else {
 6255:                 foreach my $type (@types) {
 6256:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6257:                 }
 6258:             }
 6259:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 6260:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 6261:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 6262:                 }
 6263:             } else {
 6264:                 foreach my $type (@types) {
 6265:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 6266:                 }
 6267:             }
 6268:         } else {
 6269:             foreach my $type (@types) {
 6270:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 6271:             }
 6272:         }
 6273:         if (!$currdefresponder) {
 6274:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 6275:         } elsif ($currdefresponder < 1) {
 6276:             $currdefresponder = 1;
 6277:         }
 6278:         foreach my $type (@types) {
 6279:             if ($curruploadquota{$type} eq '') {
 6280:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 6281:             }
 6282:         }
 6283:         $datatable .=
 6284:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6285:                 $choices{'anonsurvey_threshold'}.
 6286:                 '</span></td>'.
 6287:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 6288:                 '<input type="text" name="anonsurvey_threshold"'.
 6289:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 6290:                 '</td></tr>'."\n";
 6291:         $itemcount ++;
 6292:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6293:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6294:                       $choices{'uploadquota'}.
 6295:                       '</span></td>'.
 6296:                       '<td style="text-align: right" class="LC_right_item">'.
 6297:                       '<table><tr>';
 6298:         foreach my $type (@types) {
 6299:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 6300:                            '<input type="text" name="uploadquota_'.$type.'"'.
 6301:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 6302:         }
 6303:         $datatable .= '</tr></table></td></tr>'."\n";
 6304:         $itemcount ++;
 6305:         my $onclick = "toggleDisplay(this.form,'credits');";
 6306:         my $display = 'none';
 6307:         if ($currusecredits) {
 6308:             $display = 'block';
 6309:         }
 6310:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 6311:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 6312:         foreach my $type (@types) {
 6313:             next if ($type eq 'community');
 6314:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 6315:                            '<input type="text" name="'.$type.'_credits"'.
 6316:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 6317:         }
 6318:         $additional .= '</tr></table></div>'."\n";
 6319:         %defaultchecked = ('coursecredits' => 'off');
 6320:         @toggles = ('coursecredits');
 6321:         my $current = {
 6322:                         'coursecredits' => $currusecredits,
 6323:                       };
 6324:         (my $table,$itemcount) =
 6325:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 6326:                                \%choices,$itemcount,$onclick,$additional,'left');
 6327:         $datatable .= $table;
 6328:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 6329:         my $display = 'none';
 6330:         if ($postsubmitclient) {
 6331:             $display = 'block';
 6332:         }
 6333:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 6334:                       &mt('Number of seconds submit is disabled').'<br />'.
 6335:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 6336:                       '<table><tr>';
 6337:         foreach my $type (@types) {
 6338:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 6339:                            '<input type="text" name="'.$type.'_timeout" value="'.
 6340:                            $deftimeout{$type}.'" size="5" /></td>';
 6341:         }
 6342:         $additional .= '</tr></table></div>'."\n";
 6343:         %defaultchecked = ('postsubmit' => 'on');
 6344:         @toggles = ('postsubmit');
 6345:         $current = {
 6346:                        'postsubmit' => $postsubmitclient,
 6347:                    };
 6348:         ($table,$itemcount) =
 6349:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 6350:                                \%choices,$itemcount,$onclick,$additional,'left');
 6351:         $datatable .= $table;
 6352:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6353:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6354:                       $choices{'mysqltables'}.
 6355:                       '</span></td>'.
 6356:                       '<td style="text-align: right" class="LC_right_item">'.
 6357:                       '<table><tr>';
 6358:         foreach my $type (@types) {
 6359:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 6360:                            '<input type="text" name="mysqltables_'.$type.'"'.
 6361:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 6362:         }
 6363:         $datatable .= '</tr></table></td></tr>'."\n";
 6364:         $itemcount ++;
 6365: 
 6366:     }
 6367:     $$rowtotal += $itemcount;
 6368:     return $datatable;
 6369: }
 6370: 
 6371: sub print_selfenrollment {
 6372:     my ($position,$dom,$settings,$rowtotal) = @_;
 6373:     my ($css_class,$datatable);
 6374:     my $itemcount = 1;
 6375:     my @types = ('official','unofficial','community','textbook','placement');
 6376:     if (($position eq 'top') || ($position eq 'middle')) {
 6377:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 6378:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 6379:         my @rows;
 6380:         my $key;
 6381:         if ($position eq 'top') {
 6382:             $key = 'admin'; 
 6383:             if (ref($rowsref) eq 'ARRAY') {
 6384:                 @rows = @{$rowsref};
 6385:             }
 6386:         } elsif ($position eq 'middle') {
 6387:             $key = 'default';
 6388:             @rows = ('types','registered','approval','limit');
 6389:         }
 6390:         foreach my $row (@rows) {
 6391:             if (defined($titlesref->{$row})) {
 6392:                 $itemcount ++;
 6393:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6394:                 $datatable .= '<tr'.$css_class.'>'.
 6395:                               '<td>'.$titlesref->{$row}.'</td>'.
 6396:                               '<td class="LC_left_item">'.
 6397:                               '<table><tr>';
 6398:                 my (%current,%currentcap);
 6399:                 if (ref($settings) eq 'HASH') {
 6400:                     if (ref($settings->{$key}) eq 'HASH') {
 6401:                         foreach my $type (@types) {
 6402:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 6403:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 6404:                             }
 6405:                             if (($row eq 'limit') && ($key eq 'default')) {
 6406:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 6407:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 6408:                                 }
 6409:                             }
 6410:                         }
 6411:                     }
 6412:                 }
 6413:                 my %roles = (
 6414:                              '0' => &Apache::lonnet::plaintext('dc'),
 6415:                             ); 
 6416:             
 6417:                 foreach my $type (@types) {
 6418:                     unless (($row eq 'registered') && ($key eq 'default')) {
 6419:                         $datatable .= '<th>'.&mt($type).'</th>';
 6420:                     }
 6421:                 }
 6422:                 unless (($row eq 'registered') && ($key eq 'default')) {
 6423:                     $datatable .= '</tr><tr>';
 6424:                 }
 6425:                 foreach my $type (@types) {
 6426:                     if ($type eq 'community') {
 6427:                         $roles{'1'} = &mt('Community personnel');
 6428:                     } else {
 6429:                         $roles{'1'} = &mt('Course personnel');
 6430:                     }
 6431:                     $datatable .= '<td style="vertical-align: top">';
 6432:                     if ($position eq 'top') {
 6433:                         my %checked;
 6434:                         if ($current{$type} eq '0') {
 6435:                             $checked{'0'} = ' checked="checked"';
 6436:                         } else {
 6437:                             $checked{'1'} = ' checked="checked"';
 6438:                         }
 6439:                         foreach my $role ('1','0') {
 6440:                             $datatable .= '<span class="LC_nobreak"><label>'.
 6441:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 6442:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 6443:                                           $roles{$role}.'</label></span> ';
 6444:                         }
 6445:                     } else {
 6446:                         if ($row eq 'types') {
 6447:                             my %checked;
 6448:                             if ($current{$type} =~ /^(all|dom)$/) {
 6449:                                 $checked{$1} = ' checked="checked"';
 6450:                             } else {
 6451:                                 $checked{''} = ' checked="checked"';
 6452:                             }
 6453:                             foreach my $val ('','dom','all') {
 6454:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 6455:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 6456:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 6457:                             }
 6458:                         } elsif ($row eq 'registered') {
 6459:                             my %checked;
 6460:                             if ($current{$type} eq '1') {
 6461:                                 $checked{'1'} = ' checked="checked"';
 6462:                             } else {
 6463:                                 $checked{'0'} = ' checked="checked"';
 6464:                             }
 6465:                             foreach my $val ('0','1') {
 6466:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 6467:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 6468:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 6469:                             }
 6470:                         } elsif ($row eq 'approval') {
 6471:                             my %checked;
 6472:                             if ($current{$type} =~ /^([12])$/) {
 6473:                                 $checked{$1} = ' checked="checked"';
 6474:                             } else {
 6475:                                 $checked{'0'} = ' checked="checked"';
 6476:                             }
 6477:                             for my $val (0..2) {
 6478:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 6479:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 6480:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 6481:                             }
 6482:                         } elsif ($row eq 'limit') {
 6483:                             my %checked;
 6484:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 6485:                                 $checked{$1} = ' checked="checked"';
 6486:                             } else {
 6487:                                 $checked{'none'} = ' checked="checked"';
 6488:                             }
 6489:                             my $cap;
 6490:                             if ($currentcap{$type} =~ /^\d+$/) {
 6491:                                 $cap = $currentcap{$type};
 6492:                             }
 6493:                             foreach my $val ('none','allstudents','selfenrolled') {
 6494:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 6495:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 6496:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 6497:                             }
 6498:                             $datatable .= '<br />'.
 6499:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 6500:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 6501:                                           '</span>'; 
 6502:                         }
 6503:                     }
 6504:                     $datatable .= '</td>';
 6505:                 }
 6506:                 $datatable .= '</tr>';
 6507:             }
 6508:             $datatable .= '</table></td></tr>';
 6509:         }
 6510:     } elsif ($position eq 'bottom') {
 6511:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 6512:     }
 6513:     $$rowtotal += $itemcount;
 6514:     return $datatable;
 6515: }
 6516: 
 6517: sub print_validation_rows {
 6518:     my ($caller,$dom,$settings,$rowtotal) = @_;
 6519:     my ($itemsref,$namesref,$fieldsref);
 6520:     if ($caller eq 'selfenroll') { 
 6521:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 6522:     } elsif ($caller eq 'requestcourses') {
 6523:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 6524:     }
 6525:     my %currvalidation;
 6526:     if (ref($settings) eq 'HASH') {
 6527:         if (ref($settings->{'validation'}) eq 'HASH') {
 6528:             %currvalidation = %{$settings->{'validation'}};
 6529:         }
 6530:     }
 6531:     my $datatable;
 6532:     my $itemcount = 0;
 6533:     foreach my $item (@{$itemsref}) {
 6534:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6535:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 6536:                       $namesref->{$item}.
 6537:                       '</span></td>'.
 6538:                       '<td class="LC_left_item">';
 6539:         if (($item eq 'url') || ($item eq 'button')) {
 6540:             $datatable .= '<span class="LC_nobreak">'.
 6541:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 6542:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 6543:         } elsif ($item eq 'fields') {
 6544:             my @currfields;
 6545:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 6546:                 @currfields = @{$currvalidation{$item}};
 6547:             }
 6548:             foreach my $field (@{$fieldsref}) {
 6549:                 my $check = '';
 6550:                 if (grep(/^\Q$field\E$/,@currfields)) {
 6551:                     $check = ' checked="checked"';
 6552:                 }
 6553:                 $datatable .= '<span class="LC_nobreak"><label>'.
 6554:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 6555:                               ' value="'.$field.'"'.$check.' />'.$field.
 6556:                               '</label></span> ';
 6557:             }
 6558:         } elsif ($item eq 'markup') {
 6559:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5">'.
 6560:                            $currvalidation{$item}.
 6561:                               '</textarea>';
 6562:         }
 6563:         $datatable .= '</td></tr>'."\n";
 6564:         if (ref($rowtotal)) {
 6565:             $itemcount ++;
 6566:         }
 6567:     }
 6568:     if ($caller eq 'requestcourses') {
 6569:         my %currhash;
 6570:         if (ref($settings) eq 'HASH') {
 6571:             if (ref($settings->{'validation'}) eq 'HASH') {
 6572:                 if ($settings->{'validation'}{'dc'} ne '') {
 6573:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 6574:                 }
 6575:             }
 6576:         }
 6577:         my $numinrow = 2;
 6578:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 6579:                                                        'validationdc',%currhash);
 6580:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 6581:         $datatable .= '<tr'.$css_class.'><td>';
 6582:         if ($numdc > 1) {
 6583:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 6584:         } else {
 6585:             $datatable .=  &mt('Course creation processed as: ');
 6586:         }
 6587:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 6588:         $itemcount ++;
 6589:     }
 6590:     if (ref($rowtotal)) {
 6591:         $$rowtotal += $itemcount;
 6592:     }
 6593:     return $datatable;
 6594: }
 6595: 
 6596: sub print_privacy {
 6597:     my ($position,$dom,$settings,$rowtotal) = @_;
 6598:     my ($datatable,$css_class,$numinrow,@items,%names,$othertitle,$usertypes,$types);
 6599:     my $itemcount = 0;
 6600:     unless ($position eq 'top') {
 6601:         @items = ('domain','author','course','community');
 6602:         %names = &Apache::lonlocal::texthash (
 6603:                      domain => 'Assigned domain role(s)',
 6604:                      author => 'Assigned co-author role(s)',
 6605:                      course => 'Assigned course role(s)',
 6606:                      community => 'Assigned community role',
 6607:                  );
 6608:         $numinrow = 4;
 6609:         ($othertitle,$usertypes,$types) =
 6610:             &Apache::loncommon::sorted_inst_types($dom);
 6611:     }
 6612:     if (($position eq 'top') || ($position eq 'middle')) {
 6613:         my (%by_ip,%by_location,@intdoms,@instdoms);
 6614:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 6615:         if ($position eq 'top') {
 6616:             my %curr;
 6617:             my @options = ('none','user','domain','auto');
 6618:             my %titles = &Apache::lonlocal::texthash (
 6619:                 none   => 'Not allowed',
 6620:                 user   => 'User authorizes',
 6621:                 domain => 'DC authorizes',
 6622:                 auto   => 'Unrestricted',
 6623:                 instdom => 'Other domain shares institution/provider',
 6624:                 extdom => 'Other domain has different institution/provider',
 6625:             );
 6626:             my %names = &Apache::lonlocal::texthash (
 6627:                 domain => 'Domain role',
 6628:                 author => 'Co-author role',
 6629:                 course => 'Course role',
 6630:                 community => 'Community role',
 6631:             );
 6632:             my $primary_id = &Apache::lonnet::domain($dom,'primary');
 6633:             my $intdom = &Apache::lonnet::internet_dom($primary_id);
 6634:             foreach my $domtype ('instdom','extdom') {
 6635:                 my (%checked,$skip);
 6636:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6637:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$domtype}.'</td>'.
 6638:                               '<td class="LC_left_item">';
 6639:                 if ($domtype eq 'instdom') {
 6640:                     unless (@instdoms > 1) {
 6641:                         $datatable .= &mt('Nothing to set, as no domains besides [_1] are hosted by [_2]',$dom,$intdom);
 6642:                         $skip = 1;
 6643:                     }
 6644:                 } elsif ($domtype eq 'extdom') {
 6645:                     if (keys(%by_location) == 0) {
 6646:                         $datatable .= &mt('Nothing to set, as no other hosts besides [_1]',$intdom);
 6647:                         $skip = 1;
 6648:                     }
 6649:                 }
 6650:                 unless ($skip) {
 6651:                     foreach my $roletype ('domain','author','course','community') {
 6652:                         $checked{'auto'} = ' checked="checked"';
 6653:                         if (ref($settings) eq 'HASH') {
 6654:                             if (ref($settings->{approval}) eq 'HASH') {
 6655:                                 if (ref($settings->{approval}->{$domtype}) eq 'HASH') {
 6656:                                     if ($settings->{approval}->{$domtype}->{$roletype}=~ /^(none|user|domain)$/) {
 6657:                                         $checked{$1} = ' checked="checked"';
 6658:                                         $checked{'auto'} = '';
 6659:                                     }
 6660:                                 }
 6661:                             }
 6662:                         }
 6663:                         $datatable .= '<fieldset><legend>'.$names{$roletype}.'</legend>';
 6664:                         foreach my $option (@options) {
 6665:                             $datatable .= '<span class="LC_nobreak"><label>'.
 6666:                                           '<input type="radio" name="privacy_approval_'.$domtype.'_'.$roletype.'" '.
 6667:                                           'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 6668:                                           '</label></span>&nbsp; ';
 6669:                         }
 6670:                         $datatable .= '</fieldset>';
 6671:                     }
 6672:                 }
 6673:                 $datatable .= '</td></tr>';
 6674:                 $itemcount ++;
 6675:             }
 6676:         } elsif ($position eq 'middle') {
 6677:             if ((@instdoms > 1) || (keys(%by_location) > 0)) {
 6678:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 6679:                     foreach my $item (@{$types}) {
 6680:                         $datatable .= &modifiable_userdata_row('privacy','othdom_'.$item,$settings,
 6681:                                                                $numinrow,$itemcount,'','','','','',
 6682:                                                                '',$usertypes->{$item});
 6683:                         $itemcount ++;
 6684:                     }
 6685:                 }
 6686:                 $datatable .= &modifiable_userdata_row('privacy','othdom_default',$settings,
 6687:                                                        $numinrow,$itemcount,'','','','','',
 6688:                                                        '',$othertitle);
 6689:                 $itemcount ++;
 6690:             } else {
 6691:                 my (@insttypes,%insttitles);
 6692:                 if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 6693:                     @insttypes = @{$types};
 6694:                     %insttitles = %{$usertypes};
 6695:                 }
 6696:                 foreach my $item (@insttypes,'default') {
 6697:                     my $title;
 6698:                     if ($item eq 'default') {
 6699:                         $title = $othertitle;
 6700:                     } else {
 6701:                         $title = $insttitles{$item};
 6702:                     }
 6703:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6704:                     $datatable .= '<tr'.$css_class.'>'.
 6705:                                   '<td class="LC_left_item">'.$title.'</td>'.
 6706:                                   '<td class="LC_left_item">'.
 6707:                                   &mt('Nothing to set here, as there are no other domains').
 6708:                                   '</td></tr>';
 6709:                     $itemcount ++;
 6710:                 }
 6711:             }
 6712:         }
 6713:     } else {
 6714:         my $prefix;
 6715:         if ($position eq 'lower') {
 6716:             $prefix = 'priv';
 6717:         } else {
 6718:             $prefix = 'unpriv';
 6719:         }
 6720:         foreach my $item (@items) {
 6721:             $datatable .= &modifiable_userdata_row('privacy',$prefix.'_'.$item,$settings,
 6722:                                                    $numinrow,$itemcount,'','','','','',
 6723:                                                    '',$names{$item});
 6724:             $itemcount ++;
 6725:         }
 6726:     }
 6727:     if (ref($rowtotal)) {
 6728:         $$rowtotal += $itemcount;
 6729:     }
 6730:     return $datatable;
 6731: }
 6732: 
 6733: sub print_passwords {
 6734:     my ($position,$dom,$confname,$settings,$rowtotal) = @_;
 6735:     my ($datatable,$css_class);
 6736:     my $itemcount = 0;
 6737:     my %titles = &Apache::lonlocal::texthash (
 6738:         captcha        => '"Forgot Password" CAPTCHA validation',
 6739:         link           => 'Reset link expiration (hours)',
 6740:         case           => 'Case-sensitive usernames/e-mail',
 6741:         prelink        => 'Information required (form 1)',
 6742:         postlink       => 'Information required (form 2)',
 6743:         emailsrc       => 'LON-CAPA e-mail address type(s)',
 6744:         customtext     => 'Domain specific text (HTML)',
 6745:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
 6746:         intauth_check  => 'Check bcrypt cost if authenticated',
 6747:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
 6748:         permanent      => 'Permanent e-mail address',
 6749:         critical       => 'Critical notification address',
 6750:         notify         => 'Notification address',
 6751:         min            => 'Minimum password length',
 6752:         max            => 'Maximum password length',
 6753:         chars          => 'Required characters',
 6754:         expire         => 'Password expiration (days)',
 6755:         numsaved       => 'Number of previous passwords to save and disallow reuse',
 6756:     );
 6757:     if ($position eq 'top') {
 6758:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 6759:         my $shownlinklife = 2;
 6760:         my $prelink = 'both';
 6761:         my (%casesens,%postlink,%emailsrc,$nostdtext,$customurl);
 6762:         if (ref($settings) eq 'HASH') {
 6763:             if ($settings->{resetlink} =~ /^\d+(|\.\d*)$/) {
 6764:                 $shownlinklife = $settings->{resetlink};
 6765:             }
 6766:             if (ref($settings->{resetcase}) eq 'ARRAY') {
 6767:                 map { $casesens{$_} = 1; } (@{$settings->{resetcase}});
 6768:             }
 6769:             if ($settings->{resetprelink} =~ /^(both|either)$/) {
 6770:                 $prelink = $settings->{resetprelink};
 6771:             }
 6772:             if (ref($settings->{resetpostlink}) eq 'HASH') {
 6773:                 %postlink = %{$settings->{resetpostlink}};
 6774:             }
 6775:             if (ref($settings->{resetemail}) eq 'ARRAY') {
 6776:                 map { $emailsrc{$_} = 1; } (@{$settings->{resetemail}});
 6777:             }
 6778:             if ($settings->{resetremove}) {
 6779:                 $nostdtext = 1;
 6780:             }
 6781:             if ($settings->{resetcustom}) {
 6782:                 $customurl = $settings->{resetcustom};
 6783:             }
 6784:         } else {
 6785:             if (ref($types) eq 'ARRAY') {
 6786:                 foreach my $item (@{$types}) {
 6787:                     $casesens{$item} = 1;
 6788:                     $postlink{$item} = ['username','email'];
 6789:                 }
 6790:             }
 6791:             $casesens{'default'} = 1;
 6792:             $postlink{'default'} = ['username','email'];
 6793:             $prelink = 'both';
 6794:             %emailsrc = (
 6795:                           permanent => 1,
 6796:                           critical  => 1,
 6797:                           notify    => 1,
 6798:             );
 6799:         }
 6800:         $datatable = &captcha_choice('passwords',$settings,$$rowtotal);
 6801:         $itemcount ++;
 6802:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6803:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'link'}.'</td>'.
 6804:                       '<td class="LC_left_item">'.
 6805:                       '<input type="textbox" value="'.$shownlinklife.'" '.
 6806:                       'name="passwords_link" size="3" /></td></tr>';
 6807:         $itemcount ++;
 6808:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6809:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'case'}.'</td>'.
 6810:                       '<td class="LC_left_item">';
 6811:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 6812:             foreach my $item (@{$types}) {
 6813:                 my $checkedcase;
 6814:                 if ($casesens{$item}) {
 6815:                     $checkedcase = ' checked="checked"';
 6816:                 }
 6817:                 $datatable .= '<span class="LC_nobreak"><label>'.
 6818:                               '<input type="checkbox" name="passwords_case_sensitive" value="'.
 6819:                               $item.'"'.$checkedcase.' />'.$usertypes->{$item}.'</label>'.
 6820:                               '</span>&nbsp;&nbsp; ';
 6821:             }
 6822:         }
 6823:         my $checkedcase;
 6824:         if ($casesens{'default'}) {
 6825:             $checkedcase = ' checked="checked"';
 6826:         }
 6827:         $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 6828:                       'name="passwords_case_sensitive" value="default"'.$checkedcase.' />'.
 6829:                       $othertitle.'</label></span></td>';
 6830:         $itemcount ++;
 6831:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6832:         my %checkedpre = (
 6833:                              both => ' checked="checked"',
 6834:                              either => '',
 6835:                          );
 6836:         if ($prelink eq 'either') {
 6837:             $checkedpre{either} = ' checked="checked"';
 6838:             $checkedpre{both} = '';
 6839:         }
 6840:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'prelink'}.'</td>'.
 6841:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 6842:                       '<label><input type="radio" name="passwords_prelink" value="both"'.$checkedpre{'both'}.' />'.
 6843:                       &mt('Both username and e-mail address').'</label></span>&nbsp;&nbsp; '.
 6844:                       '<span class="LC_nobreak"><label>'.
 6845:                       '<input type="radio" name="passwords_prelink" value="either"'.$checkedpre{'either'}.' />'.
 6846:                       &mt('Either username or e-mail address').'</label></span></td></tr>';
 6847:         $itemcount ++;
 6848:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6849:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'postlink'}.'</td>'.
 6850:                       '<td class="LC_left_item">';
 6851:         my %postlinked;
 6852:         if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 6853:             foreach my $item (@{$types}) {
 6854:                 undef(%postlinked);
 6855:                 $datatable .= '<fieldset style="display: inline-block;">'.
 6856:                               '<legend>'.$usertypes->{$item}.'</legend>';
 6857:                 if (ref($postlink{$item}) eq 'ARRAY') {
 6858:                     map { $postlinked{$_} = 1; } (@{$postlink{$item}});
 6859:                 }
 6860:                 foreach my $field ('email','username') {
 6861:                     my $checked;
 6862:                     if ($postlinked{$field}) {
 6863:                         $checked = ' checked="checked"';
 6864:                     }
 6865:                     $datatable .= '<span class="LC_nobreak"><label>'.
 6866:                                   '<input type="checkbox" name="passwords_postlink_'.$item.'" value="'.
 6867:                                   $field.'"'.$checked.' />'.$field.'</label>'.
 6868:                                   '<span>&nbsp;&nbsp; ';
 6869:                 }
 6870:                 $datatable .= '</fieldset>';
 6871:             }
 6872:         }
 6873:         if (ref($postlink{'default'}) eq 'ARRAY') {
 6874:             map { $postlinked{$_} = 1; } (@{$postlink{'default'}});
 6875:         }
 6876:         $datatable .= '<fieldset style="display: inline-block;">'.
 6877:                       '<legend>'.$othertitle.'</legend>';
 6878:         foreach my $field ('email','username') {
 6879:             my $checked;
 6880:             if ($postlinked{$field}) {
 6881:                 $checked = ' checked="checked"';
 6882:             }
 6883:             $datatable .= '<span class="LC_nobreak"><label>'.
 6884:                           '<input type="checkbox" name="passwords_postlink_default" value="'.
 6885:                           $field.'"'.$checked.' />'.$field.'</label>'.
 6886:                           '<span>&nbsp;&nbsp; ';
 6887:         }
 6888:         $datatable .= '</fieldset></td></tr>';
 6889:         $itemcount ++;
 6890:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6891:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'emailsrc'}.'</td>'.
 6892:                       '<td class="LC_left_item">';
 6893:         foreach my $type ('permanent','critical','notify') {
 6894:             my $checkedemail;
 6895:             if ($emailsrc{$type}) {
 6896:                 $checkedemail = ' checked="checked"';
 6897:             }
 6898:             $datatable .= '<span class="LC_nobreak"><label>'.
 6899:                           '<input type="checkbox" name="passwords_emailsrc" value="'.
 6900:                           $type.'"'.$checkedemail.' />'.$titles{$type}.'</label>'.
 6901:                           '<span>&nbsp;&nbsp; ';
 6902:         }
 6903:         $datatable .= '</td></tr>';
 6904:         $itemcount ++;
 6905:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6906:         my $switchserver = &check_switchserver($dom,$confname);
 6907:         my ($showstd,$noshowstd);
 6908:         if ($nostdtext) {
 6909:             $noshowstd = ' checked="checked"';
 6910:         } else {
 6911:             $showstd = ' checked="checked"';
 6912:         }
 6913:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'customtext'}.'</td>'.
 6914:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 6915:                       &mt('Retain standard text:').
 6916:                       '<label><input type="radio" name="passwords_stdtext" value="1"'.$showstd.' />'.
 6917:                       &mt('Yes').'</label>'.'&nbsp;'.
 6918:                       '<label><input type="radio" name="passwords_stdtext" value="0"'.$noshowstd.' />'.
 6919:                       &mt('No').'</label></span><br />'.
 6920:                       '<span class="LC_fontsize_small">'.
 6921:                       &mt('(If you use the same account ...  reset a password from this page.)').'</span><br /><br />'.
 6922:                       &mt('Include custom text:');
 6923:         if ($customurl) {
 6924:             my $link =  &Apache::loncommon::modal_link($customurl,&mt('custom text'),600,500,
 6925:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 6926:             $datatable .= '<span class="LC_nobreak">&nbsp;'.$link.
 6927:                           '<label><input type="checkbox" name="passwords_custom_del"'.
 6928:                           ' value="1" />'.&mt('Delete?').'</label></span>'.
 6929:                           ' <span class="LC_nobreak">&nbsp;'.&mt('Replace:').'</span>';
 6930:         }
 6931:         if ($switchserver) {
 6932:             $datatable .= '<span class="LC_nobreak">&nbsp;'.&mt('Upload to library server: [_1]',$switchserver).'</span>';
 6933:         } else {
 6934:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 6935:                          '<input type="file" name="passwords_customfile" /></span>';
 6936:         }
 6937:         $datatable .= '</td></tr>';
 6938:     } elsif ($position eq 'middle') {
 6939:         my %domconf = &Apache::lonnet::get_dom('configuration',['defaults'],$dom);
 6940:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 6941:         my %defaults;
 6942:         if (ref($domconf{'defaults'}) eq 'HASH') {
 6943:             %defaults = %{$domconf{'defaults'}};
 6944:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 6945:                 $defaults{'intauth_cost'} = 10;
 6946:             }
 6947:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 6948:                 $defaults{'intauth_check'} = 0;
 6949:             }
 6950:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 6951:                 $defaults{'intauth_switch'} = 0;
 6952:             }
 6953:         } else {
 6954:             %defaults = (
 6955:                           'intauth_cost'   => 10,
 6956:                           'intauth_check'  => 0,
 6957:                           'intauth_switch' => 0,
 6958:                         );
 6959:         }
 6960:         foreach my $item (@items) {
 6961:             if ($itemcount%2) {
 6962:                 $css_class = '';
 6963:             } else {
 6964:                 $css_class = ' class="LC_odd_row" ';
 6965:             }
 6966:             $datatable .= '<tr'.$css_class.'>'.
 6967:                           '<td><span class="LC_nobreak">'.$titles{$item}.
 6968:                           '</span></td><td class="LC_left_item" colspan="3">';
 6969:             if ($item eq 'intauth_switch') {
 6970:                 my @options = (0,1,2);
 6971:                 my %optiondesc = &Apache::lonlocal::texthash (
 6972:                                    0 => 'No',
 6973:                                    1 => 'Yes',
 6974:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 6975:                                  );
 6976:                 $datatable .= '<table width="100%">';
 6977:                 foreach my $option (@options) {
 6978:                     my $checked = ' ';
 6979:                     if ($defaults{$item} eq $option) {
 6980:                         $checked = ' checked="checked"';
 6981:                     }
 6982:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6983:                                   '<label><input type="radio" name="'.$item.
 6984:                                   '" value="'.$option.'"'.$checked.' />'.
 6985:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6986:                 }
 6987:                 $datatable .= '</table>';
 6988:             } elsif ($item eq 'intauth_check') {
 6989:                 my @options = (0,1,2);
 6990:                 my %optiondesc = &Apache::lonlocal::texthash (
 6991:                                    0 => 'No',
 6992:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 6993:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 6994:                                  );
 6995:                 $datatable .= '<table width="100%">';
 6996:                 foreach my $option (@options) {
 6997:                     my $checked = ' ';
 6998:                     my $onclick;
 6999:                     if ($defaults{$item} eq $option) {
 7000:                         $checked = ' checked="checked"';
 7001:                     }
 7002:                     if ($option == 2) {
 7003:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 7004:                     }
 7005:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 7006:                                   '<label><input type="radio" name="'.$item.
 7007:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 7008:                                   $optiondesc{$option}.'</label></span></td></tr>';
 7009:                 }
 7010:                 $datatable .= '</table>';
 7011:             } else {
 7012:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 7013:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />';
 7014:             }
 7015:             $datatable .= '</td></tr>';
 7016:             $itemcount ++;
 7017:         }
 7018:     } elsif ($position eq 'lower') {
 7019:         my ($min,$max,%chars,$expire,$numsaved);
 7020:         $min = $Apache::lonnet::passwdmin;
 7021:         if (ref($settings) eq 'HASH') {
 7022:             if ($settings->{min}) {
 7023:                 $min = $settings->{min};
 7024:             }
 7025:             if ($settings->{max}) {
 7026:                 $max = $settings->{max};
 7027:             }
 7028:             if (ref($settings->{chars}) eq 'ARRAY') {
 7029:                 map { $chars{$_} = 1; } (@{$settings->{chars}});
 7030:             }
 7031:             if ($settings->{expire}) {
 7032:                 $expire = $settings->{expire};
 7033:             }
 7034:             if ($settings->{numsaved}) {
 7035:                 $numsaved = $settings->{numsaved};
 7036:             }
 7037:         }
 7038:         my %rulenames = &Apache::lonlocal::texthash(
 7039:                                                      uc => 'At least one upper case letter',
 7040:                                                      lc => 'At least one lower case letter',
 7041:                                                      num => 'At least one number',
 7042:                                                      spec => 'At least one non-alphanumeric',
 7043:                                                    );
 7044:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7045:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'min'}.'</td>'.
 7046:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7047:                       '<input type="text" name="passwords_min" value="'.$min.'" size="3" '.
 7048:                       'onblur="javascript:warnIntPass(this);" />'.
 7049:                       '<span class="LC_fontsize_small"> '.&mt('(Enter an integer: 7 or larger)').'</span>'.
 7050:                       '</span></td></tr>';
 7051:         $itemcount ++;
 7052:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7053:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'max'}.'</td>'.
 7054:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7055:                       '<input type="text" name="passwords_max" value="'.$max.'" size="3" '.
 7056:                       'onblur="javascript:warnIntPass(this);" />'.
 7057:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no maximum)').'</span>'.
 7058:                       '</span></td></tr>';
 7059:         $itemcount ++;
 7060:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7061:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'chars'}.'<br />'.
 7062:                       '<span class="LC_nobreak LC_fontsize_small">'.&mt('(Leave unchecked if not required)').
 7063:                       '</span></td>';
 7064:         my $numinrow = 2;
 7065:         my @possrules = ('uc','lc','num','spec');
 7066:         $datatable .= '<td class="LC_left_item"><table>';
 7067:         for (my $i=0; $i<@possrules; $i++) {
 7068:             my ($rem,$checked);
 7069:             if ($chars{$possrules[$i]}) {
 7070:                 $checked = ' checked="checked"';
 7071:             }
 7072:             $rem = $i%($numinrow);
 7073:             if ($rem == 0) {
 7074:                 if ($i > 0) {
 7075:                     $datatable .= '</tr>';
 7076:                 }
 7077:                 $datatable .= '<tr>';
 7078:             }
 7079:             $datatable .= '<td><span class="LC_nobreak"><label>'.
 7080:                           '<input type="checkbox" name="passwords_chars" value="'.$possrules[$i].'"'.$checked.' />'.
 7081:                           $rulenames{$possrules[$i]}.'</label></span></td>';
 7082:         }
 7083:         my $rem = @possrules%($numinrow);
 7084:         my $colsleft = $numinrow - $rem;
 7085:         if ($colsleft > 1 ) {
 7086:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7087:                           '&nbsp;</td>';
 7088:         } elsif ($colsleft == 1) {
 7089:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 7090:         }
 7091:         $datatable .='</table></td></tr>';
 7092:         $itemcount ++;
 7093:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7094:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'expire'}.'</td>'.
 7095:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7096:                       '<input type="text" name="passwords_expire" value="'.$expire.'" size="4" '.
 7097:                       'onblur="javascript:warnIntPass(this);" />'.
 7098:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank for no expiration)').'</span>'.
 7099:                       '</span></td></tr>';
 7100:         $itemcount ++;
 7101:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7102:         $datatable .= '<tr'.$css_class.'><td>'.$titles{'numsaved'}.'</td>'.
 7103:                       '<td class="LC_left_item"><span class="LC_nobreak">'.
 7104:                       '<input type="text" name="passwords_numsaved" value="'.$numsaved.'" size="3" '.
 7105:                       'onblur="javascript:warnIntPass(this);" />'. 
 7106:                       '<span class="LC_fontsize_small"> '.&mt('(Leave blank to not save previous passwords)').'</span>'.
 7107:                       '</span></td></tr>';
 7108:     } else {
 7109:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 7110:         my %ownerchg = (
 7111:                           by  => {},
 7112:                           for => {},
 7113:                        );
 7114:         my %ownertitles = &Apache::lonlocal::texthash (
 7115:                             by  => 'Course owner status(es) allowed',
 7116:                             for => 'Student status(es) allowed',
 7117:                           );
 7118:         if (ref($settings) eq 'HASH') {
 7119:             if (ref($settings->{crsownerchg}) eq 'HASH') {
 7120:                 if (ref($settings->{crsownerchg}{'by'}) eq 'ARRAY') {
 7121:                     map { $ownerchg{by}{$_} = 1; } (@{$settings->{crsownerchg}{'by'}});
 7122:                 }
 7123:                 if (ref($settings->{crsownerchg}{'for'}) eq 'ARRAY') {
 7124:                     map { $ownerchg{for}{$_} = 1; } (@{$settings->{crsownerchg}{'for'}});
 7125:                 }
 7126:             }
 7127:         }
 7128:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7129:         $datatable .= '<tr '.$css_class.'>'.
 7130:                       '<td>'.
 7131:                       &mt('Requirements').'<ul>'.
 7132:                       '<li>'.&mt("Course 'type' is not a Community or Placement Test").'</li>'.
 7133:                       '<li>'.&mt('User is Course Coordinator and also course owner').'</li>'.
 7134:                       '<li>'.&mt("Student's only active roles are student role(s) in course(s) owned by this user").'</li>'.
 7135:                       '<li>'.&mt('User, course, and student share same domain').'</li>'.
 7136:                       '</ul>'.
 7137:                       '</td>'.
 7138:                       '<td class="LC_left_item">';
 7139:         foreach my $item ('by','for') {
 7140:             $datatable .= '<fieldset style="display: inline-block;">'.
 7141:                           '<legend>'.$ownertitles{$item}.'</legend>';
 7142:             if ((ref($types) eq 'ARRAY') && (ref($usertypes) eq 'HASH')) {
 7143:                 foreach my $type (@{$types}) {
 7144:                     my $checked;
 7145:                     if ($ownerchg{$item}{$type}) {
 7146:                         $checked = ' checked="checked"';
 7147:                     }
 7148:                     $datatable .= '<span class="LC_nobreak"><label>'.
 7149:                                   '<input type="checkbox" name="passwords_crsowner_'.$item.'" value="'.
 7150:                                   $type.'"'.$checked.' />'.$usertypes->{$type}.'</label>'.
 7151:                                   '</span>&nbsp;&nbsp; ';
 7152:                 }
 7153:             }
 7154:             my $checked;
 7155:             if ($ownerchg{$item}{'default'}) {
 7156:                 $checked = ' checked="checked"';
 7157:             }
 7158:             $datatable .= '<span class="LC_nobreak"><label><input type="checkbox" '.
 7159:                           'name="passwords_crsowner_'.$item.'" value="default"'.$checked.' />'.
 7160:                           $othertitle.'</label></span></fieldset>';
 7161:         }
 7162:         $datatable .= '</td></tr>';
 7163:     }
 7164:     return $datatable;
 7165: }
 7166: 
 7167: sub print_usersessions {
 7168:     my ($position,$dom,$settings,$rowtotal) = @_;
 7169:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 7170:     my (%by_ip,%by_location,@intdoms,@instdoms);
 7171:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 7172: 
 7173:     my @alldoms = &Apache::lonnet::all_domains();
 7174:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 7175:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7176:     my %altids = &id_for_thisdom(%servers);
 7177:     if ($position eq 'top') {
 7178:         if (keys(%serverhomes) > 1) {
 7179:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 7180:             my ($curroffloadnow,$curroffloadoth);
 7181:             if (ref($settings) eq 'HASH') {
 7182:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 7183:                     $curroffloadnow = $settings->{'offloadnow'};
 7184:                 }
 7185:                 if (ref($settings->{'offloadoth'}) eq 'HASH') {
 7186:                     $curroffloadoth = $settings->{'offloadoth'};
 7187:                 }
 7188:             }
 7189:             my $other_insts = scalar(keys(%by_location));
 7190:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,
 7191:                                       $other_insts,$curroffloadnow,$curroffloadoth,$rowtotal);
 7192:         } else {
 7193:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 7194:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 7195:                           '</td></tr>';
 7196:         }
 7197:     } else {
 7198:         my %titles = &usersession_titles();
 7199:         my ($prefix,@types);
 7200:         if ($position eq 'bottom') {
 7201:             $prefix = 'remote';
 7202:             @types = ('version','excludedomain','includedomain');
 7203:         } else {
 7204:             $prefix = 'hosted';
 7205:             @types = ('excludedomain','includedomain');
 7206:         }
 7207:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 7208:     }
 7209:     $$rowtotal += $itemcount;
 7210:     return $datatable;
 7211: }
 7212: 
 7213: sub rules_by_location {
 7214:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 7215:     my ($datatable,$itemcount,$css_class);
 7216:     if (keys(%{$by_location}) == 0) {
 7217:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7218:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 7219:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 7220:                      '</td></tr>';
 7221:         $itemcount = 1;
 7222:     } else {
 7223:         $itemcount = 0;
 7224:         my $numinrow = 5;
 7225:         my (%current,%checkedon,%checkedoff);
 7226:         my @locations = sort(keys(%{$by_location}));
 7227:         foreach my $type (@{$types}) {
 7228:             $checkedon{$type} = '';
 7229:             $checkedoff{$type} = ' checked="checked"';
 7230:         }
 7231:         if (ref($settings) eq 'HASH') {
 7232:             if (ref($settings->{$prefix}) eq 'HASH') {
 7233:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 7234:                     $current{$key} = $settings->{$prefix}{$key};
 7235:                     if ($key eq 'version') {
 7236:                         if ($current{$key} ne '') {
 7237:                             $checkedon{$key} = ' checked="checked"';
 7238:                             $checkedoff{$key} = '';
 7239:                         }
 7240:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 7241:                         $checkedon{$key} = ' checked="checked"';
 7242:                         $checkedoff{$key} = '';
 7243:                     }
 7244:                 }
 7245:             }
 7246:         }
 7247:         foreach my $type (@{$types}) {
 7248:             next if ($type ne 'version' && !@locations);
 7249:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7250:             $datatable .= '<tr'.$css_class.'>
 7251:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 7252:                            <span class="LC_nobreak">&nbsp;
 7253:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 7254:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 7255:             if ($type eq 'version') {
 7256:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 7257:                 my $selector = '<select name="'.$prefix.'_version">';
 7258:                 foreach my $version (@lcversions) {
 7259:                     my $selected = '';
 7260:                     if ($current{'version'} eq $version) {
 7261:                         $selected = ' selected="selected"';
 7262:                     }
 7263:                     $selector .= ' <option value="'.$version.'"'.
 7264:                                  $selected.'>'.$version.'</option>';
 7265:                 }
 7266:                 $selector .= '</select> ';
 7267:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 7268:             } else {
 7269:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 7270:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 7271:                              ' />'.('&nbsp;'x2).
 7272:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 7273:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 7274:                              "\n".
 7275:                              '</div><div><table>';
 7276:                 my $rem;
 7277:                 for (my $i=0; $i<@locations; $i++) {
 7278:                     my ($showloc,$value,$checkedtype);
 7279:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 7280:                         my $ip = $by_location->{$locations[$i]}->[0];
 7281:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 7282:                             $value = join(':',@{$by_ip->{$ip}});
 7283:                             $showloc = join(', ',@{$by_ip->{$ip}});
 7284:                             if (ref($current{$type}) eq 'ARRAY') {
 7285:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 7286:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 7287:                                         $checkedtype = ' checked="checked"';
 7288:                                         last;
 7289:                                     }
 7290:                                 }
 7291:                             }
 7292:                         }
 7293:                     }
 7294:                     $rem = $i%($numinrow);
 7295:                     if ($rem == 0) {
 7296:                         if ($i > 0) {
 7297:                             $datatable .= '</tr>';
 7298:                         }
 7299:                         $datatable .= '<tr>';
 7300:                     }
 7301:                     $datatable .= '<td class="LC_left_item">'.
 7302:                                   '<span class="LC_nobreak"><label>'.
 7303:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 7304:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 7305:                                   '</label></span></td>';
 7306:                 }
 7307:                 $rem = @locations%($numinrow);
 7308:                 my $colsleft = $numinrow - $rem;
 7309:                 if ($colsleft > 1 ) {
 7310:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7311:                                   '&nbsp;</td>';
 7312:                 } elsif ($colsleft == 1) {
 7313:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 7314:                 }
 7315:                 $datatable .= '</tr></table>';
 7316:             }
 7317:             $datatable .= '</td></tr>';
 7318:             $itemcount ++;
 7319:         }
 7320:     }
 7321:     return ($datatable,$itemcount);
 7322: }
 7323: 
 7324: sub print_ssl {
 7325:     my ($position,$dom,$settings,$rowtotal) = @_;
 7326:     my ($css_class,$datatable);
 7327:     my $itemcount = 1;
 7328:     if ($position eq 'top') {
 7329:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 7330:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 7331:         my $same_institution;
 7332:         if ($intdom ne '') {
 7333:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 7334:             if (ref($internet_names) eq 'ARRAY') {
 7335:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 7336:                     $same_institution = 1;
 7337:                 }
 7338:             }
 7339:         }
 7340:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7341:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 7342:         if ($same_institution) {
 7343:             my %domservers = &Apache::lonnet::get_servers($dom);
 7344:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 7345:         } else {
 7346:             $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.");
 7347:         }
 7348:         $datatable .= '</td></tr>';
 7349:         $itemcount ++;
 7350:     } else {
 7351:         my %titles = &ssl_titles();
 7352:         my (%by_ip,%by_location,@intdoms,@instdoms);
 7353:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 7354:         my @alldoms = &Apache::lonnet::all_domains();
 7355:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 7356:         my @domservers = &Apache::lonnet::get_servers($dom);
 7357:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7358:         my %altids = &id_for_thisdom(%servers);
 7359:         if (($position eq 'connto') || ($position eq 'connfrom')) {
 7360:             my $legacy;
 7361:             unless (ref($settings) eq 'HASH') {
 7362:                 my $name;
 7363:                 if ($position eq 'connto') {
 7364:                     $name = 'loncAllowInsecure';
 7365:                 } else {
 7366:                     $name = 'londAllowInsecure';
 7367:                 }
 7368:                 my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
 7369:                 my @ids=&Apache::lonnet::current_machine_ids();
 7370:                 if (($primarylibserv ne '') && (!grep(/^\Q$primarylibserv\E$/,@ids))) {
 7371:                     my %what = (
 7372:                                    $name => 1,
 7373:                                );
 7374:                     my ($result,$returnhash) =
 7375:                         &Apache::lonnet::get_remote_globals($primarylibserv,\%what);
 7376:                     if ($result eq 'ok') {
 7377:                         if (ref($returnhash) eq 'HASH') {
 7378:                             $legacy = $returnhash->{$name};
 7379:                         }
 7380:                     }
 7381:                 } else {
 7382:                     $legacy = $Apache::lonnet::perlvar{$name};
 7383:                 }
 7384:             }
 7385:             foreach my $type ('dom','intdom','other') {
 7386:                 my %checked;
 7387:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7388:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 7389:                               '<td class="LC_right_item">';
 7390:                 my $skip; 
 7391:                 if ($type eq 'dom') {
 7392:                     unless (keys(%servers) > 1) {
 7393:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 7394:                         $skip = 1;
 7395:                     }
 7396:                 }
 7397:                 if ($type eq 'intdom') {
 7398:                     unless (@instdoms > 1) {
 7399:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 7400:                         $skip = 1;
 7401:                     } 
 7402:                 } elsif ($type eq 'other') {
 7403:                     if (keys(%by_location) == 0) {
 7404:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 7405:                         $skip = 1;
 7406:                     }
 7407:                 }
 7408:                 unless ($skip) {
 7409:                     $checked{'yes'} = ' checked="checked"'; 
 7410:                     if (ref($settings) eq 'HASH') {
 7411:                         if (ref($settings->{$position}) eq 'HASH') {
 7412:                             if ($settings->{$position}->{$type} =~ /^(no|req)$/) {
 7413:                                 $checked{$1} = $checked{'yes'};
 7414:                                 delete($checked{'yes'}); 
 7415:                             }
 7416:                         }
 7417:                     } else {
 7418:                         if ($legacy == 0) {
 7419:                             $checked{'req'} = $checked{'yes'};
 7420:                             delete($checked{'yes'});    
 7421:                         }
 7422:                     }
 7423:                     foreach my $option ('no','yes','req') {
 7424:                         $datatable .= '<span class="LC_nobreak"><label>'.
 7425:                                       '<input type="radio" name="'.$position.'_'.$type.'" '.
 7426:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 7427:                                       '</label></span>'.('&nbsp;'x2);
 7428:                     }
 7429:                 }
 7430:                 $datatable .= '</td></tr>';
 7431:                 $itemcount ++; 
 7432:             }
 7433:         } else {
 7434:             my $prefix = 'replication';
 7435:             my @types = ('certreq','nocertreq');
 7436:             if (keys(%by_location) == 0) {
 7437:                 $datatable .= '<tr'.$css_class.'><td>'.
 7438:                               &mt('Nothing to set here, as there are no other institutions').
 7439:                               '</td></tr>';
 7440:                 $itemcount ++;
 7441:             } else {
 7442:                 ($datatable,$itemcount) = 
 7443:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 7444:             }
 7445:         }
 7446:     }
 7447:     $$rowtotal += $itemcount;
 7448:     return $datatable;
 7449: }
 7450: 
 7451: sub ssl_titles {
 7452:     return &Apache::lonlocal::texthash (
 7453:                dom           => 'LON-CAPA servers/VMs from same domain',
 7454:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 7455:                other         => 'External LON-CAPA servers/VMs',
 7456:                connto        => 'Connections to other servers',
 7457:                connfrom      => 'Connections from other servers',
 7458:                replication   => 'Replicating content to other institutions',
 7459:                certreq       => 'Client certificate required, but specific domains exempt',
 7460:                nocertreq     => 'No client certificate required, except for specific domains',
 7461:                no            => 'SSL not used',
 7462:                yes           => 'SSL Optional (used if available)',
 7463:                req           => 'SSL Required',
 7464:     );
 7465: }
 7466: 
 7467: sub print_trust {
 7468:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 7469:     my ($css_class,$datatable,%checked,%choices);
 7470:     my (%by_ip,%by_location,@intdoms,@instdoms);
 7471:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 7472:     my $itemcount = 1;
 7473:     my %titles = &trust_titles();
 7474:     my @types = ('exc','inc');
 7475:     if ($prefix eq 'top') {
 7476:         $prefix = 'content';
 7477:     } elsif ($prefix eq 'bottom') {
 7478:         $prefix = 'msg';
 7479:     }
 7480:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 7481:     $$rowtotal += $itemcount;
 7482:     return $datatable;
 7483: }
 7484: 
 7485: sub trust_titles {
 7486:     return &Apache::lonlocal::texthash(
 7487:                content  => "Access to this domain's content by others",
 7488:                shared   => "Access to other domain's content by this domain",
 7489:                enroll   => "Enrollment in this domain's courses by others", 
 7490:                othcoau  => "Co-author roles in this domain for others",
 7491:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 7492:                domroles => "Domain roles in this domain assignable to others",
 7493:                catalog  => "Course Catalog for this domain displayed elsewhere",
 7494:                reqcrs   => "Requests for creation of courses in this domain by others",
 7495:                msg      => "Users in other domains can send messages to this domain",
 7496:                exc      => "Allow all, but exclude specific domains",
 7497:                inc      => "Deny all, but include specific domains",
 7498:            );
 7499: } 
 7500: 
 7501: sub build_location_hashes {
 7502:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 7503:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 7504:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 7505:     my %iphost = &Apache::lonnet::get_iphost();
 7506:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 7507:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 7508:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 7509:         foreach my $id (@{$iphost{$primary_ip}}) {
 7510:             my $intdom = &Apache::lonnet::internet_dom($id);
 7511:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 7512:                 push(@{$intdoms},$intdom);
 7513:             }
 7514:         }
 7515:     }
 7516:     foreach my $ip (keys(%iphost)) {
 7517:         if (ref($iphost{$ip}) eq 'ARRAY') {
 7518:             foreach my $id (@{$iphost{$ip}}) {
 7519:                 my $location = &Apache::lonnet::internet_dom($id);
 7520:                 if ($location) {
 7521:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 7522:                         my $dom = &Apache::lonnet::host_domain($id);
 7523:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 7524:                             push(@{$instdoms},$dom);
 7525:                         }
 7526:                         next;
 7527:                     }
 7528:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 7529:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 7530:                             push(@{$by_ip->{$ip}},$location);
 7531:                         }
 7532:                     } else {
 7533:                         $by_ip->{$ip} = [$location];
 7534:                     }
 7535:                 }
 7536:             }
 7537:         }
 7538:     }
 7539:     foreach my $ip (sort(keys(%{$by_ip}))) {
 7540:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 7541:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 7542:             my $first = $by_ip->{$ip}->[0];
 7543:             if (ref($by_location->{$first}) eq 'ARRAY') {
 7544:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 7545:                     push(@{$by_location->{$first}},$ip);
 7546:                 }
 7547:             } else {
 7548:                 $by_location->{$first} = [$ip];
 7549:             }
 7550:         }
 7551:     }
 7552:     return;
 7553: }
 7554: 
 7555: sub current_offloads_to {
 7556:     my ($dom,$settings,$servers) = @_;
 7557:     my (%spareid,%otherdomconfigs);
 7558:     if (ref($servers) eq 'HASH') {
 7559:         foreach my $lonhost (sort(keys(%{$servers}))) {
 7560:             my $gotspares;
 7561:             if (ref($settings) eq 'HASH') {
 7562:                 if (ref($settings->{'spares'}) eq 'HASH') {
 7563:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 7564:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 7565:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 7566:                         $gotspares = 1;
 7567:                     }
 7568:                 }
 7569:             }
 7570:             unless ($gotspares) {
 7571:                 my $gotspares;
 7572:                 my $serverhomeID =
 7573:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 7574:                 my $serverhomedom =
 7575:                     &Apache::lonnet::host_domain($serverhomeID);
 7576:                 if ($serverhomedom ne $dom) {
 7577:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 7578:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 7579:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 7580:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 7581:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 7582:                                 $gotspares = 1;
 7583:                             }
 7584:                         }
 7585:                     } else {
 7586:                         $otherdomconfigs{$serverhomedom} =
 7587:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 7588:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 7589:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 7590:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 7591:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 7592:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 7593:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 7594:                                         $gotspares = 1;
 7595:                                     }
 7596:                                 }
 7597:                             }
 7598:                         }
 7599:                     }
 7600:                 }
 7601:             }
 7602:             unless ($gotspares) {
 7603:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 7604:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 7605:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 7606:                } else {
 7607:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 7608:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 7609:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 7610:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 7611:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 7612:                     } else {
 7613:                         my %what = (
 7614:                              spareid => 1,
 7615:                         );
 7616:                         my ($result,$returnhash) = 
 7617:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 7618:                         if ($result eq 'ok') { 
 7619:                             if (ref($returnhash) eq 'HASH') {
 7620:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 7621:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 7622:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 7623:                                 }
 7624:                             }
 7625:                         }
 7626:                     }
 7627:                 }
 7628:             }
 7629:         }
 7630:     }
 7631:     return %spareid;
 7632: }
 7633: 
 7634: sub spares_row {
 7635:     my ($dom,$servers,$spareid,$serverhomes,$altids,$other_insts,
 7636:         $curroffloadnow,$curroffloadoth,$rowtotal) = @_;
 7637:     my $css_class;
 7638:     my $numinrow = 4;
 7639:     my $itemcount = 1;
 7640:     my $datatable;
 7641:     my %typetitles = &sparestype_titles();
 7642:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 7643:         foreach my $server (sort(keys(%{$servers}))) {
 7644:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 7645:             my ($othercontrol,$serverdom);
 7646:             if ($serverhome ne $server) {
 7647:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 7648:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 7649:             } else {
 7650:                 $serverdom = &Apache::lonnet::host_domain($server);
 7651:                 if ($serverdom ne $dom) {
 7652:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 7653:                 }
 7654:             }
 7655:             next unless (ref($spareid->{$server}) eq 'HASH');
 7656:             my ($checkednow,$checkedoth);
 7657:             if (ref($curroffloadnow) eq 'HASH') {
 7658:                 if ($curroffloadnow->{$server}) {
 7659:                     $checkednow = ' checked="checked"';
 7660:                 }
 7661:             }
 7662:             if (ref($curroffloadoth) eq 'HASH') {
 7663:                 if ($curroffloadoth->{$server}) {
 7664:                     $checkedoth = ' checked="checked"';
 7665:                 }
 7666:             }
 7667:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 7668:             $datatable .= '<tr'.$css_class.'>
 7669:                            <td rowspan="2">
 7670:                             <span class="LC_nobreak">'.
 7671:                           &mt('[_1] when busy, offloads to:'
 7672:                               ,'<b>'.$server.'</b>').'</span><br />'.
 7673:                           '<span class="LC_nobreak">'."\n".
 7674:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 7675:                           '&nbsp;'.&mt('Switch any active user on next access').'</label></span>'.
 7676:                           "\n";
 7677:             if ($other_insts) {
 7678:                 $datatable .= '<br />'.
 7679:                               '<span class="LC_nobreak">'."\n".
 7680:                           '<label><input type="checkbox" name="offloadoth" value="'.$server.'"'.$checkedoth.' />'.
 7681:                           '&nbsp;'.&mt('Switch other institutions on next access').'</label></span>'.
 7682:                           "\n";
 7683:             }
 7684:             my (%current,%canselect);
 7685:             my @choices = 
 7686:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 7687:             foreach my $type ('primary','default') {
 7688:                 if (ref($spareid->{$server}) eq 'HASH') {
 7689:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 7690:                         my @spares = @{$spareid->{$server}{$type}};
 7691:                         if (@spares > 0) {
 7692:                             if ($othercontrol) {
 7693:                                 $current{$type} = join(', ',@spares);
 7694:                             } else {
 7695:                                 $current{$type} .= '<table>';
 7696:                                 my $numspares = scalar(@spares);
 7697:                                 for (my $i=0;  $i<@spares; $i++) {
 7698:                                     my $rem = $i%($numinrow);
 7699:                                     if ($rem == 0) {
 7700:                                         if ($i > 0) {
 7701:                                             $current{$type} .= '</tr>';
 7702:                                         }
 7703:                                         $current{$type} .= '<tr>';
 7704:                                     }
 7705:                                     $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;'.
 7706:                                                        $spareid->{$server}{$type}[$i].
 7707:                                                        '</label></td>'."\n";
 7708:                                 }
 7709:                                 my $rem = @spares%($numinrow);
 7710:                                 my $colsleft = $numinrow - $rem;
 7711:                                 if ($colsleft > 1 ) {
 7712:                                     $current{$type} .= '<td colspan="'.$colsleft.
 7713:                                                        '" class="LC_left_item">'.
 7714:                                                        '&nbsp;</td>';
 7715:                                 } elsif ($colsleft == 1) {
 7716:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 7717:                                 }
 7718:                                 $current{$type} .= '</tr></table>';
 7719:                             }
 7720:                         }
 7721:                     }
 7722:                     if ($current{$type} eq '') {
 7723:                         $current{$type} = &mt('None specified');
 7724:                     }
 7725:                     if ($othercontrol) {
 7726:                         if ($type eq 'primary') {
 7727:                             $canselect{$type} = $othercontrol;
 7728:                         }
 7729:                     } else {
 7730:                         $canselect{$type} = 
 7731:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 7732:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 7733:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 7734:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 7735:                         if (@choices > 0) {
 7736:                             foreach my $lonhost (@choices) {
 7737:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 7738:                             }
 7739:                         }
 7740:                         $canselect{$type} .= '</select>'."\n";
 7741:                     }
 7742:                 } else {
 7743:                     $current{$type} = &mt('Could not be determined');
 7744:                     if ($type eq 'primary') {
 7745:                         $canselect{$type} =  $othercontrol;
 7746:                     }
 7747:                 }
 7748:                 if ($type eq 'default') {
 7749:                     $datatable .= '<tr'.$css_class.'>';
 7750:                 }
 7751:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 7752:                               '<td>'.$current{$type}.'</td>'."\n".
 7753:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 7754:             }
 7755:             $itemcount ++;
 7756:         }
 7757:     }
 7758:     $$rowtotal += $itemcount;
 7759:     return $datatable;
 7760: }
 7761: 
 7762: sub possible_newspares {
 7763:     my ($server,$currspares,$serverhomes,$altids) = @_;
 7764:     my $serverhostname = &Apache::lonnet::hostname($server);
 7765:     my %excluded;
 7766:     if ($serverhostname ne '') {
 7767:         %excluded = (
 7768:                        $serverhostname => 1,
 7769:                     );
 7770:     }
 7771:     if (ref($currspares) eq 'HASH') {
 7772:         foreach my $type (keys(%{$currspares})) {
 7773:             if (ref($currspares->{$type}) eq 'ARRAY') {
 7774:                 if (@{$currspares->{$type}} > 0) {
 7775:                     foreach my $curr (@{$currspares->{$type}}) {
 7776:                         my $hostname = &Apache::lonnet::hostname($curr);
 7777:                         $excluded{$hostname} = 1;
 7778:                     }
 7779:                 }
 7780:             }
 7781:         }
 7782:     }
 7783:     my @choices;
 7784:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 7785:         if (keys(%{$serverhomes}) > 1) {
 7786:             foreach my $name (sort(keys(%{$serverhomes}))) {
 7787:                 unless ($excluded{$name}) {
 7788:                     if (exists($altids->{$serverhomes->{$name}})) {
 7789:                         push(@choices,$altids->{$serverhomes->{$name}});
 7790:                     } else {
 7791:                         push(@choices,$serverhomes->{$name});
 7792:                     }
 7793:                 }
 7794:             }
 7795:         }
 7796:     }
 7797:     return sort(@choices);
 7798: }
 7799: 
 7800: sub print_loadbalancing {
 7801:     my ($dom,$settings,$rowtotal) = @_;
 7802:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 7803:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 7804:     my $numinrow = 1;
 7805:     my $datatable;
 7806:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7807:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
 7808:     if (ref($settings) eq 'HASH') {
 7809:         %existing = %{$settings};
 7810:     }
 7811:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 7812:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 7813:                                   \%currtargets,\%currrules,\%currcookies);
 7814:     } else {
 7815:         return;
 7816:     }
 7817:     my ($othertitle,$usertypes,$types) =
 7818:         &Apache::loncommon::sorted_inst_types($dom);
 7819:     my $rownum = 8;
 7820:     if (ref($types) eq 'ARRAY') {
 7821:         $rownum += scalar(@{$types});
 7822:     }
 7823:     my @css_class = ('LC_odd_row','LC_even_row');
 7824:     my $balnum = 0;
 7825:     my $islast;
 7826:     my (@toshow,$disabledtext);
 7827:     if (keys(%currbalancer) > 0) {
 7828:         @toshow = sort(keys(%currbalancer));
 7829:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 7830:             push(@toshow,'');
 7831:         }
 7832:     } else {
 7833:         @toshow = ('');
 7834:         $disabledtext = &mt('No existing load balancer');
 7835:     }
 7836:     foreach my $lonhost (@toshow) {
 7837:         if ($balnum == scalar(@toshow)-1) {
 7838:             $islast = 1;
 7839:         } else {
 7840:             $islast = 0;
 7841:         }
 7842:         my $cssidx = $balnum%2;
 7843:         my $targets_div_style = 'display: none';
 7844:         my $disabled_div_style = 'display: block';
 7845:         my $homedom_div_style = 'display: none';
 7846:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 7847:                       '<td rowspan="'.$rownum.'" style="vertical-align: top">'.
 7848:                       '<p>';
 7849:         if ($lonhost eq '') {
 7850:             $datatable .= '<span class="LC_nobreak">';
 7851:             if (keys(%currbalancer) > 0) {
 7852:                 $datatable .= &mt('Add balancer:');
 7853:             } else {
 7854:                 $datatable .= &mt('Enable balancer:');
 7855:             }
 7856:             $datatable .= '&nbsp;'.
 7857:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 7858:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 7859:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 7860:                           '<option value="" selected="selected">'.&mt('None').
 7861:                           '</option>'."\n";
 7862:             foreach my $server (sort(keys(%servers))) {
 7863:                 next if ($currbalancer{$server});
 7864:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 7865:             }
 7866:             $datatable .=
 7867:                 '</select>'."\n".
 7868:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 7869:         } else {
 7870:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 7871:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 7872:                            &mt('Stop balancing').'</label>'.
 7873:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 7874:             $targets_div_style = 'display: block';
 7875:             $disabled_div_style = 'display: none';
 7876:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 7877:                 $homedom_div_style = 'display: block';
 7878:             }
 7879:         }
 7880:         $datatable .= '</p></td><td rowspan="'.$rownum.'" style="vertical-align: top">'.
 7881:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 7882:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 7883:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 7884:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 7885:         my @sparestypes = ('primary','default');
 7886:         my %typetitles = &sparestype_titles();
 7887:         my %hostherechecked = (
 7888:                                   no => ' checked="checked"',
 7889:                               );
 7890:         my %balcookiechecked = (
 7891:                                   no => ' checked="checked"', 
 7892:                                );
 7893:         foreach my $sparetype (@sparestypes) {
 7894:             my $targettable;
 7895:             for (my $i=0; $i<$numspares; $i++) {
 7896:                 my $checked;
 7897:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 7898:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 7899:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 7900:                             $checked = ' checked="checked"';
 7901:                         }
 7902:                     }
 7903:                 }
 7904:                 my ($chkboxval,$disabled);
 7905:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 7906:                     $chkboxval = $spares[$i];
 7907:                 }
 7908:                 if (exists($currbalancer{$spares[$i]})) {
 7909:                     $disabled = ' disabled="disabled"';
 7910:                 }
 7911:                 $targettable .=
 7912:                     '<td><span class="LC_nobreak"><label>'.
 7913:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 7914:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 7915:                     '</span></label></span></td>';
 7916:                 my $rem = $i%($numinrow);
 7917:                 if ($rem == 0) {
 7918:                     if (($i > 0) && ($i < $numspares-1)) {
 7919:                         $targettable .= '</tr>';
 7920:                     }
 7921:                     if ($i < $numspares-1) {
 7922:                         $targettable .= '<tr>';
 7923:                     }
 7924:                 }
 7925:             }
 7926:             if ($targettable ne '') {
 7927:                 my $rem = $numspares%($numinrow);
 7928:                 my $colsleft = $numinrow - $rem;
 7929:                 if ($colsleft > 1 ) {
 7930:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7931:                                     '&nbsp;</td>';
 7932:                 } elsif ($colsleft == 1) {
 7933:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 7934:                 }
 7935:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 7936:                                '<table><tr>'.$targettable.'</tr></table><br />';
 7937:             }
 7938:             $hostherechecked{$sparetype} = '';
 7939:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 7940:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 7941:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 7942:                         $hostherechecked{$sparetype} = ' checked="checked"';
 7943:                         $hostherechecked{'no'} = '';
 7944:                     }
 7945:                 }
 7946:             }
 7947:         }
 7948:         if ($currcookies{$lonhost}) {
 7949:             %balcookiechecked = (
 7950:                                     yes => ' checked="checked"',
 7951:                                 );
 7952:         }
 7953:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 7954:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 7955:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 7956:         foreach my $sparetype (@sparestypes) {
 7957:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 7958:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 7959:                           '</i></label><br />';
 7960:         }
 7961:         $datatable .= &mt('Use balancer cookie').'<br />'.
 7962:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="1"'.
 7963:                       $balcookiechecked{'yes'}.' />'.&mt('Yes').'</label><br />'.
 7964:                       '<label><input type="radio" name="loadbalancing_cookie_'.$balnum.'" value="0"'.
 7965:                       $balcookiechecked{'no'}.' />'.&mt('No').'</label><br />'.
 7966:                       '</div></td></tr>'.
 7967:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 7968:                                            $othertitle,$usertypes,$types,\%servers,
 7969:                                            \%currbalancer,$lonhost,
 7970:                                            $targets_div_style,$homedom_div_style,
 7971:                                            $css_class[$cssidx],$balnum,$islast);
 7972:         $$rowtotal += $rownum;
 7973:         $balnum ++;
 7974:     }
 7975:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 7976:     return $datatable;
 7977: }
 7978: 
 7979: sub get_loadbalancers_config {
 7980:     my ($servers,$existing,$currbalancer,$currtargets,$currrules,$currcookies) = @_;
 7981:     return unless ((ref($servers) eq 'HASH') &&
 7982:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 7983:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH') &&
 7984:                    (ref($currcookies) eq 'HASH'));
 7985:     if (keys(%{$existing}) > 0) {
 7986:         my $oldlonhost;
 7987:         foreach my $key (sort(keys(%{$existing}))) {
 7988:             if ($key eq 'lonhost') {
 7989:                 $oldlonhost = $existing->{'lonhost'};
 7990:                 $currbalancer->{$oldlonhost} = 1;
 7991:             } elsif ($key eq 'targets') {
 7992:                 if ($oldlonhost) {
 7993:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 7994:                 }
 7995:             } elsif ($key eq 'rules') {
 7996:                 if ($oldlonhost) {
 7997:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 7998:                 }
 7999:             } elsif (ref($existing->{$key}) eq 'HASH') {
 8000:                 $currbalancer->{$key} = 1;
 8001:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 8002:                 $currrules->{$key} = $existing->{$key}{'rules'};
 8003:                 if ($existing->{$key}{'cookie'}) {
 8004:                     $currcookies->{$key} = 1;
 8005:                 }
 8006:             }
 8007:         }
 8008:     } else {
 8009:         my ($balancerref,$targetsref) =
 8010:                 &Apache::lonnet::get_lonbalancer_config($servers);
 8011:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 8012:             foreach my $server (sort(keys(%{$balancerref}))) {
 8013:                 $currbalancer->{$server} = 1;
 8014:                 $currtargets->{$server} = $targetsref->{$server};
 8015:             }
 8016:         }
 8017:     }
 8018:     return;
 8019: }
 8020: 
 8021: sub loadbalancing_rules {
 8022:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 8023:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 8024:         $css_class,$balnum,$islast) = @_;
 8025:     my $output;
 8026:     my $num = 0;
 8027:     my ($alltypes,$othertypes,$titles) =
 8028:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 8029:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 8030:         foreach my $type (@{$alltypes}) {
 8031:             $num ++;
 8032:             my $current;
 8033:             if (ref($currrules) eq 'HASH') {
 8034:                 $current = $currrules->{$type};
 8035:             }
 8036:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 8037:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 8038:                     $current = '';
 8039:                 }
 8040:             }
 8041:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 8042:                                              $servers,$currbalancer,$lonhost,$dom,
 8043:                                              $targets_div_style,$homedom_div_style,
 8044:                                              $css_class,$balnum,$num,$islast);
 8045:         }
 8046:     }
 8047:     return $output;
 8048: }
 8049: 
 8050: sub loadbalancing_titles {
 8051:     my ($dom,$intdom,$usertypes,$types) = @_;
 8052:     my %othertypes = (
 8053:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 8054:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 8055:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 8056:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 8057:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 8058:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 8059:                      );
 8060:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 8061:     my @available;
 8062:     if (ref($types) eq 'ARRAY') {
 8063:         @available = @{$types};
 8064:     }
 8065:     unless (grep(/^default$/,@available)) {
 8066:         push(@available,'default');
 8067:     }
 8068:     unshift(@alltypes,@available);
 8069:     my %titles;
 8070:     foreach my $type (@alltypes) {
 8071:         if ($type =~ /^_LC_/) {
 8072:             $titles{$type} = $othertypes{$type};
 8073:         } elsif ($type eq 'default') {
 8074:             $titles{$type} = &mt('All users from [_1]',$dom);
 8075:             if (ref($types) eq 'ARRAY') {
 8076:                 if (@{$types} > 0) {
 8077:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 8078:                 }
 8079:             }
 8080:         } elsif (ref($usertypes) eq 'HASH') {
 8081:             $titles{$type} = $usertypes->{$type};
 8082:         }
 8083:     }
 8084:     return (\@alltypes,\%othertypes,\%titles);
 8085: }
 8086: 
 8087: sub loadbalance_rule_row {
 8088:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 8089:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 8090:     my @rulenames;
 8091:     my %ruletitles = &offloadtype_text();
 8092:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 8093:         @rulenames = ('balancer','offloadedto','specific');
 8094:     } else {
 8095:         @rulenames = ('default','homeserver');
 8096:         if ($type eq '_LC_external') {
 8097:             push(@rulenames,'externalbalancer');
 8098:         } else {
 8099:             push(@rulenames,'specific');
 8100:         }
 8101:         push(@rulenames,'none');
 8102:     }
 8103:     my $style = $targets_div_style;
 8104:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 8105:         $style = $homedom_div_style;
 8106:     }
 8107:     my $space;
 8108:     if ($islast && $num == 1) {
 8109:         $space = '<div style="display:inline-block;">&nbsp;</div>';
 8110:     }
 8111:     my $output =
 8112:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td style="vertical-align: top">'.$space.
 8113:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 8114:         '<td valaign="top">'.$space.
 8115:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 8116:     for (my $i=0; $i<@rulenames; $i++) {
 8117:         my $rule = $rulenames[$i];
 8118:         my ($checked,$extra);
 8119:         if ($rulenames[$i] eq 'default') {
 8120:             $rule = '';
 8121:         }
 8122:         if ($rulenames[$i] eq 'specific') {
 8123:             if (ref($servers) eq 'HASH') {
 8124:                 my $default;
 8125:                 if (($current ne '') && (exists($servers->{$current}))) {
 8126:                     $checked = ' checked="checked"';
 8127:                 }
 8128:                 unless ($checked) {
 8129:                     $default = ' selected="selected"';
 8130:                 }
 8131:                 $extra =
 8132:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 8133:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 8134:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 8135:                     '<option value=""'.$default.'></option>'."\n";
 8136:                 foreach my $server (sort(keys(%{$servers}))) {
 8137:                     if (ref($currbalancer) eq 'HASH') {
 8138:                         next if (exists($currbalancer->{$server}));
 8139:                     }
 8140:                     my $selected;
 8141:                     if ($server eq $current) {
 8142:                         $selected = ' selected="selected"';
 8143:                     }
 8144:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 8145:                 }
 8146:                 $extra .= '</select>';
 8147:             }
 8148:         } elsif ($rule eq $current) {
 8149:             $checked = ' checked="checked"';
 8150:         }
 8151:         $output .= '<span class="LC_nobreak"><label>'.
 8152:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 8153:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 8154:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 8155:                    ')"'.$checked.' />&nbsp;';
 8156:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 8157:             $output .= $ruletitles{'particular'};
 8158:         } else {
 8159:             $output .= $ruletitles{$rulenames[$i]};
 8160:         }
 8161:         $output .= '</label>'.$extra.'</span><br />'."\n";
 8162:     }
 8163:     $output .= '</div></td></tr>'."\n";
 8164:     return $output;
 8165: }
 8166: 
 8167: sub offloadtype_text {
 8168:     my %ruletitles = &Apache::lonlocal::texthash (
 8169:            'default'          => 'Offloads to default destinations',
 8170:            'homeserver'       => "Offloads to user's home server",
 8171:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 8172:            'specific'         => 'Offloads to specific server',
 8173:            'none'             => 'No offload',
 8174:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 8175:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 8176:            'particular'       => 'Session hosted (after re-auth) on server:',
 8177:     );
 8178:     return %ruletitles;
 8179: }
 8180: 
 8181: sub sparestype_titles {
 8182:     my %typestitles = &Apache::lonlocal::texthash (
 8183:                           'primary' => 'primary',
 8184:                           'default' => 'default',
 8185:                       );
 8186:     return %typestitles;
 8187: }
 8188: 
 8189: sub contact_titles {
 8190:     my %titles = &Apache::lonlocal::texthash (
 8191:                    'supportemail'    => 'Support E-mail address',
 8192:                    'adminemail'      => 'Default Server Admin E-mail address',
 8193:                    'errormail'       => 'Error reports to be e-mailed to',
 8194:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 8195:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 8196:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 8197:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 8198:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 8199:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 8200:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 8201:                    'hostipmail'      => 'E-mail from nightly check of hostname/IP network changes',
 8202:                    'errorthreshold'  => 'Error/warning threshold for status e-mail',
 8203:                    'errorsysmail'    => 'Error threshold for e-mail to core group',
 8204:                    'errorweights'    => 'Weights used to compute error count',
 8205:                    'errorexcluded'   => 'Servers with unsent updates excluded from count',
 8206:                  );
 8207:     my %short_titles = &Apache::lonlocal::texthash (
 8208:                            adminemail   => 'Admin E-mail address',
 8209:                            supportemail => 'Support E-mail',
 8210:                        );   
 8211:     return (\%titles,\%short_titles);
 8212: }
 8213: 
 8214: sub helpform_fields {
 8215:     my %titles =  &Apache::lonlocal::texthash (
 8216:                        'username'   => 'Name',
 8217:                        'user'       => 'Username/domain',
 8218:                        'phone'      => 'Phone',
 8219:                        'cc'         => 'Cc e-mail',
 8220:                        'course'     => 'Course Details',
 8221:                        'section'    => 'Sections',
 8222:                        'screenshot' => 'File upload',
 8223:     );
 8224:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 8225:     my %possoptions = (
 8226:                         username     => ['yes','no','req'],
 8227:                         phone        => ['yes','no','req'],
 8228:                         user         => ['yes','no'],
 8229:                         cc           => ['yes','no'],
 8230:                         course       => ['yes','no'],
 8231:                         section      => ['yes','no'],
 8232:                         screenshot   => ['yes','no'],
 8233:                       );
 8234:     my %fieldoptions = &Apache::lonlocal::texthash (
 8235:                          'yes'  => 'Optional',
 8236:                          'req'  => 'Required',
 8237:                          'no'   => "Not shown",
 8238:     );
 8239:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 8240: }
 8241: 
 8242: sub tool_titles {
 8243:     my %titles = &Apache::lonlocal::texthash (
 8244:                      aboutme    => 'Personal web page',
 8245:                      blog       => 'Blog',
 8246:                      webdav     => 'WebDAV',
 8247:                      portfolio  => 'Portfolio',
 8248:                      official   => 'Official courses (with institutional codes)',
 8249:                      unofficial => 'Unofficial courses',
 8250:                      community  => 'Communities',
 8251:                      textbook   => 'Textbook courses',
 8252:                      placement  => 'Placement tests',
 8253:                  );
 8254:     return %titles;
 8255: }
 8256: 
 8257: sub courserequest_titles {
 8258:     my %titles = &Apache::lonlocal::texthash (
 8259:                                    official   => 'Official',
 8260:                                    unofficial => 'Unofficial',
 8261:                                    community  => 'Communities',
 8262:                                    textbook   => 'Textbook',
 8263:                                    placement  => 'Placement tests',
 8264:                                    lti        => 'LTI Provider',
 8265:                                    norequest  => 'Not allowed',
 8266:                                    approval   => 'Approval by DC',
 8267:                                    validate   => 'With validation',
 8268:                                    autolimit  => 'Numerical limit',
 8269:                                    unlimited  => '(blank for unlimited)',
 8270:                  );
 8271:     return %titles;
 8272: }
 8273: 
 8274: sub authorrequest_titles {
 8275:     my %titles = &Apache::lonlocal::texthash (
 8276:                                    norequest  => 'Not allowed',
 8277:                                    approval   => 'Approval by Dom. Coord.',
 8278:                                    automatic  => 'Automatic approval',
 8279:                  );
 8280:     return %titles;
 8281: }
 8282: 
 8283: sub courserequest_conditions {
 8284:     my %conditions = &Apache::lonlocal::texthash (
 8285:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 8286:        validate   => '(Processing of request subject to institutional validation).',
 8287:                  );
 8288:     return %conditions;
 8289: }
 8290: 
 8291: 
 8292: sub print_usercreation {
 8293:     my ($position,$dom,$settings,$rowtotal) = @_;
 8294:     my $numinrow = 4;
 8295:     my $datatable;
 8296:     if ($position eq 'top') {
 8297:         $$rowtotal ++;
 8298:         my $rowcount = 0;
 8299:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 8300:         if (ref($rules) eq 'HASH') {
 8301:             if (keys(%{$rules}) > 0) {
 8302:                 $datatable .= &user_formats_row('username',$settings,$rules,
 8303:                                                 $ruleorder,$numinrow,$rowcount);
 8304:                 $$rowtotal ++;
 8305:                 $rowcount ++;
 8306:             }
 8307:         }
 8308:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 8309:         if (ref($idrules) eq 'HASH') {
 8310:             if (keys(%{$idrules}) > 0) {
 8311:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 8312:                                                 $idruleorder,$numinrow,$rowcount);
 8313:                 $$rowtotal ++;
 8314:                 $rowcount ++;
 8315:             }
 8316:         }
 8317:         if ($rowcount == 0) {
 8318:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 8319:             $$rowtotal ++;
 8320:             $rowcount ++;
 8321:         }
 8322:     } elsif ($position eq 'middle') {
 8323:         my @creators = ('author','course','requestcrs');
 8324:         my ($rules,$ruleorder) =
 8325:             &Apache::lonnet::inst_userrules($dom,'username');
 8326:         my %lt = &usercreation_types();
 8327:         my %checked;
 8328:         if (ref($settings) eq 'HASH') {
 8329:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 8330:                 foreach my $item (@creators) {
 8331:                     $checked{$item} = $settings->{'cancreate'}{$item};
 8332:                 }
 8333:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 8334:                 foreach my $item (@creators) {
 8335:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 8336:                         $checked{$item} = 'none';
 8337:                     }
 8338:                 }
 8339:             }
 8340:         }
 8341:         my $rownum = 0;
 8342:         foreach my $item (@creators) {
 8343:             $rownum ++;
 8344:             if ($checked{$item} eq '') {
 8345:                 $checked{$item} = 'any';
 8346:             }
 8347:             my $css_class;
 8348:             if ($rownum%2) {
 8349:                 $css_class = '';
 8350:             } else {
 8351:                 $css_class = ' class="LC_odd_row" ';
 8352:             }
 8353:             $datatable .= '<tr'.$css_class.'>'.
 8354:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 8355:                          '</span></td><td style="text-align: right">';
 8356:             my @options = ('any');
 8357:             if (ref($rules) eq 'HASH') {
 8358:                 if (keys(%{$rules}) > 0) {
 8359:                     push(@options,('official','unofficial'));
 8360:                 }
 8361:             }
 8362:             push(@options,'none');
 8363:             foreach my $option (@options) {
 8364:                 my $type = 'radio';
 8365:                 my $check = ' ';
 8366:                 if ($checked{$item} eq $option) {
 8367:                     $check = ' checked="checked" ';
 8368:                 } 
 8369:                 $datatable .= '<span class="LC_nobreak"><label>'.
 8370:                               '<input type="'.$type.'" name="can_createuser_'.
 8371:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 8372:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 8373:             }
 8374:             $datatable .= '</td></tr>';
 8375:         }
 8376:     } else {
 8377:         my @contexts = ('author','course','domain');
 8378:         my @authtypes = ('int','krb4','krb5','loc','lti');
 8379:         my %checked;
 8380:         if (ref($settings) eq 'HASH') {
 8381:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 8382:                 foreach my $item (@contexts) {
 8383:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 8384:                         foreach my $auth (@authtypes) {
 8385:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 8386:                                 $checked{$item}{$auth} = ' checked="checked" ';
 8387:                             }
 8388:                         }
 8389:                     }
 8390:                 }
 8391:             }
 8392:         } else {
 8393:             foreach my $item (@contexts) {
 8394:                 foreach my $auth (@authtypes) {
 8395:                     $checked{$item}{$auth} = ' checked="checked" ';
 8396:                 }
 8397:             }
 8398:         }
 8399:         my %title = &context_names();
 8400:         my %authname = &authtype_names();
 8401:         my $rownum = 0;
 8402:         my $css_class; 
 8403:         foreach my $item (@contexts) {
 8404:             if ($rownum%2) {
 8405:                 $css_class = '';
 8406:             } else {
 8407:                 $css_class = ' class="LC_odd_row" ';
 8408:             }
 8409:             $datatable .=   '<tr'.$css_class.'>'.
 8410:                             '<td>'.$title{$item}.
 8411:                             '</td><td class="LC_left_item">'.
 8412:                             '<span class="LC_nobreak">';
 8413:             foreach my $auth (@authtypes) {
 8414:                 $datatable .= '<label>'. 
 8415:                               '<input type="checkbox" name="'.$item.'_auth" '.
 8416:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 8417:                               $authname{$auth}.'</label>&nbsp;';
 8418:             }
 8419:             $datatable .= '</span></td></tr>';
 8420:             $rownum ++;
 8421:         }
 8422:         $$rowtotal += $rownum;
 8423:     }
 8424:     return $datatable;
 8425: }
 8426: 
 8427: sub print_selfcreation {
 8428:     my ($position,$dom,$settings,$rowtotal) = @_;
 8429:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 8430:         $emaildomain,$datatable);
 8431:     if (ref($settings) eq 'HASH') {
 8432:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 8433:             $createsettings = $settings->{'cancreate'};
 8434:             if (ref($createsettings) eq 'HASH') {
 8435:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 8436:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 8437:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 8438:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 8439:                         @selfcreate = ('email','login','sso');
 8440:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 8441:                         @selfcreate = ($createsettings->{'selfcreate'});
 8442:                     }
 8443:                 }
 8444:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 8445:                     $processing = $createsettings->{'selfcreateprocessing'};
 8446:                 }
 8447:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 8448:                     $emailoptions = $createsettings->{'emailoptions'};
 8449:                 }
 8450:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 8451:                     $emailverified = $createsettings->{'emailverified'};
 8452:                 }
 8453:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 8454:                     $emaildomain = $createsettings->{'emaildomain'};
 8455:                 }
 8456:             }
 8457:         }
 8458:     }
 8459:     my %radiohash;
 8460:     my $numinrow = 4;
 8461:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 8462:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8463:     if ($position eq 'top') {
 8464:         my %choices = &Apache::lonlocal::texthash (
 8465:                                                       cancreate_login      => 'Institutional Login',
 8466:                                                       cancreate_sso        => 'Institutional Single Sign On',
 8467:                                                   );
 8468:         my @toggles = sort(keys(%choices));
 8469:         my %defaultchecked = (
 8470:                                'cancreate_login' => 'off',
 8471:                                'cancreate_sso'   => 'off',
 8472:                              );
 8473:         my ($onclick,$itemcount);
 8474:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 8475:                                                      \%choices,$itemcount,$onclick);
 8476:         $$rowtotal += $itemcount;
 8477:         
 8478:         if (ref($usertypes) eq 'HASH') {
 8479:             if (keys(%{$usertypes}) > 0) {
 8480:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 8481:                                              $dom,$numinrow,$othertitle,
 8482:                                              'statustocreate',$rowtotal);
 8483:                 $$rowtotal ++;
 8484:             }
 8485:         }
 8486:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 8487:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 8488:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 8489:         my $rem;
 8490:         my $numperrow = 2;
 8491:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 8492:         $datatable .= '<tr'.$css_class.'>'.
 8493:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 8494:                      '<td class="LC_left_item">'."\n".
 8495:                      '<table>'."\n";
 8496:         for (my $i=0; $i<@fields; $i++) {
 8497:             $rem = $i%($numperrow);
 8498:             if ($rem == 0) {
 8499:                 if ($i > 0) {
 8500:                     $datatable .= '</tr>';
 8501:                 }
 8502:                 $datatable .= '<tr>';
 8503:             }
 8504:             my $currval;
 8505:             if (ref($createsettings) eq 'HASH') {
 8506:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 8507:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 8508:                 }
 8509:             }
 8510:             $datatable .= '<td class="LC_left_item">'.
 8511:                           '<span class="LC_nobreak">'.
 8512:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 8513:                           'value="'.$currval.'" size="10" />&nbsp;'.
 8514:                           $fieldtitles{$fields[$i]}.'</span></td>';
 8515:         }
 8516:         my $colsleft = $numperrow - $rem;
 8517:         if ($colsleft > 1 ) {
 8518:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 8519:                          '&nbsp;</td>';
 8520:         } elsif ($colsleft == 1) {
 8521:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 8522:         }
 8523:         $datatable .= '</tr></table></td></tr>';
 8524:         $$rowtotal ++;
 8525:     } elsif ($position eq 'middle') {
 8526:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 8527:         my @posstypes;
 8528:         if (ref($types) eq 'ARRAY') {
 8529:             @posstypes = @{$types};
 8530:         }
 8531:         unless (grep(/^default$/,@posstypes)) {
 8532:             push(@posstypes,'default');
 8533:         }
 8534:         my %usertypeshash;
 8535:         if (ref($usertypes) eq 'HASH') {
 8536:             %usertypeshash = %{$usertypes};
 8537:         }
 8538:         $usertypeshash{'default'} = $othertitle;
 8539:         foreach my $status (@posstypes) {
 8540:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 8541:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 8542:             $$rowtotal ++;
 8543:         }
 8544:     } else {
 8545:         my %choices = &Apache::lonlocal::texthash (
 8546:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 8547:                                                   );
 8548:         my @toggles = sort(keys(%choices));
 8549:         my %defaultchecked = (
 8550:                                'cancreate_email' => 'off',
 8551:                              );
 8552:         my $customclass = 'LC_selfcreate_email';
 8553:         my $classprefix = 'LC_canmodify_emailusername_';
 8554:         my $optionsprefix = 'LC_options_emailusername_';
 8555:         my $display = 'none';
 8556:         my $rowstyle = 'display:none';
 8557:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 8558:             $display = 'block';
 8559:             $rowstyle = 'display:table-row';
 8560:         }
 8561:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 8562:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 8563:                                                      \%choices,$$rowtotal,$onclick);
 8564:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 8565:                                          $rowstyle);
 8566:         $$rowtotal ++;
 8567:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 8568:                                       $rowstyle);
 8569:         $$rowtotal ++;
 8570:         my (@ordered,@posstypes,%usertypeshash);
 8571:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 8572:         my ($emailrules,$emailruleorder) =
 8573:             &Apache::lonnet::inst_userrules($dom,'email');
 8574:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 8575:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 8576:         if (ref($types) eq 'ARRAY') {
 8577:             @posstypes = @{$types};
 8578:         }
 8579:         if (@posstypes) {
 8580:             unless (grep(/^default$/,@posstypes)) {
 8581:                 push(@posstypes,'default');
 8582:             }
 8583:             if (ref($usertypes) eq 'HASH') {
 8584:                 %usertypeshash = %{$usertypes};
 8585:             }
 8586:             my $currassign;
 8587:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 8588:                 $currassign = {
 8589:                                   selfassign => $domdefaults{'inststatusguest'},
 8590:                               };
 8591:                 @ordered = @{$domdefaults{'inststatusguest'}};
 8592:             } else {
 8593:                 $currassign = { selfassign => [] };
 8594:             }
 8595:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 8596:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 8597:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 8598:                                          $numinrow,$othertitle,'selfassign',
 8599:                                          $rowtotal,$onclicktypes,$customclass,
 8600:                                          $rowstyle);
 8601:             $$rowtotal ++;
 8602:             $usertypeshash{'default'} = $othertitle;
 8603:             foreach my $status (@posstypes) {
 8604:                 my $css_class;
 8605:                 if ($$rowtotal%2) {
 8606:                     $css_class = 'LC_odd_row ';
 8607:                 }
 8608:                 $css_class .= $customclass;
 8609:                 my $rowid = $optionsprefix.$status;
 8610:                 my $hidden = 1;
 8611:                 my $currstyle = 'display:none';
 8612:                 if (grep(/^\Q$status\E$/,@ordered)) {
 8613:                     $currstyle = $rowstyle;
 8614:                     $hidden = 0; 
 8615:                 }
 8616:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 8617:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 8618:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 8619:                 unless ($hidden) {
 8620:                     $$rowtotal ++;
 8621:                 }
 8622:             }
 8623:         } else {
 8624:             my $css_class;
 8625:             if ($$rowtotal%2) {
 8626:                 $css_class = 'LC_odd_row ';
 8627:             }
 8628:             $css_class .= $customclass;
 8629:             $usertypeshash{'default'} = $othertitle;
 8630:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 8631:                                          $emailrules,$emailruleorder,$settings,'default','',
 8632:                                          $othertitle,$css_class,$rowstyle,$intdom);
 8633:             $$rowtotal ++;
 8634:         }
 8635:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 8636:         $numinrow = 1;
 8637:         if (@posstypes) {
 8638:             foreach my $status (@posstypes) {
 8639:                 my $rowid = $classprefix.$status;
 8640:                 my $datarowstyle = 'display:none';
 8641:                 if (grep(/^\Q$status\E$/,@ordered)) { 
 8642:                     $datarowstyle = $rowstyle; 
 8643:                 }
 8644:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 8645:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 8646:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 8647:                 unless ($datarowstyle eq 'display:none') {
 8648:                     $$rowtotal ++;
 8649:                 }
 8650:             }
 8651:         } else {
 8652:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 8653:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 8654:                                                    $infotitles,'',$customclass,$rowstyle);
 8655:         }
 8656:     }
 8657:     return $datatable;
 8658: }
 8659: 
 8660: sub selfcreate_javascript {
 8661:     return <<"ENDSCRIPT";
 8662: 
 8663: <script type="text/javascript">
 8664: // <![CDATA[
 8665: 
 8666: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 8667:     var x = document.getElementsByClassName(target);
 8668:     var insttypes = 0;
 8669:     var insttypeRegExp = new RegExp(prefix);
 8670:     if ((x.length != undefined) && (x.length > 0)) {
 8671:         if (form.elements[radio].length != undefined) {
 8672:             for (var i=0; i<form.elements[radio].length; i++) {
 8673:                 if (form.elements[radio][i].checked) {
 8674:                     if (form.elements[radio][i].value == 1) {
 8675:                         for (var j=0; j<x.length; j++) {
 8676:                             if (x[j].id == 'undefined') {
 8677:                                 x[j].style.display = 'table-row';
 8678:                             } else if (insttypeRegExp.test(x[j].id)) {
 8679:                                 insttypes ++;
 8680:                             } else {
 8681:                                 x[j].style.display = 'table-row';
 8682:                             }
 8683:                         }
 8684:                     } else {
 8685:                         for (var j=0; j<x.length; j++) {
 8686:                             x[j].style.display = 'none';
 8687:                         }
 8688:                     }
 8689:                     break;
 8690:                 }
 8691:             }
 8692:             if (insttypes > 0) {
 8693:                 toggleDataRow(form,checkbox,target,altprefix);
 8694:                 toggleDataRow(form,checkbox,target,prefix,1);
 8695:             }
 8696:         }
 8697:     }
 8698:     return;
 8699: }
 8700: 
 8701: function toggleDataRow(form,checkbox,target,prefix,docount) {
 8702:     if (form.elements[checkbox].length != undefined) {
 8703:         var count = 0;
 8704:         if (docount) {
 8705:             for (var i=0; i<form.elements[checkbox].length; i++) {
 8706:                 if (form.elements[checkbox][i].checked) {
 8707:                     count ++;
 8708:                 }
 8709:             }
 8710:         }
 8711:         for (var i=0; i<form.elements[checkbox].length; i++) {
 8712:             var type = form.elements[checkbox][i].value;
 8713:             if (document.getElementById(prefix+type)) {
 8714:                 if (form.elements[checkbox][i].checked) {
 8715:                     document.getElementById(prefix+type).style.display = 'table-row';
 8716:                     if (count % 2 == 1) {
 8717:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 8718:                     } else {
 8719:                         document.getElementById(prefix+type).className = target;
 8720:                     }
 8721:                     count ++;
 8722:                 } else {
 8723:                     document.getElementById(prefix+type).style.display = 'none';
 8724:                 }
 8725:             }
 8726:         }
 8727:     }
 8728:     return;
 8729: }
 8730: 
 8731: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 8732:     var caller = radio+'_'+status;
 8733:     if (form.elements[caller].length != undefined) {
 8734:         for (var i=0; i<form.elements[caller].length; i++) {
 8735:             if (form.elements[caller][i].checked) {
 8736:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 8737:                     var curr = form.elements[caller][i].value;
 8738:                     if (prefix) {
 8739:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 8740:                     }
 8741:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 8742:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 8743:                     if (curr == 'custom') {
 8744:                         if (prefix) { 
 8745:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 8746:                         }
 8747:                     } else if (curr == 'inst') {
 8748:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 8749:                     } else if (curr == 'noninst') {
 8750:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 8751:                     }
 8752:                     break;
 8753:                 }
 8754:             }
 8755:         }
 8756:     }
 8757: }
 8758: 
 8759: // ]]>
 8760: </script>
 8761: 
 8762: ENDSCRIPT
 8763: }
 8764: 
 8765: sub noninst_users {
 8766:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 8767:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_; 
 8768:     my $class = 'LC_left_item';
 8769:     if ($css_class) {
 8770:         $css_class = ' class="'.$css_class.'"'; 
 8771:     }
 8772:     if ($rowid) {
 8773:         $rowid = ' id="'.$rowid.'"';
 8774:     }
 8775:     if ($rowstyle) {
 8776:         $rowstyle = ' style="'.$rowstyle.'"';
 8777:     }
 8778:     my ($output,$description);
 8779:     if ($type eq 'default') {
 8780:         $description = &mt('Requests for: [_1]',$typetitle);
 8781:     } else {
 8782:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 8783:     }
 8784:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 8785:               "<td>$description</td>\n".           
 8786:               '<td class="'.$class.'" colspan="2">'.
 8787:               '<table><tr>';
 8788:     my %headers = &Apache::lonlocal::texthash( 
 8789:               approve  => 'Processing',
 8790:               email    => 'E-mail',
 8791:               username => 'Username',
 8792:     );
 8793:     foreach my $item ('approve','email','username') {
 8794:         $output .= '<th>'.$headers{$item}.'</th>';
 8795:     }
 8796:     $output .= '</tr><tr>';
 8797:     foreach my $item ('approve','email','username') {
 8798:         $output .= '<td style="vertical-align: top">';
 8799:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 8800:         if ($item eq 'approve') {
 8801:             %choices = &Apache::lonlocal::texthash (
 8802:                                                      automatic => 'Automatically approved',
 8803:                                                      approval  => 'Queued for approval',
 8804:                                                    );
 8805:             @options = ('automatic','approval');
 8806:             $hashref = $processing;
 8807:             $defoption = 'automatic';
 8808:             $name = 'cancreate_emailprocess_'.$type;
 8809:         } elsif ($item eq 'email') {
 8810:             %choices = &Apache::lonlocal::texthash (
 8811:                                                      any     => 'Any e-mail',
 8812:                                                      inst    => 'Institutional only',
 8813:                                                      noninst => 'Non-institutional only',
 8814:                                                      custom  => 'Custom restrictions',
 8815:                                                    );
 8816:             @options = ('any','inst','noninst');
 8817:             my $showcustom;
 8818:             if (ref($emailrules) eq 'HASH') {
 8819:                 if (keys(%{$emailrules}) > 0) {
 8820:                     push(@options,'custom');
 8821:                     $showcustom = 'cancreate_emailrule';
 8822:                     if (ref($settings) eq 'HASH') {
 8823:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 8824:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 8825:                                 if (exists($emailrules->{$rule})) {
 8826:                                     $hascustom ++;
 8827:                                 }
 8828:                             }
 8829:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 8830:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 8831:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 8832:                                     if (exists($emailrules->{$rule})) {
 8833:                                         $hascustom ++;
 8834:                                     }
 8835:                                 }
 8836:                             }
 8837:                         }
 8838:                     }
 8839:                 }
 8840:             }
 8841:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 8842:                                                      "'cancreate_emaildomain','$type'".');"';
 8843:             $hashref = $emailoptions;
 8844:             $defoption = 'any';
 8845:             $name = 'cancreate_emailoptions_'.$type;
 8846:         } elsif ($item eq 'username') {
 8847:             %choices = &Apache::lonlocal::texthash (
 8848:                                                      all    => 'Same as e-mail',
 8849:                                                      first  => 'Omit @domain',
 8850:                                                      free   => 'Free to choose',
 8851:                                                    );
 8852:             @options = ('all','first','free');
 8853:             $hashref = $emailverified;
 8854:             $defoption = 'all';
 8855:             $name = 'cancreate_usernameoptions_'.$type;
 8856:         }
 8857:         foreach my $option (@options) {
 8858:             my $checked;
 8859:             if (ref($hashref) eq 'HASH') {
 8860:                 if ($type eq '') {
 8861:                     if (!exists($hashref->{'default'})) {
 8862:                         if ($option eq $defoption) {
 8863:                             $checked = ' checked="checked"';
 8864:                         }
 8865:                     } else {
 8866:                         if ($hashref->{'default'} eq $option) {
 8867:                             $checked = ' checked="checked"';
 8868:                         }
 8869:                     }
 8870:                 } else {
 8871:                     if (!exists($hashref->{$type})) {
 8872:                         if ($option eq $defoption) {
 8873:                             $checked = ' checked="checked"';
 8874:                         }
 8875:                     } else {
 8876:                         if ($hashref->{$type} eq $option) {
 8877:                             $checked = ' checked="checked"';
 8878:                         }
 8879:                     }
 8880:                 }
 8881:             } elsif (($item eq 'email') && ($hascustom)) {
 8882:                 if ($option eq 'custom') {
 8883:                     $checked = ' checked="checked"';
 8884:                 }
 8885:             } elsif ($option eq $defoption) {
 8886:                 $checked = ' checked="checked"';
 8887:             }
 8888:             $output .= '<span class="LC_nobreak"><label>'.
 8889:                        '<input type="radio" name="'.$name.'"'.
 8890:                        $checked.' value="'.$option.'"'.$onclick.' />'.
 8891:                        $choices{$option}.'</label></span><br />';
 8892:             if ($item eq 'email') {
 8893:                 if ($option eq 'custom') {
 8894:                     my $id = 'cancreate_emailrule_'.$type;
 8895:                     my $display = 'none';
 8896:                     if ($checked) {
 8897:                         $display = 'inline';
 8898:                     }
 8899:                     my $numinrow = 2;
 8900:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
 8901:                                '<legend>'.&mt('Disallow').'</legend><table>'.
 8902:                                &user_formats_row('email',$settings,$emailrules,
 8903:                                                  $emailruleorder,$numinrow,'',$type);
 8904:                               '</table></fieldset>';
 8905:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
 8906:                     my %text = &Apache::lonlocal::texthash (
 8907:                                                              inst    => 'must end:',
 8908:                                                              noninst => 'cannot end:',
 8909:                                                            );
 8910:                     my $value;
 8911:                     if (ref($emaildomain) eq 'HASH') {
 8912:                         if (ref($emaildomain->{$type}) eq 'HASH') {
 8913:                             $value = $emaildomain->{$type}->{$option}; 
 8914:                         }
 8915:                     }
 8916:                     if ($value eq '') {
 8917:                         $value = '@'.$intdom;
 8918:                     }
 8919:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
 8920:                     my $display = 'none';
 8921:                     if ($checked) {
 8922:                         $display = 'inline';
 8923:                     }
 8924:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
 8925:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
 8926:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
 8927:                                '</div>';
 8928:                 }
 8929:             }
 8930:         }
 8931:         $output .= '</td>'."\n";
 8932:     }
 8933:     $output .= "</tr></table></td></tr>\n";
 8934:     return $output;
 8935: }
 8936: 
 8937: sub captcha_choice {
 8938:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
 8939:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 8940:         $vertext,$currver);
 8941:     my %lt = &captcha_phrases();
 8942:     $keyentry = 'hidden';
 8943:     my $colspan=2;
 8944:     if ($context eq 'cancreate') {
 8945:         $rowname = &mt('CAPTCHA validation');
 8946:     } elsif ($context eq 'login') {
 8947:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 8948:     } elsif ($context eq 'passwords') {
 8949:         $rowname = &mt('"Forgot Password" CAPTCHA validation');
 8950:         $colspan=1;
 8951:     }
 8952:     if (ref($settings) eq 'HASH') {
 8953:         if ($settings->{'captcha'}) {
 8954:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 8955:         } else {
 8956:             $checked{'original'} = ' checked="checked"';
 8957:         }
 8958:         if ($settings->{'captcha'} eq 'recaptcha') {
 8959:             $pubtext = $lt{'pub'};
 8960:             $privtext = $lt{'priv'};
 8961:             $keyentry = 'text';
 8962:             $vertext = $lt{'ver'};
 8963:             $currver = $settings->{'recaptchaversion'};
 8964:             if ($currver ne '2') {
 8965:                 $currver = 1;
 8966:             }
 8967:         }
 8968:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 8969:             $currpub = $settings->{'recaptchakeys'}{'public'};
 8970:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 8971:         }
 8972:     } else {
 8973:         $checked{'original'} = ' checked="checked"';
 8974:     }
 8975:     my $css_class;
 8976:     if ($itemcount%2) {
 8977:         $css_class = 'LC_odd_row';
 8978:     }
 8979:     if ($customcss) {
 8980:         $css_class .= " $customcss";
 8981:     }
 8982:     $css_class =~ s/^\s+//;
 8983:     if ($css_class) {
 8984:         $css_class = ' class="'.$css_class.'"';
 8985:     }
 8986:     if ($rowstyle) {
 8987:         $css_class .= ' style="'.$rowstyle.'"';
 8988:     }
 8989:     my $output = '<tr'.$css_class.'>'.
 8990:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="'.$colspan.'">'."\n".
 8991:                  '<table><tr><td>'."\n";
 8992:     foreach my $option ('original','recaptcha','notused') {
 8993:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 8994:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 8995:                    $lt{$option}.'</label></span>';
 8996:         unless ($option eq 'notused') {
 8997:             $output .= ('&nbsp;'x2)."\n";
 8998:         }
 8999:     }
 9000: #
 9001: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 9002: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 9003: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 9004: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 9005: #
 9006:     $output .= '</td></tr>'."\n".
 9007:                '<tr><td class="LC_zero_height">'."\n".
 9008:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 9009:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 9010:                $currpub.'" size="40" /></span><br />'."\n".
 9011:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 9012:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 9013:                $currpriv.'" size="40" /></span><br />'.
 9014:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 9015:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 9016:                $currver.'" size="3" /></span><br />'.
 9017:                '</td></tr></table>'."\n".
 9018:                '</td></tr>';
 9019:     return $output;
 9020: }
 9021: 
 9022: sub user_formats_row {
 9023:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
 9024:     my $output;
 9025:     my %text = (
 9026:                    'username' => 'new usernames',
 9027:                    'id'       => 'IDs',
 9028:                );
 9029:     unless ($type eq 'email') {
 9030:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 9031:         $output = '<tr '.$css_class.'>'.
 9032:                   '<td><span class="LC_nobreak">'.
 9033:                   &mt("Format rules to check for $text{$type}: ").
 9034:                   '</td><td class="LC_left_item" colspan="2"><table>';
 9035:     }
 9036:     my $rem;
 9037:     if (ref($ruleorder) eq 'ARRAY') {
 9038:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 9039:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 9040:                 my $rem = $i%($numinrow);
 9041:                 if ($rem == 0) {
 9042:                     if ($i > 0) {
 9043:                         $output .= '</tr>';
 9044:                     }
 9045:                     $output .= '<tr>';
 9046:                 }
 9047:                 my $check = ' ';
 9048:                 if (ref($settings) eq 'HASH') {
 9049:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 9050:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 9051:                             $check = ' checked="checked" ';
 9052:                         }
 9053:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
 9054:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
 9055:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
 9056:                                 $check = ' checked="checked" ';
 9057:                             }
 9058:                         }
 9059:                     }
 9060:                 }
 9061:                 my $name = $type.'_rule';
 9062:                 if ($type eq 'email') {
 9063:                     $name .= '_'.$status;
 9064:                 }
 9065:                 $output .= '<td class="LC_left_item">'.
 9066:                            '<span class="LC_nobreak"><label>'.
 9067:                            '<input type="checkbox" name="'.$name.'" '.
 9068:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 9069:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 9070:             }
 9071:         }
 9072:         $rem = @{$ruleorder}%($numinrow);
 9073:     }
 9074:     my $colsleft;
 9075:     if ($rem) {
 9076:         $colsleft = $numinrow - $rem;
 9077:     }
 9078:     if ($colsleft > 1 ) {
 9079:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 9080:                    '&nbsp;</td>';
 9081:     } elsif ($colsleft == 1) {
 9082:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 9083:     }
 9084:     $output .= '</tr></table>';
 9085:     unless ($type eq 'email') {
 9086:         $output .= '</td></tr>';
 9087:     }
 9088:     return $output;
 9089: }
 9090: 
 9091: sub usercreation_types {
 9092:     my %lt = &Apache::lonlocal::texthash (
 9093:                     author     => 'When adding a co-author',
 9094:                     course     => 'When adding a user to a course',
 9095:                     requestcrs => 'When requesting a course',
 9096:                     any        => 'Any',
 9097:                     official   => 'Institutional only ',
 9098:                     unofficial => 'Non-institutional only',
 9099:                     none       => 'None',
 9100:     );
 9101:     return %lt;
 9102: }
 9103: 
 9104: sub selfcreation_types {
 9105:     my %lt = &Apache::lonlocal::texthash (
 9106:                     selfcreate => 'User creates own account',
 9107:                     any        => 'Any',
 9108:                     official   => 'Institutional only ',
 9109:                     unofficial => 'Non-institutional only',
 9110:                     email      => 'E-mail address',
 9111:                     login      => 'Institutional Login',
 9112:                     sso        => 'SSO',
 9113:              );
 9114: }
 9115: 
 9116: sub authtype_names {
 9117:     my %lt = &Apache::lonlocal::texthash(
 9118:                       int    => 'Internal',
 9119:                       krb4   => 'Kerberos 4',
 9120:                       krb5   => 'Kerberos 5',
 9121:                       loc    => 'Local',
 9122:                       lti    => 'LTI',
 9123:                   );
 9124:     return %lt;
 9125: }
 9126: 
 9127: sub context_names {
 9128:     my %context_title = &Apache::lonlocal::texthash(
 9129:        author => 'Creating users when an Author',
 9130:        course => 'Creating users when in a course',
 9131:        domain => 'Creating users when a Domain Coordinator',
 9132:     );
 9133:     return %context_title;
 9134: }
 9135: 
 9136: sub print_usermodification {
 9137:     my ($position,$dom,$settings,$rowtotal) = @_;
 9138:     my $numinrow = 4;
 9139:     my ($context,$datatable,$rowcount);
 9140:     if ($position eq 'top') {
 9141:         $rowcount = 0;
 9142:         $context = 'author'; 
 9143:         foreach my $role ('ca','aa') {
 9144:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 9145:                                                    $numinrow,$rowcount);
 9146:             $$rowtotal ++;
 9147:             $rowcount ++;
 9148:         }
 9149:     } elsif ($position eq 'bottom') {
 9150:         $context = 'course';
 9151:         $rowcount = 0;
 9152:         foreach my $role ('st','ep','ta','in','cr') {
 9153:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 9154:                                                    $numinrow,$rowcount);
 9155:             $$rowtotal ++;
 9156:             $rowcount ++;
 9157:         }
 9158:     }
 9159:     return $datatable;
 9160: }
 9161: 
 9162: sub print_defaults {
 9163:     my ($position,$dom,$settings,$rowtotal) = @_;
 9164:     my $rownum = 0;
 9165:     my ($datatable,$css_class,$titles);
 9166:     unless ($position eq 'bottom') {
 9167:         $titles = &defaults_titles($dom);
 9168:     }
 9169:     if ($position eq 'top') {
 9170:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 9171:                      'datelocale_def','portal_def');
 9172:         my %defaults;
 9173:         if (ref($settings) eq 'HASH') {
 9174:             %defaults = %{$settings};
 9175:         } else {
 9176:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9177:             foreach my $item (@items) {
 9178:                 $defaults{$item} = $domdefaults{$item};
 9179:             }
 9180:         }
 9181:         foreach my $item (@items) {
 9182:             if ($rownum%2) {
 9183:                 $css_class = '';
 9184:             } else {
 9185:                 $css_class = ' class="LC_odd_row" ';
 9186:             }
 9187:             $datatable .= '<tr'.$css_class.'>'.
 9188:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 9189:                           '</span></td><td class="LC_right_item" colspan="3">';
 9190:             if ($item eq 'auth_def') {
 9191:                 my @authtypes = ('internal','krb4','krb5','localauth','lti');
 9192:                 my %shortauth = (
 9193:                                  internal => 'int',
 9194:                                  krb4 => 'krb4',
 9195:                                  krb5 => 'krb5',
 9196:                                  localauth  => 'loc',
 9197:                                  lti => 'lti',
 9198:                                 );
 9199:                 my %authnames = &authtype_names();
 9200:                 foreach my $auth (@authtypes) {
 9201:                     my $checked = ' ';
 9202:                     if ($defaults{$item} eq $auth) {
 9203:                         $checked = ' checked="checked" ';
 9204:                     }
 9205:                     $datatable .= '<label><input type="radio" name="'.$item.
 9206:                                   '" value="'.$auth.'"'.$checked.'/>'.
 9207:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 9208:                 }
 9209:             } elsif ($item eq 'timezone_def') {
 9210:                 my $includeempty = 1;
 9211:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 9212:             } elsif ($item eq 'datelocale_def') {
 9213:                 my $includeempty = 1;
 9214:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 9215:             } elsif ($item eq 'lang_def') {
 9216:                 my $includeempty = 1;
 9217:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 9218:             } else {
 9219:                 my $size;
 9220:                 if ($item eq 'portal_def') {
 9221:                     $size = ' size="25"';
 9222:                 }
 9223:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 9224:                               $defaults{$item}.'"'.$size.' />';
 9225:             }
 9226:             $datatable .= '</td></tr>';
 9227:             $rownum ++;
 9228:         }
 9229:     } else {
 9230:         my %defaults;
 9231:         if (ref($settings) eq 'HASH') {
 9232:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 9233:                 my $maxnum = @{$settings->{'inststatusorder'}};
 9234:                 for (my $i=0; $i<$maxnum; $i++) {
 9235:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 9236:                     my $item = $settings->{'inststatusorder'}->[$i];
 9237:                     my $title = $settings->{'inststatustypes'}->{$item};
 9238:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 9239:                     $datatable .= '<tr'.$css_class.'>'.
 9240:                                   '<td><span class="LC_nobreak">'.
 9241:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 9242:                     for (my $k=0; $k<=$maxnum; $k++) {
 9243:                         my $vpos = $k+1;
 9244:                         my $selstr;
 9245:                         if ($k == $i) {
 9246:                             $selstr = ' selected="selected" ';
 9247:                         }
 9248:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 9249:                     }
 9250:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 9251:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 9252:                                   &mt('delete').'</span></td>'.
 9253:                                   '<td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.&mt('Name displayed:').
 9254:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 9255:                                   '</span></td></tr>';
 9256:                 }
 9257:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 9258:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 9259:                 $datatable .= '<tr '.$css_class.'>'.
 9260:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 9261:                 for (my $k=0; $k<=$maxnum; $k++) {
 9262:                     my $vpos = $k+1;
 9263:                     my $selstr;
 9264:                     if ($k == $maxnum) {
 9265:                         $selstr = ' selected="selected" ';
 9266:                     }
 9267:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 9268:                 }
 9269:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 9270:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 9271:                               '&nbsp;'.&mt('(new)').
 9272:                               '</span></td><td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.
 9273:                               &mt('Name displayed:').
 9274:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 9275:                               '</tr>'."\n";
 9276:                 $rownum ++;
 9277:             }
 9278:         }
 9279:     }
 9280:     $$rowtotal += $rownum;
 9281:     return $datatable;
 9282: }
 9283: 
 9284: sub get_languages_hash {
 9285:     my %langchoices;
 9286:     foreach my $id (&Apache::loncommon::languageids()) {
 9287:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 9288:         if ($code ne '') {
 9289:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 9290:         }
 9291:     }
 9292:     return %langchoices;
 9293: }
 9294: 
 9295: sub defaults_titles {
 9296:     my ($dom) = @_;
 9297:     my %titles = &Apache::lonlocal::texthash (
 9298:                    'auth_def'      => 'Default authentication type',
 9299:                    'auth_arg_def'  => 'Default authentication argument',
 9300:                    'lang_def'      => 'Default language',
 9301:                    'timezone_def'  => 'Default timezone',
 9302:                    'datelocale_def' => 'Default locale for dates',
 9303:                    'portal_def'     => 'Portal/Default URL',
 9304:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 9305:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 9306:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 9307:                  );
 9308:     if ($dom) {
 9309:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 9310:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 9311:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 9312:         $protocol = 'http' if ($protocol ne 'https');
 9313:         if ($uint_dom) {
 9314:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 9315:                                          $uint_dom);
 9316:         }
 9317:     }
 9318:     return (\%titles);
 9319: }
 9320: 
 9321: sub print_scantron {
 9322:     my ($r,$position,$dom,$confname,$settings,$rowtotal) = @_;
 9323:     if ($position eq 'top') {
 9324:         return &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
 9325:     } else {
 9326:         return &print_scantronconfig($dom,$settings,\$rowtotal);
 9327:     }
 9328: }
 9329: 
 9330: sub scantron_javascript {
 9331:     return <<"ENDSCRIPT";
 9332: 
 9333: <script type="text/javascript">
 9334: // <![CDATA[
 9335: 
 9336: function toggleScantron(form) {
 9337:     var csvfieldset = new Array();
 9338:     if (document.getElementById('scantroncsv_cols')) {
 9339:         csvfieldset.push(document.getElementById('scantroncsv_cols'));
 9340:     }
 9341:     if (document.getElementById('scantroncsv_options')) {
 9342:         csvfieldset.push(document.getElementById('scantroncsv_options'));
 9343:     }
 9344:     if (csvfieldset.length) {
 9345:         if (document.getElementById('scantronconfcsv')) {
 9346:             var scantroncsv = document.getElementById('scantronconfcsv');
 9347:             if (scantroncsv.checked) {
 9348:                 for (var i=0; i<csvfieldset.length; i++) {
 9349:                     csvfieldset[i].style.display = 'block';
 9350:                 }
 9351:             } else {
 9352:                 for (var i=0; i<csvfieldset.length; i++) {
 9353:                     csvfieldset[i].style.display = 'none';
 9354:                 }
 9355:                 var csvselects = document.getElementsByClassName('scantronconfig_csv');
 9356:                 if (csvselects.length) {
 9357:                     for (var j=0; j<csvselects.length; j++) {
 9358:                         csvselects[j].selectedIndex = 0;
 9359:                     }
 9360:                 }
 9361:             }
 9362:         }
 9363:     }
 9364:     return;
 9365: }
 9366: // ]]>
 9367: </script>
 9368: 
 9369: ENDSCRIPT
 9370: 
 9371: }
 9372: 
 9373: sub print_scantronformat {
 9374:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 9375:     my $itemcount = 1;
 9376:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 9377:         %confhash);
 9378:     my $switchserver = &check_switchserver($dom,$confname);
 9379:     my %lt = &Apache::lonlocal::texthash (
 9380:                 default => 'Default bubblesheet format file error',
 9381:                 custom  => 'Custom bubblesheet format file error',
 9382:              );
 9383:     my %scantronfiles = (
 9384:         default => 'default.tab',
 9385:         custom => 'custom.tab',
 9386:     );
 9387:     foreach my $key (keys(%scantronfiles)) {
 9388:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 9389:                               .$scantronfiles{$key};
 9390:     }
 9391:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 9392:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 9393:         if (!$switchserver) {
 9394:             my $servadm = $r->dir_config('lonAdmEMail');
 9395:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 9396:             if ($configuserok eq 'ok') {
 9397:                 if ($author_ok eq 'ok') {
 9398:                     my %legacyfile = (
 9399:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab',
 9400:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab',
 9401:                     );
 9402:                     my %md5chk;
 9403:                     foreach my $type (keys(%legacyfile)) {
 9404:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 9405:                         chomp($md5chk{$type});
 9406:                     }
 9407:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 9408:                         foreach my $type (keys(%legacyfile)) {
 9409:                             ($scantronurls{$type},my $error) =
 9410:                                 &legacy_scantronformat($r,$dom,$confname,
 9411:                                                  $type,$legacyfile{$type},
 9412:                                                  $scantronurls{$type},
 9413:                                                  $scantronfiles{$type});
 9414:                             if ($error ne '') {
 9415:                                 $error{$type} = $error;
 9416:                             }
 9417:                         }
 9418:                         if (keys(%error) == 0) {
 9419:                             $is_custom = 1;
 9420:                             $confhash{'scantron'}{'scantronformat'} =
 9421:                                 $scantronurls{'custom'};
 9422:                             my $putresult =
 9423:                                 &Apache::lonnet::put_dom('configuration',
 9424:                                                          \%confhash,$dom);
 9425:                             if ($putresult ne 'ok') {
 9426:                                 $error{'custom'} =
 9427:                                     '<span class="LC_error">'.
 9428:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 9429:                             }
 9430:                         }
 9431:                     } else {
 9432:                         ($scantronurls{'default'},my $error) =
 9433:                             &legacy_scantronformat($r,$dom,$confname,
 9434:                                           'default',$legacyfile{'default'},
 9435:                                           $scantronurls{'default'},
 9436:                                           $scantronfiles{'default'});
 9437:                         if ($error eq '') {
 9438:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 9439:                             my $putresult =
 9440:                                 &Apache::lonnet::put_dom('configuration',
 9441:                                                          \%confhash,$dom);
 9442:                             if ($putresult ne 'ok') {
 9443:                                 $error{'default'} =
 9444:                                     '<span class="LC_error">'.
 9445:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 9446:                             }
 9447:                         } else {
 9448:                             $error{'default'} = $error;
 9449:                         }
 9450:                     }
 9451:                 }
 9452:             }
 9453:         } else {
 9454:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 9455:         }
 9456:     }
 9457:     if (ref($settings) eq 'HASH') {
 9458:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 9459:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 9460:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 9461:                 $scantronurl = '';
 9462:             } else {
 9463:                 $scantronurl = $settings->{'scantronformat'};
 9464:             }
 9465:             $is_custom = 1;
 9466:         } else {
 9467:             $scantronurl = $scantronurls{'default'};
 9468:         }
 9469:     } else {
 9470:         if ($is_custom) {
 9471:             $scantronurl = $scantronurls{'custom'};
 9472:         } else {
 9473:             $scantronurl = $scantronurls{'default'};
 9474:         }
 9475:     }
 9476:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 9477:     $datatable .= '<tr'.$css_class.'>';
 9478:     if (!$is_custom) {
 9479:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 9480:                       '<span class="LC_nobreak">';
 9481:         if ($scantronurl) {
 9482:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 9483:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 9484:         } else {
 9485:             $datatable = &mt('File unavailable for display');
 9486:         }
 9487:         $datatable .= '</span></td>';
 9488:         if (keys(%error) == 0) { 
 9489:             $datatable .= '<td style="vertical-align: bottom">';
 9490:             if (!$switchserver) {
 9491:                 $datatable .= &mt('Upload:').'<br />';
 9492:             }
 9493:         } else {
 9494:             my $errorstr;
 9495:             foreach my $key (sort(keys(%error))) {
 9496:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 9497:             }
 9498:             $datatable .= '<td>'.$errorstr;
 9499:         }
 9500:     } else {
 9501:         if (keys(%error) > 0) {
 9502:             my $errorstr;
 9503:             foreach my $key (sort(keys(%error))) {
 9504:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 9505:             } 
 9506:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 9507:         } elsif ($scantronurl) {
 9508:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 9509:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 9510:             $datatable .= '<td><span class="LC_nobreak">'.
 9511:                           $link.
 9512:                           '<label><input type="checkbox" name="scantronformat_del"'.
 9513:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 9514:                           '<td><span class="LC_nobreak">&nbsp;'.
 9515:                           &mt('Replace:').'</span><br />';
 9516:         }
 9517:     }
 9518:     if (keys(%error) == 0) {
 9519:         if ($switchserver) {
 9520:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 9521:         } else {
 9522:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 9523:                          '<input type="file" name="scantronformat" /></span>';
 9524:         }
 9525:     }
 9526:     $datatable .= '</td></tr>';
 9527:     $$rowtotal ++;
 9528:     return $datatable;
 9529: }
 9530: 
 9531: sub legacy_scantronformat {
 9532:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 9533:     my ($url,$error);
 9534:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 9535:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 9536:         (my $result,$url) =
 9537:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 9538:                          '','',$newfile);
 9539:         if ($result ne 'ok') {
 9540:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 9541:         }
 9542:     }
 9543:     return ($url,$error);
 9544: }
 9545: 
 9546: sub print_scantronconfig {
 9547:     my ($dom,$settings,$rowtotal) = @_;
 9548:     my $itemcount = 2;
 9549:     my $is_checked = ' checked="checked"';
 9550:     my %optionson = (
 9551:                      hdr => ' checked="checked"',
 9552:                      pad => ' checked="checked"',
 9553:                      rem => ' checked="checked"',
 9554:                     );
 9555:     my %optionsoff = (
 9556:                       hdr => '',
 9557:                       pad => '',
 9558:                       rem => '',
 9559:                      );
 9560:     my $currcsvsty = 'none';
 9561:     my ($datatable,%csvfields,%checked,%onclick,%csvoptions);
 9562:     my @fields = &scantroncsv_fields();
 9563:     my %titles = &scantronconfig_titles();
 9564:     if (ref($settings) eq 'HASH') {
 9565:         if (ref($settings->{config}) eq 'HASH') {
 9566:             if ($settings->{config}->{dat}) {
 9567:                 $checked{'dat'} = $is_checked;
 9568:             }
 9569:             if (ref($settings->{config}->{csv}) eq 'HASH') {
 9570:                 if (ref($settings->{config}->{csv}->{fields}) eq 'HASH') {
 9571:                     %csvfields = %{$settings->{config}->{csv}->{fields}};
 9572:                     if (keys(%csvfields) > 0) {
 9573:                         $checked{'csv'} = $is_checked;
 9574:                         $currcsvsty = 'block';
 9575:                     }
 9576:                 }
 9577:                 if (ref($settings->{config}->{csv}->{options}) eq 'HASH') {
 9578:                     %csvoptions = %{$settings->{config}->{csv}->{options}};
 9579:                     foreach my $option (keys(%optionson)) {
 9580:                         unless ($csvoptions{$option}) {
 9581:                             $optionsoff{$option} = $optionson{$option};
 9582:                             $optionson{$option} = '';
 9583:                         }
 9584:                     }
 9585:                 }
 9586:             }
 9587:         } else {
 9588:             $checked{'dat'} = $is_checked;
 9589:         }
 9590:     } else {
 9591:         $checked{'dat'} = $is_checked;
 9592:     }
 9593:     $onclick{'csv'} = ' onclick="toggleScantron(this.form);"';
 9594:     my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 9595:     $datatable = '<tr '.$css_class.'><td>'.&mt('Supported formats').'</td>'.
 9596:                  '<td class="LC_left_item" valign="top"><span class="LC_nobreak">';
 9597:     foreach my $item ('dat','csv') {
 9598:         my $id;
 9599:         if ($item eq 'csv') {
 9600:             $id = 'id="scantronconfcsv" ';
 9601:         }
 9602:         $datatable .= '<label><input type="checkbox" name="scantronconfig" '.$id.'value="'.$item.'"'.$checked{$item}.$onclick{$item}.' />'.
 9603:                       $titles{$item}.'</label>'.('&nbsp;'x3);
 9604:         if ($item eq 'csv') {
 9605:             $datatable .= '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_cols">'.
 9606:                           '<legend>'.&mt('CSV Column Mapping').'</legend>'.
 9607:                           '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Location').'</th></tr>'."\n";
 9608:             foreach my $col (@fields) {
 9609:                 my $selnone;
 9610:                 if ($csvfields{$col} eq '') {
 9611:                     $selnone = ' selected="selected"';
 9612:                 }
 9613:                 $datatable .= '<tr><td>'.$titles{$col}.'</td>'.
 9614:                               '<td><select name="scantronconfig_csv_'.$col.'" class="scantronconfig_csv">'.
 9615:                               '<option value=""'.$selnone.'></option>';
 9616:                 for (my $i=0; $i<20; $i++) {
 9617:                     my $shown = $i+1;
 9618:                     my $sel;
 9619:                     unless ($selnone) {
 9620:                         if (exists($csvfields{$col})) {
 9621:                             if ($csvfields{$col} == $i) {
 9622:                                 $sel = ' selected="selected"';
 9623:                             }
 9624:                         }
 9625:                     }
 9626:                     $datatable .= '<option value="'.$i.'"'.$sel.'>'.$shown.'</option>';
 9627:                 }
 9628:                 $datatable .= '</select></td></tr>';
 9629:            }
 9630:            $datatable .= '</table></fieldset>'.
 9631:                          '<fieldset style="display:'.$currcsvsty.'" id="scantroncsv_options">'.
 9632:                          '<legend>'.&mt('CSV Options').'</legend>';
 9633:            foreach my $option ('hdr','pad','rem') {
 9634:                $datatable .= '<span class="LC_nobreak">'.$titles{$option}.':'.
 9635:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="1"'.$optionson{$option}.' />'.
 9636:                          &mt('Yes').'</label>'.('&nbsp;'x2)."\n".
 9637:                          '<label><input type="radio" name="scantroncsv_'.$option.'" value="0"'.$optionsoff{$option}.' />'.&mt('No').'</label></span><br />';
 9638:            }
 9639:            $datatable .= '</fieldset>';
 9640:            $itemcount ++;
 9641:         }
 9642:     }
 9643:     $datatable .= '</td></tr>';
 9644:     $$rowtotal ++;
 9645:     return $datatable;
 9646: }
 9647: 
 9648: sub scantronconfig_titles {
 9649:     return &Apache::lonlocal::texthash(
 9650:                                           dat => 'Standard format (.dat)',
 9651:                                           csv => 'Comma separated values (.csv)',
 9652:                                           hdr => 'Remove first line in file (contains column titles)',
 9653:                                           pad => 'Prepend 0s to PaperID',
 9654:                                           rem => 'Remove leading spaces (except Question Response columns)',
 9655:                                           CODE => 'CODE',
 9656:                                           ID   => 'Student ID',
 9657:                                           PaperID => 'Paper ID',
 9658:                                           FirstName => 'First Name',
 9659:                                           LastName => 'Last Name',
 9660:                                           FirstQuestion => 'First Question Response',
 9661:                                           Section => 'Section',
 9662:     );
 9663: }
 9664: 
 9665: sub scantroncsv_fields {
 9666:     return ('PaperID','LastName','FirstName','ID','Section','CODE','FirstQuestion');
 9667: }
 9668: 
 9669: sub print_coursecategories {
 9670:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 9671:     my $datatable;
 9672:     if ($position eq 'top') {
 9673:         my (%checked);
 9674:         my @catitems = ('unauth','auth');
 9675:         my @cattypes = ('std','domonly','codesrch','none');
 9676:         $checked{'unauth'} = 'std';
 9677:         $checked{'auth'} = 'std';
 9678:         if (ref($settings) eq 'HASH') {
 9679:             foreach my $type (@cattypes) {
 9680:                 if ($type eq $settings->{'unauth'}) {
 9681:                     $checked{'unauth'} = $type;
 9682:                 }
 9683:                 if ($type eq $settings->{'auth'}) {
 9684:                     $checked{'auth'} = $type;
 9685:                 }
 9686:             }
 9687:         }
 9688:         my %lt = &Apache::lonlocal::texthash (
 9689:                                                unauth   => 'Catalog type for unauthenticated users',
 9690:                                                auth     => 'Catalog type for authenticated users',
 9691:                                                none     => 'No catalog',
 9692:                                                std      => 'Standard catalog',
 9693:                                                domonly  => 'Domain-only catalog',
 9694:                                                codesrch => "Code search form",
 9695:                                              );
 9696:        my $itemcount = 0;
 9697:        foreach my $item (@catitems) {
 9698:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 9699:            $datatable .= '<tr '.$css_class.'>'.
 9700:                          '<td>'.$lt{$item}.'</td>'.
 9701:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 9702:            foreach my $type (@cattypes) {
 9703:                my $ischecked;
 9704:                if ($checked{$item} eq $type) {
 9705:                    $ischecked=' checked="checked"';
 9706:                }
 9707:                $datatable .= '<label>'.
 9708:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 9709:                              ' />'.$lt{$type}.'</label>&nbsp;';
 9710:            }
 9711:            $datatable .= '</span></td></tr>';
 9712:            $itemcount ++;
 9713:         }
 9714:         $$rowtotal += $itemcount;
 9715:     } elsif ($position eq 'middle') {
 9716:         my $toggle_cats_crs = ' ';
 9717:         my $toggle_cats_dom = ' checked="checked" ';
 9718:         my $can_cat_crs = ' ';
 9719:         my $can_cat_dom = ' checked="checked" ';
 9720:         my $toggle_catscomm_comm = ' ';
 9721:         my $toggle_catscomm_dom = ' checked="checked" ';
 9722:         my $can_catcomm_comm = ' ';
 9723:         my $can_catcomm_dom = ' checked="checked" ';
 9724:         my $toggle_catsplace_place = ' ';
 9725:         my $toggle_catsplace_dom = ' checked="checked" ';
 9726:         my $can_catplace_place = ' ';
 9727:         my $can_catplace_dom = ' checked="checked" ';
 9728: 
 9729:         if (ref($settings) eq 'HASH') {
 9730:             if ($settings->{'togglecats'} eq 'crs') {
 9731:                 $toggle_cats_crs = $toggle_cats_dom;
 9732:                 $toggle_cats_dom = ' ';
 9733:             }
 9734:             if ($settings->{'categorize'} eq 'crs') {
 9735:                 $can_cat_crs = $can_cat_dom;
 9736:                 $can_cat_dom = ' ';
 9737:             }
 9738:             if ($settings->{'togglecatscomm'} eq 'comm') {
 9739:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 9740:                 $toggle_catscomm_dom = ' ';
 9741:             }
 9742:             if ($settings->{'categorizecomm'} eq 'comm') {
 9743:                 $can_catcomm_comm = $can_catcomm_dom;
 9744:                 $can_catcomm_dom = ' ';
 9745:             }
 9746:             if ($settings->{'togglecatsplace'} eq 'place') {
 9747:                 $toggle_catsplace_place = $toggle_catsplace_dom;
 9748:                 $toggle_catsplace_dom = ' ';
 9749:             }
 9750:             if ($settings->{'categorizeplace'} eq 'place') {
 9751:                 $can_catplace_place = $can_catplace_dom;
 9752:                 $can_catplace_dom = ' ';
 9753:             }
 9754:         }
 9755:         my %title = &Apache::lonlocal::texthash (
 9756:                      togglecats      => 'Show/Hide a course in catalog',
 9757:                      togglecatscomm  => 'Show/Hide a community in catalog',
 9758:                      togglecatsplace => 'Show/Hide a placement test in catalog',
 9759:                      categorize      => 'Assign a category to a course',
 9760:                      categorizecomm  => 'Assign a category to a community',
 9761:                      categorizeplace => 'Assign a category to a placement test',
 9762:                     );
 9763:         my %level = &Apache::lonlocal::texthash (
 9764:                      dom   => 'Set in Domain',
 9765:                      crs   => 'Set in Course',
 9766:                      comm  => 'Set in Community',
 9767:                      place => 'Set in Placement Test',
 9768:                     );
 9769:         $datatable = '<tr class="LC_odd_row">'.
 9770:                   '<td>'.$title{'togglecats'}.'</td>'.
 9771:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 9772:                   '<input type="radio" name="togglecats"'.
 9773:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 9774:                   '<label><input type="radio" name="togglecats"'.
 9775:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 9776:                   '</tr><tr>'.
 9777:                   '<td>'.$title{'categorize'}.'</td>'.
 9778:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 9779:                   '<label><input type="radio" name="categorize"'.
 9780:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 9781:                   '<label><input type="radio" name="categorize"'.
 9782:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 9783:                   '</tr><tr class="LC_odd_row">'.
 9784:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 9785:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 9786:                   '<input type="radio" name="togglecatscomm"'.
 9787:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 9788:                   '<label><input type="radio" name="togglecatscomm"'.
 9789:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 9790:                   '</tr><tr>'.
 9791:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 9792:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 9793:                   '<label><input type="radio" name="categorizecomm"'.
 9794:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 9795:                   '<label><input type="radio" name="categorizecomm"'.
 9796:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 9797:                   '</tr><tr class="LC_odd_row">'.
 9798:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
 9799:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 9800:                   '<input type="radio" name="togglecatsplace"'.
 9801:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 9802:                   '<label><input type="radio" name="togglecatscomm"'.
 9803:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
 9804:                   '</tr><tr>'.
 9805:                   '<td>'.$title{'categorizeplace'}.'</td>'.
 9806:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 9807:                   '<label><input type="radio" name="categorizeplace"'.
 9808:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 9809:                   '<label><input type="radio" name="categorizeplace"'.
 9810:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
 9811:                   '</tr>';
 9812:         $$rowtotal += 6;
 9813:     } else {
 9814:         my $css_class;
 9815:         my $itemcount = 1;
 9816:         my $cathash; 
 9817:         if (ref($settings) eq 'HASH') {
 9818:             $cathash = $settings->{'cats'};
 9819:         }
 9820:         if (ref($cathash) eq 'HASH') {
 9821:             my (@cats,@trails,%allitems,%idx,@jsarray);
 9822:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 9823:                                                    \%allitems,\%idx,\@jsarray);
 9824:             my $maxdepth = scalar(@cats);
 9825:             my $colattrib = '';
 9826:             if ($maxdepth > 2) {
 9827:                 $colattrib = ' colspan="2" ';
 9828:             }
 9829:             my @path;
 9830:             if (@cats > 0) {
 9831:                 if (ref($cats[0]) eq 'ARRAY') {
 9832:                     my $numtop = @{$cats[0]};
 9833:                     my $maxnum = $numtop;
 9834:                     my %default_names = (
 9835:                           instcode    => &mt('Official courses'),
 9836:                           communities => &mt('Communities'),
 9837:                           placement   => &mt('Placement Tests'),
 9838:                     );
 9839: 
 9840:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 9841:                         ($cathash->{'instcode::0'} eq '') ||
 9842:                         (!grep(/^communities$/,@{$cats[0]})) || 
 9843:                         ($cathash->{'communities::0'} eq '') ||
 9844:                         (!grep(/^placement$/,@{$cats[0]})) ||
 9845:                         ($cathash->{'placement::0'} eq '')) {
 9846:                         $maxnum ++;
 9847:                     }
 9848:                     my $lastidx;
 9849:                     for (my $i=0; $i<$numtop; $i++) {
 9850:                         my $parent = $cats[0][$i];
 9851:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 9852:                         my $item = &escape($parent).'::0';
 9853:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 9854:                         $lastidx = $idx{$item};
 9855:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 9856:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 9857:                         for (my $k=0; $k<=$maxnum; $k++) {
 9858:                             my $vpos = $k+1;
 9859:                             my $selstr;
 9860:                             if ($k == $i) {
 9861:                                 $selstr = ' selected="selected" ';
 9862:                             }
 9863:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 9864:                         }
 9865:                         $datatable .= '</select></span></td><td>';
 9866:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
 9867:                             $datatable .=  '<span class="LC_nobreak">'
 9868:                                            .$default_names{$parent}.'</span>';
 9869:                             if ($parent eq 'instcode') {
 9870:                                 $datatable .= '<br /><span class="LC_nobreak">('
 9871:                                               .&mt('with institutional codes')
 9872:                                               .')</span></td><td'.$colattrib.'>';
 9873:                             } else {
 9874:                                 $datatable .= '<table><tr><td>';
 9875:                             }
 9876:                             $datatable .= '<span class="LC_nobreak">'
 9877:                                           .'<label><input type="radio" name="'
 9878:                                           .$parent.'" value="1" checked="checked" />'
 9879:                                           .&mt('Display').'</label>';
 9880:                             if ($parent eq 'instcode') {
 9881:                                 $datatable .= '&nbsp;';
 9882:                             } else {
 9883:                                 $datatable .= '</span></td></tr><tr><td>'
 9884:                                               .'<span class="LC_nobreak">';
 9885:                             }
 9886:                             $datatable .= '<label><input type="radio" name="'
 9887:                                           .$parent.'" value="0" />'
 9888:                                           .&mt('Do not display').'</label></span>';
 9889:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
 9890:                                 $datatable .= '</td></tr></table>';
 9891:                             }
 9892:                             $datatable .= '</td>';
 9893:                         } else {
 9894:                             $datatable .= $parent
 9895:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 9896:                                           .'<input type="checkbox" name="deletecategory" '
 9897:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 9898:                         }
 9899:                         my $depth = 1;
 9900:                         push(@path,$parent);
 9901:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 9902:                         pop(@path);
 9903:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 9904:                         $itemcount ++;
 9905:                     }
 9906:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 9907:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 9908:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 9909:                     for (my $k=0; $k<=$maxnum; $k++) {
 9910:                         my $vpos = $k+1;
 9911:                         my $selstr;
 9912:                         if ($k == $numtop) {
 9913:                             $selstr = ' selected="selected" ';
 9914:                         }
 9915:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 9916:                     }
 9917:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 9918:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 9919:                                   .'</tr>'."\n";
 9920:                     $itemcount ++;
 9921:                     foreach my $default ('instcode','communities','placement') {
 9922:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 9923:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 9924:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 9925:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 9926:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 9927:                             for (my $k=0; $k<=$maxnum; $k++) {
 9928:                                 my $vpos = $k+1;
 9929:                                 my $selstr;
 9930:                                 if ($k == $maxnum) {
 9931:                                     $selstr = ' selected="selected" ';
 9932:                                 }
 9933:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 9934:                             }
 9935:                             $datatable .= '</select></span></td>'.
 9936:                                           '<td><span class="LC_nobreak">'.
 9937:                                           $default_names{$default}.'</span>';
 9938:                             if ($default eq 'instcode') {
 9939:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 9940:                                               .&mt('with institutional codes').')</span>';
 9941:                             }
 9942:                             $datatable .= '</td>'
 9943:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 9944:                                           .&mt('Display').'</label>&nbsp;'
 9945:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 9946:                                           .&mt('Do not display').'</label></span></td></tr>';
 9947:                         }
 9948:                     }
 9949:                 }
 9950:             } else {
 9951:                 $datatable .= &initialize_categories($itemcount);
 9952:             }
 9953:         } else {
 9954:             $datatable .= '<tr><td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td></tr>'
 9955:                           .&initialize_categories($itemcount);
 9956:         }
 9957:         $$rowtotal += $itemcount;
 9958:     }
 9959:     return $datatable;
 9960: }
 9961: 
 9962: sub print_serverstatuses {
 9963:     my ($dom,$settings,$rowtotal) = @_;
 9964:     my $datatable;
 9965:     my @pages = &serverstatus_pages();
 9966:     my (%namedaccess,%machineaccess);
 9967:     foreach my $type (@pages) {
 9968:         $namedaccess{$type} = '';
 9969:         $machineaccess{$type}= '';
 9970:     }
 9971:     if (ref($settings) eq 'HASH') {
 9972:         foreach my $type (@pages) {
 9973:             if (exists($settings->{$type})) {
 9974:                 if (ref($settings->{$type}) eq 'HASH') {
 9975:                     foreach my $key (keys(%{$settings->{$type}})) {
 9976:                         if ($key eq 'namedusers') {
 9977:                             $namedaccess{$type} = $settings->{$type}->{$key};
 9978:                         } elsif ($key eq 'machines') {
 9979:                             $machineaccess{$type} = $settings->{$type}->{$key};
 9980:                         }
 9981:                     }
 9982:                 }
 9983:             }
 9984:         }
 9985:     }
 9986:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 9987:     my $rownum = 0;
 9988:     my $css_class;
 9989:     foreach my $type (@pages) {
 9990:         $rownum ++;
 9991:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 9992:         $datatable .= '<tr'.$css_class.'>'.
 9993:                       '<td><span class="LC_nobreak">'.
 9994:                       $titles->{$type}.'</span></td>'.
 9995:                       '<td class="LC_left_item">'.
 9996:                       '<input type="text" name="'.$type.'_namedusers" '.
 9997:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 9998:                       '<td class="LC_right_item">'.
 9999:                       '<span class="LC_nobreak">'.
10000:                       '<input type="text" name="'.$type.'_machines" '.
10001:                       'value="'.$machineaccess{$type}.'" size="10" />'.
10002:                       '</span></td></tr>'."\n";
10003:     }
10004:     $$rowtotal += $rownum;
10005:     return $datatable;
10006: }
10007: 
10008: sub serverstatus_pages {
10009:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
10010:             'checksums','clusterstatus','certstatus','metadata_keywords',
10011:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
10012:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
10013: }
10014: 
10015: sub defaults_javascript {
10016:     my ($settings) = @_;
10017:     return unless (ref($settings) eq 'HASH');
10018:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
10019:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
10020:         if ($maxnum eq '') {
10021:             $maxnum = 0;
10022:         }
10023:         $maxnum ++;
10024:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
10025:         return <<"ENDSCRIPT";
10026: <script type="text/javascript">
10027: // <![CDATA[
10028: function reorderTypes(form,caller) {
10029:     var changedVal;
10030: $jstext 
10031:     var newpos = 'addinststatus_pos';
10032:     var current = new Array;
10033:     var maxh = $maxnum;
10034:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
10035:     var oldVal;
10036:     if (caller == newpos) {
10037:         changedVal = newitemVal;
10038:     } else {
10039:         var curritem = 'inststatus_pos_'+caller;
10040:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
10041:         current[newitemVal] = newpos;
10042:     }
10043:     for (var i=0; i<inststatuses.length; i++) {
10044:         if (inststatuses[i] != caller) {
10045:             var elementName = 'inststatus_pos_'+inststatuses[i];
10046:             if (form.elements[elementName]) {
10047:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
10048:                 current[currVal] = elementName;
10049:             }
10050:         }
10051:     }
10052:     for (var j=0; j<maxh; j++) {
10053:         if (current[j] == undefined) {
10054:             oldVal = j;
10055:         }
10056:     }
10057:     if (oldVal < changedVal) {
10058:         for (var k=oldVal+1; k<=changedVal ; k++) {
10059:            var elementName = current[k];
10060:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
10061:         }
10062:     } else {
10063:         for (var k=changedVal; k<oldVal; k++) {
10064:             var elementName = current[k];
10065:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
10066:         }
10067:     }
10068:     return;
10069: }
10070: 
10071: // ]]>
10072: </script>
10073: 
10074: ENDSCRIPT
10075:     }
10076:     return;
10077: }
10078: 
10079: sub passwords_javascript {
10080:     my %intalert = &Apache::lonlocal::texthash (
10081:         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.',
10082:         authcost => 'Warning: bcrypt encryption cost for internal authentication must be an integer.',
10083:         passmin => 'Warning: minimum password length must be a positive integer greater than 6.',
10084:         passmax => 'Warning: maximum password length must be a positive integer (or blank).',
10085:         passexp => 'Warning: days before password expiration must be a positive integer (or blank).',
10086:         passnum => 'Warning: number of previous passwords to save must be a positive integer (or blank).',
10087:     );
10088:     &js_escape(\%intalert);
10089:     my $defmin = $Apache::lonnet::passwdmin;
10090:     my $intauthjs = <<"ENDSCRIPT";
10091: 
10092: function warnIntAuth(field) {
10093:     if (field.name == 'intauth_check') {
10094:         if (field.value == '2') {
10095:             alert('$intalert{authcheck}');
10096:         }
10097:     }
10098:     if (field.name == 'intauth_cost') {
10099:         field.value.replace(/\s/g,'');
10100:         if (field.value != '') {
10101:             var regexdigit=/^\\d+\$/;
10102:             if (!regexdigit.test(field.value)) {
10103:                 alert('$intalert{authcost}');
10104:             }
10105:         }
10106:     }
10107:     return;
10108: }
10109: 
10110: function warnIntPass(field) {
10111:     field.value.replace(/^\s+/,'');
10112:     field.value.replace(/\s+\$/,'');
10113:     var regexdigit=/^\\d+\$/;
10114:     if (field.name == 'passwords_min') {
10115:         if (field.value == '') {
10116:             alert('$intalert{passmin}');
10117:             field.value = '$defmin';
10118:         } else {
10119:             if (!regexdigit.test(field.value)) {
10120:                 alert('$intalert{passmin}');
10121:                 field.value = '$defmin';
10122:             }
10123:             var minval = parseInt(field.value,10);
10124:             if (minval < $defmin) {
10125:                 alert('$intalert{passmin}');
10126:                 field.value = '$defmin';
10127:             }
10128:         }
10129:     } else {
10130:         if (field.value == '0') {
10131:             field.value = '';
10132:         }
10133:         if (field.value != '') {
10134:             if (field.name == 'passwords_expire') {
10135:                 var regexpposnum=/^\\d+(|\\.\\d*)\$/; 
10136:                 if (!regexpposnum.test(field.value)) {
10137:                     alert('$intalert{passexp}');
10138:                     field.value = '';
10139:                 } else {
10140:                     var expval = parseFloat(field.value);
10141:                     if (expval == 0) {
10142:                         alert('$intalert{passexp}');
10143:                         field.value = '';
10144:                     }
10145:                 }
10146:             } else {
10147:                 if (!regexdigit.test(field.value)) {
10148:                     if (field.name == 'passwords_max') {
10149:                         alert('$intalert{passmax}');
10150:                     } else {
10151:                         if (field.name == 'passwords_numsaved') {
10152:                             alert('$intalert{passnum}');
10153:                         }
10154:                     }
10155:                     field.value = '';
10156:                 }
10157:             }
10158:         }
10159:     }
10160:     return;
10161: }
10162: 
10163: ENDSCRIPT
10164:     return &Apache::lonhtmlcommon::scripttag($intauthjs);
10165: }
10166: 
10167: sub coursecategories_javascript {
10168:     my ($settings) = @_;
10169:     my ($output,$jstext,$cathash);
10170:     if (ref($settings) eq 'HASH') {
10171:         $cathash = $settings->{'cats'};
10172:     }
10173:     if (ref($cathash) eq 'HASH') {
10174:         my (@cats,@jsarray,%idx);
10175:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
10176:         if (@jsarray > 0) {
10177:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
10178:             for (my $i=0; $i<@jsarray; $i++) {
10179:                 if (ref($jsarray[$i]) eq 'ARRAY') {
10180:                     my $catstr = join('","',@{$jsarray[$i]});
10181:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
10182:                 }
10183:             }
10184:         }
10185:     } else {
10186:         $jstext  = '    var categories = Array(1);'."\n".
10187:                    '    categories[0] = Array("instcode_pos");'."\n"; 
10188:     }
10189:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
10190:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
10191:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
10192:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
10193:     &js_escape(\$instcode_reserved);
10194:     &js_escape(\$communities_reserved);
10195:     &js_escape(\$placement_reserved);
10196:     &js_escape(\$choose_again);
10197:     $output = <<"ENDSCRIPT";
10198: <script type="text/javascript">
10199: // <![CDATA[
10200: function reorderCats(form,parent,item,idx) {
10201:     var changedVal;
10202: $jstext
10203:     var newpos = 'addcategory_pos';
10204:     if (parent == '') {
10205:         var has_instcode = 0;
10206:         var maxtop = categories[idx].length;
10207:         for (var j=0; j<maxtop; j++) {
10208:             if (categories[idx][j] == 'instcode::0') {
10209:                 has_instcode == 1;
10210:             }
10211:         }
10212:         if (has_instcode == 0) {
10213:             categories[idx][maxtop] = 'instcode_pos';
10214:         }
10215:     } else {
10216:         newpos += '_'+parent;
10217:     }
10218:     var maxh = 1 + categories[idx].length;
10219:     var current = new Array;
10220:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
10221:     if (item == newpos) {
10222:         changedVal = newitemVal;
10223:     } else {
10224:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
10225:         current[newitemVal] = newpos;
10226:     }
10227:     for (var i=0; i<categories[idx].length; i++) {
10228:         var elementName = categories[idx][i];
10229:         if (elementName != item) {
10230:             if (form.elements[elementName]) {
10231:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
10232:                 current[currVal] = elementName;
10233:             }
10234:         }
10235:     }
10236:     var oldVal;
10237:     for (var j=0; j<maxh; j++) {
10238:         if (current[j] == undefined) {
10239:             oldVal = j;
10240:         }
10241:     }
10242:     if (oldVal < changedVal) {
10243:         for (var k=oldVal+1; k<=changedVal ; k++) {
10244:            var elementName = current[k];
10245:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
10246:         }
10247:     } else {
10248:         for (var k=changedVal; k<oldVal; k++) {
10249:             var elementName = current[k];
10250:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
10251:         }
10252:     }
10253:     return;
10254: }
10255: 
10256: function categoryCheck(form) {
10257:     if (form.elements['addcategory_name'].value == 'instcode') {
10258:         alert('$instcode_reserved\\n$choose_again');
10259:         return false;
10260:     }
10261:     if (form.elements['addcategory_name'].value == 'communities') {
10262:         alert('$communities_reserved\\n$choose_again');
10263:         return false;
10264:     }
10265:     if (form.elements['addcategory_name'].value == 'placement') {
10266:         alert('$placement_reserved\\n$choose_again');
10267:         return false;
10268:     }
10269:     return true;
10270: }
10271: 
10272: // ]]>
10273: </script>
10274: 
10275: ENDSCRIPT
10276:     return $output;
10277: }
10278: 
10279: sub initialize_categories {
10280:     my ($itemcount) = @_;
10281:     my ($datatable,$css_class,$chgstr);
10282:     my %default_names = (
10283:                       instcode    => 'Official courses (with institutional codes)',
10284:                       communities => 'Communities',
10285:                       placement   => 'Placement Tests',
10286:                         );
10287:     my %selnum = (
10288:                    instcode    => '0',
10289:                    communities => '1',
10290:                    placement   => '2',
10291:                  );
10292:     my %selected;
10293:     foreach my $default ('instcode','communities','placement') {
10294:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
10295:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','0'".');"';
10296:         map { $selected{$selnum{$_}} = '' } keys(%selnum);
10297:         $selected{$selnum{$default}} = ' selected="selected"';
10298:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
10299:                      .'<select name="'.$default.'_pos"'.$chgstr.'>'
10300:                      .'<option value="0"'.$selected{'0'}.'>1</option>'
10301:                      .'<option value="1"'.$selected{'1'}.'>2</option>'
10302:                      .'<option value="2"'.$selected{'2'}.'>3</option>'
10303:                      .'<option value="3">4</option></select>&nbsp;'
10304:                      .$default_names{$default}
10305:                      .'</span></td><td><span class="LC_nobreak">'
10306:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
10307:                      .&mt('Display').'</label>&nbsp;<label>'
10308:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
10309:                  .'</label></span></td></tr>';
10310:         $itemcount ++;
10311:     }
10312:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
10313:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
10314:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
10315:                   .'<select name="addcategory_pos"'.$chgstr.'>'
10316:                   .'<option value="0">1</option>'
10317:                   .'<option value="1">2</option>'
10318:                   .'<option value="2">3</option>'
10319:                   .'<option value="3" selected="selected">4</option></select>&nbsp;'
10320:                   .&mt('Add category').'</span></td><td><span class="LC_nobreak">'.&mt('Name:')
10321:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></span>'
10322:                   .'</td></tr>';
10323:     return $datatable;
10324: }
10325: 
10326: sub build_category_rows {
10327:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
10328:     my ($text,$name,$item,$chgstr);
10329:     if (ref($cats) eq 'ARRAY') {
10330:         my $maxdepth = scalar(@{$cats});
10331:         if (ref($cats->[$depth]) eq 'HASH') {
10332:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
10333:                 my $numchildren = @{$cats->[$depth]{$parent}};
10334:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
10335:                 $text .= '<td><table class="LC_data_table">';
10336:                 my ($idxnum,$parent_name,$parent_item);
10337:                 my $higher = $depth - 1;
10338:                 if ($higher == 0) {
10339:                     $parent_name = &escape($parent).'::'.$higher;
10340:                 } else {
10341:                     if (ref($path) eq 'ARRAY') {
10342:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
10343:                     }
10344:                 }
10345:                 $parent_item = 'addcategory_pos_'.$parent_name;
10346:                 for (my $j=0; $j<=$numchildren; $j++) {
10347:                     if ($j < $numchildren) {
10348:                         $name = $cats->[$depth]{$parent}[$j];
10349:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
10350:                         $idxnum = $idx->{$item};
10351:                     } else {
10352:                         $name = $parent_name;
10353:                         $item = $parent_item;
10354:                     }
10355:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
10356:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
10357:                     for (my $i=0; $i<=$numchildren; $i++) {
10358:                         my $vpos = $i+1;
10359:                         my $selstr;
10360:                         if ($j == $i) {
10361:                             $selstr = ' selected="selected" ';
10362:                         }
10363:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
10364:                     }
10365:                     $text .= '</select>&nbsp;';
10366:                     if ($j < $numchildren) {
10367:                         my $deeper = $depth+1;
10368:                         $text .= $name.'&nbsp;'
10369:                                  .'<label><input type="checkbox" name="deletecategory" value="'
10370:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
10371:                         if(ref($path) eq 'ARRAY') {
10372:                             push(@{$path},$name);
10373:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
10374:                             pop(@{$path});
10375:                         }
10376:                     } else {
10377:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="text" size="20" name="addcategory_name_';
10378:                         if ($j == $numchildren) {
10379:                             $text .= $name;
10380:                         } else {
10381:                             $text .= $item;
10382:                         }
10383:                         $text .= '" value="" />';
10384:                     }
10385:                     $text .= '</td></tr>';
10386:                 }
10387:                 $text .= '</table></td>';
10388:             } else {
10389:                 my $higher = $depth-1;
10390:                 if ($higher == 0) {
10391:                     $name = &escape($parent).'::'.$higher;
10392:                 } else {
10393:                     if (ref($path) eq 'ARRAY') {
10394:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
10395:                     }
10396:                 }
10397:                 my $colspan;
10398:                 if ($parent ne 'instcode') {
10399:                     $colspan = $maxdepth - $depth - 1;
10400:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="text" size="20" name="subcat_'.$name.'" value="" /></td>';
10401:                 }
10402:             }
10403:         }
10404:     }
10405:     return $text;
10406: }
10407: 
10408: sub modifiable_userdata_row {
10409:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
10410:         $rowid,$customcss,$rowstyle,$itemdesc) = @_;
10411:     my ($role,$rolename,$statustype);
10412:     $role = $item;
10413:     if ($context eq 'cancreate') {
10414:         if ($item =~ /^(emailusername)_(.+)$/) {
10415:             $role = $1;
10416:             $statustype = $2;
10417:             if (ref($usertypes) eq 'HASH') {
10418:                 if ($usertypes->{$statustype}) {
10419:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
10420:                 } else {
10421:                     $rolename = &mt('Data provided by user');
10422:                 }
10423:             }
10424:         }
10425:     } elsif ($context eq 'selfcreate') {
10426:         if (ref($usertypes) eq 'HASH') {
10427:             $rolename = $usertypes->{$role};
10428:         } else {
10429:             $rolename = $role;
10430:         }
10431:     } elsif ($context eq 'lti') {
10432:         $rolename = &mt('Institutional data used (if available)');
10433:     } elsif ($context eq 'privacy') {
10434:         $rolename = $itemdesc;
10435:     } else {
10436:         if ($role eq 'cr') {
10437:             $rolename = &mt('Custom role');
10438:         } else {
10439:             $rolename = &Apache::lonnet::plaintext($role);
10440:         }
10441:     }
10442:     my (@fields,%fieldtitles);
10443:     if (ref($fieldsref) eq 'ARRAY') {
10444:         @fields = @{$fieldsref};
10445:     } else {
10446:         @fields = ('lastname','firstname','middlename','generation',
10447:                    'permanentemail','id');
10448:     }
10449:     if ((ref($titlesref) eq 'HASH')) {
10450:         %fieldtitles = %{$titlesref};
10451:     } else {
10452:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
10453:     }
10454:     my $output;
10455:     my $css_class;
10456:     if ($rowcount%2) {
10457:         $css_class = 'LC_odd_row';
10458:     }
10459:     if ($customcss) {
10460:         $css_class .= " $customcss";
10461:     }
10462:     $css_class =~ s/^\s+//;
10463:     if ($css_class) {
10464:         $css_class = ' class="'.$css_class.'"';
10465:     }
10466:     if ($rowstyle) {
10467:         $css_class .= ' style="'.$rowstyle.'"';
10468:     }
10469:     if ($rowid) {
10470:         $rowid = ' id="'.$rowid.'"';
10471:     }
10472:     $output = '<tr '.$css_class.$rowid.'>'.
10473:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
10474:               '<td class="LC_left_item" colspan="2"><table>';
10475:     my $rem;
10476:     my %checks;
10477:     my %current;
10478:     if (ref($settings) eq 'HASH') {
10479:         my $hashref;
10480:         if ($context eq 'lti') {
10481:             if (ref($settings) eq 'HASH') {
10482:                 $hashref = $settings->{'instdata'};
10483:             }
10484:         } elsif ($context eq 'privacy') {
10485:             my ($key,$inner) = split(/_/,$role);
10486:             if (ref($settings) eq 'HASH') {
10487:                 if (ref($settings->{$key}) eq 'HASH') {
10488:                     $hashref = $settings->{$key}->{$inner};
10489:                 }
10490:             }
10491:         } elsif (ref($settings->{$context}) eq 'HASH') {
10492:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
10493:                 $hashref = $settings->{'lti_instdata'};
10494:             }
10495:             if ($role eq 'emailusername') {
10496:                 if ($statustype) {
10497:                     if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
10498:                         $hashref = $settings->{$context}->{$role}->{$statustype};
10499:                     }
10500:                 }
10501:             }
10502:         }
10503:         if (ref($hashref) eq 'HASH') { 
10504:             foreach my $field (@fields) {
10505:                 if ($hashref->{$field}) {
10506:                     if ($role eq 'emailusername') {
10507:                         $checks{$field} = $hashref->{$field};
10508:                     } else {
10509:                         $checks{$field} = ' checked="checked" ';
10510:                     }
10511:                 }
10512:             }
10513:         }
10514:     }
10515:  
10516:     my $total = scalar(@fields);
10517:     for (my $i=0; $i<$total; $i++) {
10518:         $rem = $i%($numinrow);
10519:         if ($rem == 0) {
10520:             if ($i > 0) {
10521:                 $output .= '</tr>';
10522:             }
10523:             $output .= '<tr>';
10524:         }
10525:         my $check = ' ';
10526:         unless ($role eq 'emailusername') {
10527:             if (exists($checks{$fields[$i]})) {
10528:                 $check = $checks{$fields[$i]};
10529:             } elsif ($context eq 'privacy') {
10530:                 if ($role =~ /^priv_(domain|course)$/) {
10531:                     if (ref($settings) ne 'HASH') {
10532:                         $check = ' checked="checked" ';
10533:                     }
10534:                 } elsif ($role =~ /^priv_(author|community)$/) {
10535:                     if (ref($settings) ne 'HASH') {
10536:                         unless ($fields[$i] eq 'id') {
10537:                             $check = ' checked="checked" ';
10538:                         }
10539:                     }
10540:                 } elsif ($role =~ /^(unpriv|othdom)_/) {
10541:                     if (ref($settings) ne 'HASH') {
10542:                         if (($fields[$i] eq 'lastname') || ($fields[$i] eq 'firstname')) {
10543:                             $check = ' checked="checked" ';
10544:                         }
10545:                     }
10546:                 }
10547:             } elsif ($context ne 'lti') {
10548:                 if ($role eq 'st') {
10549:                     if (ref($settings) ne 'HASH') {
10550:                         $check = ' checked="checked" '; 
10551:                     }
10552:                 }
10553:             }
10554:         }
10555:         $output .= '<td class="LC_left_item">'.
10556:                    '<span class="LC_nobreak">';
10557:         my $prefix = 'canmodify';
10558:         if ($role eq 'emailusername') {
10559:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
10560:                 $checks{$fields[$i]} = 'omit';
10561:             }
10562:             foreach my $option ('required','optional','omit') {
10563:                 my $checked='';
10564:                 if ($checks{$fields[$i]} eq $option) {
10565:                     $checked='checked="checked" ';
10566:                 }
10567:                 $output .= '<label>'.
10568:                            '<input type="radio" name="'.$prefix.'_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
10569:                            &mt($option).'</label>'.('&nbsp;' x2);
10570:             }
10571:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
10572:         } else {
10573:             if ($context eq 'lti') {
10574:                 $prefix = 'lti';
10575:             } elsif ($context eq 'privacy') {
10576:                 $prefix = 'privacy';
10577:             }
10578:             $output .= '<label>'.
10579:                        '<input type="checkbox" name="'.$prefix.'_'.$role.'" '.
10580:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
10581:                        '</label>';
10582:         }
10583:         $output .= '</span></td>';
10584:     }
10585:     $rem = $total%$numinrow;
10586:     my $colsleft;
10587:     if ($rem) {
10588:         $colsleft = $numinrow - $rem;
10589:     }
10590:     if ($colsleft > 1) {
10591:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
10592:                    '&nbsp;</td>';
10593:     } elsif ($colsleft == 1) {
10594:         $output .= '<td class="LC_left_item">&nbsp;</td>';
10595:     }
10596:     $output .= '</tr></table></td></tr>';
10597:     return $output;
10598: }
10599: 
10600: sub insttypes_row {
10601:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
10602:         $customcss,$rowstyle) = @_;
10603:     my %lt = &Apache::lonlocal::texthash (
10604:                       cansearch => 'Users allowed to search',
10605:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
10606:                       lockablenames => 'User preference to lock name',
10607:                       selfassign    => 'Self-reportable affiliations',
10608:                       overrides     => "Override domain's helpdesk settings based on requester's affiliation",
10609:              );
10610:     my $showdom;
10611:     if ($context eq 'cansearch') {
10612:         $showdom = ' ('.$dom.')';
10613:     }
10614:     my $class = 'LC_left_item';
10615:     if ($context eq 'statustocreate') {
10616:         $class = 'LC_right_item';
10617:     }
10618:     my $css_class;
10619:     if ($$rowtotal%2) {
10620:         $css_class = 'LC_odd_row';
10621:     }
10622:     if ($customcss) {
10623:         $css_class .= ' '.$customcss;
10624:     }
10625:     $css_class =~ s/^\s+//;
10626:     if ($css_class) {
10627:         $css_class = ' class="'.$css_class.'"';
10628:     }
10629:     if ($rowstyle) {
10630:         $css_class .= ' style="'.$rowstyle.'"';
10631:     }
10632:     if ($onclick) {
10633:         $onclick = 'onclick="'.$onclick.'" ';
10634:     }
10635:     my $output = '<tr'.$css_class.'>'.
10636:                  '<td>'.$lt{$context}.$showdom.
10637:                  '</td><td class="'.$class.'" colspan="2"><table>';
10638:     my $rem;
10639:     if (ref($types) eq 'ARRAY') {
10640:         for (my $i=0; $i<@{$types}; $i++) {
10641:             if (defined($usertypes->{$types->[$i]})) {
10642:                 my $rem = $i%($numinrow);
10643:                 if ($rem == 0) {
10644:                     if ($i > 0) {
10645:                         $output .= '</tr>';
10646:                     }
10647:                     $output .= '<tr>';
10648:                 }
10649:                 my $check = ' ';
10650:                 if (ref($settings) eq 'HASH') {
10651:                     if (ref($settings->{$context}) eq 'ARRAY') {
10652:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
10653:                             $check = ' checked="checked" ';
10654:                         }
10655:                     } elsif (ref($settings->{$context}) eq 'HASH') {
10656:                         if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
10657:                             $check = ' checked="checked" ';
10658:                         }
10659:                     } elsif ($context eq 'statustocreate') {
10660:                         $check = ' checked="checked" ';
10661:                     }
10662:                 }
10663:                 $output .= '<td class="LC_left_item">'.
10664:                            '<span class="LC_nobreak"><label>'.
10665:                            '<input type="checkbox" name="'.$context.'" '.
10666:                            'value="'.$types->[$i].'"'.$check.$onclick.' />'.
10667:                            $usertypes->{$types->[$i]}.'</label></span></td>';
10668:             }
10669:         }
10670:         $rem = @{$types}%($numinrow);
10671:     }
10672:     my $colsleft = $numinrow - $rem;
10673:     if ($context eq 'overrides') {
10674:         if ($colsleft > 1) {
10675:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
10676:         } else {
10677:             $output .= '<td class="LC_left_item">';
10678:         }
10679:         $output .= '&nbsp;';  
10680:     } else {
10681:         if ($rem == 0) {
10682:             $output .= '<tr>';
10683:         }
10684:         if ($colsleft > 1) {
10685:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
10686:         } else {
10687:             $output .= '<td class="LC_left_item">';
10688:         }
10689:         my $defcheck = ' ';
10690:         if (ref($settings) eq 'HASH') {  
10691:             if (ref($settings->{$context}) eq 'ARRAY') {
10692:                 if (grep(/^default$/,@{$settings->{$context}})) {
10693:                     $defcheck = ' checked="checked" ';
10694:                 }
10695:             } elsif ($context eq 'statustocreate') {
10696:                 $defcheck = ' checked="checked" ';
10697:             }
10698:         }
10699:         $output .= '<span class="LC_nobreak"><label>'.
10700:                    '<input type="checkbox" name="'.$context.'" '.
10701:                    'value="default"'.$defcheck.$onclick.' />'.
10702:                    $othertitle.'</label></span>';
10703:     }
10704:     $output .= '</td></tr></table></td></tr>';
10705:     return $output;
10706: }
10707: 
10708: sub sorted_searchtitles {
10709:     my %searchtitles = &Apache::lonlocal::texthash(
10710:                          'uname' => 'username',
10711:                          'lastname' => 'last name',
10712:                          'lastfirst' => 'last name, first name',
10713:                      );
10714:     my @titleorder = ('uname','lastname','lastfirst');
10715:     return (\%searchtitles,\@titleorder);
10716: }
10717: 
10718: sub sorted_searchtypes {
10719:     my %srchtypes_desc = (
10720:                            exact    => 'is exact match',
10721:                            contains => 'contains ..',
10722:                            begins   => 'begins with ..',
10723:                          );
10724:     my @srchtypeorder = ('exact','begins','contains');
10725:     return (\%srchtypes_desc,\@srchtypeorder);
10726: }
10727: 
10728: sub usertype_update_row {
10729:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
10730:     my $datatable;
10731:     my $numinrow = 4;
10732:     foreach my $type (@{$types}) {
10733:         if (defined($usertypes->{$type})) {
10734:             $$rownums ++;
10735:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
10736:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
10737:                           '</td><td class="LC_left_item"><table>';
10738:             for (my $i=0; $i<@{$fields}; $i++) {
10739:                 my $rem = $i%($numinrow);
10740:                 if ($rem == 0) {
10741:                     if ($i > 0) {
10742:                         $datatable .= '</tr>';
10743:                     }
10744:                     $datatable .= '<tr>';
10745:                 }
10746:                 my $check = ' ';
10747:                 if (ref($settings) eq 'HASH') {
10748:                     if (ref($settings->{'fields'}) eq 'HASH') {
10749:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
10750:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
10751:                                 $check = ' checked="checked" ';
10752:                             }
10753:                         }
10754:                     }
10755:                 }
10756: 
10757:                 if ($i == @{$fields}-1) {
10758:                     my $colsleft = $numinrow - $rem;
10759:                     if ($colsleft > 1) {
10760:                         $datatable .= '<td colspan="'.$colsleft.'">';
10761:                     } else {
10762:                         $datatable .= '<td>';
10763:                     }
10764:                 } else {
10765:                     $datatable .= '<td>';
10766:                 }
10767:                 $datatable .= '<span class="LC_nobreak"><label>'.
10768:                               '<input type="checkbox" name="updateable_'.$type.
10769:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
10770:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
10771:             }
10772:             $datatable .= '</tr></table></td></tr>';
10773:         }
10774:     }
10775:     return $datatable;
10776: }
10777: 
10778: sub modify_login {
10779:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
10780:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
10781:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
10782:     %title = ( coursecatalog => 'Display course catalog',
10783:                adminmail => 'Display administrator E-mail address',
10784:                helpdesk  => 'Display "Contact Helpdesk" link',
10785:                newuser => 'Link for visitors to create a user account',
10786:                loginheader => 'Log-in box header');
10787:     @offon = ('off','on');
10788:     if (ref($domconfig{login}) eq 'HASH') {
10789:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
10790:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
10791:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
10792:             }
10793:         }
10794:     }
10795:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
10796:                                            \%domconfig,\%loginhash);
10797:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
10798:     foreach my $item (@toggles) {
10799:         $loginhash{login}{$item} = $env{'form.'.$item};
10800:     }
10801:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
10802:     if (ref($colchanges{'login'}) eq 'HASH') {  
10803:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
10804:                                          \%loginhash);
10805:     }
10806: 
10807:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
10808:     my %domservers = &Apache::lonnet::get_servers($dom);
10809:     my @loginvia_attribs = ('serverpath','custompath','exempt');
10810:     if (keys(%servers) > 1) {
10811:         foreach my $lonhost (keys(%servers)) {
10812:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
10813:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
10814:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
10815:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
10816:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
10817:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
10818:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
10819:                         $changes{'loginvia'}{$lonhost} = 1;
10820:                     } else {
10821:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
10822:                         $changes{'loginvia'}{$lonhost} = 1;
10823:                     }
10824:                 } else {
10825:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
10826:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
10827:                         $changes{'loginvia'}{$lonhost} = 1;
10828:                     }
10829:                 }
10830:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
10831:                     foreach my $item (@loginvia_attribs) {
10832:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
10833:                     }
10834:                 } else {
10835:                     foreach my $item (@loginvia_attribs) {
10836:                         my $new = $env{'form.'.$lonhost.'_'.$item};
10837:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
10838:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
10839:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
10840:                                 $new = '/';
10841:                             }
10842:                         }
10843:                         if (($item eq 'custompath') && 
10844:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
10845:                             $new = '';
10846:                         }
10847:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
10848:                             $changes{'loginvia'}{$lonhost} = 1;
10849:                         }
10850:                         if ($item eq 'exempt') {
10851:                             $new = &check_exempt_addresses($new);
10852:                         }
10853:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
10854:                     }
10855:                 }
10856:             } else {
10857:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
10858:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
10859:                     $changes{'loginvia'}{$lonhost} = 1;
10860:                     foreach my $item (@loginvia_attribs) {
10861:                         my $new = $env{'form.'.$lonhost.'_'.$item};
10862:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
10863:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
10864:                                 $new = '/';
10865:                             }
10866:                         }
10867:                         if (($item eq 'custompath') && 
10868:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
10869:                             $new = '';
10870:                         }
10871:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
10872:                     }
10873:                 }
10874:             }
10875:         }
10876:     }
10877: 
10878:     my $servadm = $r->dir_config('lonAdmEMail');
10879:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
10880:     if (ref($domconfig{'login'}) eq 'HASH') {
10881:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
10882:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
10883:                 if ($lang eq 'nolang') {
10884:                     push(@currlangs,$lang);
10885:                 } elsif (defined($langchoices{$lang})) {
10886:                     push(@currlangs,$lang);
10887:                 } else {
10888:                     next;
10889:                 }
10890:             }
10891:         }
10892:     }
10893:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
10894:     if (@currlangs > 0) {
10895:         foreach my $lang (@currlangs) {
10896:             if (grep(/^\Q$lang\E$/,@delurls)) {
10897:                 $changes{'helpurl'}{$lang} = 1;
10898:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
10899:                 $changes{'helpurl'}{$lang} = 1;
10900:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
10901:                 push(@newlangs,$lang);
10902:             } else {
10903:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
10904:             }
10905:         }
10906:     }
10907:     unless (grep(/^nolang$/,@currlangs)) {
10908:         if ($env{'form.loginhelpurl_nolang.filename'}) {
10909:             $changes{'helpurl'}{'nolang'} = 1;
10910:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
10911:             push(@newlangs,'nolang');
10912:         }
10913:     }
10914:     if ($env{'form.loginhelpurl_add_lang'}) {
10915:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
10916:             ($env{'form.loginhelpurl_add_file.filename'})) {
10917:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
10918:             $addedfile = $env{'form.loginhelpurl_add_lang'};
10919:         }
10920:     }
10921:     if ((@newlangs > 0) || ($addedfile)) {
10922:         my $error;
10923:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
10924:         if ($configuserok eq 'ok') {
10925:             if ($switchserver) {
10926:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
10927:             } elsif ($author_ok eq 'ok') {
10928:                 my @allnew = @newlangs;
10929:                 if ($addedfile ne '') {
10930:                     push(@allnew,$addedfile);
10931:                 }
10932:                 foreach my $lang (@allnew) {
10933:                     my $formelem = 'loginhelpurl_'.$lang;
10934:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
10935:                         $formelem = 'loginhelpurl_add_file';
10936:                     }
10937:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
10938:                                                                "help/$lang",'','',$newfile{$lang});
10939:                     if ($result eq 'ok') {
10940:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
10941:                         $changes{'helpurl'}{$lang} = 1;
10942:                     } else {
10943:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
10944:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
10945:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
10946:                             (!grep(/^\Q$lang\E$/,@delurls))) {
10947:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
10948:                         }
10949:                     }
10950:                 }
10951:             } else {
10952:                 $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);
10953:             }
10954:         } else {
10955:             $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);
10956:         }
10957:         if ($error) {
10958:             &Apache::lonnet::logthis($error);
10959:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10960:         }
10961:     }
10962: 
10963:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
10964:     if (ref($domconfig{'login'}) eq 'HASH') {
10965:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
10966:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
10967:                 if ($domservers{$lonhost}) {
10968:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
10969:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
10970:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
10971:                     }
10972:                 }
10973:             }
10974:         }
10975:     }
10976:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
10977:     foreach my $lonhost (sort(keys(%domservers))) {
10978:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
10979:             $changes{'headtag'}{$lonhost} = 1;
10980:         } else {
10981:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
10982:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
10983:             }
10984:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
10985:                 push(@newhosts,$lonhost);
10986:             } elsif ($currheadtagurls{$lonhost}) {
10987:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
10988:                 if ($currexempt{$lonhost}) {
10989:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
10990:                         $changes{'headtag'}{$lonhost} = 1;
10991:                     }
10992:                 } elsif ($possexempt{$lonhost}) {
10993:                     $changes{'headtag'}{$lonhost} = 1;
10994:                 }
10995:                 if ($possexempt{$lonhost}) {
10996:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
10997:                 }
10998:             }
10999:         }
11000:     }
11001:     if (@newhosts) {
11002:         my $error;
11003:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
11004:         if ($configuserok eq 'ok') {
11005:             if ($switchserver) {
11006:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
11007:             } elsif ($author_ok eq 'ok') {
11008:                 foreach my $lonhost (@newhosts) {
11009:                     my $formelem = 'loginheadtag_'.$lonhost;
11010:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
11011:                                                                           "login/headtag/$lonhost",'','',
11012:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
11013:                     if ($result eq 'ok') {
11014:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
11015:                         $changes{'headtag'}{$lonhost} = 1;
11016:                         if ($possexempt{$lonhost}) {
11017:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
11018:                         }
11019:                     } else {
11020:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
11021:                                            $newheadtagurls{$lonhost},$result);
11022:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
11023:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
11024:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
11025:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
11026:                         }
11027:                     }
11028:                 }
11029:             } else {
11030:                 $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);
11031:             }
11032:         } else {
11033:             $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);
11034:         }
11035:         if ($error) {
11036:             &Apache::lonnet::logthis($error);
11037:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11038:         }
11039:     }
11040:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
11041: 
11042:     my $defaulthelpfile = '/adm/loginproblems.html';
11043:     my $defaulttext = &mt('Default in use');
11044: 
11045:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
11046:                                              $dom);
11047:     if ($putresult eq 'ok') {
11048:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
11049:         my %defaultchecked = (
11050:                     'coursecatalog' => 'on',
11051:                     'helpdesk'      => 'on',
11052:                     'adminmail'     => 'off',
11053:                     'newuser'       => 'off',
11054:         );
11055:         if (ref($domconfig{'login'}) eq 'HASH') {
11056:             foreach my $item (@toggles) {
11057:                 if ($defaultchecked{$item} eq 'on') { 
11058:                     if (($domconfig{'login'}{$item} eq '0') &&
11059:                         ($env{'form.'.$item} eq '1')) {
11060:                         $changes{$item} = 1;
11061:                     } elsif (($domconfig{'login'}{$item} eq '' ||
11062:                               $domconfig{'login'}{$item} eq '1') &&
11063:                              ($env{'form.'.$item} eq '0')) {
11064:                         $changes{$item} = 1;
11065:                     }
11066:                 } elsif ($defaultchecked{$item} eq 'off') {
11067:                     if (($domconfig{'login'}{$item} eq '1') &&
11068:                         ($env{'form.'.$item} eq '0')) {
11069:                         $changes{$item} = 1;
11070:                     } elsif (($domconfig{'login'}{$item} eq '' ||
11071:                               $domconfig{'login'}{$item} eq '0') &&
11072:                              ($env{'form.'.$item} eq '1')) {
11073:                         $changes{$item} = 1;
11074:                     }
11075:                 }
11076:             }
11077:         }
11078:         if (keys(%changes) > 0 || $colchgtext) {
11079:             &Apache::loncommon::devalidate_domconfig_cache($dom);
11080:             if (ref($lastactref) eq 'HASH') {
11081:                 $lastactref->{'domainconfig'} = 1;
11082:             }
11083:             $resulttext = &mt('Changes made:').'<ul>';
11084:             foreach my $item (sort(keys(%changes))) {
11085:                 if ($item eq 'loginvia') {
11086:                     if (ref($changes{$item}) eq 'HASH') {
11087:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
11088:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
11089:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
11090:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
11091:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
11092:                                     $protocol = 'http' if ($protocol ne 'https');
11093:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
11094: 
11095:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
11096:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
11097:                                     } else {
11098:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
11099:                                     }
11100:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
11101:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
11102:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
11103:                                     }
11104:                                     $resulttext .= '</li>';
11105:                                 } else {
11106:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
11107:                                 }
11108:                             } else {
11109:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
11110:                             }
11111:                         }
11112:                         $resulttext .= '</ul></li>';
11113:                     }
11114:                 } elsif ($item eq 'helpurl') {
11115:                     if (ref($changes{$item}) eq 'HASH') {
11116:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
11117:                             if (grep(/^\Q$lang\E$/,@delurls)) {
11118:                                 my ($chg,$link);
11119:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
11120:                                 if ($lang eq 'nolang') {
11121:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
11122:                                 } else {
11123:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
11124:                                 }
11125:                                 $resulttext .= '<li>'.$chg.'</li>';
11126:                             } else {
11127:                                 my $chg;
11128:                                 if ($lang eq 'nolang') {
11129:                                     $chg = &mt('custom log-in help file for no preferred language');
11130:                                 } else {
11131:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
11132:                                 }
11133:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
11134:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
11135:                                                       '?inhibitmenu=yes',$chg,600,500).
11136:                                                '</li>';
11137:                             }
11138:                         }
11139:                     }
11140:                 } elsif ($item eq 'headtag') {
11141:                     if (ref($changes{$item}) eq 'HASH') {
11142:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
11143:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
11144:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
11145:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
11146:                                 $resulttext .= '<li><a href="'.
11147:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
11148:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
11149:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
11150:                                 if ($possexempt{$lonhost}) {
11151:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
11152:                                 } else {
11153:                                     $resulttext .= &mt('included for any client IP');
11154:                                 }
11155:                                 $resulttext .= '</li>';
11156:                             }
11157:                         }
11158:                     }
11159:                 } elsif ($item eq 'captcha') {
11160:                     if (ref($loginhash{'login'}) eq 'HASH') {
11161:                         my $chgtxt;
11162:                         if ($loginhash{'login'}{$item} eq 'notused') {
11163:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
11164:                         } else {
11165:                             my %captchas = &captcha_phrases();
11166:                             if ($captchas{$loginhash{'login'}{$item}}) {
11167:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
11168:                             } else {
11169:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
11170:                             }
11171:                         }
11172:                         $resulttext .= '<li>'.$chgtxt.'</li>';
11173:                     }
11174:                 } elsif ($item eq 'recaptchakeys') {
11175:                     if (ref($loginhash{'login'}) eq 'HASH') {
11176:                         my ($privkey,$pubkey);
11177:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
11178:                             $pubkey = $loginhash{'login'}{$item}{'public'};
11179:                             $privkey = $loginhash{'login'}{$item}{'private'};
11180:                         }
11181:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
11182:                         if (!$pubkey) {
11183:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
11184:                         } else {
11185:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
11186:                         }
11187:                         if (!$privkey) {
11188:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
11189:                         } else {
11190:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
11191:                         }
11192:                         $chgtxt .= '</ul>';
11193:                         $resulttext .= '<li>'.$chgtxt.'</li>';
11194:                     }
11195:                 } elsif ($item eq 'recaptchaversion') {
11196:                     if (ref($loginhash{'login'}) eq 'HASH') {
11197:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
11198:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
11199:                                            '</li>';
11200:                         }
11201:                     }
11202:                 } else {
11203:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
11204:                 }
11205:             }
11206:             $resulttext .= $colchgtext.'</ul>';
11207:         } else {
11208:             $resulttext = &mt('No changes made to log-in page settings');
11209:         }
11210:     } else {
11211:         $resulttext = '<span class="LC_error">'.
11212: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
11213:     }
11214:     if ($errors) {
11215:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
11216:                        $errors.'</ul>';
11217:     }
11218:     return $resulttext;
11219: }
11220: 
11221: sub check_exempt_addresses {
11222:     my ($iplist) = @_;
11223:     $iplist =~ s/^\s+//;
11224:     $iplist =~ s/\s+$//;
11225:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
11226:     my (@okips,$new);
11227:     foreach my $ip (@poss_ips) {
11228:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
11229:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
11230:                 push(@okips,$ip);
11231:             }
11232:         }
11233:     }
11234:     if (@okips > 0) {
11235:         $new = join(',',@okips);
11236:     } else {
11237:         $new = '';
11238:     }
11239:     return $new;
11240: }
11241: 
11242: sub color_font_choices {
11243:     my %choices =
11244:         &Apache::lonlocal::texthash (
11245:             img => "Header",
11246:             bgs => "Background colors",
11247:             links => "Link colors",
11248:             images => "Images",
11249:             font => "Font color",
11250:             fontmenu => "Font menu",
11251:             pgbg => "Page",
11252:             tabbg => "Header",
11253:             sidebg => "Border",
11254:             link => "Link",
11255:             alink => "Active link",
11256:             vlink => "Visited link",
11257:         );
11258:     return %choices;
11259: }
11260: 
11261: sub modify_rolecolors {
11262:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
11263:     my ($resulttext,%rolehash);
11264:     $rolehash{'rolecolors'} = {};
11265:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
11266:         if ($domconfig{'rolecolors'} eq '') {
11267:             $domconfig{'rolecolors'} = {};
11268:         }
11269:     }
11270:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
11271:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
11272:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
11273:                                              $dom);
11274:     if ($putresult eq 'ok') {
11275:         if (keys(%changes) > 0) {
11276:             &Apache::loncommon::devalidate_domconfig_cache($dom);
11277:             if (ref($lastactref) eq 'HASH') {
11278:                 $lastactref->{'domainconfig'} = 1;
11279:             }
11280:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
11281:                                              $rolehash{'rolecolors'});
11282:         } else {
11283:             $resulttext = &mt('No changes made to default color schemes');
11284:         }
11285:     } else {
11286:         $resulttext = '<span class="LC_error">'.
11287: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
11288:     }
11289:     if ($errors) {
11290:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
11291:                        $errors.'</ul>';
11292:     }
11293:     return $resulttext;
11294: }
11295: 
11296: sub modify_colors {
11297:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
11298:     my (%changes,%choices);
11299:     my @bgs;
11300:     my @links = ('link','alink','vlink');
11301:     my @logintext;
11302:     my @images;
11303:     my $servadm = $r->dir_config('lonAdmEMail');
11304:     my $errors;
11305:     my %defaults;
11306:     foreach my $role (@{$roles}) {
11307:         if ($role eq 'login') {
11308:             %choices = &login_choices();
11309:             @logintext = ('textcol','bgcol');
11310:         } else {
11311:             %choices = &color_font_choices();
11312:         }
11313:         if ($role eq 'login') {
11314:             @images = ('img','logo','domlogo','login');
11315:             @bgs = ('pgbg','mainbg','sidebg');
11316:         } else {
11317:             @images = ('img');
11318:             @bgs = ('pgbg','tabbg','sidebg');
11319:         }
11320:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
11321:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
11322:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
11323:         }
11324:         if ($role eq 'login') {
11325:             foreach my $item (@logintext) {
11326:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
11327:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
11328:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
11329:                 }
11330:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
11331:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
11332:                 }
11333:             }
11334:         } else {
11335:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
11336:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
11337:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
11338:             }
11339:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
11340:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
11341:             }
11342:         }
11343:         foreach my $item (@bgs) {
11344:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
11345:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
11346:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
11347:             }
11348:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
11349:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
11350:             }
11351:         }
11352:         foreach my $item (@links) {
11353:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
11354:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
11355:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
11356:             }
11357:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
11358:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
11359:             }
11360:         }
11361:         my ($configuserok,$author_ok,$switchserver) = 
11362:             &config_check($dom,$confname,$servadm);
11363:         my ($width,$height) = &thumb_dimensions();
11364:         if (ref($domconfig->{$role}) ne 'HASH') {
11365:             $domconfig->{$role} = {};
11366:         }
11367:         foreach my $img (@images) {
11368:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
11369:                 if (defined($env{'form.login_showlogo_'.$img})) {
11370:                     $confhash->{$role}{'showlogo'}{$img} = 1;
11371:                 } else { 
11372:                     $confhash->{$role}{'showlogo'}{$img} = 0;
11373:                 }
11374:             } 
11375: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
11376: 		 && !defined($domconfig->{$role}{$img})
11377: 		 && !$env{'form.'.$role.'_del_'.$img}
11378: 		 && $env{'form.'.$role.'_import_'.$img}) {
11379: 		# import the old configured image from the .tab setting
11380: 		# if they haven't provided a new one 
11381: 		$domconfig->{$role}{$img} = 
11382: 		    $env{'form.'.$role.'_import_'.$img};
11383: 	    }
11384:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
11385:                 my $error;
11386:                 if ($configuserok eq 'ok') {
11387:                     if ($switchserver) {
11388:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
11389:                     } else {
11390:                         if ($author_ok eq 'ok') {
11391:                             my ($result,$logourl) = 
11392:                                 &publishlogo($r,'upload',$role.'_'.$img,
11393:                                            $dom,$confname,$img,$width,$height);
11394:                             if ($result eq 'ok') {
11395:                                 $confhash->{$role}{$img} = $logourl;
11396:                                 $changes{$role}{'images'}{$img} = 1;
11397:                             } else {
11398:                                 $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);
11399:                             }
11400:                         } else {
11401:                             $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);
11402:                         }
11403:                     }
11404:                 } else {
11405:                     $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);
11406:                 }
11407:                 if ($error) {
11408:                     &Apache::lonnet::logthis($error);
11409:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
11410:                 }
11411:             } elsif ($domconfig->{$role}{$img} ne '') {
11412:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
11413:                     my $error;
11414:                     if ($configuserok eq 'ok') {
11415: # is confname an author?
11416:                         if ($switchserver eq '') {
11417:                             if ($author_ok eq 'ok') {
11418:                                 my ($result,$logourl) = 
11419:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
11420:                                             $dom,$confname,$img,$width,$height);
11421:                                 if ($result eq 'ok') {
11422:                                     $confhash->{$role}{$img} = $logourl;
11423: 				    $changes{$role}{'images'}{$img} = 1;
11424:                                 }
11425:                             }
11426:                         }
11427:                     }
11428:                 }
11429:             }
11430:         }
11431:         if (ref($domconfig) eq 'HASH') {
11432:             if (ref($domconfig->{$role}) eq 'HASH') {
11433:                 foreach my $img (@images) {
11434:                     if ($domconfig->{$role}{$img} ne '') {
11435:                         if ($env{'form.'.$role.'_del_'.$img}) {
11436:                             $confhash->{$role}{$img} = '';
11437:                             $changes{$role}{'images'}{$img} = 1;
11438:                         } else {
11439:                             if ($confhash->{$role}{$img} eq '') {
11440:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
11441:                             }
11442:                         }
11443:                     } else {
11444:                         if ($env{'form.'.$role.'_del_'.$img}) {
11445:                             $confhash->{$role}{$img} = '';
11446:                             $changes{$role}{'images'}{$img} = 1;
11447:                         } 
11448:                     }
11449:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
11450:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
11451:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
11452:                                 $domconfig->{$role}{'showlogo'}{$img}) {
11453:                                 $changes{$role}{'showlogo'}{$img} = 1; 
11454:                             }
11455:                         } else {
11456:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
11457:                                 $changes{$role}{'showlogo'}{$img} = 1;
11458:                             }
11459:                         }
11460:                     }
11461:                 }
11462:                 if ($domconfig->{$role}{'font'} ne '') {
11463:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
11464:                         $changes{$role}{'font'} = 1;
11465:                     }
11466:                 } else {
11467:                     if ($confhash->{$role}{'font'}) {
11468:                         $changes{$role}{'font'} = 1;
11469:                     }
11470:                 }
11471:                 if ($role ne 'login') {
11472:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
11473:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
11474:                             $changes{$role}{'fontmenu'} = 1;
11475:                         }
11476:                     } else {
11477:                         if ($confhash->{$role}{'fontmenu'}) {
11478:                             $changes{$role}{'fontmenu'} = 1;
11479:                         }
11480:                     }
11481:                 }
11482:                 foreach my $item (@bgs) {
11483:                     if ($domconfig->{$role}{$item} ne '') {
11484:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
11485:                             $changes{$role}{'bgs'}{$item} = 1;
11486:                         } 
11487:                     } else {
11488:                         if ($confhash->{$role}{$item}) {
11489:                             $changes{$role}{'bgs'}{$item} = 1;
11490:                         }
11491:                     }
11492:                 }
11493:                 foreach my $item (@links) {
11494:                     if ($domconfig->{$role}{$item} ne '') {
11495:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
11496:                             $changes{$role}{'links'}{$item} = 1;
11497:                         }
11498:                     } else {
11499:                         if ($confhash->{$role}{$item}) {
11500:                             $changes{$role}{'links'}{$item} = 1;
11501:                         }
11502:                     }
11503:                 }
11504:                 foreach my $item (@logintext) {
11505:                     if ($domconfig->{$role}{$item} ne '') {
11506:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
11507:                             $changes{$role}{'logintext'}{$item} = 1;
11508:                         }
11509:                     } else {
11510:                         if ($confhash->{$role}{$item}) {
11511:                             $changes{$role}{'logintext'}{$item} = 1;
11512:                         }
11513:                     }
11514:                 }
11515:             } else {
11516:                 &default_change_checker($role,\@images,\@links,\@bgs,
11517:                                         \@logintext,$confhash,\%changes); 
11518:             }
11519:         } else {
11520:             &default_change_checker($role,\@images,\@links,\@bgs,
11521:                                     \@logintext,$confhash,\%changes); 
11522:         }
11523:     }
11524:     return ($errors,%changes);
11525: }
11526: 
11527: sub config_check {
11528:     my ($dom,$confname,$servadm) = @_;
11529:     my ($configuserok,$author_ok,$switchserver,%currroles);
11530:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
11531:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
11532:                                                    $confname,$servadm);
11533:     if ($configuserok eq 'ok') {
11534:         $switchserver = &check_switchserver($dom,$confname);
11535:         if ($switchserver eq '') {
11536:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
11537:         }
11538:     }
11539:     return ($configuserok,$author_ok,$switchserver);
11540: }
11541: 
11542: sub default_change_checker {
11543:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
11544:     foreach my $item (@{$links}) {
11545:         if ($confhash->{$role}{$item}) {
11546:             $changes->{$role}{'links'}{$item} = 1;
11547:         }
11548:     }
11549:     foreach my $item (@{$bgs}) {
11550:         if ($confhash->{$role}{$item}) {
11551:             $changes->{$role}{'bgs'}{$item} = 1;
11552:         }
11553:     }
11554:     foreach my $item (@{$logintext}) {
11555:         if ($confhash->{$role}{$item}) {
11556:             $changes->{$role}{'logintext'}{$item} = 1;
11557:         }
11558:     }
11559:     foreach my $img (@{$images}) {
11560:         if ($env{'form.'.$role.'_del_'.$img}) {
11561:             $confhash->{$role}{$img} = '';
11562:             $changes->{$role}{'images'}{$img} = 1;
11563:         }
11564:         if ($role eq 'login') {
11565:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
11566:                 $changes->{$role}{'showlogo'}{$img} = 1;
11567:             }
11568:         }
11569:     }
11570:     if ($confhash->{$role}{'font'}) {
11571:         $changes->{$role}{'font'} = 1;
11572:     }
11573: }
11574: 
11575: sub display_colorchgs {
11576:     my ($dom,$changes,$roles,$confhash) = @_;
11577:     my (%choices,$resulttext);
11578:     if (!grep(/^login$/,@{$roles})) {
11579:         $resulttext = &mt('Changes made:').'<br />';
11580:     }
11581:     foreach my $role (@{$roles}) {
11582:         if ($role eq 'login') {
11583:             %choices = &login_choices();
11584:         } else {
11585:             %choices = &color_font_choices();
11586:         }
11587:         if (ref($changes->{$role}) eq 'HASH') {
11588:             if ($role ne 'login') {
11589:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
11590:             }
11591:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
11592:                 if ($role ne 'login') {
11593:                     $resulttext .= '<ul>';
11594:                 }
11595:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
11596:                     if ($role ne 'login') {
11597:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
11598:                     }
11599:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
11600:                         if (($role eq 'login') && ($key eq 'showlogo')) {
11601:                             if ($confhash->{$role}{$key}{$item}) {
11602:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
11603:                             } else {
11604:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
11605:                             }
11606:                         } elsif ($confhash->{$role}{$item} eq '') {
11607:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
11608:                         } else {
11609:                             my $newitem = $confhash->{$role}{$item};
11610:                             if ($key eq 'images') {
11611:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" style="vertical-align: bottom" />';
11612:                             }
11613:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
11614:                         }
11615:                     }
11616:                     if ($role ne 'login') {
11617:                         $resulttext .= '</ul></li>';
11618:                     }
11619:                 } else {
11620:                     if ($confhash->{$role}{$key} eq '') {
11621:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
11622:                     } else {
11623:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
11624:                     }
11625:                 }
11626:                 if ($role ne 'login') {
11627:                     $resulttext .= '</ul>';
11628:                 }
11629:             }
11630:         }
11631:     }
11632:     return $resulttext;
11633: }
11634: 
11635: sub thumb_dimensions {
11636:     return ('200','50');
11637: }
11638: 
11639: sub check_dimensions {
11640:     my ($inputfile) = @_;
11641:     my ($fullwidth,$fullheight);
11642:     if ($inputfile =~ m|^[/\w.\-]+$|) {
11643:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
11644:             my $imageinfo = <PIPE>;
11645:             if (!close(PIPE)) {
11646:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
11647:             }
11648:             chomp($imageinfo);
11649:             my ($fullsize) = 
11650:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
11651:             if ($fullsize) {
11652:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
11653:             }
11654:         }
11655:     }
11656:     return ($fullwidth,$fullheight);
11657: }
11658: 
11659: sub check_configuser {
11660:     my ($uhome,$dom,$confname,$servadm) = @_;
11661:     my ($configuserok,%currroles);
11662:     if ($uhome eq 'no_host') {
11663:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
11664:         my $configpass = &LONCAPA::Enrollment::create_password($dom);
11665:         $configuserok = 
11666:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
11667:                              $configpass,'','','','','',undef,$servadm);
11668:     } else {
11669:         $configuserok = 'ok';
11670:         %currroles = 
11671:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
11672:     }
11673:     return ($configuserok,%currroles);
11674: }
11675: 
11676: sub check_authorstatus {
11677:     my ($dom,$confname,%currroles) = @_;
11678:     my $author_ok;
11679:     if (!$currroles{':'.$dom.':au'}) {
11680:         my $start = time;
11681:         my $end = 0;
11682:         $author_ok = 
11683:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
11684:                                         'au',$end,$start,'','','domconfig');
11685:     } else {
11686:         $author_ok = 'ok';
11687:     }
11688:     return $author_ok;
11689: }
11690: 
11691: sub publishlogo {
11692:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
11693:     my ($output,$fname,$logourl,$madethumb);
11694:     if ($action eq 'upload') {
11695:         $fname=$env{'form.'.$formname.'.filename'};
11696:         chop($env{'form.'.$formname});
11697:     } else {
11698:         ($fname) = ($formname =~ /([^\/]+)$/);
11699:     }
11700:     if ($savefileas ne '') {
11701:         $fname = $savefileas;
11702:     }
11703:     $fname=&Apache::lonnet::clean_filename($fname);
11704: # See if there is anything left
11705:     unless ($fname) { return ('error: no uploaded file'); }
11706:     $fname="$subdir/$fname";
11707:     my $docroot=$r->dir_config('lonDocRoot');
11708:     my $filepath="$docroot/priv";
11709:     my $relpath = "$dom/$confname";
11710:     my ($fnamepath,$file,$fetchthumb);
11711:     $file=$fname;
11712:     if ($fname=~m|/|) {
11713:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
11714:     }
11715:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
11716:     my $count;
11717:     for ($count=5;$count<=$#parts;$count++) {
11718:         $filepath.="/$parts[$count]";
11719:         if ((-e $filepath)!=1) {
11720:             mkdir($filepath,02770);
11721:         }
11722:     }
11723:     # Check for bad extension and disallow upload
11724:     if ($file=~/\.(\w+)$/ &&
11725:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
11726:         $output = 
11727:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
11728:     } elsif ($file=~/\.(\w+)$/ &&
11729:         !defined(&Apache::loncommon::fileembstyle($1))) {
11730:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
11731:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
11732:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
11733:     } elsif (-d "$filepath/$file") {
11734:         $output = &mt('Filename is a directory name - rename the file and re-upload');
11735:     } else {
11736:         my $source = $filepath.'/'.$file;
11737:         my $logfile;
11738:         if (!open($logfile,">>",$source.'.log')) {
11739:             return (&mt('No write permission to Authoring Space'));
11740:         }
11741:         print $logfile
11742: "\n================= Publish ".localtime()." ================\n".
11743: $env{'user.name'}.':'.$env{'user.domain'}."\n";
11744: # Save the file
11745:         if (!open(FH,">",$source)) {
11746:             &Apache::lonnet::logthis('Failed to create '.$source);
11747:             return (&mt('Failed to create file'));
11748:         }
11749:         if ($action eq 'upload') {
11750:             if (!print FH ($env{'form.'.$formname})) {
11751:                 &Apache::lonnet::logthis('Failed to write to '.$source);
11752:                 return (&mt('Failed to write file'));
11753:             }
11754:         } else {
11755:             my $original = &Apache::lonnet::filelocation('',$formname);
11756:             if(!copy($original,$source)) {
11757:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
11758:                 return (&mt('Failed to write file'));
11759:             }
11760:         }
11761:         close(FH);
11762:         chmod(0660, $source); # Permissions to rw-rw---.
11763: 
11764:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
11765:         my $copyfile=$targetdir.'/'.$file;
11766: 
11767:         my @parts=split(/\//,$targetdir);
11768:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
11769:         for (my $count=5;$count<=$#parts;$count++) {
11770:             $path.="/$parts[$count]";
11771:             if (!-e $path) {
11772:                 print $logfile "\nCreating directory ".$path;
11773:                 mkdir($path,02770);
11774:             }
11775:         }
11776:         my $versionresult;
11777:         if (-e $copyfile) {
11778:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
11779:         } else {
11780:             $versionresult = 'ok';
11781:         }
11782:         if ($versionresult eq 'ok') {
11783:             if (copy($source,$copyfile)) {
11784:                 print $logfile "\nCopied original source to ".$copyfile."\n";
11785:                 $output = 'ok';
11786:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
11787:                 push(@{$modified_urls},[$copyfile,$source]);
11788:                 my $metaoutput = 
11789:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
11790:                 unless ($registered_cleanup) {
11791:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
11792:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
11793:                     $registered_cleanup=1;
11794:                 }
11795:             } else {
11796:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
11797:                 $output = &mt('Failed to copy file to RES space').", $!";
11798:             }
11799:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
11800:                 my $inputfile = $filepath.'/'.$file;
11801:                 my $outfile = $filepath.'/'.'tn-'.$file;
11802:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
11803:                 if ($fullwidth ne '' && $fullheight ne '') { 
11804:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
11805:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
11806:                         my @args = ('convert','-sample',$thumbsize,$inputfile,$outfile);
11807:                         system({$args[0]} @args);
11808:                         chmod(0660, $filepath.'/tn-'.$file);
11809:                         if (-e $outfile) {
11810:                             my $copyfile=$targetdir.'/tn-'.$file;
11811:                             if (copy($outfile,$copyfile)) {
11812:                                 print $logfile "\nCopied source to ".$copyfile."\n";
11813:                                 my $thumb_metaoutput = 
11814:                                     &write_metadata($dom,$confname,$formname,
11815:                                                     $targetdir,'tn-'.$file,$logfile);
11816:                                 push(@{$modified_urls},[$copyfile,$outfile]);
11817:                                 unless ($registered_cleanup) {
11818:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
11819:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
11820:                                     $registered_cleanup=1;
11821:                                 }
11822:                                 $madethumb = 1;
11823:                             } else {
11824:                                 print $logfile "\nUnable to write ".$copyfile.
11825:                                                ':'.$!."\n";
11826:                             }
11827:                         }
11828:                     }
11829:                 }
11830:             }
11831:         } else {
11832:             $output = $versionresult;
11833:         }
11834:     }
11835:     return ($output,$logourl,$madethumb);
11836: }
11837: 
11838: sub logo_versioning {
11839:     my ($targetdir,$file,$logfile) = @_;
11840:     my $target = $targetdir.'/'.$file;
11841:     my ($maxversion,$fn,$extn,$output);
11842:     $maxversion = 0;
11843:     if ($file =~ /^(.+)\.(\w+)$/) {
11844:         $fn=$1;
11845:         $extn=$2;
11846:     }
11847:     opendir(DIR,$targetdir);
11848:     while (my $filename=readdir(DIR)) {
11849:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
11850:             $maxversion=($1>$maxversion)?$1:$maxversion;
11851:         }
11852:     }
11853:     $maxversion++;
11854:     print $logfile "\nCreating old version ".$maxversion."\n";
11855:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
11856:     if (copy($target,$copyfile)) {
11857:         print $logfile "Copied old target to ".$copyfile."\n";
11858:         $copyfile=$copyfile.'.meta';
11859:         if (copy($target.'.meta',$copyfile)) {
11860:             print $logfile "Copied old target metadata to ".$copyfile."\n";
11861:             $output = 'ok';
11862:         } else {
11863:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
11864:             $output = &mt('Failed to copy old meta').", $!, ";
11865:         }
11866:     } else {
11867:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
11868:         $output = &mt('Failed to copy old target').", $!, ";
11869:     }
11870:     return $output;
11871: }
11872: 
11873: sub write_metadata {
11874:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
11875:     my (%metadatafields,%metadatakeys,$output);
11876:     $metadatafields{'title'}=$formname;
11877:     $metadatafields{'creationdate'}=time;
11878:     $metadatafields{'lastrevisiondate'}=time;
11879:     $metadatafields{'copyright'}='public';
11880:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
11881:                                          $env{'user.domain'};
11882:     $metadatafields{'authorspace'}=$confname.':'.$dom;
11883:     $metadatafields{'domain'}=$dom;
11884:     {
11885:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
11886:         my $mfh;
11887:         if (open($mfh,">",$targetdir.'/'.$file.'.meta')) {
11888:             foreach (sort(keys(%metadatafields))) {
11889:                 unless ($_=~/\./) {
11890:                     my $unikey=$_;
11891:                     $unikey=~/^([A-Za-z]+)/;
11892:                     my $tag=$1;
11893:                     $tag=~tr/A-Z/a-z/;
11894:                     print $mfh "\n\<$tag";
11895:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
11896:                         my $value=$metadatafields{$unikey.'.'.$_};
11897:                         $value=~s/\"/\'\'/g;
11898:                         print $mfh ' '.$_.'="'.$value.'"';
11899:                     }
11900:                     print $mfh '>'.
11901:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
11902:                             .'</'.$tag.'>';
11903:                 }
11904:             }
11905:             $output = 'ok';
11906:             print $logfile "\nWrote metadata";
11907:             close($mfh);
11908:         } else {
11909:             print $logfile "\nFailed to open metadata file";
11910:             $output = &mt('Could not write metadata');
11911:         }
11912:     }
11913:     return $output;
11914: }
11915: 
11916: sub notifysubscribed {
11917:     foreach my $targetsource (@{$modified_urls}){
11918:         next unless (ref($targetsource) eq 'ARRAY');
11919:         my ($target,$source)=@{$targetsource};
11920:         if ($source ne '') {
11921:             if (open(my $logfh,">>",$source.'.log')) {
11922:                 print $logfh "\nCleanup phase: Notifications\n";
11923:                 my @subscribed=&subscribed_hosts($target);
11924:                 foreach my $subhost (@subscribed) {
11925:                     print $logfh "\nNotifying host ".$subhost.':';
11926:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
11927:                     print $logfh $reply;
11928:                 }
11929:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
11930:                 foreach my $subhost (@subscribedmeta) {
11931:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
11932:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
11933:                                                         $subhost);
11934:                     print $logfh $reply;
11935:                 }
11936:                 print $logfh "\n============ Done ============\n";
11937:                 close($logfh);
11938:             }
11939:         }
11940:     }
11941:     return OK;
11942: }
11943: 
11944: sub subscribed_hosts {
11945:     my ($target) = @_;
11946:     my @subscribed;
11947:     if (open(my $fh,"<","$target.subscription")) {
11948:         while (my $subline=<$fh>) {
11949:             if ($subline =~ /^($match_lonid):/) {
11950:                 my $host = $1;
11951:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
11952:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
11953:                         push(@subscribed,$host);
11954:                     }
11955:                 }
11956:             }
11957:         }
11958:     }
11959:     return @subscribed;
11960: }
11961: 
11962: sub check_switchserver {
11963:     my ($dom,$confname) = @_;
11964:     my ($allowed,$switchserver);
11965:     my $home = &Apache::lonnet::homeserver($confname,$dom);
11966:     if ($home eq 'no_host') {
11967:         $home = &Apache::lonnet::domain($dom,'primary');
11968:     }
11969:     my @ids=&Apache::lonnet::current_machine_ids();
11970:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
11971:     if (!$allowed) {
11972: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
11973:     }
11974:     return $switchserver;
11975: }
11976: 
11977: sub modify_quotas {
11978:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
11979:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
11980:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
11981:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
11982:         $validationfieldsref);
11983:     if ($action eq 'quotas') {
11984:         $context = 'tools'; 
11985:     } else {
11986:         $context = $action;
11987:     }
11988:     if ($context eq 'requestcourses') {
11989:         @usertools = ('official','unofficial','community','textbook','placement','lti');
11990:         @options =('norequest','approval','validate','autolimit');
11991:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
11992:         %titles = &courserequest_titles();
11993:         $toolregexp = join('|',@usertools);
11994:         %conditions = &courserequest_conditions();
11995:         $confname = $dom.'-domainconfig';
11996:         my $servadm = $r->dir_config('lonAdmEMail');
11997:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
11998:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
11999:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
12000:     } elsif ($context eq 'requestauthor') {
12001:         @usertools = ('author');
12002:         %titles = &authorrequest_titles();
12003:     } else {
12004:         @usertools = ('aboutme','blog','webdav','portfolio');
12005:         %titles = &tool_titles();
12006:     }
12007:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12008:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12009:     foreach my $key (keys(%env)) {
12010:         if ($context eq 'requestcourses') {
12011:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
12012:                 my $item = $1;
12013:                 my $type = $2;
12014:                 if ($type =~ /^limit_(.+)/) {
12015:                     $limithash{$item}{$1} = $env{$key};
12016:                 } else {
12017:                     $confhash{$item}{$type} = $env{$key};
12018:                 }
12019:             }
12020:         } elsif ($context eq 'requestauthor') {
12021:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
12022:                 $confhash{$1} = $env{$key};
12023:             }
12024:         } else {
12025:             if ($key =~ /^form\.quota_(.+)$/) {
12026:                 $confhash{'defaultquota'}{$1} = $env{$key};
12027:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
12028:                 $confhash{'authorquota'}{$1} = $env{$key};
12029:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
12030:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
12031:             }
12032:         }
12033:     }
12034:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
12035:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
12036:         @approvalnotify = sort(@approvalnotify);
12037:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
12038:         my @crstypes = ('official','unofficial','community','textbook','placement','lti');
12039:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
12040:         foreach my $type (@hasuniquecode) {
12041:             if (grep(/^\Q$type\E$/,@crstypes)) {
12042:                 $confhash{'uniquecode'}{$type} = 1;
12043:             }
12044:         }
12045:         my (%newbook,%allpos);
12046:         if ($context eq 'requestcourses') {
12047:             foreach my $type ('textbooks','templates') {
12048:                 @{$allpos{$type}} = (); 
12049:                 my $invalid;
12050:                 if ($type eq 'textbooks') {
12051:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
12052:                 } else {
12053:                     $invalid = &mt('Invalid LON-CAPA course for template');
12054:                 }
12055:                 if ($env{'form.'.$type.'_addbook'}) {
12056:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
12057:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
12058:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
12059:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
12060:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
12061:                         } else {
12062:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
12063:                             my $position = $env{'form.'.$type.'_addbook_pos'};
12064:                             $position =~ s/\D+//g;
12065:                             if ($position ne '') {
12066:                                 $allpos{$type}[$position] = $newbook{$type};
12067:                             }
12068:                         }
12069:                     } else {
12070:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
12071:                     }
12072:                 }
12073:             } 
12074:         }
12075:         if (ref($domconfig{$action}) eq 'HASH') {
12076:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
12077:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
12078:                     $changes{'notify'}{'approval'} = 1;
12079:                 }
12080:             } else {
12081:                 if ($confhash{'notify'}{'approval'}) {
12082:                     $changes{'notify'}{'approval'} = 1;
12083:                 }
12084:             }
12085:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
12086:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
12087:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
12088:                         unless ($confhash{'uniquecode'}{$crstype}) {
12089:                             $changes{'uniquecode'} = 1;
12090:                         }
12091:                     }
12092:                     unless ($changes{'uniquecode'}) {
12093:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
12094:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
12095:                                 $changes{'uniquecode'} = 1;
12096:                             }
12097:                         }
12098:                     }
12099:                } else {
12100:                    $changes{'uniquecode'} = 1;
12101:                }
12102:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
12103:                 $changes{'uniquecode'} = 1;
12104:             }
12105:             if ($context eq 'requestcourses') {
12106:                 foreach my $type ('textbooks','templates') {
12107:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
12108:                         my %deletions;
12109:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
12110:                         if (@todelete) {
12111:                             map { $deletions{$_} = 1; } @todelete;
12112:                         }
12113:                         my %imgdeletions;
12114:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
12115:                         if (@todeleteimages) {
12116:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
12117:                         }
12118:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
12119:                         for (my $i=0; $i<=$maxnum; $i++) {
12120:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
12121:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
12122:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
12123:                                 if ($deletions{$key}) {
12124:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
12125:                                         #FIXME need to obsolete item in RES space
12126:                                     }
12127:                                     next;
12128:                                 } else {
12129:                                     my $newpos = $env{'form.'.$itemid};
12130:                                     $newpos =~ s/\D+//g;
12131:                                     foreach my $item ('subject','title','publisher','author') {
12132:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
12133:                                                  ($type eq 'templates'));
12134:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
12135:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
12136:                                             $changes{$type}{$key} = 1;
12137:                                         }
12138:                                     }
12139:                                     $allpos{$type}[$newpos] = $key;
12140:                                 }
12141:                                 if ($imgdeletions{$key}) {
12142:                                     $changes{$type}{$key} = 1;
12143:                                     #FIXME need to obsolete item in RES space
12144:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
12145:                                     my ($cdom,$cnum) = split(/_/,$key);
12146:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
12147:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
12148:                                     } else {
12149:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
12150:                                                                                       $cdom,$cnum,$type,$configuserok,
12151:                                                                                       $switchserver,$author_ok);
12152:                                         if ($imgurl) {
12153:                                             $confhash{$type}{$key}{'image'} = $imgurl;
12154:                                             $changes{$type}{$key} = 1; 
12155:                                         }
12156:                                         if ($error) {
12157:                                             &Apache::lonnet::logthis($error);
12158:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12159:                                         }
12160:                                     }
12161:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
12162:                                     $confhash{$type}{$key}{'image'} = 
12163:                                         $domconfig{$action}{$type}{$key}{'image'};
12164:                                 }
12165:                             }
12166:                         }
12167:                     }
12168:                 }
12169:             }
12170:         } else {
12171:             if ($confhash{'notify'}{'approval'}) {
12172:                 $changes{'notify'}{'approval'} = 1;
12173:             }
12174:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
12175:                 $changes{'uniquecode'} = 1;
12176:             }
12177:         }
12178:         if ($context eq 'requestcourses') {
12179:             foreach my $type ('textbooks','templates') {
12180:                 if ($newbook{$type}) {
12181:                     $changes{$type}{$newbook{$type}} = 1;
12182:                     foreach my $item ('subject','title','publisher','author') {
12183:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
12184:                                  ($type eq 'template'));
12185:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
12186:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
12187:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
12188:                         }
12189:                     }
12190:                     if ($type eq 'textbooks') {
12191:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
12192:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
12193:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
12194:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
12195:                             } else {
12196:                                 my ($imageurl,$error) =
12197:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
12198:                                                             $configuserok,$switchserver,$author_ok);
12199:                                 if ($imageurl) {
12200:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
12201:                                 }
12202:                                 if ($error) {
12203:                                     &Apache::lonnet::logthis($error);
12204:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12205:                                 }
12206:                             }
12207:                         }
12208:                     }
12209:                 }
12210:                 if (@{$allpos{$type}} > 0) {
12211:                     my $idx = 0;
12212:                     foreach my $item (@{$allpos{$type}}) {
12213:                         if ($item ne '') {
12214:                             $confhash{$type}{$item}{'order'} = $idx;
12215:                             if (ref($domconfig{$action}) eq 'HASH') {
12216:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
12217:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
12218:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
12219:                                             $changes{$type}{$item} = 1;
12220:                                         }
12221:                                     }
12222:                                 }
12223:                             }
12224:                             $idx ++;
12225:                         }
12226:                     }
12227:                 }
12228:             }
12229:             if (ref($validationitemsref) eq 'ARRAY') {
12230:                 foreach my $item (@{$validationitemsref}) {
12231:                     if ($item eq 'fields') {
12232:                         my @changed;
12233:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
12234:                         if (@{$confhash{'validation'}{$item}} > 0) {
12235:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
12236:                         }
12237:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
12238:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
12239:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
12240:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
12241:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
12242:                                 } else {
12243:                                     @changed = @{$confhash{'validation'}{$item}};
12244:                                 }
12245:                             } else {
12246:                                 @changed = @{$confhash{'validation'}{$item}};
12247:                             }
12248:                         } else {
12249:                             @changed = @{$confhash{'validation'}{$item}};
12250:                         }
12251:                         if (@changed) {
12252:                             if ($confhash{'validation'}{$item}) {
12253:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
12254:                             } else {
12255:                                 $changes{'validation'}{$item} = &mt('None');
12256:                             }
12257:                         }
12258:                     } else {
12259:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
12260:                         if ($item eq 'markup') {
12261:                             if ($env{'form.requestcourses_validation_'.$item}) {
12262:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
12263:                             }
12264:                         }
12265:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
12266:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
12267:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
12268:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
12269:                                 }
12270:                             } else {
12271:                                 if ($confhash{'validation'}{$item} ne '') {
12272:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
12273:                                 }
12274:                             }
12275:                         } else {
12276:                             if ($confhash{'validation'}{$item} ne '') {
12277:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
12278:                             }
12279:                         }
12280:                     }
12281:                 }
12282:             }
12283:             if ($env{'form.validationdc'}) {
12284:                 my $newval = $env{'form.validationdc'};
12285:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
12286:                 if (exists($domcoords{$newval})) {
12287:                     $confhash{'validation'}{'dc'} = $newval;
12288:                 }
12289:             }
12290:             if (ref($confhash{'validation'}) eq 'HASH') {
12291:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
12292:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
12293:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
12294:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
12295:                                 if ($confhash{'validation'}{'dc'} eq '') {
12296:                                     $changes{'validation'}{'dc'} = &mt('None');
12297:                                 } else {
12298:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
12299:                                 }
12300:                             }
12301:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
12302:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
12303:                         }
12304:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
12305:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
12306:                     }
12307:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
12308:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
12309:                 }
12310:             } else {
12311:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
12312:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
12313:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
12314:                             $changes{'validation'}{'dc'} = &mt('None');
12315:                         }
12316:                     }
12317:                 }
12318:             }
12319:         }
12320:     } else {
12321:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
12322:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
12323:     }
12324:     foreach my $item (@usertools) {
12325:         foreach my $type (@{$types},'default','_LC_adv') {
12326:             my $unset; 
12327:             if ($context eq 'requestcourses') {
12328:                 $unset = '0';
12329:                 if ($type eq '_LC_adv') {
12330:                     $unset = '';
12331:                 }
12332:                 if ($confhash{$item}{$type} eq 'autolimit') {
12333:                     $confhash{$item}{$type} .= '=';
12334:                     unless ($limithash{$item}{$type} =~ /\D/) {
12335:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
12336:                     }
12337:                 }
12338:             } elsif ($context eq 'requestauthor') {
12339:                 $unset = '0';
12340:                 if ($type eq '_LC_adv') {
12341:                     $unset = '';
12342:                 }
12343:             } else {
12344:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
12345:                     $confhash{$item}{$type} = 1;
12346:                 } else {
12347:                     $confhash{$item}{$type} = 0;
12348:                 }
12349:             }
12350:             if (ref($domconfig{$action}) eq 'HASH') {
12351:                 if ($action eq 'requestauthor') {
12352:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
12353:                         $changes{$type} = 1;
12354:                     }
12355:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
12356:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
12357:                         $changes{$item}{$type} = 1;
12358:                     }
12359:                 } else {
12360:                     if ($context eq 'requestcourses') {
12361:                         if ($confhash{$item}{$type} ne $unset) {
12362:                             $changes{$item}{$type} = 1;
12363:                         }
12364:                     } else {
12365:                         if (!$confhash{$item}{$type}) {
12366:                             $changes{$item}{$type} = 1;
12367:                         }
12368:                     }
12369:                 }
12370:             } else {
12371:                 if ($context eq 'requestcourses') {
12372:                     if ($confhash{$item}{$type} ne $unset) {
12373:                         $changes{$item}{$type} = 1;
12374:                     }
12375:                 } elsif ($context eq 'requestauthor') {
12376:                     if ($confhash{$type} ne $unset) {
12377:                         $changes{$type} = 1;
12378:                     }
12379:                 } else {
12380:                     if (!$confhash{$item}{$type}) {
12381:                         $changes{$item}{$type} = 1;
12382:                     }
12383:                 }
12384:             }
12385:         }
12386:     }
12387:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
12388:         if (ref($domconfig{'quotas'}) eq 'HASH') {
12389:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
12390:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
12391:                     if (exists($confhash{'defaultquota'}{$key})) {
12392:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
12393:                             $changes{'defaultquota'}{$key} = 1;
12394:                         }
12395:                     } else {
12396:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
12397:                     }
12398:                 }
12399:             } else {
12400:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
12401:                     if (exists($confhash{'defaultquota'}{$key})) {
12402:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
12403:                             $changes{'defaultquota'}{$key} = 1;
12404:                         }
12405:                     } else {
12406:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
12407:                     }
12408:                 }
12409:             }
12410:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
12411:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
12412:                     if (exists($confhash{'authorquota'}{$key})) {
12413:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
12414:                             $changes{'authorquota'}{$key} = 1;
12415:                         }
12416:                     } else {
12417:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
12418:                     }
12419:                 }
12420:             }
12421:         }
12422:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
12423:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
12424:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
12425:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
12426:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
12427:                             $changes{'defaultquota'}{$key} = 1;
12428:                         }
12429:                     } else {
12430:                         if (!exists($domconfig{'quotas'}{$key})) {
12431:                             $changes{'defaultquota'}{$key} = 1;
12432:                         }
12433:                     }
12434:                 } else {
12435:                     $changes{'defaultquota'}{$key} = 1;
12436:                 }
12437:             }
12438:         }
12439:         if (ref($confhash{'authorquota'}) eq 'HASH') {
12440:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
12441:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
12442:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
12443:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
12444:                             $changes{'authorquota'}{$key} = 1;
12445:                         }
12446:                     } else {
12447:                         $changes{'authorquota'}{$key} = 1;
12448:                     }
12449:                 } else {
12450:                     $changes{'authorquota'}{$key} = 1;
12451:                 }
12452:             }
12453:         }
12454:     }
12455: 
12456:     if ($context eq 'requestauthor') {
12457:         $domdefaults{'requestauthor'} = \%confhash;
12458:     } else {
12459:         foreach my $key (keys(%confhash)) {
12460:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
12461:                 $domdefaults{$key} = $confhash{$key};
12462:             }
12463:         }
12464:     }
12465: 
12466:     my %quotahash = (
12467:                       $action => { %confhash }
12468:                     );
12469:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
12470:                                              $dom);
12471:     if ($putresult eq 'ok') {
12472:         if (keys(%changes) > 0) {
12473:             my $cachetime = 24*60*60;
12474:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12475:             if (ref($lastactref) eq 'HASH') {
12476:                 $lastactref->{'domdefaults'} = 1;
12477:             }
12478:             $resulttext = &mt('Changes made:').'<ul>';
12479:             unless (($context eq 'requestcourses') ||
12480:                     ($context eq 'requestauthor')) {
12481:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
12482:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
12483:                     foreach my $type (@{$types},'default') {
12484:                         if (defined($changes{'defaultquota'}{$type})) {
12485:                             my $typetitle = $usertypes->{$type};
12486:                             if ($type eq 'default') {
12487:                                 $typetitle = $othertitle;
12488:                             }
12489:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
12490:                         }
12491:                     }
12492:                     $resulttext .= '</ul></li>';
12493:                 }
12494:                 if (ref($changes{'authorquota'}) eq 'HASH') {
12495:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
12496:                     foreach my $type (@{$types},'default') {
12497:                         if (defined($changes{'authorquota'}{$type})) {
12498:                             my $typetitle = $usertypes->{$type};
12499:                             if ($type eq 'default') {
12500:                                 $typetitle = $othertitle;
12501:                             }
12502:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
12503:                         }
12504:                     }
12505:                     $resulttext .= '</ul></li>';
12506:                 }
12507:             }
12508:             my %newenv;
12509:             foreach my $item (@usertools) {
12510:                 my (%haschgs,%inconf);
12511:                 if ($context eq 'requestauthor') {
12512:                     %haschgs = %changes;
12513:                     %inconf = %confhash;
12514:                 } else {
12515:                     if (ref($changes{$item}) eq 'HASH') {
12516:                         %haschgs = %{$changes{$item}};
12517:                     }
12518:                     if (ref($confhash{$item}) eq 'HASH') {
12519:                         %inconf = %{$confhash{$item}};
12520:                     }
12521:                 }
12522:                 if (keys(%haschgs) > 0) {
12523:                     my $newacc = 
12524:                         &Apache::lonnet::usertools_access($env{'user.name'},
12525:                                                           $env{'user.domain'},
12526:                                                           $item,'reload',$context);
12527:                     if (($context eq 'requestcourses') ||
12528:                         ($context eq 'requestauthor')) {
12529:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
12530:                             $newenv{'environment.canrequest.'.$item} = $newacc;
12531:                         }
12532:                     } else {
12533:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
12534:                             $newenv{'environment.availabletools.'.$item} = $newacc;
12535:                         }
12536:                     }
12537:                     unless ($context eq 'requestauthor') {
12538:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
12539:                     }
12540:                     foreach my $type (@{$types},'default','_LC_adv') {
12541:                         if ($haschgs{$type}) {
12542:                             my $typetitle = $usertypes->{$type};
12543:                             if ($type eq 'default') {
12544:                                 $typetitle = $othertitle;
12545:                             } elsif ($type eq '_LC_adv') {
12546:                                 $typetitle = 'LON-CAPA Advanced Users'; 
12547:                             }
12548:                             if ($inconf{$type}) {
12549:                                 if ($context eq 'requestcourses') {
12550:                                     my $cond;
12551:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
12552:                                         if ($1 eq '') {
12553:                                             $cond = &mt('(Automatic processing of any request).');
12554:                                         } else {
12555:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
12556:                                         }
12557:                                     } else { 
12558:                                         $cond = $conditions{$inconf{$type}};
12559:                                     }
12560:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
12561:                                 } elsif ($context eq 'requestauthor') {
12562:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
12563:                                                              $titles{$inconf{$type}},$typetitle);
12564: 
12565:                                 } else {
12566:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
12567:                                 }
12568:                             } else {
12569:                                 if ($type eq '_LC_adv') {
12570:                                     if ($inconf{$type} eq '0') {
12571:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
12572:                                     } else { 
12573:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
12574:                                     }
12575:                                 } else {
12576:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
12577:                                 }
12578:                             }
12579:                         }
12580:                     }
12581:                     unless ($context eq 'requestauthor') {
12582:                         $resulttext .= '</ul></li>';
12583:                     }
12584:                 }
12585:             }
12586:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
12587:                 if (ref($changes{'notify'}) eq 'HASH') {
12588:                     if ($changes{'notify'}{'approval'}) {
12589:                         if (ref($confhash{'notify'}) eq 'HASH') {
12590:                             if ($confhash{'notify'}{'approval'}) {
12591:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
12592:                             } else {
12593:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
12594:                             }
12595:                         }
12596:                     }
12597:                 }
12598:             }
12599:             if ($action eq 'requestcourses') {
12600:                 my @offon = ('off','on');
12601:                 if ($changes{'uniquecode'}) {
12602:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
12603:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
12604:                         $resulttext .= '<li>'.
12605:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
12606:                                        '</li>';
12607:                     } else {
12608:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
12609:                                        '</li>';
12610:                     }
12611:                 }
12612:                 foreach my $type ('textbooks','templates') {
12613:                     if (ref($changes{$type}) eq 'HASH') {
12614:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
12615:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
12616:                             my %coursehash = &Apache::lonnet::coursedescription($key);
12617:                             my $coursetitle = $coursehash{'description'};
12618:                             my $position = $confhash{$type}{$key}{'order'} + 1;
12619:                             $resulttext .= '<li>';
12620:                             foreach my $item ('subject','title','publisher','author') {
12621:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
12622:                                          ($type eq 'templates'));
12623:                                 my $name = $item.':';
12624:                                 $name =~ s/^(\w)/\U$1/;
12625:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
12626:                             }
12627:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
12628:                             if ($type eq 'textbooks') {
12629:                                 if ($confhash{$type}{$key}{'image'}) {
12630:                                     $resulttext .= ' '.&mt('Image: [_1]',
12631:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
12632:                                                    ' alt="Textbook cover" />').'<br />';
12633:                                 }
12634:                             }
12635:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
12636:                         }
12637:                         $resulttext .= '</ul></li>';
12638:                     }
12639:                 }
12640:                 if (ref($changes{'validation'}) eq 'HASH') {
12641:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
12642:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
12643:                         foreach my $item (@{$validationitemsref}) {
12644:                             if (exists($changes{'validation'}{$item})) {
12645:                                 if ($item eq 'markup') {
12646:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
12647:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
12648:                                 } else {
12649:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
12650:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
12651:                                 }
12652:                             }
12653:                         }
12654:                         if (exists($changes{'validation'}{'dc'})) {
12655:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
12656:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
12657:                         }
12658:                     }
12659:                 }
12660:             }
12661:             $resulttext .= '</ul>';
12662:             if (keys(%newenv)) {
12663:                 &Apache::lonnet::appenv(\%newenv);
12664:             }
12665:         } else {
12666:             if ($context eq 'requestcourses') {
12667:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
12668:             } elsif ($context eq 'requestauthor') {
12669:                 $resulttext = &mt('No changes made to rights to request author space.');
12670:             } else {
12671:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
12672:             }
12673:         }
12674:     } else {
12675:         $resulttext = '<span class="LC_error">'.
12676: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
12677:     }
12678:     if ($errors) {
12679:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
12680:                        '<ul>'.$errors.'</ul></p>';
12681:     }
12682:     return $resulttext;
12683: }
12684: 
12685: sub process_textbook_image {
12686:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
12687:     my $filename = $env{'form.'.$caller.'.filename'};
12688:     my ($error,$url);
12689:     my ($width,$height) = (50,50);
12690:     if ($configuserok eq 'ok') {
12691:         if ($switchserver) {
12692:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
12693:                          $switchserver);
12694:         } elsif ($author_ok eq 'ok') {
12695:             my ($result,$imageurl) =
12696:                 &publishlogo($r,'upload',$caller,$dom,$confname,
12697:                              "$type/$cdom/$cnum/cover",$width,$height);
12698:             if ($result eq 'ok') {
12699:                 $url = $imageurl;
12700:             } else {
12701:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
12702:             }
12703:         } else {
12704:             $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);
12705:         }
12706:     } else {
12707:         $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);
12708:     }
12709:     return ($url,$error);
12710: }
12711: 
12712: sub modify_ltitools {
12713:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
12714:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12715:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
12716:     my $confname = $dom.'-domainconfig';
12717:     my $servadm = $r->dir_config('lonAdmEMail');
12718:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
12719:     my (%posslti,%possfield);
12720:     my @courseroles = ('cc','in','ta','ep','st');
12721:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
12722:     map { $posslti{$_} = 1; } @ltiroles;
12723:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
12724:     map { $possfield{$_} = 1; } @allfields;
12725:     my %lt = &ltitools_names(); 
12726:     if ($env{'form.ltitools_add'}) {
12727:         my $title = $env{'form.ltitools_add_title'};
12728:         $title =~ s/(`)/'/g;
12729:         ($newid,my $error) = &get_ltitools_id($dom,$title);
12730:         if ($newid) {
12731:             my $position = $env{'form.ltitools_add_pos'};
12732:             $position =~ s/\D+//g;
12733:             if ($position ne '') {
12734:                 $allpos[$position] = $newid;
12735:             }
12736:             $changes{$newid} = 1;
12737:             foreach my $item ('title','url','key','secret','lifetime') {
12738:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
12739:                 if ($item eq 'lifetime') {
12740:                     $env{'form.ltitools_add_'.$item} =~ s/[^\d.]//g;
12741:                 }
12742:                 if ($env{'form.ltitools_add_'.$item}) {
12743:                     if (($item eq 'key') || ($item eq 'secret')) {
12744:                         $encconfig{$newid}{$item} = $env{'form.ltitools_add_'.$item};
12745:                     } else {
12746:                         $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
12747:                     }
12748:                 }
12749:             }
12750:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
12751:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
12752:             }
12753:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
12754:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
12755:             }
12756:             if ($env{'form.ltitools_add_sigmethod'} eq 'HMAC-SHA256') {
12757:                 $confhash{$newid}{'sigmethod'} = $env{'form.ltitools_add_sigmethod'};
12758:             } else {
12759:                 $confhash{$newid}{'sigmethod'} = 'HMAC-SHA1';
12760:             }
12761:             foreach my $item ('width','height','linktext','explanation') {
12762:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
12763:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
12764:                 if (($item eq 'width') || ($item eq 'height')) {
12765:                     if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
12766:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
12767:                     }
12768:                 } else {
12769:                     if ($env{'form.ltitools_add_'.$item} ne '') {
12770:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item}; 
12771:                     }
12772:                 }
12773:             }
12774:             if ($env{'form.ltitools_add_target'} eq 'window') {
12775:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
12776:             } elsif ($env{'form.ltitools_add_target'} eq 'tab') {
12777:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
12778:             } else {
12779:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
12780:             }
12781:             foreach my $item ('passback','roster') {
12782:                 if ($env{'form.ltitools_'.$item.'_add'}) {
12783:                     $confhash{$newid}{$item} = 1;
12784:                     if ($env{'form.ltitools_'.$item.'valid_add'} ne '') {
12785:                         my $lifetime = $env{'form.ltitools_'.$item.'valid_add'};
12786:                         $lifetime =~ s/^\s+|\s+$//g;
12787:                         if ($lifetime =~ /^\d+\.?\d*$/) {
12788:                             $confhash{$newid}{$item.'valid'} = $lifetime;
12789:                         }
12790:                     }
12791:                 }
12792:             }
12793:             if ($env{'form.ltitools_add_image.filename'} ne '') {
12794:                 my ($imageurl,$error) =
12795:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$newid,
12796:                                             $configuserok,$switchserver,$author_ok);
12797:                 if ($imageurl) {
12798:                     $confhash{$newid}{'image'} = $imageurl;
12799:                 }
12800:                 if ($error) {
12801:                     &Apache::lonnet::logthis($error);
12802:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12803:                 }
12804:             }
12805:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
12806:             foreach my $field (@fields) {
12807:                 if ($possfield{$field}) {
12808:                     if ($field eq 'roles') {
12809:                         foreach my $role (@courseroles) {
12810:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
12811:                             if (($choice ne '') && ($posslti{$choice})) {
12812:                                 $confhash{$newid}{'roles'}{$role} = $choice;
12813:                                 if ($role eq 'cc') {
12814:                                     $confhash{$newid}{'roles'}{'co'} = $choice; 
12815:                                 }
12816:                             }
12817:                         }
12818:                     } else {
12819:                         $confhash{$newid}{'fields'}{$field} = 1;
12820:                     }
12821:                 }
12822:             }
12823:             if (ref($confhash{$newid}{'fields'}) eq 'HASH') {
12824:                 if ($confhash{$newid}{'fields'}{'user'}) {
12825:                     if ($env{'form.ltitools_userincdom_add'}) {
12826:                         $confhash{$newid}{'incdom'} = 1;
12827:                     }
12828:                 }
12829:             }
12830:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
12831:             foreach my $item (@courseconfig) {
12832:                 $confhash{$newid}{'crsconf'}{$item} = 1;
12833:             }
12834:             if ($env{'form.ltitools_add_custom'}) {
12835:                 my $name = $env{'form.ltitools_add_custom_name'};
12836:                 my $value = $env{'form.ltitools_add_custom_value'};
12837:                 $value =~ s/(`)/'/g;
12838:                 $name =~ s/(`)/'/g;
12839:                 $confhash{$newid}{'custom'}{$name} = $value;
12840:             }
12841:         } else {
12842:             my $error = &mt('Failed to acquire unique ID for new external tool');   
12843:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12844:         }
12845:     }
12846:     if (ref($domconfig{$action}) eq 'HASH') {
12847:         my %deletions;
12848:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
12849:         if (@todelete) {
12850:             map { $deletions{$_} = 1; } @todelete;
12851:         }
12852:         my %customadds;
12853:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
12854:         if (@newcustom) {
12855:             map { $customadds{$_} = 1; } @newcustom;
12856:         } 
12857:         my %imgdeletions;
12858:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
12859:         if (@todeleteimages) {
12860:             map { $imgdeletions{$_} = 1; } @todeleteimages;
12861:         }
12862:         my $maxnum = $env{'form.ltitools_maxnum'};
12863:         for (my $i=0; $i<=$maxnum; $i++) {
12864:             my $itemid = $env{'form.ltitools_id_'.$i};
12865:             $itemid =~ s/\D+//g;
12866:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
12867:                 if ($deletions{$itemid}) {
12868:                     if ($domconfig{$action}{$itemid}{'image'}) {
12869:                         #FIXME need to obsolete item in RES space
12870:                     }
12871:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
12872:                     next;
12873:                 } else {
12874:                     my $newpos = $env{'form.ltitools_'.$itemid};
12875:                     $newpos =~ s/\D+//g;
12876:                     foreach my $item ('title','url','lifetime') {
12877:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
12878:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
12879:                             $changes{$itemid} = 1;
12880:                         }
12881:                     }
12882:                     foreach my $item ('key','secret') {
12883:                         $encconfig{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
12884:                         if ($domconfig{$action}{$itemid}{$item} ne $encconfig{$itemid}{$item}) {
12885:                             $changes{$itemid} = 1;
12886:                         }
12887:                     }
12888:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
12889:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
12890:                     }
12891:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
12892:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
12893:                     }
12894:                     if ($env{'form.ltitools_sigmethod_'.$i} eq 'HMAC-SHA256') {
12895:                         $confhash{$itemid}{'sigmethod'} = $env{'form.ltitools_sigmethod_'.$i};
12896:                     } else {
12897:                         $confhash{$itemid}{'sigmethod'} = 'HMAC-SHA1'; 
12898:                     }
12899:                     if ($domconfig{$action}{$itemid}{'sigmethod'} eq '') {
12900:                         if ($confhash{$itemid}{'sigmethod'} ne 'HMAC-SHA1') {
12901:                             $changes{$itemid} = 1;
12902:                         }
12903:                     } elsif ($domconfig{$action}{$itemid}{'sigmethod'} ne $confhash{$itemid}{'sigmethod'}) {
12904:                         $changes{$itemid} = 1;
12905:                     }
12906:                     foreach my $size ('width','height') {
12907:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
12908:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
12909:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
12910:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
12911:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
12912:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
12913:                                     $changes{$itemid} = 1;
12914:                                 }
12915:                             } else {
12916:                                 $changes{$itemid} = 1;
12917:                             }
12918:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
12919:                             if ($domconfig{$action}{$itemid}{'display'}{$size} ne '') {
12920:                                 $changes{$itemid} = 1;
12921:                             }
12922:                         }
12923:                     }
12924:                     foreach my $item ('linktext','explanation') {
12925:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/^\s+//;
12926:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/\s+$//;
12927:                         if ($env{'form.ltitools_'.$item.'_'.$i} ne '') {
12928:                             $confhash{$itemid}{'display'}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
12929:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
12930:                                 if ($domconfig{$action}{$itemid}{'display'}{$item} ne $confhash{$itemid}{'display'}{$item}) {
12931:                                     $changes{$itemid} = 1;
12932:                                 }
12933:                             } else {
12934:                                 $changes{$itemid} = 1;
12935:                             }
12936:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
12937:                             if ($domconfig{$action}{$itemid}{'display'}{$item} ne '') {
12938:                                 $changes{$itemid} = 1;
12939:                             }
12940:                         }
12941:                     }
12942:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
12943:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
12944:                     } elsif ($env{'form.ltitools_target_'.$i} eq 'tab') {
12945:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
12946:                     } else {
12947:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
12948:                     }
12949:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
12950:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
12951:                             $changes{$itemid} = 1;
12952:                         }
12953:                     } else {
12954:                         $changes{$itemid} = 1;
12955:                     }
12956:                     foreach my $extra ('passback','roster') {
12957:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
12958:                             $confhash{$itemid}{$extra} = 1;
12959:                             if ($env{'form.ltitools_'.$extra.'valid_'.$i} ne '') {
12960:                                 my $lifetime = $env{'form.ltitools_'.$extra.'valid_'.$i};
12961:                                 $lifetime =~ s/^\s+|\s+$//g;
12962:                                 if ($lifetime =~ /^\d+\.?\d*$/) {
12963:                                     $confhash{$itemid}{$extra.'valid'} = $lifetime;
12964:                                 }
12965:                             }
12966:                         }
12967:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
12968:                             $changes{$itemid} = 1;
12969:                         }
12970:                         if ($domconfig{$action}{$itemid}{$extra.'valid'} ne $confhash{$itemid}{$extra.'valid'}) {
12971:                             $changes{$itemid} = 1;
12972:                         }
12973:                     }
12974:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
12975:                     foreach my $item ('label','title','target','linktext','explanation','append') {
12976:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
12977:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
12978:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
12979:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
12980:                                     $changes{$itemid} = 1;
12981:                                 }
12982:                             } else {
12983:                                 $changes{$itemid} = 1;
12984:                             }
12985:                         }
12986:                     }
12987:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
12988:                     foreach my $field (@fields) {
12989:                         if ($possfield{$field}) {
12990:                             if ($field eq 'roles') {
12991:                                 foreach my $role (@courseroles) {
12992:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
12993:                                     if (($choice ne '') && ($posslti{$choice})) {
12994:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
12995:                                         if ($role eq 'cc') {
12996:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
12997:                                         }
12998:                                     }
12999:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
13000:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
13001:                                             $changes{$itemid} = 1;
13002:                                         }
13003:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
13004:                                         $changes{$itemid} = 1;
13005:                                     }
13006:                                 }
13007:                             } else {
13008:                                 $confhash{$itemid}{'fields'}{$field} = 1;
13009:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
13010:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
13011:                                         $changes{$itemid} = 1;
13012:                                     }
13013:                                 } else {
13014:                                     $changes{$itemid} = 1;
13015:                                 }
13016:                             }
13017:                         }
13018:                     }
13019:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
13020:                         if ($confhash{$itemid}{'fields'}{'user'}) {
13021:                             if ($env{'form.ltitools_userincdom_'.$i}) {
13022:                                 $confhash{$itemid}{'incdom'} = 1;
13023:                             }
13024:                             if ($domconfig{$action}{$itemid}{'incdom'} ne $confhash{$itemid}{'incdom'}) {
13025:                                 $changes{$itemid} = 1;
13026:                             }
13027:                         }
13028:                     }
13029:                     $allpos[$newpos] = $itemid;
13030:                 }
13031:                 if ($imgdeletions{$itemid}) {
13032:                     $changes{$itemid} = 1;
13033:                     #FIXME need to obsolete item in RES space
13034:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
13035:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
13036:                                                                  $itemid,$configuserok,$switchserver,
13037:                                                                  $author_ok);
13038:                     if ($imgurl) {
13039:                         $confhash{$itemid}{'image'} = $imgurl;
13040:                         $changes{$itemid} = 1;
13041:                     }
13042:                     if ($error) {
13043:                         &Apache::lonnet::logthis($error);
13044:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13045:                     }
13046:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
13047:                     $confhash{$itemid}{'image'} =
13048:                        $domconfig{$action}{$itemid}{'image'};
13049:                 }
13050:                 if ($customadds{$i}) {
13051:                     my $name = $env{'form.ltitools_custom_name_'.$i};
13052:                     $name =~ s/(`)/'/g;
13053:                     $name =~ s/^\s+//;
13054:                     $name =~ s/\s+$//;
13055:                     my $value = $env{'form.ltitools_custom_value_'.$i};
13056:                     $value =~ s/(`)/'/g;
13057:                     $value =~ s/^\s+//;
13058:                     $value =~ s/\s+$//;
13059:                     if ($name ne '') {
13060:                         $confhash{$itemid}{'custom'}{$name} = $value;
13061:                         $changes{$itemid} = 1;
13062:                     }
13063:                 }
13064:                 my %customdels;
13065:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i); 
13066:                 if (@customdeletions) {
13067:                     $changes{$itemid} = 1;
13068:                 }
13069:                 map { $customdels{$_} = 1; } @customdeletions;
13070:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
13071:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
13072:                         unless ($customdels{$key}) {
13073:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
13074:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i}; 
13075:                             }
13076:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
13077:                                 $changes{$itemid} = 1;
13078:                             }
13079:                         }
13080:                     }
13081:                 }
13082:                 unless ($changes{$itemid}) {
13083:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
13084:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
13085:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
13086:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
13087:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
13088:                                         $changes{$itemid} = 1;
13089:                                         last;
13090:                                     }
13091:                                 }
13092:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
13093:                                 $changes{$itemid} = 1;
13094:                             }
13095:                         }
13096:                         last if ($changes{$itemid});
13097:                     }
13098:                 }
13099:             }
13100:         }
13101:     }
13102:     if (@allpos > 0) {
13103:         my $idx = 0;
13104:         foreach my $itemid (@allpos) {
13105:             if ($itemid ne '') {
13106:                 $confhash{$itemid}{'order'} = $idx;
13107:                 if (ref($domconfig{$action}) eq 'HASH') {
13108:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
13109:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
13110:                             $changes{$itemid} = 1;
13111:                         }
13112:                     }
13113:                 }
13114:                 $idx ++;
13115:             }
13116:         }
13117:     }
13118:     my %ltitoolshash = (
13119:                           $action => { %confhash }
13120:                        );
13121:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
13122:                                              $dom);
13123:     if ($putresult eq 'ok') {
13124:         my %ltienchash = (
13125:                              $action => { %encconfig }
13126:                          );
13127:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
13128:         if (keys(%changes) > 0) {
13129:             my $cachetime = 24*60*60;
13130:             my %ltiall = %confhash;
13131:             foreach my $id (keys(%ltiall)) {
13132:                 if (ref($encconfig{$id}) eq 'HASH') {
13133:                     foreach my $item ('key','secret') {
13134:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
13135:                     }
13136:                 }
13137:             }
13138:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%ltiall,$cachetime);
13139:             if (ref($lastactref) eq 'HASH') {
13140:                 $lastactref->{'ltitools'} = 1;
13141:             }
13142:             $resulttext = &mt('Changes made:').'<ul>';
13143:             my %bynum;
13144:             foreach my $itemid (sort(keys(%changes))) {
13145:                 my $position = $confhash{$itemid}{'order'};
13146:                 $bynum{$position} = $itemid;
13147:             }
13148:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
13149:                 my $itemid = $bynum{$pos}; 
13150:                 if (ref($confhash{$itemid}) ne 'HASH') {
13151:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
13152:                 } else {
13153:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
13154:                     if ($confhash{$itemid}{'image'}) {
13155:                         $resulttext .= '&nbsp;'.
13156:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
13157:                                        ' alt="'.&mt('Tool Provider icon').'" />';
13158:                     }
13159:                     $resulttext .= '</li><ul>';
13160:                     my $position = $pos + 1;
13161:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
13162:                     foreach my $item ('version','msgtype','sigmethod','url','lifetime') {
13163:                         if ($confhash{$itemid}{$item} ne '') {
13164:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
13165:                         }
13166:                     }
13167:                     if ($encconfig{$itemid}{'key'} ne '') {
13168:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
13169:                     }
13170:                     if ($encconfig{$itemid}{'secret'} ne '') {
13171:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
13172:                         my $num = length($encconfig{$itemid}{'secret'});
13173:                         $resulttext .= ('*'x$num).'</li>';
13174:                     }
13175:                     $resulttext .= '<li>'.&mt('Configurable in course:');
13176:                     my @possconfig = ('label','title','target','linktext','explanation','append');
13177:                     my $numconfig = 0; 
13178:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') { 
13179:                         foreach my $item (@possconfig) {
13180:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
13181:                                 $numconfig ++;
13182:                                 $resulttext .= ' "'.$lt{'crs'.$item}.'"';
13183:                             }
13184:                         }
13185:                     }
13186:                     if (!$numconfig) {
13187:                         $resulttext .= '&nbsp;'.&mt('None');
13188:                     }
13189:                     $resulttext .= '</li>';
13190:                     foreach my $item ('passback','roster') {
13191:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
13192:                         if ($confhash{$itemid}{$item}) {
13193:                             $resulttext .= &mt('Yes');
13194:                             if ($confhash{$itemid}{$item.'valid'}) {
13195:                                 if ($item eq 'passback') {
13196:                                     $resulttext .= ' '.&mt('valid for at least [quant,_1,day] after launch',
13197:                                                            $confhash{$itemid}{$item.'valid'});
13198:                                 } else {
13199:                                     $resulttext .= ' '.&mt('valid for at least [quant,_1,second] after launch',
13200:                                                            $confhash{$itemid}{$item.'valid'});
13201:                                 }
13202:                             }
13203:                         } else {
13204:                             $resulttext .= &mt('No');
13205:                         }
13206:                         $resulttext .= '</li>';
13207:                     }
13208:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
13209:                         my $displaylist;
13210:                         if ($confhash{$itemid}{'display'}{'target'}) {
13211:                             $displaylist = &mt('Display target').':&nbsp;'.
13212:                                            $confhash{$itemid}{'display'}{'target'}.',';
13213:                         }
13214:                         foreach my $size ('width','height') { 
13215:                             if ($confhash{$itemid}{'display'}{$size}) {
13216:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
13217:                                                 $confhash{$itemid}{'display'}{$size}.',';
13218:                             }
13219:                         }
13220:                         if ($displaylist) {
13221:                             $displaylist =~ s/,$//;
13222:                             $resulttext .= '<li>'.$displaylist.'</li>';
13223:                         }
13224:                         foreach my $item ('linktext','explanation') {
13225:                             if ($confhash{$itemid}{'display'}{$item}) {
13226:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{'display'}{$item}.'</li>';
13227:                             }
13228:                         }
13229:                     }
13230:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
13231:                         my $fieldlist;
13232:                         foreach my $field (@allfields) {
13233:                             if ($confhash{$itemid}{'fields'}{$field}) {
13234:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
13235:                             }
13236:                         }
13237:                         if ($fieldlist) {
13238:                             $fieldlist =~ s/,$//;
13239:                             if ($confhash{$itemid}{'fields'}{'user'}) {
13240:                                 if ($confhash{$itemid}{'incdom'}) {
13241:                                     $fieldlist .= ' ('.&mt('username:domain').')';
13242:                                 } else {
13243:                                     $fieldlist .= ' ('.&mt('username').')';
13244:                                 }
13245:                             }
13246:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
13247:                         }
13248:                     }
13249:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
13250:                         my $rolemaps;
13251:                         foreach my $role (@courseroles) {
13252:                             if ($confhash{$itemid}{'roles'}{$role}) {
13253:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
13254:                                              $confhash{$itemid}{'roles'}{$role}.',';
13255:                             }
13256:                         }
13257:                         if ($rolemaps) {
13258:                             $rolemaps =~ s/,$//; 
13259:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
13260:                         }
13261:                     }
13262:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
13263:                         my $customlist;
13264:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
13265:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
13266:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
13267:                             } 
13268:                         }
13269:                         if ($customlist) {
13270:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
13271:                         }
13272:                     } 
13273:                     $resulttext .= '</ul></li>';
13274:                 }
13275:             }
13276:             $resulttext .= '</ul>';
13277:         } else {
13278:             $resulttext = &mt('No changes made.');
13279:         }
13280:     } else {
13281:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
13282:     }
13283:     if ($errors) {
13284:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
13285:                        $errors.'</ul>';
13286:     }
13287:     return $resulttext;
13288: }
13289: 
13290: sub process_ltitools_image {
13291:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
13292:     my $filename = $env{'form.'.$caller.'.filename'};
13293:     my ($error,$url);
13294:     my ($width,$height) = (21,21);
13295:     if ($configuserok eq 'ok') {
13296:         if ($switchserver) {
13297:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
13298:                          $switchserver);
13299:         } elsif ($author_ok eq 'ok') {
13300:             my ($result,$imageurl,$madethumb) =
13301:                 &publishlogo($r,'upload',$caller,$dom,$confname,
13302:                              "ltitools/$itemid/icon",$width,$height);
13303:             if ($result eq 'ok') {
13304:                 if ($madethumb) {
13305:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
13306:                     my $imagethumb = "$path/tn-".$imagefile;
13307:                     $url = $imagethumb;
13308:                 } else {
13309:                     $url = $imageurl;
13310:                 }
13311:             } else {
13312:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
13313:             }
13314:         } else {
13315:             $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);
13316:         }
13317:     } else {
13318:         $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);
13319:     }
13320:     return ($url,$error);
13321: }
13322: 
13323: sub get_ltitools_id {
13324:     my ($cdom,$title) = @_;
13325:     # get lock on ltitools db
13326:     my $lockhash = {
13327:                       lock => $env{'user.name'}.
13328:                               ':'.$env{'user.domain'},
13329:                    };
13330:     my $tries = 0;
13331:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
13332:     my ($id,$error);
13333:  
13334:     while (($gotlock ne 'ok') && ($tries<10)) {
13335:         $tries ++;
13336:         sleep (0.1);
13337:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
13338:     }
13339:     if ($gotlock eq 'ok') {
13340:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
13341:         if ($currids{'lock'}) {
13342:             delete($currids{'lock'});
13343:             if (keys(%currids)) {
13344:                 my @curr = sort { $a <=> $b } keys(%currids);
13345:                 if ($curr[-1] =~ /^\d+$/) {
13346:                     $id = 1 + $curr[-1];
13347:                 }
13348:             } else {
13349:                 $id = 1;
13350:             }
13351:             if ($id) {
13352:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
13353:                     $error = 'nostore';
13354:                 }
13355:             } else {
13356:                 $error = 'nonumber';
13357:             }
13358:         }
13359:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
13360:     } else {
13361:         $error = 'nolock';
13362:     }
13363:     return ($id,$error);
13364: }
13365: 
13366: sub modify_proctoring {
13367:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
13368:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13369:     my (@allpos,%changes,%confhash,%encconfhash,$errors,$resulttext,%imgdeletions);
13370:     my $confname = $dom.'-domainconfig';
13371:     my $servadm = $r->dir_config('lonAdmEMail');
13372:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
13373:     my %providernames = &proctoring_providernames();
13374:     my $maxnum = scalar(keys(%providernames));
13375: 
13376:     my (%requserfields,%optuserfields,%defaults,%extended,%crsconf,@courseroles,@ltiroles);
13377:     my ($requref,$opturef,$defref,$extref,$crsref,$rolesref,$ltiref) = &proctoring_data();
13378:     if (ref($requref) eq 'HASH') {
13379:         %requserfields = %{$requref};
13380:     }
13381:     if (ref($opturef) eq 'HASH') {
13382:         %optuserfields = %{$opturef};
13383:     }
13384:     if (ref($defref) eq 'HASH') {
13385:         %defaults = %{$defref};
13386:     }
13387:     if (ref($extref) eq 'HASH') {
13388:         %extended = %{$extref};
13389:     }
13390:     if (ref($crsref) eq 'HASH') {
13391:         %crsconf = %{$crsref};
13392:     }
13393:     if (ref($rolesref) eq 'ARRAY') {
13394:         @courseroles = @{$rolesref};
13395:     }
13396:     if (ref($ltiref) eq 'ARRAY') {
13397:         @ltiroles = @{$ltiref};
13398:     }
13399: 
13400:     if (ref($domconfig{$action}) eq 'HASH') {
13401:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.proctoring_image_del');
13402:         if (@todeleteimages) {
13403:             map { $imgdeletions{$_} = 1; } @todeleteimages;
13404:         }
13405:     }
13406:     my %customadds;
13407:     my @newcustom = &Apache::loncommon::get_env_multiple('form.proctoring_customadd');
13408:     if (@newcustom) {
13409:         map { $customadds{$_} = 1; } @newcustom;
13410:     }
13411:     foreach my $provider (sort(keys(%providernames))) {
13412:         $confhash{$provider} = {};
13413:         my $pos = $env{'form.proctoring_pos_'.$provider};
13414:         $pos =~ s/\D+//g;
13415:         $allpos[$pos] = $provider;
13416:         my (%current,%currentenc);
13417:         my $showroles = 0;
13418:         if (ref($domconfig{$action}) eq 'HASH') {
13419:             if (ref($domconfig{$action}{$provider}) eq 'HASH') {
13420:                 %current = %{$domconfig{$action}{$provider}};
13421:                 foreach my $item ('key','secret') { 
13422:                     $currentenc{$item} = $current{$item};
13423:                     delete($current{$item});
13424:                 }
13425:             }
13426:         }
13427:         if ($env{'form.proctoring_available_'.$provider}) {
13428:             $confhash{$provider}{'available'} = 1;
13429:             unless ($current{'available'}) {
13430:                 $changes{$provider} = 1;
13431:             }
13432:         } else {
13433:             %{$confhash{$provider}} = %current;
13434:             %{$encconfhash{$provider}} = %currentenc;
13435:             $confhash{$provider}{'available'} = 0;
13436:             if ($current{'available'}) {
13437:                 $changes{$provider} = 1;
13438:             }
13439:         }
13440:         if ($confhash{$provider}{'available'}) {
13441:             foreach my $field ('lifetime','version','sigmethod','url','key','secret') {
13442:                 my $possval = $env{'form.proctoring_'.$provider.'_'.$field};
13443:                 if ($field eq 'lifetime') {
13444:                     if ($possval =~ /^\d+$/) {
13445:                         $confhash{$provider}{$field} = $possval;
13446:                     }
13447:                 } elsif ($field eq 'version') {
13448:                     if ($possval =~ /^\d+\.\d+$/) {
13449:                         $confhash{$provider}{$field} = $possval;
13450:                     }
13451:                 } elsif ($field eq 'sigmethod') {
13452:                     if ($possval =~ /^\QHMAC-SHA\E(1|256)$/) {
13453:                         $confhash{$provider}{$field} = $possval;
13454:                     }
13455:                 } elsif ($field eq 'url') {
13456:                     $confhash{$provider}{$field} = $possval;
13457:                 } elsif (($field eq 'key') || ($field eq 'secret')) {
13458:                     $encconfhash{$provider}{$field} = $possval;
13459:                     unless ($currentenc{$field} eq $possval) {
13460:                         $changes{$provider} = 1;
13461:                     }
13462:                 }
13463:                 unless (($field eq 'key') || ($field eq 'secret')) {
13464:                     unless ($current{$field} eq $confhash{$provider}{$field}) {
13465:                         $changes{$provider} = 1;
13466:                     }
13467:                 }
13468:             }
13469:             if ($imgdeletions{$provider}) {
13470:                 $changes{$provider} = 1;
13471:             } elsif ($env{'form.proctoring_image_'.$provider.'.filename'} ne '') {
13472:                 my ($imageurl,$error) =
13473:                     &process_proctoring_image($r,$dom,$confname,'proctoring_image_'.$provider,$provider,
13474:                                               $configuserok,$switchserver,$author_ok);
13475:                 if ($imageurl) {
13476:                     $confhash{$provider}{'image'} = $imageurl;
13477:                     $changes{$provider} = 1; 
13478:                 }
13479:                 if ($error) {
13480:                     &Apache::lonnet::logthis($error);
13481:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13482:                 }
13483:             } elsif (exists($current{'image'})) {
13484:                 $confhash{$provider}{'image'} = $current{'image'};
13485:             }
13486:             if (ref($requserfields{$provider}) eq 'ARRAY') {
13487:                 if (@{$requserfields{$provider}} > 0) {
13488:                     if (grep(/^user$/,@{$requserfields{$provider}})) {
13489:                         if ($env{'form.proctoring_userincdom_'.$provider}) {
13490:                             $confhash{$provider}{'incdom'} = 1;
13491:                         }
13492:                         unless ($current{'incdom'} eq $confhash{$provider}{'incdom'}) {
13493:                             $changes{$provider} = 1;
13494:                         }
13495:                     }
13496:                     if (grep(/^roles$/,@{$requserfields{$provider}})) {
13497:                         $showroles = 1;
13498:                     }
13499:                 }
13500:             }
13501:             $confhash{$provider}{'fields'} = [];
13502:             if (ref($optuserfields{$provider}) eq 'ARRAY') {
13503:                 if (@{$optuserfields{$provider}} > 0) {
13504:                     my @optfields = &Apache::loncommon::get_env_multiple('form.proctoring_optional_'.$provider);
13505:                     foreach my $field (@{$optuserfields{$provider}}) {
13506:                         if (grep(/^\Q$field\E$/,@optfields)) {
13507:                             push(@{$confhash{$provider}{'fields'}},$field);
13508:                         }
13509:                     }
13510:                 }
13511:                 if (ref($current{'fields'}) eq 'ARRAY') {
13512:                     unless ($changes{$provider}) {
13513:                         my @new = sort(@{$confhash{$provider}{'fields'}});
13514:                         my @old = sort(@{$current{'fields'}}); 
13515:                         my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
13516:                         if (@diffs) {
13517:                             $changes{$provider} = 1;
13518:                         }
13519:                     }
13520:                 } elsif (@{$confhash{$provider}{'fields'}}) {
13521:                     $changes{$provider} = 1;
13522:                 }
13523:             }
13524:             if (ref($defaults{$provider}) eq 'ARRAY') {  
13525:                 if (@{$defaults{$provider}} > 0) {
13526:                     my %options;
13527:                     if (ref($extended{$provider}) eq 'HASH') {
13528:                         %options = %{$extended{$provider}};
13529:                     }
13530:                     my @checked = &Apache::loncommon::get_env_multiple('form.proctoring_defaults_'.$provider);
13531:                     foreach my $field (@{$defaults{$provider}}) {
13532:                         if ((exists($options{$field})) && (ref($options{$field}) eq 'ARRAY')) {
13533:                             my $poss = $env{'form.proctoring_defaults_'.$field.'_'.$provider};
13534:                             if (grep(/^\Q$poss\E$/,@{$options{$field}})) {
13535:                                 push(@{$confhash{$provider}{'defaults'}},$poss); 
13536:                             }
13537:                         } elsif ((exists($options{$field})) && (ref($options{$field}) eq 'HASH')) {
13538:                             foreach my $inner (keys(%{$options{$field}})) {
13539:                                 if (ref($options{$field}{$inner}) eq 'ARRAY') {
13540:                                     my $poss = $env{'form.proctoring_'.$inner.'_'.$provider};
13541:                                     if (grep(/^\Q$poss\E$/,@{$options{$field}{$inner}})) {
13542:                                         $confhash{$provider}{'defaults'}{$inner} = $poss;
13543:                                     }
13544:                                 } else {
13545:                                     $confhash{$provider}{'defaults'}{$inner} = $env{'form.proctoring_'.$inner.'_'.$provider};
13546:                                 }
13547:                             }
13548:                         } else {
13549:                             if (grep(/^\Q$field\E$/,@checked)) {
13550:                                 push(@{$confhash{$provider}{'defaults'}},$field);
13551:                             }
13552:                         }
13553:                     }
13554:                     if (ref($confhash{$provider}{'defaults'}) eq 'ARRAY') {
13555:                         if (ref($current{'defaults'}) eq 'ARRAY') {
13556:                             unless ($changes{$provider}) {
13557:                                 my @new = sort(@{$confhash{$provider}{'defaults'}});
13558:                                 my @old = sort(@{$current{'defaults'}});
13559:                                 my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
13560:                                 if (@diffs) {
13561:                                     $changes{$provider} = 1; 
13562:                                 }
13563:                             }
13564:                         } elsif (ref($current{'defaults'}) eq 'ARRAY') {
13565:                             if (@{$current{'defaults'}}) {
13566:                                 $changes{$provider} = 1;
13567:                             }
13568:                         }
13569:                     } elsif (ref($confhash{$provider}{'defaults'}) eq 'HASH') {
13570:                         if (ref($current{'defaults'}) eq 'HASH') {
13571:                             unless ($changes{$provider}) {
13572:                                 foreach my $key (keys(%{$confhash{$provider}{'defaults'}})) {
13573:                                     unless ($confhash{$provider}{'defaults'}{$key} eq $current{'defaults'}{$key}) {
13574:                                         $changes{$provider} = 1;
13575:                                         last;
13576:                                     }
13577:                                 }
13578:                             }
13579:                             unless ($changes{$provider}) {
13580:                                 foreach my $key (keys(%{$current{'defaults'}})) {
13581:                                     unless ($current{'defaults'}{$key} eq $confhash{$provider}{'defaults'}{$key}) {
13582:                                         $changes{$provider} = 1;
13583:                                         last;
13584:                                     }
13585:                                 }
13586:                             }
13587:                         } elsif (keys(%{$confhash{$provider}{'defaults'}})) {
13588:                             $changes{$provider} = 1;
13589:                         }
13590:                     }
13591:                 }
13592:             }
13593:             if (ref($crsconf{$provider}) eq 'ARRAY') {
13594:                 if (@{$crsconf{$provider}} > 0) {
13595:                     $confhash{$provider}{'crsconf'} = [];
13596:                     my @checked = &Apache::loncommon::get_env_multiple('form.proctoring_crsconf_'.$provider);
13597:                     foreach my $crsfield (@{$crsconf{$provider}}) {
13598:                         if (grep(/^\Q$crsfield\E$/,@checked)) {
13599:                             push(@{$confhash{$provider}{'crsconf'}},$crsfield);
13600:                         }
13601:                     }
13602:                     if (ref($current{'crsconf'}) eq 'ARRAY') {
13603:                         unless ($changes{$provider}) {  
13604:                             my @new = sort(@{$confhash{$provider}{'crsconf'}});
13605:                             my @old = sort(@{$current{'crsconf'}});
13606:                             my @diffs = &Apache::loncommon::compare_arrays(\@new,\@old);
13607:                             if (@diffs) {
13608:                                 $changes{$provider} = 1;
13609:                             }
13610:                         }
13611:                     } elsif (@{$confhash{$provider}{'crsconf'}}) {
13612:                         $changes{$provider} = 1;
13613:                     }
13614:                 }
13615:             }
13616:             if ($showroles) {
13617:                 $confhash{$provider}{'roles'} = {};
13618:                 foreach my $role (@courseroles) {
13619:                     my $poss = $env{'form.proctoring_roles_'.$role.'_'.$provider};
13620:                     if (grep(/^\Q$poss\E$/,@ltiroles)) {
13621:                         $confhash{$provider}{'roles'}{$role} = $poss;
13622:                     }
13623:                 }
13624:                 unless ($changes{$provider}) {
13625:                     if (ref($current{'roles'}) eq 'HASH') {
13626:                         foreach my $role (keys(%{$current{'roles'}})) {
13627:                             unless ($current{'roles'}{$role} eq $confhash{$provider}{'roles'}{$role}) {
13628:                                 $changes{$provider} = 1;
13629:                                 last
13630:                             }
13631:                         }
13632:                         unless ($changes{$provider}) {
13633:                             foreach my $role (keys(%{$confhash{$provider}{'roles'}})) {
13634:                                 unless ($confhash{$provider}{'roles'}{$role} eq $current{'roles'}{$role}) {
13635:                                     $changes{$provider} = 1;
13636:                                     last;
13637:                                 }
13638:                             }
13639:                         }
13640:                     } elsif (keys(%{$confhash{$provider}{'roles'}})) {
13641:                         $changes{$provider} = 1;
13642:                     }
13643:                 }
13644:             }
13645:             if (ref($current{'custom'}) eq 'HASH') {
13646:                 my @customdels = &Apache::loncommon::get_env_multiple('form.proctoring_customdel_'.$provider);
13647:                 foreach my $key (keys(%{$current{'custom'}})) {
13648:                     if (grep(/^\Q$key\E$/,@customdels)) {
13649:                         $changes{$provider} = 1;
13650:                     } else {
13651:                         $confhash{$provider}{'custom'}{$key} = $env{'form.proctoring_customval_'.$key.'_'.$provider};
13652:                         if ($confhash{$provider}{'custom'}{$key} ne $current{'custom'}{$key}) {
13653:                             $changes{$provider} = 1;
13654:                         }
13655:                     }
13656:                 }
13657:             }
13658:             if ($customadds{$provider}) {
13659:                 my $name = $env{'form.proctoring_custom_name_'.$provider};
13660:                 $name =~ s/(`)/'/g;
13661:                 $name =~ s/^\s+//;
13662:                 $name =~ s/\s+$//;
13663:                 my $value = $env{'form.proctoring_custom_value_'.$provider};
13664:                 $value =~ s/(`)/'/g;
13665:                 $value =~ s/^\s+//;
13666:                 $value =~ s/\s+$//;
13667:                 if ($name ne '') {
13668:                     $confhash{$provider}{'custom'}{$name} = $value;
13669:                     $changes{$provider} = 1;
13670:                 }
13671:             }
13672:         }
13673:     }
13674:     if (@allpos > 0) {
13675:         my $idx = 0;
13676:         foreach my $provider (@allpos) {
13677:             if ($provider ne '') {
13678:                 $confhash{$provider}{'order'} = $idx;
13679:                 unless ($changes{$provider}) {
13680:                     if (ref($domconfig{$action}) eq 'HASH') {
13681:                         if (ref($domconfig{$action}{$provider}) eq 'HASH') {
13682:                             if ($domconfig{$action}{$provider}{'order'} ne $idx) {
13683:                                 $changes{$provider} = 1;
13684:                             }
13685:                         }
13686:                     }
13687:                 }
13688:                 $idx ++;
13689:             }
13690:         }
13691:     }
13692:     my %proc_hash = (
13693:                           $action => { %confhash }
13694:                        );
13695:     my $putresult = &Apache::lonnet::put_dom('configuration',\%proc_hash,
13696:                                              $dom);
13697:     if ($putresult eq 'ok') {
13698:         my %proc_enchash = (
13699:                              $action => { %encconfhash }
13700:                          );
13701:         &Apache::lonnet::put_dom('encconfig',\%proc_enchash,$dom);
13702:         if (keys(%changes) > 0) {
13703:             my $cachetime = 24*60*60;
13704:             my %procall = %confhash;
13705:             foreach my $provider (keys(%procall)) {
13706:                 if (ref($encconfhash{$provider}) eq 'HASH') {
13707:                     foreach my $key ('key','secret') {
13708:                         $procall{$provider}{$key} = $encconfhash{$provider}{$key};
13709:                     }
13710:                 }
13711:             }
13712:             &Apache::lonnet::do_cache_new('proctoring',$dom,\%procall,$cachetime);
13713:             if (ref($lastactref) eq 'HASH') {
13714:                 $lastactref->{'proctoring'} = 1;
13715:             }
13716:             $resulttext = &mt('Configuration for Provider(s) with changes:').'<ul>';
13717:             my %bynum;
13718:             foreach my $provider (sort(keys(%changes))) {
13719:                 my $position = $confhash{$provider}{'order'};
13720:                 $bynum{$position} = $provider;
13721:             }
13722:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
13723:                 my $provider = $bynum{$pos};
13724:                 my %lt = &proctoring_titles($provider);
13725:                 my %fieldtitles = &proctoring_fieldtitles($provider);
13726:                 if (!$confhash{$provider}{'available'}) {
13727:                     $resulttext .= '<li>'.&mt('Proctoring integration unavailable for: [_1]','<b>'.$providernames{$provider}.'</b>').'</li>';
13728:                 } else {
13729:                     $resulttext .= '<li>'.&mt('Proctoring integration available for: [_1]','<b>'.$providernames{$provider}.'</b>');
13730:                     if ($confhash{$provider}{'image'}) {
13731:                         $resulttext .= '&nbsp;'.
13732:                                        '<img src="'.$confhash{$provider}{'image'}.'"'.
13733:                                        ' alt="'.&mt('Proctoring icon').'" />';
13734:                     }
13735:                     $resulttext .= '<ul>';
13736:                     my $position = $pos + 1;
13737:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
13738:                     foreach my $key ('version','sigmethod','url','lifetime') {
13739:                         if ($confhash{$provider}{$key} ne '') {
13740:                             $resulttext .= '<li>'.$lt{$key}.':&nbsp;'.$confhash{$provider}{$key}.'</li>';
13741:                         }
13742:                     }
13743:                     if ($encconfhash{$provider}{'key'} ne '') {
13744:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfhash{$provider}{'key'}.'</li>';
13745:                     }
13746:                     if ($encconfhash{$provider}{'secret'} ne '') {
13747:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
13748:                         my $num = length($encconfhash{$provider}{'secret'});
13749:                         $resulttext .= ('*'x$num).'</li>';
13750:                     }
13751:                     my (@fields,$showroles);
13752:                     if (ref($requserfields{$provider}) eq 'ARRAY') {
13753:                         push(@fields,@{$requserfields{$provider}});
13754:                     }
13755:                     if (ref($confhash{$provider}{'fields'}) eq 'ARRAY') {
13756:                         push(@fields,@{$confhash{$provider}{'fields'}});
13757:                     } elsif (ref($confhash{$provider}{'fields'}) eq 'HASH') {
13758:                         push(@fields,(keys(%{$confhash{$provider}{'fields'}})));
13759:                     }
13760:                     if (@fields) {
13761:                         if (grep(/^roles$/,@fields)) {
13762:                             $showroles = 1;
13763:                         }
13764:                         $resulttext .= '<li>'.$lt{'udsl'}.':&nbsp;"'.
13765:                                        join('", "', map { $lt{$_}; } @fields).'"</li>';
13766:                     }
13767:                     if (ref($requserfields{$provider}) eq 'ARRAY') {
13768:                         if (grep(/^user$/,@{$requserfields{$provider}})) {
13769:                             if ($confhash{$provider}{'incdom'}) {
13770:                                 $resulttext .= '<li>'.&mt('[_1] sent as [_2]',$lt{'user'},$lt{'uname:dom'}).'</li>';
13771:                             } else { 
13772:                                 $resulttext .= '<li>'.&mt('[_1] sent as [_2]',$lt{'user'},$lt{'username'}).'</li>';
13773:                             }
13774:                         }
13775:                     }
13776:                     if (ref($confhash{$provider}{'defaults'}) eq 'ARRAY') {
13777:                         if (@{$confhash{$provider}{'defaults'}} > 0) {
13778:                             $resulttext .= '<li>'.$lt{'defa'};
13779:                             foreach my $field (@{$confhash{$provider}{'defaults'}}) {
13780:                                 $resulttext .= ' "'.$fieldtitles{$field}.'",';
13781:                             }
13782:                             $resulttext =~ s/,$//;
13783:                             $resulttext .= '</li>';
13784:                         }
13785:                     } elsif (ref($confhash{$provider}{'defaults'}) eq 'HASH') {
13786:                         if (keys(%{$confhash{$provider}{'defaults'}})) {
13787:                             $resulttext .= '<li>'.$lt{'defa'}.':&nbsp;<ul>';
13788:                             foreach my $key (sort(keys(%{$confhash{$provider}{'defaults'}}))) {
13789:                                 if ($confhash{$provider}{'defaults'}{$key} ne '') {
13790:                                     $resulttext .= '<li>'.$fieldtitles{$key}.' = '.$confhash{$provider}{'defaults'}{$key}.'</li>';
13791:                                 }
13792:                             }
13793:                             $resulttext .= '</ul></li>';
13794:                         }
13795:                     }
13796:                     if (ref($crsconf{$provider}) eq 'ARRAY') {
13797:                         if (@{$crsconf{$provider}} > 0) {
13798:                             $resulttext .= '<li>'.&mt('Configurable in course:');
13799:                             my $numconfig = 0;
13800:                             if (ref($confhash{$provider}{'crsconf'}) eq 'ARRAY') {
13801:                                 if (@{$confhash{$provider}{'crsconf'}} > 0) {
13802:                                     foreach my $field (@{$confhash{$provider}{'crsconf'}}) {
13803:                                         $numconfig ++;
13804:                                         if ($provider eq 'examity') {
13805:                                             $resulttext .= ' "'.$lt{'crs'.$field}.'",';
13806:                                         } else {
13807:                                             $resulttext .= ' "'.$fieldtitles{$field}.'",';
13808:                                         }
13809:                                     }
13810:                                     $resulttext =~ s/,$//;
13811:                                 }
13812:                             }
13813:                             if (!$numconfig) {
13814:                                 $resulttext .= '&nbsp;'.&mt('None');
13815:                             }
13816:                             $resulttext .= '</li>';
13817:                         }
13818:                     }
13819:                     if ($showroles) {
13820:                         if (ref($confhash{$provider}{'roles'}) eq 'HASH') {
13821:                             my $rolemaps;
13822:                             foreach my $role (@courseroles) {
13823:                                 if ($confhash{$provider}{'roles'}{$role}) {
13824:                                     $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
13825:                                                  $confhash{$provider}{'roles'}{$role}.',';
13826:                                 }
13827:                             }
13828:                             if ($rolemaps) {
13829:                                 $rolemaps =~ s/,$//;
13830:                                 $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
13831:                             }
13832:                         }
13833:                     }
13834:                     if (ref($confhash{$provider}{'custom'}) eq 'HASH') {
13835:                         my $customlist;
13836:                         if (keys(%{$confhash{$provider}{'custom'}})) {
13837:                             foreach my $key (sort(keys(%{$confhash{$provider}{'custom'}}))) {
13838:                                 $customlist .= $key.'='.$confhash{$provider}{'custom'}{$key}.', ';
13839:                             }
13840:                             $customlist =~ s/,$//;
13841:                         }
13842:                         if ($customlist) {
13843:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
13844:                         }
13845:                     } 
13846:                     $resulttext .= '</ul></li>';
13847:                 }
13848:             }
13849:             $resulttext .= '</ul>';
13850:         } else {
13851:             $resulttext = &mt('No changes made.');
13852:         }
13853:     } else {
13854:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
13855:     }
13856:     if ($errors) {
13857:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
13858:                        $errors.'</ul>';
13859:     }
13860:     return $resulttext;
13861: }
13862: 
13863: sub process_proctoring_image {
13864:     my ($r,$dom,$confname,$caller,$provider,$configuserok,$switchserver,$author_ok) = @_;
13865:     my $filename = $env{'form.'.$caller.'.filename'};
13866:     my ($error,$url);
13867:     my ($width,$height) = (21,21);
13868:     if ($configuserok eq 'ok') {
13869:         if ($switchserver) {
13870:             $error = &mt('Upload of Remote Proctoring Provider icon is not permitted to this server: [_1]',
13871:                          $switchserver);
13872:         } elsif ($author_ok eq 'ok') {
13873:             my ($result,$imageurl,$madethumb) =
13874:                 &publishlogo($r,'upload',$caller,$dom,$confname,
13875:                              "proctoring/$provider/icon",$width,$height);
13876:             if ($result eq 'ok') {
13877:                 if ($madethumb) {
13878:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
13879:                     my $imagethumb = "$path/tn-".$imagefile;
13880:                     $url = $imagethumb;
13881:                 } else {
13882:                     $url = $imageurl;
13883:                 }
13884:             } else {
13885:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
13886:             }
13887:         } else {
13888:             $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);
13889:         }
13890:     } else {
13891:         $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);
13892:     }
13893:     return ($url,$error);
13894: }
13895: 
13896: sub modify_lti {
13897:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
13898:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13899:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
13900:     my (%posslti,%posslticrs,%posscrstype);
13901:     my @courseroles = ('cc','in','ta','ep','st');
13902:     my @ltiroles = qw(Learner Instructor ContentDeveloper TeachingAssistant Mentor Member Manager Administrator);
13903:     my @lticourseroles = qw(Instructor TeachingAssistant Mentor Learner);
13904:     my @coursetypes = ('official','unofficial','community','textbook','placement');
13905:     my %coursetypetitles = &Apache::lonlocal::texthash (
13906:                                official   => 'Official',
13907:                                unofficial => 'Unofficial',
13908:                                community  => 'Community',
13909:                                textbook   => 'Textbook',
13910:                                placement  => 'Placement Test',
13911:     );
13912:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
13913:     my %lt = &lti_names();
13914:     map { $posslti{$_} = 1; } @ltiroles;
13915:     map { $posslticrs{$_} = 1; } @lticourseroles;
13916:     map { $posscrstype{$_} = 1; } @coursetypes;
13917: 
13918:     my %menutitles = &ltimenu_titles();
13919: 
13920:     my (@items,%deletions,%itemids);
13921:     if ($env{'form.lti_add'}) {
13922:         my $consumer = $env{'form.lti_consumer_add'};
13923:         $consumer =~ s/(`)/'/g;
13924:         ($newid,my $error) = &get_lti_id($dom,$consumer);
13925:         if ($newid) {
13926:             $itemids{'add'} = $newid;
13927:             push(@items,'add');
13928:             $changes{$newid} = 1;
13929:         } else {
13930:             my $error = &mt('Failed to acquire unique ID for new LTI configuration');
13931:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13932:         }
13933:     }
13934:     if (ref($domconfig{$action}) eq 'HASH') {
13935:         my @todelete = &Apache::loncommon::get_env_multiple('form.lti_del');
13936:         if (@todelete) {
13937:             map { $deletions{$_} = 1; } @todelete;
13938:         }
13939:         my $maxnum = $env{'form.lti_maxnum'};
13940:         for (my $i=0; $i<=$maxnum; $i++) {
13941:             my $itemid = $env{'form.lti_id_'.$i};
13942:             $itemid =~ s/\D+//g;
13943:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
13944:                 if ($deletions{$itemid}) {
13945:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'consumer'};
13946:                 } else {
13947:                    push(@items,$i);
13948:                    $itemids{$i} = $itemid;
13949:                 }
13950:             }
13951:         }
13952:     }
13953:     foreach my $idx (@items) {
13954:         my $itemid = $itemids{$idx};
13955:         next unless ($itemid);
13956:         my $position = $env{'form.lti_pos_'.$idx};
13957:         $position =~ s/\D+//g;
13958:         if ($position ne '') {
13959:             $allpos[$position] = $itemid;
13960:         }
13961:         foreach my $item ('consumer','key','secret','lifetime','requser') {
13962:             my $formitem = 'form.lti_'.$item.'_'.$idx;
13963:             $env{$formitem} =~ s/(`)/'/g;
13964:             if ($item eq 'lifetime') {
13965:                 $env{$formitem} =~ s/[^\d.]//g;
13966:             }
13967:             if ($env{$formitem} ne '') {
13968:                 if (($item eq 'key') || ($item eq 'secret')) {
13969:                     $encconfig{$itemid}{$item} = $env{$formitem};
13970:                 } else {
13971:                     $confhash{$itemid}{$item} = $env{$formitem};
13972:                     unless (($idx eq 'add') || ($changes{$itemid})) {
13973:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
13974:                             $changes{$itemid} = 1;
13975:                         }
13976:                     }
13977:                 }
13978:             }
13979:         }
13980:         if ($env{'form.lti_version_'.$idx} eq 'LTI-1p0') {
13981:             $confhash{$itemid}{'version'} = $env{'form.lti_version_'.$idx};
13982:         }
13983:         if ($confhash{$itemid}{'requser'}) {
13984:             if ($env{'form.lti_mapuser_'.$idx} eq 'sourcedid') {
13985:                 $confhash{$itemid}{'mapuser'} = 'lis_person_sourcedid'; 
13986:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'email') {
13987:                 $confhash{$itemid}{'mapuser'} = 'lis_person_contact_email_primary';
13988:             } elsif ($env{'form.lti_mapuser_'.$idx} eq 'other') {
13989:                 my $mapuser = $env{'form.lti_customuser_'.$idx};
13990:                 $mapuser =~ s/(`)/'/g;
13991:                 $mapuser =~ s/^\s+|\s+$//g; 
13992:                 $confhash{$itemid}{'mapuser'} = $mapuser; 
13993:             }
13994:             foreach my $ltirole (@lticourseroles) {
13995:                 my $possrole = $env{'form.lti_maprole_'.$ltirole.'_'.$idx};
13996:                 if (grep(/^\Q$possrole\E$/,@courseroles)) {
13997:                     $confhash{$itemid}{'maproles'}{$ltirole} = $possrole;
13998:                 }
13999:             }
14000:             my @possmakeuser = &Apache::loncommon::get_env_multiple('form.lti_makeuser_'.$idx);
14001:             my @makeuser;
14002:             foreach my $ltirole (sort(@possmakeuser)) {
14003:                 if ($posslti{$ltirole}) {
14004:                     push(@makeuser,$ltirole);
14005:                 }
14006:             }
14007:             $confhash{$itemid}{'makeuser'} = \@makeuser;
14008:             if (@makeuser) {
14009:                 my $lcauth = $env{'form.lti_lcauth_'.$idx};
14010:                 if ($lcauth =~ /^(internal|krb4|krb5|localauth)$/) {
14011:                     $confhash{$itemid}{'lcauth'} = $lcauth;
14012:                     if ($lcauth ne 'internal') {
14013:                         my $lcauthparm = $env{'form.lti_lcauthparm_'.$idx};
14014:                         $lcauthparm =~ s/^(\s+|\s+)$//g;
14015:                         $lcauthparm =~ s/`//g;
14016:                         if ($lcauthparm ne '') {
14017:                             $confhash{$itemid}{'lcauthparm'} = $lcauthparm;
14018:                         }
14019:                     }
14020:                 } else {
14021:                     $confhash{$itemid}{'lcauth'} = 'lti';
14022:                 }
14023:             }
14024:             my @possinstdata =  &Apache::loncommon::get_env_multiple('form.lti_instdata_'.$idx);
14025:             if (@possinstdata) {
14026:                 foreach my $field (@possinstdata) {
14027:                     if (exists($fieldtitles{$field})) {
14028:                         push(@{$confhash{$itemid}{'instdata'}});
14029:                     }
14030:                 }
14031:             }
14032:             if (($env{'form.lti_mapcrs_'.$idx} eq 'course_offering_sourcedid') ||
14033:                 ($env{'form.lti_mapcrs_'.$idx} eq 'context_id'))  {
14034:                 $confhash{$itemid}{'mapcrs'} = $env{'form.lti_mapcrs_'.$idx};
14035:             } elsif ($env{'form.lti_mapcrs_'.$idx} eq 'other') {
14036:                 my $mapcrs = $env{'form.lti_mapcrsfield_'.$idx}; 
14037:                 $mapcrs =~ s/(`)/'/g;
14038:                 $mapcrs =~ s/^\s+|\s+$//g;
14039:                 $confhash{$itemid}{'mapcrs'} = $mapcrs;
14040:             }
14041:             my @posstypes = &Apache::loncommon::get_env_multiple('form.lti_mapcrstype_'.$idx);
14042:             my @crstypes;
14043:             foreach my $type (sort(@posstypes)) {
14044:                 if ($posscrstype{$type}) {
14045:                     push(@crstypes,$type);
14046:                 }
14047:             }
14048:             $confhash{$itemid}{'mapcrstype'} = \@crstypes;
14049:             if ($env{'form.lti_makecrs_'.$idx}) {
14050:                 $confhash{$itemid}{'makecrs'} = 1;
14051:             }
14052:             my @possenroll = &Apache::loncommon::get_env_multiple('form.lti_selfenroll_'.$idx);
14053:             my @selfenroll;
14054:             foreach my $type (sort(@possenroll)) {
14055:                 if ($posslticrs{$type}) {
14056:                     push(@selfenroll,$type);
14057:                 }
14058:             }
14059:             $confhash{$itemid}{'selfenroll'} = \@selfenroll;
14060:             if ($env{'form.lti_crssec_'.$idx}) {
14061:                 if ($env{'form.lti_crssecsrc_'.$idx} eq 'course_section_sourcedid') {
14062:                     $confhash{$itemid}{'section'} = $env{'form.lti_crssecsrc_'.$idx};
14063:                 } elsif ($env{'form.lti_crssecsrc_'.$idx} eq 'other') {
14064:                     my $section = $env{'form.lti_customsection_'.$idx};
14065:                     $section =~ s/(`)/'/g;
14066:                     $section =~ s/^\s+|\s+$//g;
14067:                     if ($section ne '') {
14068:                         $confhash{$itemid}{'section'} = $section;
14069:                     }
14070:                 }
14071:             }
14072:             if ($env{'form.lti_callback_'.$idx}) {
14073:                 if ($env{'form.lti_callbackparam_'.$idx}) {
14074:                     my $callback = $env{'form.lti_callbackparam_'.$idx};
14075:                     $callback =~ s/^\s+|\s+$//g;
14076:                     $confhash{$itemid}{'callback'} = $callback;
14077:                 }
14078:             }
14079:             foreach my $field ('passback','roster','topmenu','inlinemenu') {
14080:                 if ($env{'form.lti_'.$field.'_'.$idx}) {
14081:                     $confhash{$itemid}{$field} = 1;
14082:                 }
14083:             }
14084:             if ($env{'form.lti_passback_'.$idx}) {
14085:                 if ($env{'form.lti_passbackformat_'.$idx} eq '1.0') {
14086:                     $confhash{$itemid}{'passbackformat'} = '1.0';
14087:                 } else {
14088:                     $confhash{$itemid}{'passbackformat'} = '1.1';
14089:                 }
14090:             }
14091:             if ($env{'form.lti_topmenu_'.$idx} || $env{'form.lti_inlinemenu_'.$idx}) {
14092:                 $confhash{$itemid}{lcmenu} = [];
14093:                 my @possmenu = &Apache::loncommon::get_env_multiple('form.lti_menuitem_'.$idx);
14094:                 foreach my $field (@possmenu) {
14095:                     if (exists($menutitles{$field})) {
14096:                         if ($field eq 'grades') {
14097:                             next unless ($env{'form.lti_inlinemenu_'.$idx});
14098:                         }
14099:                         push(@{$confhash{$itemid}{lcmenu}},$field);
14100:                     }
14101:                 }
14102:             }
14103:             unless (($idx eq 'add') || ($changes{$itemid})) {
14104:                 foreach my $field ('mapuser','mapcrs','makecrs','section','passback','roster','lcauth','lcauthparm','topmenu','inlinemenu','callback') {
14105:                     if ($domconfig{$action}{$itemid}{$field} ne $confhash{$itemid}{$field}) {
14106:                         $changes{$itemid} = 1;
14107:                     }
14108:                 }
14109:                 unless ($changes{$itemid}) {
14110:                     if ($domconfig{$action}{$itemid}{'passback'} eq $confhash{$itemid}{'passback'}) {
14111:                         if ($domconfig{$action}{$itemid}{'passbackformat'} ne $confhash{$itemid}{'passbackformat'}) {
14112:                             $changes{$itemid} = 1;
14113:                         }
14114:                     }
14115:                 }
14116:                 foreach my $field ('makeuser','mapcrstype','selfenroll','instdata','lcmenu') {
14117:                     unless ($changes{$itemid}) {
14118:                         if (ref($domconfig{$action}{$itemid}{$field}) eq 'ARRAY') {
14119:                             if (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
14120:                                 my @diffs = &Apache::loncommon::compare_arrays($domconfig{$action}{$itemid}{$field},
14121:                                                                                $confhash{$itemid}{$field});
14122:                                 if (@diffs) {
14123:                                     $changes{$itemid} = 1;
14124:                                 }
14125:                             } elsif (@{$domconfig{$action}{$itemid}{$field}} > 0) {
14126:                                 $changes{$itemid} = 1;
14127:                             }
14128:                         } elsif (ref($confhash{$itemid}{$field}) eq 'ARRAY') {
14129:                             if (@{$confhash{$itemid}{$field}} > 0) {
14130:                                 $changes{$itemid} = 1;
14131:                             }
14132:                         }
14133:                     }
14134:                 }
14135:                 unless ($changes{$itemid}) {
14136:                     if (ref($domconfig{$action}{$itemid}{'maproles'}) eq 'HASH') {
14137:                         if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
14138:                             foreach my $ltirole (keys(%{$domconfig{$action}{$itemid}{'maproles'}})) {
14139:                                 if ($domconfig{$action}{$itemid}{'maproles'}{$ltirole} ne 
14140:                                     $confhash{$itemid}{'maproles'}{$ltirole}) {
14141:                                     $changes{$itemid} = 1;
14142:                                     last;
14143:                                 }
14144:                             }
14145:                             unless ($changes{$itemid}) {
14146:                                 foreach my $ltirole (keys(%{$confhash{$itemid}{'maproles'}})) {
14147:                                     if ($confhash{$itemid}{'maproles'}{$ltirole} ne 
14148:                                         $domconfig{$action}{$itemid}{'maproles'}{$ltirole}) {
14149:                                         $changes{$itemid} = 1;
14150:                                         last;
14151:                                     }
14152:                                 }
14153:                             }
14154:                         } elsif (keys(%{$domconfig{$action}{$itemid}{'maproles'}}) > 0) {
14155:                             $changes{$itemid} = 1;
14156:                         }
14157:                     } elsif (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
14158:                         unless ($changes{$itemid}) {
14159:                             if (keys(%{$confhash{$itemid}{'maproles'}}) > 0) {
14160:                                 $changes{$itemid} = 1;
14161:                             }
14162:                         }
14163:                     }
14164:                 }
14165:             }
14166:         }
14167:     }
14168:     if (@allpos > 0) {
14169:         my $idx = 0;
14170:         foreach my $itemid (@allpos) {
14171:             if ($itemid ne '') {
14172:                 $confhash{$itemid}{'order'} = $idx;
14173:                 if (ref($domconfig{$action}) eq 'HASH') {
14174:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
14175:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
14176:                             $changes{$itemid} = 1;
14177:                         }
14178:                     }
14179:                 }
14180:                 $idx ++;
14181:             }
14182:         }
14183:     }
14184:     my %ltihash = (
14185:                           $action => { %confhash }
14186:                        );
14187:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltihash,
14188:                                              $dom);
14189:     if ($putresult eq 'ok') {
14190:         my %ltienchash = (
14191:                              $action => { %encconfig }
14192:                          );
14193:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
14194:         if (keys(%changes) > 0) {
14195:             my $cachetime = 24*60*60;
14196:             my %ltiall = %confhash;
14197:             foreach my $id (keys(%ltiall)) {
14198:                 if (ref($encconfig{$id}) eq 'HASH') {
14199:                     foreach my $item ('key','secret') {
14200:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
14201:                     }
14202:                 }
14203:             }
14204:             &Apache::lonnet::do_cache_new('lti',$dom,\%ltiall,$cachetime);
14205:             if (ref($lastactref) eq 'HASH') {
14206:                 $lastactref->{'lti'} = 1;
14207:             }
14208:             $resulttext = &mt('Changes made:').'<ul>';
14209:             my %bynum;
14210:             foreach my $itemid (sort(keys(%changes))) {
14211:                 my $position = $confhash{$itemid}{'order'};
14212:                 $bynum{$position} = $itemid;
14213:             }
14214:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
14215:                 my $itemid = $bynum{$pos};
14216:                 if (ref($confhash{$itemid}) ne 'HASH') {
14217:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
14218:                 } else {
14219:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'consumer'}.'</b></li><ul>';
14220:                     my $position = $pos + 1;
14221:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
14222:                     foreach my $item ('version','lifetime') {
14223:                         if ($confhash{$itemid}{$item} ne '') {
14224:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
14225:                         }
14226:                     }
14227:                     if ($encconfig{$itemid}{'key'} ne '') {
14228:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
14229:                     }
14230:                     if ($encconfig{$itemid}{'secret'} ne '') {
14231:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
14232:                         my $num = length($encconfig{$itemid}{'secret'});
14233:                         $resulttext .= ('*'x$num).'</li>';
14234:                     }
14235:                     if ($confhash{$itemid}{'requser'}) {
14236:                         if ($confhash{$itemid}{'mapuser'}) {
14237:                             my $shownmapuser;
14238:                             if ($confhash{$itemid}{'mapuser'} eq 'lis_person_sourcedid') {
14239:                                 $shownmapuser = $lt{'sourcedid'}.' (lis_person_sourcedid)';
14240:                             } elsif ($confhash{$itemid}{'mapuser'} eq 'lis_person_contact_email_primary') {
14241:                                 $shownmapuser = $lt{'email'}.' (lis_person_contact_email_primary)';
14242:                             } else {
14243:                                 $shownmapuser = &mt('Other').' ('.$confhash{$itemid}{'mapuser'}.')';
14244:                             }
14245:                             $resulttext .= '<li>'.&mt('LON-CAPA username').': '.$shownmapuser.'</li>';
14246:                         }
14247:                         if (ref($confhash{$itemid}{'maproles'}) eq 'HASH') {
14248:                             my $rolemaps;
14249:                             foreach my $role (@ltiroles) {
14250:                                 if ($confhash{$itemid}{'maproles'}{$role}) {
14251:                                     $rolemaps .= ('&nbsp;'x2).$role.'='.
14252:                                                  &Apache::lonnet::plaintext($confhash{$itemid}{'maproles'}{$role},
14253:                                                                             'Course').',';
14254:                                 }
14255:                             }
14256:                             if ($rolemaps) {
14257:                                 $rolemaps =~ s/,$//;
14258:                                 $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
14259:                             }
14260:                         }
14261:                         if (ref($confhash{$itemid}{'makeuser'}) eq 'ARRAY') {
14262:                             if (@{$confhash{$itemid}{'makeuser'}} > 0) { 
14263:                                 $resulttext .= '<li>'.&mt('Following roles may create user accounts: [_1]',
14264:                                                           join(', ',@{$confhash{$itemid}{'makeuser'}})).'<br />';
14265:                                 if ($confhash{$itemid}{'lcauth'} eq 'lti') {
14266:                                     $resulttext .= &mt('New users will only be able to authenticate via LTI').'</li>';
14267:                                 } else {
14268:                                     $resulttext .= &mt('New users will be assigned LON-CAPA authentication: [_1]',
14269:                                                        $confhash{$itemid}{'lcauth'});
14270:                                     if ($confhash{$itemid}{'lcauth'} eq 'internal') {
14271:                                         $resulttext .= '; '.&mt('a randomly generated password will be created');
14272:                                     } elsif ($confhash{$itemid}{'lcauth'} eq 'localauth') {
14273:                                         if ($confhash{$itemid}{'lcauthparm'} ne '') {
14274:                                             $resulttext .= ' '.&mt('with argument: [_1]',$confhash{$itemid}{'lcauthparm'});
14275:                                         }
14276:                                     } else {
14277:                                         $resulttext .= '; '.&mt('Kerberos domain: [_1]',$confhash{$itemid}{'lcauthparm'});
14278:                                     }
14279:                                 }
14280:                                 $resulttext .= '</li>';
14281:                             } else {
14282:                                 $resulttext .= '<li>'.&mt('User account creation not permitted.').'</li>';
14283:                             }
14284:                         }
14285:                         if (ref($confhash{$itemid}{'instdata'}) eq 'ARRAY') {
14286:                             if (@{$confhash{$itemid}{'instdata'}} > 0) {
14287:                                 $resulttext .= '<li>'.&mt('Institutional data will be used when creating a new user for: [_1]',
14288:                                                           join(', ',map { $fieldtitles{$_}; } @{$confhash{$itemid}{'instdata'}})).'</li>';
14289:                             } else {
14290:                                 $resulttext .= '<li>'.&mt('No institutional data used when creating a new user.').'</li>';
14291:                             }
14292:                         }
14293:                         if ($confhash{$itemid}{'mapcrs'}) {
14294:                             $resulttext .= '<li>'.&mt('Unique course identifier').': '.$confhash{$itemid}{'mapcrs'}.'</li>';
14295:                         }
14296:                         if (ref($confhash{$itemid}{'mapcrstype'}) eq 'ARRAY') {
14297:                             if (@{$confhash{$itemid}{'mapcrstype'}} > 0) {
14298:                                 $resulttext .= '<li>'.&mt('Mapping for the following LON-CAPA course types: [_1]',
14299:                                                join(', ',map { $coursetypetitles{$_}; } @coursetypes)).
14300:                                                '</li>';
14301:                             } else {
14302:                                 $resulttext .= '<li>'.&mt('No mapping to LON-CAPA courses').'</li>';
14303:                             }
14304:                         }
14305:                         if ($confhash{$itemid}{'makecrs'}) {
14306:                             $resulttext .= '<li>'.&mt('Instructor may create course (if absent).').'</li>';
14307:                         } else {
14308:                             $resulttext .= '<li>'.&mt('Instructor may not create course (if absent).').'</li>';
14309:                         }
14310:                         if (ref($confhash{$itemid}{'selfenroll'}) eq 'ARRAY') {
14311:                             if (@{$confhash{$itemid}{'selfenroll'}} > 0) {
14312:                                 $resulttext .= '<li>'.&mt('Self-enrollment for following roles: [_1]',
14313:                                                           join(', ',@{$confhash{$itemid}{'selfenroll'}})).
14314:                                                '</li>';
14315:                             } else {
14316:                                 $resulttext .= '<li>'.&mt('Self-enrollment not permitted').'</li>';
14317:                             }
14318:                         }
14319:                         if ($confhash{$itemid}{'section'}) {
14320:                             if ($confhash{$itemid}{'section'} eq 'course_section_sourcedid') {
14321:                                 $resulttext .= '<li>'.&mt('User section from standard field:').
14322:                                                      ' (course_section_sourcedid)'.'</li>';  
14323:                             } else {
14324:                                 $resulttext .= '<li>'.&mt('User section from:').' '.
14325:                                                       $confhash{$itemid}{'section'}.'</li>';
14326:                             }
14327:                         } else {
14328:                             $resulttext .= '<li>'.&mt('No section assignment').'</li>';
14329:                         }
14330:                         if ($confhash{$itemid}{'callback'}) {
14331:                             $resulttext .= '<li>'.&mt('Callback setting').': '.$confhash{$itemid}{'callback'}.'</li>';
14332:                         } else {
14333:                             $resulttext .= '<li>'.&mt('No callback to logout LON-CAPA session when user logs out of Comsumer');
14334:                         }
14335:                         foreach my $item ('passback','roster','topmenu','inlinemenu') {
14336:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;';
14337:                             if ($confhash{$itemid}{$item}) {
14338:                                 $resulttext .= &mt('Yes');
14339:                                 if ($item eq 'passback') {
14340:                                     if ($confhash{$itemid}{'passbackformat'} eq '1.0') {
14341:                                         $resulttext .= '&nbsp;('.&mt('Outcomes Extension (1.0)').')';
14342:                                     } elsif ($confhash{$itemid}{'passbackformat'} eq '1.1') {
14343:                                         $resulttext .= '&nbsp;('.&mt('Outcomes Service (1.1)').')';
14344:                                     }
14345:                                 }
14346:                             } else {
14347:                                 $resulttext .= &mt('No');
14348:                             }
14349:                             $resulttext .= '</li>';
14350:                         }
14351:                         if (ref($confhash{$itemid}{'lcmenu'}) eq 'ARRAY') {
14352:                             if (@{$confhash{$itemid}{'lcmenu'}} > 0) {
14353:                                 $resulttext .= '<li>'.&mt('Menu items:').' '.
14354:                                                join(', ', map { $menutitles{$_}; } (@{$confhash{$itemid}{'lcmenu'}})).'</li>'; 
14355:                             } else {
14356:                                 $resulttext .= '<li>'.&mt('No menu items displayed in header or online menu').'</li>'; 
14357:                             }
14358:                         }
14359:                     }
14360:                     $resulttext .= '</ul></li>';
14361:                 }
14362:             }
14363:             $resulttext .= '</ul>';
14364:         } else {
14365:             $resulttext = &mt('No changes made.');
14366:         }
14367:     } else {
14368:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
14369:     }
14370:     if ($errors) {
14371:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
14372:                        $errors.'</ul>';
14373:     }
14374:     return $resulttext;
14375: }
14376: 
14377: sub get_lti_id {
14378:     my ($domain,$consumer) = @_;
14379:     # get lock on lti db
14380:     my $lockhash = {
14381:                       lock => $env{'user.name'}.
14382:                               ':'.$env{'user.domain'},
14383:                    };
14384:     my $tries = 0;
14385:     my $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
14386:     my ($id,$error);
14387: 
14388:     while (($gotlock ne 'ok') && ($tries<10)) {
14389:         $tries ++;
14390:         sleep (0.1);
14391:         $gotlock = &Apache::lonnet::newput_dom('lti',$lockhash,$domain);
14392:     }
14393:     if ($gotlock eq 'ok') {
14394:         my %currids = &Apache::lonnet::dump_dom('lti',$domain);
14395:         if ($currids{'lock'}) {
14396:             delete($currids{'lock'});
14397:             if (keys(%currids)) {
14398:                 my @curr = sort { $a <=> $b } keys(%currids);
14399:                 if ($curr[-1] =~ /^\d+$/) {
14400:                     $id = 1 + $curr[-1];
14401:                 }
14402:             } else {
14403:                 $id = 1;
14404:             }
14405:             if ($id) {
14406:                 unless (&Apache::lonnet::newput_dom('lti',{ $id => $consumer },$domain) eq 'ok') {
14407:                     $error = 'nostore';
14408:                 }
14409:             } else {
14410:                 $error = 'nonumber';
14411:             }
14412:         }
14413:         my $dellockoutcome = &Apache::lonnet::del_dom('lti',['lock'],$domain);
14414:     } else {
14415:         $error = 'nolock';
14416:     }
14417:     return ($id,$error);
14418: }
14419: 
14420: sub modify_autoenroll {
14421:     my ($dom,$lastactref,%domconfig) = @_;
14422:     my ($resulttext,%changes);
14423:     my %currautoenroll;
14424:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
14425:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
14426:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
14427:         }
14428:     }
14429:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
14430:     my %title = ( run => 'Auto-enrollment active',
14431:                   sender => 'Sender for notification messages',
14432:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
14433:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
14434:     my @offon = ('off','on');
14435:     my $sender_uname = $env{'form.sender_uname'};
14436:     my $sender_domain = $env{'form.sender_domain'};
14437:     if ($sender_domain eq '') {
14438:         $sender_uname = '';
14439:     } elsif ($sender_uname eq '') {
14440:         $sender_domain = '';
14441:     }
14442:     my $coowners = $env{'form.autoassign_coowners'};
14443:     my $failsafe = $env{'form.autoenroll_failsafe'};
14444:     $failsafe =~ s{^\s+|\s+$}{}g;
14445:     if ($failsafe =~ /\D/) {
14446:         undef($failsafe);
14447:     }
14448:     my %autoenrollhash =  (
14449:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
14450:                                        'sender_uname' => $sender_uname,
14451:                                        'sender_domain' => $sender_domain,
14452:                                        'co-owners' => $coowners,
14453:                                        'autofailsafe' => $failsafe,
14454:                                 }
14455:                      );
14456:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
14457:                                              $dom);
14458:     if ($putresult eq 'ok') {
14459:         if (exists($currautoenroll{'run'})) {
14460:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
14461:                  $changes{'run'} = 1;
14462:              }
14463:         } elsif ($autorun) {
14464:             if ($env{'form.autoenroll_run'} ne '1') {
14465:                  $changes{'run'} = 1;
14466:             }
14467:         }
14468:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
14469:             $changes{'sender'} = 1;
14470:         }
14471:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
14472:             $changes{'sender'} = 1;
14473:         }
14474:         if ($currautoenroll{'co-owners'} ne '') {
14475:             if ($currautoenroll{'co-owners'} ne $coowners) {
14476:                 $changes{'coowners'} = 1;
14477:             }
14478:         } elsif ($coowners) {
14479:             $changes{'coowners'} = 1;
14480:         }
14481:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
14482:             $changes{'autofailsafe'} = 1;
14483:         }
14484:         if (keys(%changes) > 0) {
14485:             $resulttext = &mt('Changes made:').'<ul>';
14486:             if ($changes{'run'}) {
14487:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
14488:             }
14489:             if ($changes{'sender'}) {
14490:                 if ($sender_uname eq '' || $sender_domain eq '') {
14491:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
14492:                 } else {
14493:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
14494:                 }
14495:             }
14496:             if ($changes{'coowners'}) {
14497:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
14498:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
14499:                 if (ref($lastactref) eq 'HASH') {
14500:                     $lastactref->{'domainconfig'} = 1;
14501:                 }
14502:             }
14503:             if ($changes{'autofailsafe'}) {
14504:                 if ($failsafe ne '') {
14505:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
14506:                 } else {
14507:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
14508:                 }
14509:                 &Apache::lonnet::get_domain_defaults($dom,1);
14510:                 if (ref($lastactref) eq 'HASH') {
14511:                     $lastactref->{'domdefaults'} = 1;
14512:                 }
14513:             }
14514:             $resulttext .= '</ul>';
14515:         } else {
14516:             $resulttext = &mt('No changes made to auto-enrollment settings');
14517:         }
14518:     } else {
14519:         $resulttext = '<span class="LC_error">'.
14520: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
14521:     }
14522:     return $resulttext;
14523: }
14524: 
14525: sub modify_autoupdate {
14526:     my ($dom,%domconfig) = @_;
14527:     my ($resulttext,%currautoupdate,%fields,%changes);
14528:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
14529:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
14530:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
14531:         }
14532:     }
14533:     my @offon = ('off','on');
14534:     my %title = &Apache::lonlocal::texthash (
14535:                    run => 'Auto-update:',
14536:                    classlists => 'Updates to user information in classlists?'
14537:                 );
14538:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
14539:     my %fieldtitles = &Apache::lonlocal::texthash (
14540:                         id => 'Student/Employee ID',
14541:                         permanentemail => 'E-mail address',
14542:                         lastname => 'Last Name',
14543:                         firstname => 'First Name',
14544:                         middlename => 'Middle Name',
14545:                         generation => 'Generation',
14546:                       );
14547:     $othertitle = &mt('All users');
14548:     if (keys(%{$usertypes}) >  0) {
14549:         $othertitle = &mt('Other users');
14550:     }
14551:     foreach my $key (keys(%env)) {
14552:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
14553:             my ($usertype,$item) = ($1,$2);
14554:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
14555:                 if ($usertype eq 'default') {   
14556:                     push(@{$fields{$1}},$2);
14557:                 } elsif (ref($types) eq 'ARRAY') {
14558:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
14559:                         push(@{$fields{$1}},$2);
14560:                     }
14561:                 }
14562:             }
14563:         }
14564:     }
14565:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
14566:     @lockablenames = sort(@lockablenames);
14567:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
14568:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
14569:         if (@changed) {
14570:             $changes{'lockablenames'} = 1;
14571:         }
14572:     } else {
14573:         if (@lockablenames) {
14574:             $changes{'lockablenames'} = 1;
14575:         }
14576:     }
14577:     my %updatehash = (
14578:                       autoupdate => { run => $env{'form.autoupdate_run'},
14579:                                       classlists => $env{'form.classlists'},
14580:                                       fields => {%fields},
14581:                                       lockablenames => \@lockablenames,
14582:                                     }
14583:                      );
14584:     foreach my $key (keys(%currautoupdate)) {
14585:         if (($key eq 'run') || ($key eq 'classlists')) {
14586:             if (exists($updatehash{autoupdate}{$key})) {
14587:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
14588:                     $changes{$key} = 1;
14589:                 }
14590:             }
14591:         } elsif ($key eq 'fields') {
14592:             if (ref($currautoupdate{$key}) eq 'HASH') {
14593:                 foreach my $item (@{$types},'default') {
14594:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
14595:                         my $change = 0;
14596:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
14597:                             if (!exists($fields{$item})) {
14598:                                 $change = 1;
14599:                                 last;
14600:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
14601:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
14602:                                     $change = 1;
14603:                                     last;
14604:                                 }
14605:                             }
14606:                         }
14607:                         if ($change) {
14608:                             push(@{$changes{$key}},$item);
14609:                         }
14610:                     } 
14611:                 }
14612:             }
14613:         } elsif ($key eq 'lockablenames') {
14614:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
14615:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
14616:                 if (@changed) {
14617:                     $changes{'lockablenames'} = 1;
14618:                 }
14619:             } else {
14620:                 if (@lockablenames) {
14621:                     $changes{'lockablenames'} = 1;
14622:                 }
14623:             }
14624:         }
14625:     }
14626:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
14627:         if (@lockablenames) {
14628:             $changes{'lockablenames'} = 1;
14629:         }
14630:     }
14631:     foreach my $item (@{$types},'default') {
14632:         if (defined($fields{$item})) {
14633:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
14634:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
14635:                     my $change = 0;
14636:                     if (ref($fields{$item}) eq 'ARRAY') {
14637:                         foreach my $type (@{$fields{$item}}) {
14638:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
14639:                                 $change = 1;
14640:                                 last;
14641:                             }
14642:                         }
14643:                     }
14644:                     if ($change) {
14645:                         push(@{$changes{'fields'}},$item);
14646:                     }
14647:                 } else {
14648:                     push(@{$changes{'fields'}},$item);
14649:                 }
14650:             } else {
14651:                 push(@{$changes{'fields'}},$item);
14652:             }
14653:         }
14654:     }
14655:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
14656:                                              $dom);
14657:     if ($putresult eq 'ok') {
14658:         if (keys(%changes) > 0) {
14659:             $resulttext = &mt('Changes made:').'<ul>';
14660:             foreach my $key (sort(keys(%changes))) {
14661:                 if ($key eq 'lockablenames') {
14662:                     $resulttext .= '<li>';
14663:                     if (@lockablenames) {
14664:                         $usertypes->{'default'} = $othertitle;
14665:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
14666:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
14667:                     } else {
14668:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
14669:                     }
14670:                     $resulttext .= '</li>';
14671:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
14672:                     foreach my $item (@{$changes{$key}}) {
14673:                         my @newvalues;
14674:                         foreach my $type (@{$fields{$item}}) {
14675:                             push(@newvalues,$fieldtitles{$type});
14676:                         }
14677:                         my $newvaluestr;
14678:                         if (@newvalues > 0) {
14679:                             $newvaluestr = join(', ',@newvalues);
14680:                         } else {
14681:                             $newvaluestr = &mt('none');
14682:                         }
14683:                         if ($item eq 'default') {
14684:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
14685:                         } else {
14686:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
14687:                         }
14688:                     }
14689:                 } else {
14690:                     my $newvalue;
14691:                     if ($key eq 'run') {
14692:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
14693:                     } else {
14694:                         $newvalue = $offon[$env{'form.'.$key}];
14695:                     }
14696:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
14697:                 }
14698:             }
14699:             $resulttext .= '</ul>';
14700:         } else {
14701:             $resulttext = &mt('No changes made to autoupdates');
14702:         }
14703:     } else {
14704:         $resulttext = '<span class="LC_error">'.
14705: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
14706:     }
14707:     return $resulttext;
14708: }
14709: 
14710: sub modify_autocreate {
14711:     my ($dom,%domconfig) = @_;
14712:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
14713:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
14714:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
14715:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
14716:         }
14717:     }
14718:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
14719:                  req => 'Auto-creation of validated requests for official courses',
14720:                  xmldc => 'Identity of course creator of courses from XML files',
14721:                );
14722:     my @types = ('xml','req');
14723:     foreach my $item (@types) {
14724:         $newvals{$item} = $env{'form.autocreate_'.$item};
14725:         $newvals{$item} =~ s/\D//g;
14726:         $newvals{$item} = 0 if ($newvals{$item} eq '');
14727:     }
14728:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
14729:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
14730:     unless (exists($domcoords{$newvals{'xmldc'}})) {
14731:         $newvals{'xmldc'} = '';
14732:     } 
14733:     %autocreatehash =  (
14734:                         autocreate => { xml => $newvals{'xml'},
14735:                                         req => $newvals{'req'},
14736:                                       }
14737:                        );
14738:     if ($newvals{'xmldc'} ne '') {
14739:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
14740:     }
14741:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
14742:                                              $dom);
14743:     if ($putresult eq 'ok') {
14744:         my @items = @types;
14745:         if ($newvals{'xml'}) {
14746:             push(@items,'xmldc');
14747:         }
14748:         foreach my $item (@items) {
14749:             if (exists($currautocreate{$item})) {
14750:                 if ($currautocreate{$item} ne $newvals{$item}) {
14751:                     $changes{$item} = 1;
14752:                 }
14753:             } elsif ($newvals{$item}) {
14754:                 $changes{$item} = 1;
14755:             }
14756:         }
14757:         if (keys(%changes) > 0) {
14758:             my @offon = ('off','on'); 
14759:             $resulttext = &mt('Changes made:').'<ul>';
14760:             foreach my $item (@types) {
14761:                 if ($changes{$item}) {
14762:                     my $newtxt = $offon[$newvals{$item}];
14763:                     $resulttext .= '<li>'.
14764:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
14765:                                        '<b>','</b>').
14766:                                    '</li>';
14767:                 }
14768:             }
14769:             if ($changes{'xmldc'}) {
14770:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
14771:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
14772:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
14773:             }
14774:             $resulttext .= '</ul>';
14775:         } else {
14776:             $resulttext = &mt('No changes made to auto-creation settings');
14777:         }
14778:     } else {
14779:         $resulttext = '<span class="LC_error">'.
14780:             &mt('An error occurred: [_1]',$putresult).'</span>';
14781:     }
14782:     return $resulttext;
14783: }
14784: 
14785: sub modify_directorysrch {
14786:     my ($dom,$lastactref,%domconfig) = @_;
14787:     my ($resulttext,%changes);
14788:     my %currdirsrch;
14789:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
14790:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
14791:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
14792:         }
14793:     }
14794:     my %title = ( available => 'Institutional directory search available',
14795:                   localonly => 'Other domains can search institution',
14796:                   lcavailable => 'LON-CAPA directory search available',
14797:                   lclocalonly => 'Other domains can search LON-CAPA domain',
14798:                   searchby => 'Search types',
14799:                   searchtypes => 'Search latitude');
14800:     my @offon = ('off','on');
14801:     my @otherdoms = ('Yes','No');
14802: 
14803:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
14804:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
14805:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
14806: 
14807:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
14808:     if (keys(%{$usertypes}) == 0) {
14809:         @cansearch = ('default');
14810:     } else {
14811:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
14812:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
14813:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
14814:                     push(@{$changes{'cansearch'}},$type);
14815:                 }
14816:             }
14817:             foreach my $type (@cansearch) {
14818:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
14819:                     push(@{$changes{'cansearch'}},$type);
14820:                 }
14821:             }
14822:         } else {
14823:             push(@{$changes{'cansearch'}},@cansearch);
14824:         }
14825:     }
14826: 
14827:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
14828:         foreach my $by (@{$currdirsrch{'searchby'}}) {
14829:             if (!grep(/^\Q$by\E$/,@searchby)) {
14830:                 push(@{$changes{'searchby'}},$by);
14831:             }
14832:         }
14833:         foreach my $by (@searchby) {
14834:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
14835:                 push(@{$changes{'searchby'}},$by);
14836:             }
14837:         }
14838:     } else {
14839:         push(@{$changes{'searchby'}},@searchby);
14840:     }
14841: 
14842:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
14843:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
14844:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
14845:                 push(@{$changes{'searchtypes'}},$type);
14846:             }
14847:         }
14848:         foreach my $type (@searchtypes) {
14849:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
14850:                 push(@{$changes{'searchtypes'}},$type);
14851:             }
14852:         }
14853:     } else {
14854:         if (exists($currdirsrch{'searchtypes'})) {
14855:             foreach my $type (@searchtypes) {  
14856:                 if ($type ne $currdirsrch{'searchtypes'}) { 
14857:                     push(@{$changes{'searchtypes'}},$type);
14858:                 }
14859:             }
14860:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
14861:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
14862:             }   
14863:         } else {
14864:             push(@{$changes{'searchtypes'}},@searchtypes); 
14865:         }
14866:     }
14867: 
14868:     my %dirsrch_hash =  (
14869:             directorysrch => { available => $env{'form.dirsrch_available'},
14870:                                cansearch => \@cansearch,
14871:                                localonly => $env{'form.dirsrch_instlocalonly'},
14872:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
14873:                                lcavailable => $env{'form.dirsrch_domavailable'},
14874:                                searchby => \@searchby,
14875:                                searchtypes => \@searchtypes,
14876:                              }
14877:             );
14878:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
14879:                                              $dom);
14880:     if ($putresult eq 'ok') {
14881:         if (exists($currdirsrch{'available'})) {
14882:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
14883:                  $changes{'available'} = 1;
14884:              }
14885:         } else {
14886:             if ($env{'form.dirsrch_available'} eq '1') {
14887:                 $changes{'available'} = 1;
14888:             }
14889:         }
14890:         if (exists($currdirsrch{'lcavailable'})) {
14891:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
14892:                 $changes{'lcavailable'} = 1;
14893:             }
14894:         } else {
14895:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
14896:                 $changes{'lcavailable'} = 1;
14897:             }
14898:         }
14899:         if (exists($currdirsrch{'localonly'})) {
14900:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
14901:                 $changes{'localonly'} = 1;
14902:             }
14903:         } else {
14904:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
14905:                 $changes{'localonly'} = 1;
14906:             }
14907:         }
14908:         if (exists($currdirsrch{'lclocalonly'})) {
14909:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
14910:                 $changes{'lclocalonly'} = 1;
14911:             }
14912:         } else {
14913:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
14914:                 $changes{'lclocalonly'} = 1;
14915:             }
14916:         }
14917:         if (keys(%changes) > 0) {
14918:             $resulttext = &mt('Changes made:').'<ul>';
14919:             if ($changes{'available'}) {
14920:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
14921:             }
14922:             if ($changes{'lcavailable'}) {
14923:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
14924:             }
14925:             if ($changes{'localonly'}) {
14926:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
14927:             }
14928:             if ($changes{'lclocalonly'}) {
14929:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
14930:             }
14931:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
14932:                 my $chgtext;
14933:                 if (ref($usertypes) eq 'HASH') {
14934:                     if (keys(%{$usertypes}) > 0) {
14935:                         foreach my $type (@{$types}) {
14936:                             if (grep(/^\Q$type\E$/,@cansearch)) {
14937:                                 $chgtext .= $usertypes->{$type}.'; ';
14938:                             }
14939:                         }
14940:                         if (grep(/^default$/,@cansearch)) {
14941:                             $chgtext .= $othertitle;
14942:                         } else {
14943:                             $chgtext =~ s/\; $//;
14944:                         }
14945:                         $resulttext .=
14946:                             '<li>'.
14947:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
14948:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
14949:                             '</li>';
14950:                     }
14951:                 }
14952:             }
14953:             if (ref($changes{'searchby'}) eq 'ARRAY') {
14954:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
14955:                 my $chgtext;
14956:                 foreach my $type (@{$titleorder}) {
14957:                     if (grep(/^\Q$type\E$/,@searchby)) {
14958:                         if (defined($searchtitles->{$type})) {
14959:                             $chgtext .= $searchtitles->{$type}.'; ';
14960:                         }
14961:                     }
14962:                 }
14963:                 $chgtext =~ s/\; $//;
14964:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
14965:             }
14966:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
14967:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
14968:                 my $chgtext;
14969:                 foreach my $type (@{$srchtypeorder}) {
14970:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
14971:                         if (defined($srchtypes_desc->{$type})) {
14972:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
14973:                         }
14974:                     }
14975:                 }
14976:                 $chgtext =~ s/\; $//;
14977:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
14978:             }
14979:             $resulttext .= '</ul>';
14980:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
14981:             if (ref($lastactref) eq 'HASH') {
14982:                 $lastactref->{'directorysrch'} = 1;
14983:             }
14984:         } else {
14985:             $resulttext = &mt('No changes made to directory search settings');
14986:         }
14987:     } else {
14988:         $resulttext = '<span class="LC_error">'.
14989:                       &mt('An error occurred: [_1]',$putresult).'</span>';
14990:     }
14991:     return $resulttext;
14992: }
14993: 
14994: sub modify_contacts {
14995:     my ($dom,$lastactref,%domconfig) = @_;
14996:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
14997:     if (ref($domconfig{'contacts'}) eq 'HASH') {
14998:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
14999:             $currsetting{$key} = $domconfig{'contacts'}{$key};
15000:         }
15001:     }
15002:     my (%others,%to,%bcc,%includestr,%includeloc);
15003:     my @contacts = ('supportemail','adminemail');
15004:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
15005:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail','hostipmail');
15006:     my @toggles = ('reporterrors','reportupdates','reportstatus');
15007:     my @lonstatus = ('threshold','sysmail','weights','excluded');
15008:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
15009:     foreach my $type (@mailings) {
15010:         @{$newsetting{$type}} = 
15011:             &Apache::loncommon::get_env_multiple('form.'.$type);
15012:         foreach my $item (@contacts) {
15013:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
15014:                 $contacts_hash{contacts}{$type}{$item} = 1;
15015:             } else {
15016:                 $contacts_hash{contacts}{$type}{$item} = 0;
15017:             }
15018:         }
15019:         $others{$type} = $env{'form.'.$type.'_others'};
15020:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
15021:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
15022:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
15023:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
15024:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
15025:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
15026:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
15027:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
15028:             }
15029:         }
15030:     }
15031:     foreach my $item (@contacts) {
15032:         $to{$item} = $env{'form.'.$item};
15033:         $contacts_hash{'contacts'}{$item} = $to{$item};
15034:     }
15035:     foreach my $item (@toggles) {
15036:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
15037:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
15038:         }
15039:     }
15040:     my ($lonstatus_defs,$lonstatus_names) = &Apache::loncommon::lon_status_items();
15041:     foreach my $item (@lonstatus) {
15042:         if ($item eq 'excluded') {
15043:             my (%serverhomes,@excluded);
15044:             map { $serverhomes{$_} = 1; } values(%Apache::lonnet::serverhomeIDs);
15045:             my @possexcluded = &Apache::loncommon::get_env_multiple('form.errorexcluded');
15046:             if (@possexcluded) {
15047:                 foreach my $id (sort(@possexcluded)) {
15048:                     if ($serverhomes{$id}) {
15049:                         push(@excluded,$id);
15050:                     }
15051:                 }
15052:             }
15053:             if (@excluded) {
15054:                 $contacts_hash{'contacts'}{'lonstatus'}{$item} = \@excluded;
15055:             }
15056:         } elsif ($item eq 'weights') {
15057:             foreach my $type ('E','W','N') {
15058:                 $env{'form.error'.$item.'_'.$type} =~ s/^\s+|\s+$//g;
15059:                 if ($env{'form.error'.$item.'_'.$type} =~ /^\d+$/) {
15060:                     unless ($env{'form.error'.$item.'_'.$type} == $lonstatus_defs->{$type}) {
15061:                         $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type} =
15062:                             $env{'form.error'.$item.'_'.$type};
15063:                     }
15064:                 }
15065:             }
15066:         } elsif (($item eq 'threshold') || ($item eq 'sysmail')) {
15067:             $env{'form.error'.$item} =~ s/^\s+|\s+$//g;
15068:             if ($env{'form.error'.$item} =~ /^\d+$/) {
15069:                 unless ($env{'form.error'.$item} == $lonstatus_defs->{$item}) {
15070:                     $contacts_hash{'contacts'}{'lonstatus'}{$item} = $env{'form.error'.$item};
15071:                 }
15072:             }
15073:         }
15074:     }
15075:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
15076:         foreach my $field (@{$fields}) {
15077:             if (ref($possoptions->{$field}) eq 'ARRAY') {
15078:                 my $value = $env{'form.helpform_'.$field};
15079:                 $value =~ s/^\s+|\s+$//g;
15080:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
15081:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
15082:                     if ($field eq 'screenshot') {
15083:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
15084:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
15085:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
15086:                         }
15087:                     }
15088:                 }
15089:             }
15090:         }
15091:     }
15092:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
15093:     my (@statuses,%usertypeshash,@overrides);
15094:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
15095:         @statuses = @{$types};
15096:         if (ref($usertypes) eq 'HASH') {
15097:             %usertypeshash = %{$usertypes};
15098:         }
15099:     }
15100:     if (@statuses) {
15101:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
15102:         foreach my $type (@possoverrides) {
15103:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
15104:                 push(@overrides,$type);
15105:             }
15106:         }
15107:         if (@overrides) {
15108:             foreach my $type (@overrides) {
15109:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
15110:                 foreach my $item (@contacts) {
15111:                     if (grep(/^\Q$item\E$/,@standard)) {
15112:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
15113:                         $newsetting{'override_'.$type}{$item} = 1;
15114:                     } else {
15115:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
15116:                         $newsetting{'override_'.$type}{$item} = 0;
15117:                     }
15118:                 }
15119:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
15120:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
15121:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
15122:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
15123:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
15124:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
15125:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
15126:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
15127:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
15128:                 }
15129:             }     
15130:         }
15131:     }
15132:     if (keys(%currsetting) > 0) {
15133:         foreach my $item (@contacts) {
15134:             if ($to{$item} ne $currsetting{$item}) {
15135:                 $changes{$item} = 1;
15136:             }
15137:         }
15138:         foreach my $type (@mailings) {
15139:             foreach my $item (@contacts) {
15140:                 if (ref($currsetting{$type}) eq 'HASH') {
15141:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
15142:                         push(@{$changes{$type}},$item);
15143:                     }
15144:                 } else {
15145:                     push(@{$changes{$type}},@{$newsetting{$type}});
15146:                 }
15147:             }
15148:             if ($others{$type} ne $currsetting{$type}{'others'}) {
15149:                 push(@{$changes{$type}},'others');
15150:             }
15151:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
15152:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
15153:                     push(@{$changes{$type}},'bcc'); 
15154:                 }
15155:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
15156:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
15157:                     push(@{$changes{$type}},'include');
15158:                 }
15159:             }
15160:         }
15161:         if (ref($fields) eq 'ARRAY') {
15162:             if (ref($currsetting{'helpform'}) eq 'HASH') {
15163:                 foreach my $field (@{$fields}) {
15164:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
15165:                         push(@{$changes{'helpform'}},$field);
15166:                     }
15167:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
15168:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
15169:                             push(@{$changes{'helpform'}},'maxsize');
15170:                         }
15171:                     }
15172:                 }
15173:             } else {
15174:                 foreach my $field (@{$fields}) {
15175:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
15176:                         push(@{$changes{'helpform'}},$field);
15177:                     }
15178:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
15179:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
15180:                             push(@{$changes{'helpform'}},'maxsize');
15181:                         }
15182:                     }
15183:                 }
15184:             }
15185:         }
15186:         if (@statuses) {
15187:             if (ref($currsetting{'overrides'}) eq 'HASH') { 
15188:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
15189:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
15190:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
15191:                             foreach my $item (@contacts,'bcc','others','include') {
15192:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) { 
15193:                                     push(@{$changes{'overrides'}},$key);
15194:                                     last;
15195:                                 }
15196:                             }
15197:                         } else {
15198:                             push(@{$changes{'overrides'}},$key);
15199:                         }
15200:                     }
15201:                 }
15202:                 foreach my $key (@overrides) {
15203:                     unless (exists($currsetting{'overrides'}{$key})) {
15204:                         push(@{$changes{'overrides'}},$key);
15205:                     }
15206:                 }
15207:             } else {
15208:                 foreach my $key (@overrides) {
15209:                     push(@{$changes{'overrides'}},$key); 
15210:                 }
15211:             }
15212:         }
15213:         if (ref($currsetting{'lonstatus'}) eq 'HASH') {
15214:             foreach my $key ('excluded','weights','threshold','sysmail') {
15215:                 if ($key eq 'excluded') {
15216:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
15217:                         (ref($contacts_hash{contacts}{lonstatus}{excluded}) eq 'ARRAY')) {
15218:                         if ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
15219:                             (@{$currsetting{'lonstatus'}{$key}})) {
15220:                             my @diffs =
15221:                                 &Apache::loncommon::compare_arrays($contacts_hash{contacts}{lonstatus}{excluded},
15222:                                                                    $currsetting{'lonstatus'}{$key});
15223:                             if (@diffs) {
15224:                                 push(@{$changes{'lonstatus'}},$key);
15225:                             }
15226:                         } elsif (@{$contacts_hash{contacts}{lonstatus}{excluded}}) {
15227:                             push(@{$changes{'lonstatus'}},$key);
15228:                         }
15229:                     } elsif ((ref($currsetting{'lonstatus'}{$key}) eq 'ARRAY') &&
15230:                              (@{$currsetting{'lonstatus'}{$key}})) {
15231:                         push(@{$changes{'lonstatus'}},$key);
15232:                     }
15233:                 } elsif ($key eq 'weights') {
15234:                     if ((ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') &&
15235:                         (ref($contacts_hash{contacts}{lonstatus}{$key}) eq 'HASH')) {
15236:                         if (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
15237:                             foreach my $type ('E','W','N','U') {
15238:                                 unless ($contacts_hash{contacts}{lonstatus}{$key}{$type} eq
15239:                                         $currsetting{'lonstatus'}{$key}{$type}) {
15240:                                     push(@{$changes{'lonstatus'}},$key);
15241:                                     last;
15242:                                 }
15243:                             }
15244:                         } else {
15245:                             foreach my $type ('E','W','N','U') {
15246:                                 if ($contacts_hash{contacts}{lonstatus}{$key}{$type} ne '') {
15247:                                     push(@{$changes{'lonstatus'}},$key);
15248:                                     last;
15249:                                 }
15250:                             }
15251:                         }
15252:                     } elsif (ref($currsetting{'lonstatus'}{$key}) eq 'HASH') {
15253:                         foreach my $type ('E','W','N','U') {
15254:                             if ($currsetting{'lonstatus'}{$key}{$type} ne '') {
15255:                                 push(@{$changes{'lonstatus'}},$key);
15256:                                 last;
15257:                             }
15258:                         }
15259:                     }
15260:                 } elsif (($key eq 'threshold') || ($key eq 'sysmail')) {
15261:                     if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
15262:                         if ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
15263:                             if ($currsetting{'lonstatus'}{$key} != $contacts_hash{contacts}{lonstatus}{$key}) {
15264:                                 push(@{$changes{'lonstatus'}},$key);
15265:                             }
15266:                         } elsif ($contacts_hash{contacts}{lonstatus}{$key} =~ /^\d+$/) {
15267:                             push(@{$changes{'lonstatus'}},$key);
15268:                         }
15269:                     } elsif ($currsetting{'lonstatus'}{$key} =~ /^\d+$/) {
15270:                         push(@{$changes{'lonstatus'}},$key);
15271:                     }
15272:                 }
15273:             }
15274:         } else {
15275:             if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
15276:                 foreach my $key ('excluded','weights','threshold','sysmail') {
15277:                     if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
15278:                         push(@{$changes{'lonstatus'}},$key);
15279:                     }
15280:                 }
15281:             }
15282:         }
15283:     } else {
15284:         my %default;
15285:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
15286:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
15287:         $default{'errormail'} = 'adminemail';
15288:         $default{'packagesmail'} = 'adminemail';
15289:         $default{'helpdeskmail'} = 'supportemail';
15290:         $default{'otherdomsmail'} = 'supportemail';
15291:         $default{'lonstatusmail'} = 'adminemail';
15292:         $default{'requestsmail'} = 'adminemail';
15293:         $default{'updatesmail'} = 'adminemail';
15294:         $default{'hostipmail'} = 'adminemail';
15295:         foreach my $item (@contacts) {
15296:            if ($to{$item} ne $default{$item}) {
15297:                $changes{$item} = 1;
15298:            }
15299:         }
15300:         foreach my $type (@mailings) {
15301:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
15302:                 push(@{$changes{$type}},@{$newsetting{$type}});
15303:             }
15304:             if ($others{$type} ne '') {
15305:                 push(@{$changes{$type}},'others');
15306:             }
15307:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
15308:                 if ($bcc{$type} ne '') {
15309:                     push(@{$changes{$type}},'bcc');
15310:                 }
15311:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
15312:                     push(@{$changes{$type}},'include');
15313:                 }
15314:             }
15315:         }
15316:         if (ref($fields) eq 'ARRAY') {
15317:             foreach my $field (@{$fields}) {
15318:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
15319:                     push(@{$changes{'helpform'}},$field);
15320:                 }
15321:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
15322:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
15323:                         push(@{$changes{'helpform'}},'maxsize');
15324:                     }
15325:                 }
15326:             }
15327:         }
15328:         if (ref($contacts_hash{contacts}{lonstatus}) eq 'HASH') {
15329:             foreach my $key ('excluded','weights','threshold','sysmail') {
15330:                 if (exists($contacts_hash{contacts}{lonstatus}{$key})) {
15331:                     push(@{$changes{'lonstatus'}},$key);
15332:                 }
15333:             }
15334:         }
15335:     }
15336:     foreach my $item (@toggles) {
15337:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
15338:             $changes{$item} = 1;
15339:         } elsif ((!$env{'form.'.$item}) &&
15340:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
15341:             $changes{$item} = 1;
15342:         }
15343:     }
15344:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
15345:                                              $dom);
15346:     if ($putresult eq 'ok') {
15347:         if (keys(%changes) > 0) {
15348:             &Apache::loncommon::devalidate_domconfig_cache($dom);
15349:             if (ref($lastactref) eq 'HASH') {
15350:                 $lastactref->{'domainconfig'} = 1;
15351:             }
15352:             my ($titles,$short_titles)  = &contact_titles();
15353:             $resulttext = &mt('Changes made:').'<ul>';
15354:             foreach my $item (@contacts) {
15355:                 if ($changes{$item}) {
15356:                     $resulttext .= '<li>'.$titles->{$item}.
15357:                                     &mt(' set to: ').
15358:                                     '<span class="LC_cusr_emph">'.
15359:                                     $to{$item}.'</span></li>';
15360:                 }
15361:             }
15362:             foreach my $type (@mailings) {
15363:                 if (ref($changes{$type}) eq 'ARRAY') {
15364:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
15365:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
15366:                     } else {
15367:                         $resulttext .= '<li>'.$titles->{$type}.': ';
15368:                     }
15369:                     my @text;
15370:                     foreach my $item (@{$newsetting{$type}}) {
15371:                         push(@text,$short_titles->{$item});
15372:                     }
15373:                     if ($others{$type} ne '') {
15374:                         push(@text,$others{$type});
15375:                     }
15376:                     if (@text) {
15377:                         $resulttext .= '<span class="LC_cusr_emph">'.
15378:                                        join(', ',@text).'</span>';
15379:                     }
15380:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
15381:                         if ($bcc{$type} ne '') {
15382:                             my $bcctext;
15383:                             if (@text) {
15384:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
15385:                             } else {
15386:                                 $bcctext = '(Bcc)';
15387:                             }
15388:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
15389:                         } elsif (!@text) {
15390:                             $resulttext .= &mt('No one');
15391:                         }   
15392:                         if ($includestr{$type} ne '') {
15393:                             if ($includeloc{$type} eq 'b') {
15394:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
15395:                             } elsif ($includeloc{$type} eq 's') {
15396:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
15397:                             }
15398:                         }
15399:                     } elsif (!@text) {
15400:                         $resulttext .= &mt('No recipients');
15401:                     }
15402:                     $resulttext .= '</li>';
15403:                 }
15404:             }
15405:             if (ref($changes{'overrides'}) eq 'ARRAY') {
15406:                 my @deletions;
15407:                 foreach my $type (@{$changes{'overrides'}}) {
15408:                     if ($usertypeshash{$type}) {
15409:                         if (grep(/^\Q$type\E/,@overrides)) {
15410:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
15411:                                                       $usertypeshash{$type}).'<ul><li>';
15412:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
15413:                                 my @text;
15414:                                 foreach my $item (@contacts) {
15415:                                     if ($newsetting{'override_'.$type}{$item}) { 
15416:                                         push(@text,$short_titles->{$item});
15417:                                     }
15418:                                 }
15419:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
15420:                                     push(@text,$newsetting{'override_'.$type}{'others'});
15421:                                 }
15422:   
15423:                                 if (@text) {
15424:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
15425:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
15426:                                 }
15427:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
15428:                                     my $bcctext;
15429:                                     if (@text) {
15430:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
15431:                                     } else {
15432:                                         $bcctext = '(Bcc)';
15433:                                     }
15434:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
15435:                                 } elsif (!@text) {
15436:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
15437:                                 }
15438:                                 $resulttext .= '</li>';
15439:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
15440:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
15441:                                     if ($loc eq 'b') {
15442:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
15443:                                     } elsif ($loc eq 's') {
15444:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
15445:                                     }
15446:                                 }
15447:                             }
15448:                             $resulttext .= '</li></ul></li>';
15449:                         } else {
15450:                             push(@deletions,$usertypeshash{$type});
15451:                         }
15452:                     }
15453:                 }
15454:                 if (@deletions) {
15455:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
15456:                                               join(', ',@deletions)).'</li>';
15457:                 }
15458:             }
15459:             my @offon = ('off','on');
15460:             my $corelink = &core_link_msu();
15461:             if ($changes{'reporterrors'}) {
15462:                 $resulttext .= '<li>'.
15463:                                &mt('E-mail error reports to [_1] set to "'.
15464:                                    $offon[$env{'form.reporterrors'}].'".',
15465:                                    $corelink).
15466:                                '</li>';
15467:             }
15468:             if ($changes{'reportupdates'}) {
15469:                 $resulttext .= '<li>'.
15470:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
15471:                                     $offon[$env{'form.reportupdates'}].'".',
15472:                                     $corelink).
15473:                                 '</li>';
15474:             }
15475:             if ($changes{'reportstatus'}) {
15476:                 $resulttext .= '<li>'.
15477:                                 &mt('E-mail status if errors above threshold to [_1] set to "'.
15478:                                     $offon[$env{'form.reportstatus'}].'".',
15479:                                     $corelink).
15480:                                 '</li>';
15481:             }
15482:             if (ref($changes{'lonstatus'}) eq 'ARRAY') {
15483:                 $resulttext .= '<li>'.
15484:                                &mt('Nightly status check e-mail settings').':<ul>';
15485:                 my (%defval,%use_def,%shown);
15486:                 $defval{'threshold'} = $lonstatus_defs->{'threshold'};
15487:                 $defval{'sysmail'} = $lonstatus_defs->{'sysmail'};
15488:                 $defval{'weights'} =
15489:                     join(', ',map { $lonstatus_names->{$_}.'='.$lonstatus_defs->{$_}; } ('E','W','N','U'));
15490:                 $defval{'excluded'} = &mt('None');
15491:                 if (ref($contacts_hash{'contacts'}{'lonstatus'}) eq 'HASH') {
15492:                     foreach my $item ('threshold','sysmail','weights','excluded') {
15493:                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item})) {
15494:                             if (($item eq 'threshold') || ($item eq 'sysmail')) {
15495:                                 $shown{$item} = $contacts_hash{'contacts'}{'lonstatus'}{$item};
15496:                             } elsif ($item eq 'weights') {
15497:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'HASH') {
15498:                                     foreach my $type ('E','W','N','U') {
15499:                                         $shown{$item} .= $lonstatus_names->{$type}.'=';
15500:                                         if (exists($contacts_hash{'contacts'}{'lonstatus'}{$item}{$type})) {
15501:                                             $shown{$item} .= $contacts_hash{'contacts'}{'lonstatus'}{$item}{$type};
15502:                                         } else {
15503:                                             $shown{$item} .= $lonstatus_defs->{$type};
15504:                                         }
15505:                                         $shown{$item} .= ', ';
15506:                                     }
15507:                                     $shown{$item} =~ s/, $//;
15508:                                 } else {
15509:                                     $shown{$item} = $defval{$item};
15510:                                 }
15511:                             } elsif ($item eq 'excluded') {
15512:                                 if (ref($contacts_hash{'contacts'}{'lonstatus'}{$item}) eq 'ARRAY') {
15513:                                     $shown{$item} = join(', ',@{$contacts_hash{'contacts'}{'lonstatus'}{$item}});
15514:                                 } else {
15515:                                     $shown{$item} = $defval{$item};
15516:                                 }
15517:                             }
15518:                         } else {
15519:                             $shown{$item} = $defval{$item};
15520:                         }
15521:                     }
15522:                 } else {
15523:                     foreach my $item ('threshold','weights','excluded','sysmail') {
15524:                         $shown{$item} = $defval{$item};
15525:                     }
15526:                 }
15527:                 foreach my $item ('threshold','weights','excluded','sysmail') {
15528:                     $resulttext .= '<li>'.&mt($titles->{'error'.$item}.' -- [_1]',
15529:                                           $shown{$item}).'</li>';
15530:                 }
15531:                 $resulttext .= '</ul></li>';
15532:             }
15533:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
15534:                 my (@optional,@required,@unused,$maxsizechg);
15535:                 foreach my $field (@{$changes{'helpform'}}) {
15536:                     if ($field eq 'maxsize') {
15537:                         $maxsizechg = 1;
15538:                         next;
15539:                     }
15540:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
15541:                         push(@optional,$field);
15542:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
15543:                         push(@unused,$field);
15544:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
15545:                         push(@required,$field);
15546:                     }
15547:                 }
15548:                 if (@optional) {
15549:                     $resulttext .= '<li>'.
15550:                                    &mt('Help form fields changed to "Optional": [_1].',
15551:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
15552:                                    '</li>';
15553:                 }
15554:                 if (@required) {
15555:                     $resulttext .= '<li>'.
15556:                                    &mt('Help form fields changed to "Required": [_1].',
15557:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
15558:                                    '</li>';
15559:                 }
15560:                 if (@unused) {
15561:                     $resulttext .= '<li>'.
15562:                                    &mt('Help form fields changed to "Not shown": [_1].',
15563:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
15564:                                    '</li>';
15565:                 }
15566:                 if ($maxsizechg) {
15567:                     $resulttext .= '<li>'.
15568:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
15569:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
15570:                                    '</li>';
15571:                 }
15572:             }
15573:             $resulttext .= '</ul>';
15574:         } else {
15575:             $resulttext = &mt('No changes made to contacts and form settings');
15576:         }
15577:     } else {
15578:         $resulttext = '<span class="LC_error">'.
15579:             &mt('An error occurred: [_1].',$putresult).'</span>';
15580:     }
15581:     return $resulttext;
15582: }
15583: 
15584: sub modify_privacy {
15585:     my ($dom,%domconfig) = @_;
15586:     my ($resulttext,%current,%changes);
15587:     if (ref($domconfig{'privacy'}) eq 'HASH') {
15588:         %current = %{$domconfig{'privacy'}};
15589:     }
15590:     my @fields = ('lastname','firstname','middlename','generation','permanentemail','id');
15591:     my @items = ('domain','author','course','community');
15592:     my %names = &Apache::lonlocal::texthash (
15593:                    domain => 'Assigned domain role(s)',
15594:                    author => 'Assigned co-author role(s)',
15595:                    course => 'Assigned course role(s)',
15596:                    community => 'Assigned community role',
15597:                 );
15598:     my %roles = &Apache::lonlocal::texthash (
15599:                    domain => 'Domain role',
15600:                    author => 'Co-author role',
15601:                    course => 'Course role',
15602:                    community => 'Community role',
15603:                 );
15604:     my %titles = &Apache::lonlocal::texthash (
15605:                   approval => 'Approval for role in different domain',
15606:                   othdom   => 'User information available in other domain',
15607:                   priv     => 'Information viewable by privileged user in same domain',
15608:                   unpriv   => 'Information viewable by unprivileged user in same domain',
15609:                   instdom  => 'Other domain shares institution/provider',
15610:                   extdom   => 'Other domain has different institution/provider',
15611:                   none     => 'Not allowed',
15612:                   user     => 'User authorizes',
15613:                   domain   => 'Domain Coordinator authorizes',
15614:                   auto     => 'Unrestricted',
15615:     );
15616:     my %fieldnames = &Apache::lonlocal::texthash (
15617:                         id => 'Student/Employee ID',
15618:                         permanentemail => 'E-mail address',
15619:                         lastname => 'Last Name',
15620:                         firstname => 'First Name',
15621:                         middlename => 'Middle Name',
15622:                         generation => 'Generation',
15623:     );
15624:     my ($othertitle,$usertypes,$types) =
15625:         &Apache::loncommon::sorted_inst_types($dom);
15626:     my (%by_ip,%by_location,@intdoms,@instdoms);
15627:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
15628: 
15629:     my %privacyhash = (
15630:                        'approval' => {
15631:                                        instdom => {},
15632:                                        extdom  => {},
15633:                                      },
15634:                        'othdom'   => {},
15635:                        'priv'     => {},
15636:                        'unpriv'   => {},
15637:                       );
15638:     foreach my $item (@items) {
15639:         if (@instdoms > 1) {
15640:             if ($env{'form.privacy_approval_instdom'.$item} =~ /^(none|user|domain|auto)$/) {
15641:                 $privacyhash{'approval'}{'instdom'}{$item} = $env{'form.privacy_approval_instdom_'.$item};
15642:             }
15643:             if (ref($current{'approval'}) eq 'HASH') {
15644:                 if (ref($current{'approval'}{'instdom'}) eq 'HASH') {
15645:                     unless ($privacyhash{'approval'}{'instdom'}{$item} eq $current{'approval'}{'instdom'}{$item}) {
15646:                         $changes{'approval'} = 1;
15647:                     }
15648:                 }
15649:             } elsif ($privacyhash{'approval'}{'instdom'}{$item} ne 'auto') {
15650:                 $changes{'approval'} = 1;
15651:             }
15652:         }
15653:         if (keys(%by_location) > 0) {
15654:             if ($env{'form.privacy_approval_extdom_'.$item} =~ /^(none|user|domain|auto)$/) {
15655:                 $privacyhash{'approval'}{'extdom'}{$item} = $env{'form.privacy_approval_extdom_'.$item};
15656:             }
15657:             if (ref($current{'approval'}) eq 'HASH') {
15658:                 if (ref($current{'approval'}{'extdom'}) eq 'HASH') {
15659:                     unless ($privacyhash{'approval'}{'extdom'}{$item} eq $current{'approval'}{'extdom'}{$item}) {
15660:                         $changes{'approval'} = 1;
15661:                     }
15662:                 }
15663:             } elsif ($privacyhash{'approval'}{'extdom'}{$item} ne 'auto') {
15664:                 $changes{'approval'} = 1;
15665:             }
15666:         }
15667:         foreach my $status ('priv','unpriv') {
15668:             my @possibles = sort(&Apache::loncommon::get_env_multiple('form.privacy_'.$status.'_'.$item));
15669:             my @newvalues;
15670:             foreach my $field (@possibles) {
15671:                 if (grep(/^\Q$field\E$/,@fields)) {
15672:                     $privacyhash{$status}{$item}{$field} = 1;
15673:                     push(@newvalues,$field);
15674:                 }
15675:             }
15676:             @newvalues = sort(@newvalues);
15677:             if (ref($current{$status}) eq 'HASH') {
15678:                 if (ref($current{$status}{$item}) eq 'HASH') {
15679:                     my @currvalues = sort(keys(%{$current{$status}{$item}}));
15680:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
15681:                     if (@diffs > 0) {
15682:                         $changes{$status} = 1;
15683:                     }
15684:                 }
15685:             } else {
15686:                 my @stdfields;
15687:                 foreach my $field (@fields) {
15688:                     if ($field eq 'id') {
15689:                         next if ($status eq 'unpriv');
15690:                         next if (($status eq 'priv') && ($item eq 'community'));
15691:                     }
15692:                     push(@stdfields,$field);
15693:                 }
15694:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
15695:                 if (@diffs > 0) {
15696:                     $changes{$status} = 1;
15697:                 }
15698:             }
15699:         }
15700:     }
15701:     if ((@instdoms > 1) || (keys(%by_location) > 0)) {
15702:         my @statuses;
15703:         if (ref($types) eq 'ARRAY') {
15704:             @statuses = @{$types};
15705:         }
15706:         foreach my $type (@statuses,'default') {
15707:             my @possfields = &Apache::loncommon::get_env_multiple('form.privacy_othdom_'.$type);
15708:             my @newvalues;
15709:             foreach my $field (sort(@possfields)) {
15710:                 if (grep(/^\Q$field\E$/,@fields)) {
15711:                     $privacyhash{'othdom'}{$type}{$field} = 1;
15712:                     push(@newvalues,$field);
15713:                 }
15714:             }
15715:             @newvalues = sort(@newvalues);
15716:             if (ref($current{'othdom'}) eq 'HASH') {
15717:                 if (ref($current{'othdom'}{$type}) eq 'HASH') {
15718:                     my @currvalues = sort(keys(%{$current{'othdom'}{$type}}));
15719:                     my @diffs = &Apache::loncommon::compare_arrays(\@currvalues,\@newvalues);
15720:                     if (@diffs > 0) {
15721:                         $changes{'othdom'} = 1;
15722:                     }
15723:                 }
15724:             } else {
15725:                 my @stdfields = ('lastname','firstname','middlename','generation','permanentemail');
15726:                 my @diffs = &Apache::loncommon::compare_arrays(\@stdfields,\@newvalues);
15727:                 if (@diffs > 0) {
15728:                     $changes{'othdom'} = 1;
15729:                 }
15730:             }
15731:         }
15732:     }
15733:     my %confighash = (
15734:                         privacy => \%privacyhash,
15735:                      );
15736:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
15737:     if ($putresult eq 'ok') {
15738:         if (keys(%changes) > 0) {
15739:             $resulttext = &mt('Changes made: ').'<ul>';
15740:             foreach my $key ('approval','othdom','priv','unpriv') {
15741:                 if ($changes{$key}) {
15742:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
15743:                     if ($key eq 'approval') {
15744:                         if (keys(%{$privacyhash{$key}{instdom}})) {
15745:                             $resulttext .= '<li>'.$titles{'instdom'}.'<ul>';
15746:                             foreach my $item (@items) {
15747:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{instdom}{$item}}.'</li>';
15748:                             }
15749:                             $resulttext .= '</ul></li>';
15750:                         }
15751:                         if (keys(%{$privacyhash{$key}{extdom}})) {
15752:                             $resulttext .= '<li>'.$titles{'extdom'}.'<ul>';
15753:                             foreach my $item (@items) {
15754:                                 $resulttext .=  '<li>'.$roles{$item}.': '.$titles{$privacyhash{$key}{extdom}{$item}}.'</li>';
15755:                             }
15756:                             $resulttext .= '</ul></li>';
15757:                         }
15758:                     } elsif ($key eq 'othdom') {
15759:                         my @statuses;
15760:                         if (ref($types) eq 'ARRAY') {
15761:                             @statuses = @{$types};
15762:                         }
15763:                         if (ref($privacyhash{$key}) eq 'HASH') {
15764:                             foreach my $status (@statuses,'default') {
15765:                                 if ($status eq 'default') {
15766:                                     $resulttext .= '<li>'.$othertitle.': ';
15767:                                 } elsif (ref($usertypes) eq 'HASH') {
15768:                                     $resulttext .= '<li>'.$usertypes->{$status}.': ';
15769:                                 } else {
15770:                                     next;
15771:                                 }
15772:                                 if (ref($privacyhash{$key}{$status}) eq 'HASH') {
15773:                                     if (keys(%{$privacyhash{$key}{$status}})) {
15774:                                         $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$status}}))));
15775:                                     } else {
15776:                                         $resulttext .= &mt('none');
15777:                                     }
15778:                                 }
15779:                                 $resulttext .= '</li>';
15780:                             }
15781:                         }
15782:                     } else {
15783:                         foreach my $item (@items) {
15784:                             if (ref($privacyhash{$key}{$item}) eq 'HASH') {
15785:                                 $resulttext .= '<li>'.$names{$item}.': ';
15786:                                 if (keys(%{$privacyhash{$key}{$item}})) {
15787:                                     $resulttext .= join(', ', map { $fieldnames{$_}; } (sort(keys(%{$privacyhash{$key}{$item}}))));
15788:                                 } else {
15789:                                     $resulttext .= &mt('none');
15790:                                 }
15791:                                 $resulttext .= '</li>';
15792:                             }
15793:                         }
15794:                     }
15795:                     $resulttext .= '</ul></li>';
15796:                 }
15797:             }
15798:         } else {
15799:             $resulttext = &mt('No changes made to user information settings');
15800:         }
15801:     } else {
15802:         $resulttext = '<span class="LC_error">'.
15803:             &mt('An error occurred: [_1]',$putresult).'</span>';
15804:     }
15805:     return $resulttext;
15806: }
15807: 
15808: sub modify_passwords {
15809:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
15810:     my ($resulttext,%current,%changes,%newvalues,@oktypes,$errors,
15811:         $updatedefaults,$updateconf);
15812:     my $customfn = 'resetpw.html';
15813:     if (ref($domconfig{'passwords'}) eq 'HASH') {
15814:         %current = %{$domconfig{'passwords'}};
15815:     }
15816:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15817:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
15818:     if (ref($types) eq 'ARRAY') {
15819:         @oktypes = @{$types};
15820:     }
15821:     push(@oktypes,'default');
15822: 
15823:     my %titles = &Apache::lonlocal::texthash (
15824:         intauth_cost   => 'Encryption cost for bcrypt (positive integer)',
15825:         intauth_check  => 'Check bcrypt cost if authenticated',
15826:         intauth_switch => 'Existing crypt-based switched to bcrypt on authentication',
15827:         permanent      => 'Permanent e-mail address',
15828:         critical       => 'Critical notification address',
15829:         notify         => 'Notification address',
15830:         min            => 'Minimum password length',
15831:         max            => 'Maximum password length',
15832:         chars          => 'Required characters',
15833:         expire         => 'Password expiration (days)',
15834:         numsaved       => 'Number of previous passwords to save',
15835:         reset          => 'Resetting Forgotten Password',
15836:         intauth        => 'Encryption of Stored Passwords (Internal Auth)',
15837:         rules          => 'Rules for LON-CAPA Passwords',
15838:         crsownerchg    => 'Course Owner Changing Student Passwords',
15839:         username       => 'Username',
15840:         email          => 'E-mail address',
15841:     );
15842: 
15843: #
15844: # Retrieve current domain configuration for internal authentication from $domconfig{'defaults'}.
15845: #
15846:     my (%curr_defaults,%save_defaults);
15847:     if (ref($domconfig{'defaults'}) eq 'HASH') {
15848:         foreach my $key (keys(%{$domconfig{'defaults'}})) {
15849:             if ($key =~ /^intauth_(cost|check|switch)$/) {
15850:                 $curr_defaults{$key} = $domconfig{'defaults'}{$key};
15851:             } else {
15852:                 $save_defaults{$key} = $domconfig{'defaults'}{$key};
15853:             }
15854:         }
15855:     }
15856:     my %staticdefaults = (
15857:         'resetlink'      => 2,
15858:         'resetcase'      => \@oktypes,
15859:         'resetprelink'   => 'both',
15860:         'resetemail'     => ['critical','notify','permanent'],
15861:         'intauth_cost'   => 10,
15862:         'intauth_check'  => 0,
15863:         'intauth_switch' => 0,
15864:     );
15865:     $staticdefaults{'min'} = $Apache::lonnet::passwdmin;
15866:     foreach my $type (@oktypes) {
15867:         $staticdefaults{'resetpostlink'}{$type} = ['email','username'];
15868:     }
15869:     my $linklife = $env{'form.passwords_link'};
15870:     $linklife =~ s/^\s+|\s+$//g;
15871:     if (($linklife =~ /^\d+(|\.\d*)$/) && ($linklife > 0)) {
15872:         $newvalues{'resetlink'} = $linklife;
15873:         if ($current{'resetlink'}) {
15874:             if ($current{'resetlink'} ne $linklife) {
15875:                 $changes{'reset'} = 1;
15876:             }
15877:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
15878:             if ($staticdefaults{'resetlink'} ne $linklife) {
15879:                 $changes{'reset'} = 1;
15880:             }
15881:         }
15882:     } elsif ($current{'resetlink'}) {
15883:         $changes{'reset'} = 1;
15884:     }
15885:     my @casesens;
15886:     my @posscase = &Apache::loncommon::get_env_multiple('form.passwords_case_sensitive');
15887:     foreach my $case (sort(@posscase)) {
15888:         if (grep(/^\Q$case\E$/,@oktypes)) {
15889:             push(@casesens,$case);
15890:         }
15891:     }
15892:     $newvalues{'resetcase'} = \@casesens;
15893:     if (ref($current{'resetcase'}) eq 'ARRAY') {
15894:         my @diffs = &Apache::loncommon::compare_arrays($current{'resetcase'},\@casesens);
15895:         if (@diffs > 0) {
15896:             $changes{'reset'} = 1;
15897:         }
15898:     } elsif (!ref($domconfig{passwords}) eq 'HASH') {
15899:         my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetcase'},\@casesens);
15900:         if (@diffs > 0) {
15901:             $changes{'reset'} = 1;
15902:         }
15903:     }
15904:     if ($env{'form.passwords_prelink'} =~ /^(both|either)$/) {
15905:         $newvalues{'resetprelink'} = $env{'form.passwords_prelink'};
15906:         if (exists($current{'resetprelink'})) {
15907:             if ($current{'resetprelink'} ne $newvalues{'resetprelink'}) {
15908:                 $changes{'reset'} = 1;
15909:             }
15910:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
15911:             if ($staticdefaults{'resetprelink'} ne $newvalues{'resetprelink'}) {
15912:                 $changes{'reset'} = 1;
15913:             }
15914:         }
15915:     } elsif ($current{'resetprelink'}) {
15916:         $changes{'reset'} = 1;
15917:     }
15918:     foreach my $type (@oktypes) {
15919:         my @possplink = &Apache::loncommon::get_env_multiple('form.passwords_postlink_'.$type);
15920:         my @postlink;
15921:         foreach my $item (sort(@possplink)) {
15922:             if ($item =~ /^(email|username)$/) {
15923:                 push(@postlink,$item);
15924:             }
15925:         }
15926:         $newvalues{'resetpostlink'}{$type} = \@postlink;
15927:         unless ($changes{'reset'}) {
15928:             if (ref($current{'resetpostlink'}) eq 'HASH') {
15929:                 if (ref($current{'resetpostlink'}{$type}) eq 'ARRAY') {
15930:                     my @diffs = &Apache::loncommon::compare_arrays($current{'resetpostlink'}{$type},\@postlink);
15931:                     if (@diffs > 0) {
15932:                         $changes{'reset'} = 1;
15933:                     }
15934:                 } else {
15935:                     $changes{'reset'} = 1;
15936:                 }
15937:             } elsif (!ref($domconfig{passwords}) eq 'HASH') {
15938:                 my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetpostlink'}{$type},\@postlink);
15939:                 if (@diffs > 0) {
15940:                     $changes{'reset'} = 1;
15941:                 }
15942:             }
15943:         }
15944:     }
15945:     my @possemailsrc = &Apache::loncommon::get_env_multiple('form.passwords_emailsrc');
15946:     my @resetemail;
15947:     foreach my $item (sort(@possemailsrc)) {
15948:         if ($item =~ /^(permanent|critical|notify)$/) {
15949:             push(@resetemail,$item);
15950:         }
15951:     }
15952:     $newvalues{'resetemail'} = \@resetemail;
15953:     unless ($changes{'reset'}) {
15954:         if (ref($current{'resetemail'}) eq 'ARRAY') {
15955:             my @diffs = &Apache::loncommon::compare_arrays($current{'resetemail'},\@resetemail);
15956:             if (@diffs > 0) {
15957:                 $changes{'reset'} = 1;
15958:             }
15959:         } elsif (!ref($domconfig{passwords}) eq 'HASH') {
15960:             my @diffs = &Apache::loncommon::compare_arrays($staticdefaults{'resetemail'},\@resetemail);
15961:             if (@diffs > 0) {
15962:                 $changes{'reset'} = 1;
15963:             }
15964:         }
15965:     }
15966:     if ($env{'form.passwords_stdtext'} == 0) {
15967:         $newvalues{'resetremove'} = 1;
15968:         unless ($current{'resetremove'}) {
15969:             $changes{'reset'} = 1;
15970:         }
15971:     } elsif ($current{'resetremove'}) {
15972:         $changes{'reset'} = 1;
15973:     }
15974:     if ($env{'form.passwords_customfile.filename'} ne '') {
15975:         my $servadm = $r->dir_config('lonAdmEMail');
15976:         my ($configuserok,$author_ok,$switchserver) =
15977:             &config_check($dom,$confname,$servadm);
15978:         my $error;
15979:         if ($configuserok eq 'ok') {
15980:             if ($switchserver) {
15981:                 $error = &mt("Upload of file containing domain-specific text is not permitted to this server: [_1]",$switchserver);
15982:             } else {
15983:                 if ($author_ok eq 'ok') {
15984:                     my ($result,$customurl) =
15985:                         &publishlogo($r,'upload','passwords_customfile',$dom,
15986:                                      $confname,'customtext/resetpw','','',$customfn);
15987:                     if ($result eq 'ok') {
15988:                         $newvalues{'resetcustom'} = $customurl;
15989:                         $changes{'reset'} = 1;
15990:                     } else {
15991:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$customfn,$result);
15992:                     }
15993:                 } else {
15994:                     $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);
15995:                 }
15996:             }
15997:         } else {
15998:             $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);
15999:         }
16000:         if ($error) {
16001:             &Apache::lonnet::logthis($error);
16002:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
16003:         }
16004:     } elsif ($current{'resetcustom'}) {
16005:         if ($env{'form.passwords_custom_del'}) {
16006:             $changes{'reset'} = 1;
16007:         } else {
16008:             $newvalues{'resetcustom'} = $current{'resetcustom'};
16009:         }
16010:     }
16011:     $env{'form.intauth_cost'} =~ s/^\s+|\s+$//g;
16012:     if (($env{'form.intauth_cost'} ne '') && ($env{'form.intauth_cost'} =~ /^\d+$/)) {
16013:         $save_defaults{'intauth_cost'} = $env{'form.intauth_cost'};
16014:         if ($save_defaults{'intauth_cost'} ne $curr_defaults{'intauth_cost'}) {
16015:             $changes{'intauth'} = 1;
16016:         }
16017:     } else {
16018:         $save_defaults{'intauth_cost'} = $curr_defaults{'intauth_cost'};
16019:     }
16020:     if ($env{'form.intauth_check'} =~ /^(0|1|2)$/) {
16021:         $save_defaults{'intauth_check'} = $env{'form.intauth_check'};
16022:         if ($save_defaults{'intauth_check'} ne $curr_defaults{'intauth_check'}) {
16023:             $changes{'intauth'} = 1;
16024:         }
16025:     } else {
16026:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
16027:     }
16028:     if ($env{'form.intauth_switch'} =~ /^(0|1|2)$/) {
16029:         $save_defaults{'intauth_switch'} = $env{'form.intauth_switch'};
16030:         if ($save_defaults{'intauth_switch'} ne $curr_defaults{'intauth_switch'}) {
16031:             $changes{'intauth'} = 1;
16032:         }
16033:     } else {
16034:         $save_defaults{'intauth_check'} = $curr_defaults{'intauth_check'};
16035:     }
16036:     foreach my $item ('cost','check','switch') {
16037:         if ($save_defaults{'intauth_'.$item} ne $domdefaults{'intauth_'.$item}) {
16038:             $domdefaults{'intauth_'.$item} = $save_defaults{'intauth_'.$item};
16039:             $updatedefaults = 1;
16040:         }
16041:     }
16042:     foreach my $rule ('min','max','expire','numsaved') {
16043:         $env{'form.passwords_'.$rule} =~ s/^\s+|\s+$//g;
16044:         my $ruleok;
16045:         if ($rule eq 'expire') {
16046:             if (($env{'form.passwords_'.$rule} =~ /^\d+(|\.\d*)$/) &&
16047:                 ($env{'form.passwords_'.$rule} ne '0')) {
16048:                 $ruleok = 1;
16049:             }
16050:         } elsif ($rule eq 'min') {
16051:             if ($env{'form.passwords_'.$rule} =~ /^\d+$/) {
16052:                 if ($env{'form.passwords_'.$rule} >= $Apache::lonnet::passwdmin) {
16053:                     $ruleok = 1;
16054:                 }
16055:             }
16056:         } elsif (($env{'form.passwords_'.$rule} =~ /^\d+$/) &&
16057:                  ($env{'form.passwords_'.$rule} ne '0')) {
16058:             $ruleok = 1;
16059:         }
16060:         if ($ruleok) {
16061:             $newvalues{$rule} = $env{'form.passwords_'.$rule};
16062:             if (exists($current{$rule})) {
16063:                 if ($newvalues{$rule} ne $current{$rule}) {
16064:                     $changes{'rules'} = 1;
16065:                 }
16066:             } elsif ($rule eq 'min') {
16067:                 if ($staticdefaults{$rule} ne $newvalues{$rule}) {
16068:                     $changes{'rules'} = 1;
16069:                 }
16070:             } else {
16071:                 $changes{'rules'} = 1;
16072:             }
16073:         } elsif (exists($current{$rule})) {
16074:             $changes{'rules'} = 1;
16075:         }
16076:     }
16077:     my @posschars = &Apache::loncommon::get_env_multiple('form.passwords_chars');
16078:     my @chars;
16079:     foreach my $item (sort(@posschars)) {
16080:         if ($item =~ /^(uc|lc|num|spec)$/) {
16081:             push(@chars,$item);
16082:         }
16083:     }
16084:     $newvalues{'chars'} = \@chars;
16085:     unless ($changes{'rules'}) {
16086:         if (ref($current{'chars'}) eq 'ARRAY') {
16087:             my @diffs = &Apache::loncommon::compare_arrays($current{'chars'},\@chars);
16088:             if (@diffs > 0) {
16089:                 $changes{'rules'} = 1;
16090:             }
16091:         } else {
16092:             if (@chars > 0) {
16093:                 $changes{'rules'} = 1;
16094:             }
16095:         }
16096:     }
16097:     my %crsownerchg = (
16098:                         by => [],
16099:                         for => [],
16100:                       );
16101:     foreach my $item ('by','for') {
16102:         my @posstypes = &Apache::loncommon::get_env_multiple('form.passwords_crsowner_'.$item);
16103:         foreach my $type (sort(@posstypes)) {
16104:             if (grep(/^\Q$type\E$/,@oktypes)) {
16105:                 push(@{$crsownerchg{$item}},$type);
16106:             }
16107:         }
16108:     }
16109:     $newvalues{'crsownerchg'} = \%crsownerchg;
16110:     if (ref($current{'crsownerchg'}) eq 'HASH') {
16111:         foreach my $item ('by','for') {
16112:             if (ref($current{'crsownerchg'}{$item}) eq 'ARRAY') {
16113:                 my @diffs = &Apache::loncommon::compare_arrays($current{'crsownerchg'}{$item},$crsownerchg{$item});
16114:                 if (@diffs > 0) {
16115:                     $changes{'crsownerchg'} = 1;
16116:                     last;
16117:                 }
16118:             }
16119:         }
16120:     } elsif (!(ref($domconfig{passwords}) eq 'HASH')) {
16121:         foreach my $item ('by','for') {
16122:             if (@{$crsownerchg{$item}} > 0) {
16123:                 $changes{'crsownerchg'} = 1;
16124:                 last;
16125:             }
16126:         }
16127:     }
16128: 
16129:     my %confighash = (
16130:                         defaults  => \%save_defaults,
16131:                         passwords => \%newvalues,
16132:                      );
16133:     &process_captcha('passwords',\%changes,$confighash{'passwords'},$domconfig{'passwords'});
16134: 
16135:     my $putresult = &Apache::lonnet::put_dom('configuration',\%confighash,$dom);
16136:     if ($putresult eq 'ok') {
16137:         if (keys(%changes) > 0) {
16138:             $resulttext = &mt('Changes made: ').'<ul>';
16139:             foreach my $key ('reset','intauth','rules','crsownerchg') {
16140:                 if ($changes{$key}) {
16141:                     unless ($key eq 'intauth') {
16142:                         $updateconf = 1;
16143:                     }
16144:                     $resulttext .= '<li>'.$titles{$key}.':<ul>';
16145:                     if ($key eq 'reset') {
16146:                         if ($confighash{'passwords'}{'captcha'} eq 'original') {
16147:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: original CAPTCHA').'</li>';
16148:                         } elsif ($confighash{'passwords'}{'captcha'} eq 'recaptcha') {
16149:                             $resulttext .= '<li>'.&mt('CAPTCHA validation set to use: reCAPTCHA').' '.
16150:                                            &mt('version: [_1]',$confighash{'passwords'}{'recaptchaversion'}).'<br />';
16151:                             if (ref($confighash{'passwords'}{'recaptchakeys'}) eq 'HASH') {
16152:                                 $resulttext .= &mt('Public key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'public'}).'</br>'.
16153:                                                &mt('Private key: [_1]',$confighash{'passwords'}{'recaptchakeys'}{'private'}).'</li>';
16154:                             }
16155:                         } else {
16156:                             $resulttext .= '<li>'.&mt('No CAPTCHA validation').'</li>';
16157:                         }
16158:                         if ($confighash{'passwords'}{'resetlink'}) {
16159:                             $resulttext .= '<li>'.&mt('Reset link expiration set to [quant,_1,hour]',$confighash{'passwords'}{'resetlink'}).'</li>';
16160:                         } else {
16161:                             $resulttext .= '<li>'.&mt('No reset link expiration set.').' '.
16162:                                                   &mt('Will default to 2 hours').'</li>';
16163:                         }
16164:                         if (ref($confighash{'passwords'}{'resetcase'}) eq 'ARRAY') {
16165:                             if (@{$confighash{'passwords'}{'resetcase'}} == 0) {
16166:                                 $resulttext .= '<li>'.&mt('User input for username and/or e-mail address not case sensitive for "Forgot Password" web form').'</li>';
16167:                             } else {
16168:                                 my $casesens;
16169:                                 foreach my $type (@{$confighash{'passwords'}{'resetcase'}}) {
16170:                                     if ($type eq 'default') {
16171:                                         $casesens .= $othertitle.', ';
16172:                                     } elsif ($usertypes->{$type} ne '') {
16173:                                         $casesens .= $usertypes->{$type}.', ';
16174:                                     }
16175:                                 }
16176:                                 $casesens =~ s/\Q, \E$//;
16177:                                 $resulttext .= '<li>'.&mt('"Forgot Password" web form input for username and/or e-mail address is case-sensitive for: [_1]',$casesens).'</li>';
16178:                             }
16179:                         } else {
16180:                             $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>';
16181:                         }
16182:                         if ($confighash{'passwords'}{'resetprelink'} eq 'either') {
16183:                             $resulttext .= '<li>'.&mt('Users can enter either a username or an e-mail address in "Forgot Password" web form').'</li>';
16184:                         } else {
16185:                             $resulttext .= '<li>'.&mt('Users can enter both a username and an e-mail address in "Forgot Password" web form').'</li>';
16186:                         }
16187:                         if (ref($confighash{'passwords'}{'resetpostlink'}) eq 'HASH') {
16188:                             my $output;
16189:                             if (ref($types) eq 'ARRAY') {
16190:                                 foreach my $type (@{$types}) {
16191:                                     if (ref($confighash{'passwords'}{'resetpostlink'}{$type}) eq 'ARRAY') {
16192:                                         if (@{$confighash{'passwords'}{'resetpostlink'}{$type}} == 0) {
16193:                                             $output .= $usertypes->{$type}.' -- '.&mt('none');
16194:                                         } else {
16195:                                             $output .= $usertypes->{$type}.' -- '.
16196:                                                        join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{$type}})).'; ';
16197:                                         }
16198:                                     }
16199:                                 }
16200:                             }
16201:                             if (ref($confighash{'passwords'}{'resetpostlink'}{'default'}) eq 'ARRAY') {
16202:                                 if (@{$confighash{'passwords'}{'resetpostlink'}{'default'}} == 0) {
16203:                                     $output .= $othertitle.' -- '.&mt('none');
16204:                                 } else {
16205:                                     $output .= $othertitle.' -- '.
16206:                                                join(', ',map { $titles{$_}; } (@{$confighash{'passwords'}{'resetpostlink'}{'default'}}));
16207:                                 }
16208:                             }
16209:                             if ($output) {
16210:                                 $resulttext .= '<li>'.&mt('Information required for new password form (by user type) set to: [_1]',$output).'</li>';
16211:                             } else {
16212:                                 $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>';
16213:                             }
16214:                         } else {
16215:                             $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>';
16216:                         }
16217:                         if (ref($confighash{'passwords'}{'resetemail'}) eq 'ARRAY') {
16218:                             if (@{$confighash{'passwords'}{'resetemail'}} > 0) {
16219:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$confighash{'passwords'}{'resetemail'}})).'</li>';
16220:                             } else {
16221:                                 $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA used for verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
16222:                             }
16223:                         } else {
16224:                             $resulttext .= '<li>'.&mt('E-mail address(es) in LON-CAPA usedfor verification will include: [_1]',join(', ',map { $titles{$_}; } @{$staticdefaults{'resetemail'}})).'</li>';
16225:                         }
16226:                         if ($confighash{'passwords'}{'resetremove'}) {
16227:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form not shown').'</li>';
16228:                         } else {
16229:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" web form is shown').'</li>';
16230:                         }
16231:                         if ($confighash{'passwords'}{'resetcustom'}) {
16232:                             my $customlink = &Apache::loncommon::modal_link($confighash{'passwords'}{'resetcustom'},
16233:                                                                             &mt('custom text'),600,500,undef,undef,
16234:                                                                             undef,undef,'background-color:#ffffff');
16235:                             $resulttext .= '<li>'.&mt('Preamble to "Forgot Password" form includes: [_1]',$customlink).'</li>';
16236:                         } else {
16237:                             $resulttext .= '<li>'.&mt('No custom text included in preamble to "Forgot Password" form').'</li>';
16238:                         }
16239:                     } elsif ($key eq 'intauth') {
16240:                         foreach my $item ('cost','switch','check') {
16241:                             my $value = $save_defaults{$key.'_'.$item};
16242:                             if ($item eq 'switch') {
16243:                                 my %optiondesc = &Apache::lonlocal::texthash (
16244:                                                      0 => 'No',
16245:                                                      1 => 'Yes',
16246:                                                      2 => 'Yes, and copy existing passwd file to passwd.bak file',
16247:                                                  );
16248:                                 if ($value =~ /^(0|1|2)$/) {
16249:                                     $value = $optiondesc{$value};
16250:                                 } else {
16251:                                     $value = &mt('none -- defaults to No');
16252:                                 }
16253:                             } elsif ($item eq 'check') {
16254:                                 my %optiondesc = &Apache::lonlocal::texthash (
16255:                                                      0 => 'No',
16256:                                                      1 => 'Yes, allow login then update passwd file using default cost (if higher)',
16257:                                                      2 => 'Yes, disallow login if stored cost is less than domain default',
16258:                                                  );
16259:                                 if ($value =~ /^(0|1|2)$/) {
16260:                                     $value = $optiondesc{$value};
16261:                                 } else {
16262:                                     $value = &mt('none -- defaults to No');
16263:                                 }
16264:                             }
16265:                             $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$titles{$key.'_'.$item},$value).'</li>';
16266:                         }
16267:                     } elsif ($key eq 'rules') {
16268:                         foreach my $rule ('min','max','expire','numsaved') {
16269:                             if ($confighash{'passwords'}{$rule} eq '') {
16270:                                 if ($rule eq 'min') {
16271:                                     $resulttext .= '<li>'.&mt('[_1] not set.',$titles{$rule});
16272:                                                    ' '.&mt('Default of [_1] will be used',
16273:                                                            $Apache::lonnet::passwdmin).'</li>';
16274:                                 } else {
16275:                                     $resulttext .= '<li>'.&mt('[_1] set to none',$titles{$rule}).'</li>';
16276:                                 }
16277:                             } else {
16278:                                 $resulttext .= '<li>'.&mt('[_1] set to [_2]',$titles{$rule},$confighash{'passwords'}{$rule}).'</li>';
16279:                             }
16280:                         }
16281:                         if (ref($confighash{'passwords'}{'chars'}) eq 'ARRAY') {
16282:                             if (@{$confighash{'passwords'}{'chars'}} > 0) {
16283:                                 my %rulenames = &Apache::lonlocal::texthash(
16284:                                                      uc => 'At least one upper case letter',
16285:                                                      lc => 'At least one lower case letter',
16286:                                                      num => 'At least one number',
16287:                                                      spec => 'At least one non-alphanumeric',
16288:                                                    );
16289:                                 my $needed = '<ul><li>'.
16290:                                              join('</li><li>',map {$rulenames{$_} } @{$confighash{'passwords'}{'chars'}}).
16291:                                              '</li></ul>'; 
16292:                                 $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$titles{'chars'},$needed).'</li>';
16293:                             } else {
16294:                                 $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
16295:                             }
16296:                         } else {
16297:                             $resulttext .= '<li>'.&mt('[_1] set to none',$titles{'chars'}).'</li>';
16298:                         }
16299:                     } elsif ($key eq 'crsownerchg') {
16300:                         if (ref($confighash{'passwords'}{'crsownerchg'}) eq 'HASH') {
16301:                             if ((@{$confighash{'passwords'}{'crsownerchg'}{'by'}} == 0) ||
16302:                                 (@{$confighash{'passwords'}{'crsownerchg'}{'for'}} == 0)) {
16303:                                 $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
16304:                             } else {
16305:                                 my %crsownerstr;
16306:                                 foreach my $item ('by','for') {
16307:                                     if (ref($confighash{'passwords'}{'crsownerchg'}{$item}) eq 'ARRAY') {
16308:                                         foreach my $type (@{$confighash{'passwords'}{'crsownerchg'}{$item}}) {
16309:                                             if ($type eq 'default') {
16310:                                                 $crsownerstr{$item} .= $othertitle.', ';
16311:                                             } elsif ($usertypes->{$type} ne '') {
16312:                                                 $crsownerstr{$item} .= $usertypes->{$type}.', ';
16313:                                             }
16314:                                         }
16315:                                         $crsownerstr{$item} =~ s/\Q, \E$//;
16316:                                     }
16317:                                 }
16318:                                 $resulttext .= '<li>'.&mt('Course owner (with status: [_1]) may change passwords for students (with status: [_2]).',
16319:                                            $crsownerstr{'by'},$crsownerstr{'for'}).'</li>';
16320:                             }
16321:                         } else {
16322:                             $resulttext .= '<li>'.&mt('Course owner may not change student passwords.').'</li>';
16323:                         }
16324:                     }
16325:                     $resulttext .= '</ul></li>';
16326:                 }
16327:             }
16328:             $resulttext .= '</ul>';
16329:         } else {
16330:             $resulttext = &mt('No changes made to password settings');
16331:         }
16332:         my $cachetime = 24*60*60;
16333:         if ($updatedefaults) {
16334:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
16335:             if (ref($lastactref) eq 'HASH') {
16336:                 $lastactref->{'domdefaults'} = 1;
16337:             }
16338:         }
16339:         if ($updateconf) {
16340:             &Apache::lonnet::do_cache_new('passwdconf',$dom,$confighash{'passwords'},$cachetime);
16341:             if (ref($lastactref) eq 'HASH') {
16342:                 $lastactref->{'passwdconf'} = 1;
16343:             }
16344:         }
16345:     } else {
16346:         $resulttext = '<span class="LC_error">'.
16347:             &mt('An error occurred: [_1]',$putresult).'</span>';
16348:     }
16349:     if ($errors) {
16350:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
16351:                        $errors.'</ul></p>';
16352:     }
16353:     return $resulttext;
16354: }
16355: 
16356: sub modify_usercreation {
16357:     my ($dom,%domconfig) = @_;
16358:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
16359:     my $warningmsg;
16360:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
16361:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
16362:             if ($key eq 'cancreate') {
16363:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
16364:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
16365:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
16366:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
16367:                         } else {
16368:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
16369:                         }
16370:                     }
16371:                 }
16372:             } elsif ($key eq 'email_rule') {
16373:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
16374:             } else {
16375:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
16376:             }
16377:         }
16378:     }
16379:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
16380:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
16381:     my @contexts = ('author','course','requestcrs');
16382:     foreach my $item(@contexts) {
16383:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
16384:     }
16385:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
16386:         foreach my $item (@contexts) {
16387:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
16388:                 push(@{$changes{'cancreate'}},$item);
16389:             }
16390:         }
16391:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
16392:         foreach my $item (@contexts) {
16393:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
16394:                 if ($cancreate{$item} ne 'any') {
16395:                     push(@{$changes{'cancreate'}},$item);
16396:                 }
16397:             } else {
16398:                 if ($cancreate{$item} ne 'none') {
16399:                     push(@{$changes{'cancreate'}},$item);
16400:                 }
16401:             }
16402:         }
16403:     } else {
16404:         foreach my $item (@contexts)  {
16405:             push(@{$changes{'cancreate'}},$item);
16406:         }
16407:     }
16408: 
16409:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
16410:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
16411:             if (!grep(/^\Q$type\E$/,@username_rule)) {
16412:                 push(@{$changes{'username_rule'}},$type);
16413:             }
16414:         }
16415:         foreach my $type (@username_rule) {
16416:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
16417:                 push(@{$changes{'username_rule'}},$type);
16418:             }
16419:         }
16420:     } else {
16421:         push(@{$changes{'username_rule'}},@username_rule);
16422:     }
16423: 
16424:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
16425:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
16426:             if (!grep(/^\Q$type\E$/,@id_rule)) {
16427:                 push(@{$changes{'id_rule'}},$type);
16428:             }
16429:         }
16430:         foreach my $type (@id_rule) {
16431:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
16432:                 push(@{$changes{'id_rule'}},$type);
16433:             }
16434:         }
16435:     } else {
16436:         push(@{$changes{'id_rule'}},@id_rule);
16437:     }
16438: 
16439:     my @authen_contexts = ('author','course','domain');
16440:     my @authtypes = ('int','krb4','krb5','loc','lti');
16441:     my %authhash;
16442:     foreach my $item (@authen_contexts) {
16443:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
16444:         foreach my $auth (@authtypes) {
16445:             if (grep(/^\Q$auth\E$/,@authallowed)) {
16446:                 $authhash{$item}{$auth} = 1;
16447:             } else {
16448:                 $authhash{$item}{$auth} = 0;
16449:             }
16450:         }
16451:     }
16452:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
16453:         foreach my $item (@authen_contexts) {
16454:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
16455:                 foreach my $auth (@authtypes) {
16456:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
16457:                         push(@{$changes{'authtypes'}},$item);
16458:                         last;
16459:                     }
16460:                 }
16461:             }
16462:         }
16463:     } else {
16464:         foreach my $item (@authen_contexts) {
16465:             push(@{$changes{'authtypes'}},$item);
16466:         }
16467:     }
16468: 
16469:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
16470:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
16471:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
16472:     $save_usercreate{'id_rule'} = \@id_rule;
16473:     $save_usercreate{'username_rule'} = \@username_rule,
16474:     $save_usercreate{'authtypes'} = \%authhash;
16475: 
16476:     my %usercreation_hash =  (
16477:         usercreation     => \%save_usercreate,
16478:     );
16479: 
16480:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
16481:                                              $dom);
16482: 
16483:     if ($putresult eq 'ok') {
16484:         if (keys(%changes) > 0) {
16485:             $resulttext = &mt('Changes made:').'<ul>';
16486:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
16487:                 my %lt = &usercreation_types();
16488:                 foreach my $type (@{$changes{'cancreate'}}) {
16489:                     my $chgtext = $lt{$type}.', ';
16490:                     if ($cancreate{$type} eq 'none') {
16491:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
16492:                     } elsif ($cancreate{$type} eq 'any') {
16493:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
16494:                     } elsif ($cancreate{$type} eq 'official') {
16495:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
16496:                     } elsif ($cancreate{$type} eq 'unofficial') {
16497:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
16498:                     }
16499:                     $resulttext .= '<li>'.$chgtext.'</li>';
16500:                 }
16501:             }
16502:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
16503:                 my ($rules,$ruleorder) = 
16504:                     &Apache::lonnet::inst_userrules($dom,'username');
16505:                 my $chgtext = '<ul>';
16506:                 foreach my $type (@username_rule) {
16507:                     if (ref($rules->{$type}) eq 'HASH') {
16508:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
16509:                     }
16510:                 }
16511:                 $chgtext .= '</ul>';
16512:                 if (@username_rule > 0) {
16513:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
16514:                 } else {
16515:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
16516:                 }
16517:             }
16518:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
16519:                 my ($idrules,$idruleorder) = 
16520:                     &Apache::lonnet::inst_userrules($dom,'id');
16521:                 my $chgtext = '<ul>';
16522:                 foreach my $type (@id_rule) {
16523:                     if (ref($idrules->{$type}) eq 'HASH') {
16524:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
16525:                     }
16526:                 }
16527:                 $chgtext .= '</ul>';
16528:                 if (@id_rule > 0) {
16529:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
16530:                 } else {
16531:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
16532:                 }
16533:             }
16534:             my %authname = &authtype_names();
16535:             my %context_title = &context_names();
16536:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
16537:                 my $chgtext = '<ul>';
16538:                 foreach my $type (@{$changes{'authtypes'}}) {
16539:                     my @allowed;
16540:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
16541:                     foreach my $auth (@authtypes) {
16542:                         if ($authhash{$type}{$auth}) {
16543:                             push(@allowed,$authname{$auth});
16544:                         }
16545:                     }
16546:                     if (@allowed > 0) {
16547:                         $chgtext .= join(', ',@allowed).'</li>';
16548:                     } else {
16549:                         $chgtext .= &mt('none').'</li>';
16550:                     }
16551:                 }
16552:                 $chgtext .= '</ul>';
16553:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
16554:                 $resulttext .= '</li>';
16555:             }
16556:             $resulttext .= '</ul>';
16557:         } else {
16558:             $resulttext = &mt('No changes made to user creation settings');
16559:         }
16560:     } else {
16561:         $resulttext = '<span class="LC_error">'.
16562:             &mt('An error occurred: [_1]',$putresult).'</span>';
16563:     }
16564:     if ($warningmsg ne '') {
16565:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
16566:     }
16567:     return $resulttext;
16568: }
16569: 
16570: sub modify_selfcreation {
16571:     my ($dom,$lastactref,%domconfig) = @_;
16572:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
16573:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
16574:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
16575:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
16576:     if (ref($typesref) eq 'ARRAY') {
16577:         @types = @{$typesref};
16578:     }
16579:     if (ref($usertypesref) eq 'HASH') {
16580:         %usertypes = %{$usertypesref};
16581:     }
16582:     $usertypes{'default'} = $othertitle;
16583: #
16584: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
16585: #
16586:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
16587:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
16588:             if ($key eq 'cancreate') {
16589:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
16590:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
16591:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
16592:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
16593:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
16594:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
16595:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
16596:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
16597:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
16598:                         } else {
16599:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
16600:                         }
16601:                     }
16602:                 }
16603:             } elsif ($key eq 'email_rule') {
16604:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
16605:             } else {
16606:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
16607:             }
16608:         }
16609:     }
16610: #
16611: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
16612: #
16613:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
16614:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
16615:             if ($key eq 'selfcreate') {
16616:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
16617:             } else {
16618:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
16619:             }
16620:         }
16621:     }
16622: #
16623: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
16624: #
16625:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
16626:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
16627:             if ($key eq 'inststatusguest') {
16628:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
16629:             } else {
16630:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
16631:             }
16632:         }
16633:     }
16634: 
16635:     my @contexts = ('selfcreate');
16636:     @{$cancreate{'selfcreate'}} = ();
16637:     %{$cancreate{'emailusername'}} = ();
16638:     if (@types) {
16639:         @{$cancreate{'statustocreate'}} = ();
16640:     }
16641:     %{$cancreate{'selfcreateprocessing'}} = ();
16642:     %{$cancreate{'shibenv'}} = ();
16643:     %{$cancreate{'emailverified'}} = ();
16644:     %{$cancreate{'emailoptions'}} = ();
16645:     %{$cancreate{'emaildomain'}} = ();
16646:     my %selfcreatetypes = (
16647:                              sso   => 'users authenticated by institutional single sign on',
16648:                              login => 'users authenticated by institutional log-in',
16649:                              email => 'users verified by e-mail',
16650:                           );
16651: #
16652: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
16653: # is permitted.
16654: #
16655:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
16656: 
16657:     my (@statuses,%email_rule);
16658:     foreach my $item ('login','sso','email') {
16659:         if ($item eq 'email') {
16660:             if ($env{'form.cancreate_email'}) {
16661:                 if (@types) {
16662:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
16663:                     foreach my $status (@poss_statuses) {
16664:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
16665:                             push(@statuses,$status);
16666:                         }
16667:                     }
16668:                     $save_inststatus{'inststatusguest'} = \@statuses;
16669:                 } else {
16670:                     push(@statuses,'default');
16671:                 }
16672:                 if (@statuses) {
16673:                     my %curr_rule;
16674:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
16675:                         foreach my $type (@statuses) {
16676:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
16677:                         }
16678:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
16679:                         foreach my $type (@statuses) {
16680:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
16681:                         }
16682:                     }
16683:                     push(@{$cancreate{'selfcreate'}},'email');
16684:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
16685:                     my %curremaildom;
16686:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
16687:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
16688:                     }
16689:                     foreach my $type (@statuses) {
16690:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
16691:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
16692:                         }
16693:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
16694:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
16695:                         }
16696:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
16697: #
16698: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
16699: #
16700:                             my $chosen = $1;
16701:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
16702:                                 my $emaildom;
16703:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
16704:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type}; 
16705:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
16706:                                     if (ref($curremaildom{$type}) eq 'HASH') {
16707:                                         if (exists($curremaildom{$type}{$chosen})) {
16708:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
16709:                                                 push(@{$changes{'cancreate'}},'emaildomain');
16710:                                             }
16711:                                         } elsif ($emaildom ne '') {
16712:                                             push(@{$changes{'cancreate'}},'emaildomain');
16713:                                         }
16714:                                     } elsif ($emaildom ne '') {
16715:                                         push(@{$changes{'cancreate'}},'emaildomain');
16716:                                     } 
16717:                                 }
16718:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
16719:                             } elsif ($chosen eq 'custom') {
16720:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
16721:                                 $email_rule{$type} = [];
16722:                                 if (ref($emailrules) eq 'HASH') {
16723:                                     foreach my $rule (@possemail_rules) {
16724:                                         if (exists($emailrules->{$rule})) {
16725:                                             push(@{$email_rule{$type}},$rule);
16726:                                         }
16727:                                     }
16728:                                 }
16729:                                 if (@{$email_rule{$type}}) {
16730:                                     $cancreate{'emailoptions'}{$type} = 'custom';
16731:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
16732:                                         if (@{$curr_rule{$type}} > 0) {
16733:                                             foreach my $rule (@{$curr_rule{$type}}) {
16734:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
16735:                                                     push(@{$changes{'email_rule'}},$type);
16736:                                                 }
16737:                                             }
16738:                                         }
16739:                                         foreach my $type (@{$email_rule{$type}}) {
16740:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
16741:                                                 push(@{$changes{'email_rule'}},$type);
16742:                                             }
16743:                                         }
16744:                                     } else {
16745:                                         push(@{$changes{'email_rule'}},$type);
16746:                                     }
16747:                                 }
16748:                             } else {
16749:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
16750:                             }
16751:                         }
16752:                     }
16753:                     if (@types) {
16754:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
16755:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
16756:                             if (@changed) {
16757:                                 push(@{$changes{'inststatus'}},'inststatusguest');
16758:                             }
16759:                         } else {
16760:                             push(@{$changes{'inststatus'}},'inststatusguest');
16761:                         }
16762:                     }
16763:                 } else {
16764:                     delete($env{'form.cancreate_email'});
16765:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
16766:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
16767:                             push(@{$changes{'inststatus'}},'inststatusguest');
16768:                         }
16769:                     }
16770:                 }
16771:             } else {
16772:                 $save_inststatus{'inststatusguest'} = [];
16773:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
16774:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
16775:                         push(@{$changes{'inststatus'}},'inststatusguest');
16776:                     }
16777:                 }
16778:             }
16779:         } else {
16780:             if ($env{'form.cancreate_'.$item}) {
16781:                 push(@{$cancreate{'selfcreate'}},$item);
16782:             }
16783:         }
16784:     }
16785:     my (%userinfo,%savecaptcha);
16786:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
16787: #
16788: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
16789: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
16790: #
16791: 
16792:     if ($env{'form.cancreate_email'}) {
16793:         push(@contexts,'emailusername');
16794:         if (@statuses) {
16795:             foreach my $type (@statuses) {
16796:                 if (ref($infofields) eq 'ARRAY') {
16797:                     foreach my $field (@{$infofields}) {
16798:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
16799:                             $cancreate{'emailusername'}{$type}{$field} = $1;
16800:                         }
16801:                     }
16802:                 }
16803:             }
16804:         }
16805: #
16806: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
16807: # queued requests for self-creation of account verified by e-mail.
16808: #
16809: 
16810:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
16811:         @approvalnotify = sort(@approvalnotify);
16812:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
16813:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
16814:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
16815:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
16816:                     push(@{$changes{'cancreate'}},'notify');
16817:                 }
16818:             } else {
16819:                 if ($cancreate{'notify'}{'approval'}) {
16820:                     push(@{$changes{'cancreate'}},'notify');
16821:                 }
16822:             }
16823:         } elsif ($cancreate{'notify'}{'approval'}) {
16824:             push(@{$changes{'cancreate'}},'notify');
16825:         }
16826: 
16827:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
16828:     }
16829: #  
16830: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
16831: # institutional log-in.
16832: #
16833:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
16834:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
16835:                ($domdefaults{'auth_def'} eq 'localauth'))) {
16836:             $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.').' '.
16837:                           &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.');
16838:         }
16839:     }
16840:     my @fields = ('lastname','firstname','middlename','generation',
16841:                   'permanentemail','id');
16842:     my @shibfields = (@fields,'inststatus');
16843:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
16844: #
16845: # Where usernames may created for institutional log-in and/or institutional single sign on:
16846: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
16847: # may self-create accounts 
16848: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
16849: # which the user may supply, if institutional data is unavailable.
16850: #
16851:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
16852:         if (@types) {
16853:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
16854:             push(@contexts,'statustocreate');
16855:             foreach my $type (@types) {
16856:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
16857:                 foreach my $field (@fields) {
16858:                     if (grep(/^\Q$field\E$/,@modifiable)) {
16859:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
16860:                     } else {
16861:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
16862:                     }
16863:                 }
16864:             }
16865:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
16866:                 foreach my $type (@types) {
16867:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
16868:                         foreach my $field (@fields) {
16869:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
16870:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
16871:                                 push(@{$changes{'selfcreate'}},$type);
16872:                                 last;
16873:                             }
16874:                         }
16875:                     }
16876:                 }
16877:             } else {
16878:                 foreach my $type (@types) {
16879:                     push(@{$changes{'selfcreate'}},$type);
16880:                 }
16881:             }
16882:         }
16883:         foreach my $field (@shibfields) {
16884:             if ($env{'form.shibenv_'.$field} ne '') {
16885:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
16886:             }
16887:         }
16888:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
16889:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
16890:                 foreach my $field (@shibfields) {
16891:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
16892:                         push(@{$changes{'cancreate'}},'shibenv');
16893:                     }
16894:                 }
16895:             } else {
16896:                 foreach my $field (@shibfields) {
16897:                     if ($env{'form.shibenv_'.$field}) {
16898:                         push(@{$changes{'cancreate'}},'shibenv');
16899:                         last;
16900:                     }
16901:                 }
16902:             }
16903:         }
16904:     }
16905:     foreach my $item (@contexts) {
16906:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
16907:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
16908:                 if (ref($cancreate{$item}) eq 'ARRAY') {
16909:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
16910:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
16911:                             push(@{$changes{'cancreate'}},$item);
16912:                         }
16913:                     }
16914:                 }
16915:             }
16916:             if (ref($cancreate{$item}) eq 'ARRAY') {
16917:                 foreach my $type (@{$cancreate{$item}}) {
16918:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
16919:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
16920:                             push(@{$changes{'cancreate'}},$item);
16921:                         }
16922:                     }
16923:                 }
16924:             }
16925:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
16926:             if (ref($cancreate{$item}) eq 'HASH') {
16927:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
16928:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
16929:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
16930:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
16931:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
16932:                                     push(@{$changes{'cancreate'}},$item);
16933:                                 }
16934:                             }
16935:                         }
16936:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
16937:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
16938:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
16939:                                 push(@{$changes{'cancreate'}},$item);
16940:                             }
16941:                         }
16942:                     }
16943:                 }
16944:                 foreach my $type (keys(%{$cancreate{$item}})) {
16945:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
16946:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
16947:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
16948:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
16949:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
16950:                                         push(@{$changes{'cancreate'}},$item);
16951:                                     }
16952:                                 }
16953:                             } else {
16954:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
16955:                                     push(@{$changes{'cancreate'}},$item);
16956:                                 }
16957:                             }
16958:                         }
16959:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
16960:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
16961:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
16962:                                 push(@{$changes{'cancreate'}},$item);
16963:                             }
16964:                         }
16965:                     }
16966:                 }
16967:             }
16968:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
16969:             if (ref($cancreate{$item}) eq 'ARRAY') {
16970:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
16971:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
16972:                         push(@{$changes{'cancreate'}},$item);
16973:                     }
16974:                 }
16975:             }
16976:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
16977:             if (ref($cancreate{$item}) eq 'HASH') {
16978:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
16979:                     push(@{$changes{'cancreate'}},$item);
16980:                 }
16981:             }
16982:         } elsif ($item eq 'emailusername') {
16983:             if (ref($cancreate{$item}) eq 'HASH') {
16984:                 foreach my $type (keys(%{$cancreate{$item}})) {
16985:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
16986:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
16987:                             if ($cancreate{$item}{$type}{$field}) {
16988:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
16989:                                     push(@{$changes{'cancreate'}},$item);
16990:                                 }
16991:                                 last;
16992:                             }
16993:                         }
16994:                     }
16995:                 }
16996:             }
16997:         }
16998:     }
16999: #
17000: # Populate %save_usercreate hash with updates to self-creation configuration.
17001: #
17002:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
17003:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
17004:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
17005:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
17006:     if (ref($cancreate{'notify'}) eq 'HASH') {
17007:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
17008:     }
17009:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
17010:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
17011:     }
17012:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
17013:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
17014:     }
17015:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
17016:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
17017:     }
17018:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
17019:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
17020:     }
17021:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
17022:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
17023:     }
17024:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
17025:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
17026:     }
17027:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
17028:     $save_usercreate{'email_rule'} = \%email_rule;
17029: 
17030:     my %userconfig_hash = (
17031:             usercreation     => \%save_usercreate,
17032:             usermodification => \%save_usermodify,
17033:             inststatus       => \%save_inststatus,
17034:     );
17035: 
17036:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
17037:                                              $dom);
17038: #
17039: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
17040: #
17041:     if ($putresult eq 'ok') {
17042:         if (keys(%changes) > 0) {
17043:             $resulttext = &mt('Changes made:').'<ul>';
17044:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
17045:                 my %lt = &selfcreation_types();
17046:                 foreach my $type (@{$changes{'cancreate'}}) {
17047:                     my $chgtext = '';
17048:                     if ($type eq 'selfcreate') {
17049:                         if (@{$cancreate{$type}} == 0) {
17050:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
17051:                         } else {
17052:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
17053:                                         '<ul>';
17054:                             foreach my $case (@{$cancreate{$type}}) {
17055:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
17056:                             }
17057:                             $chgtext .= '</ul>';
17058:                             if (ref($cancreate{$type}) eq 'ARRAY') {
17059:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
17060:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
17061:                                         if (@{$cancreate{'statustocreate'}} == 0) {
17062:                                             $chgtext .= '<span class="LC_warning">'.
17063:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
17064:                                                         '</span><br />';
17065:                                         }
17066:                                     }
17067:                                 }
17068:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
17069:                                     if (!@statuses) {
17070:                                         $chgtext .= '<span class="LC_warning">'.
17071:                                                     &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.").
17072:                                                     '</span><br />';
17073: 
17074:                                     }
17075:                                 }
17076:                             }
17077:                         }
17078:                     } elsif ($type eq 'shibenv') {
17079:                         if (keys(%{$cancreate{$type}}) == 0) {
17080:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
17081:                         } else {
17082:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
17083:                                         '<ul>';
17084:                             foreach my $field (@shibfields) {
17085:                                 next if ($cancreate{$type}{$field} eq '');
17086:                                 if ($field eq 'inststatus') {
17087:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
17088:                                 } else {
17089:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
17090:                                 }
17091:                             }
17092:                             $chgtext .= '</ul>';
17093:                         }
17094:                     } elsif ($type eq 'statustocreate') {
17095:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
17096:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
17097:                             if (@{$cancreate{'selfcreate'}} > 0) {
17098:                                 if (@{$cancreate{'statustocreate'}} == 0) {
17099:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
17100:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
17101:                                         $chgtext .= '<br />'.
17102:                                                     '<span class="LC_warning">'.
17103:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
17104:                                                     '</span>';
17105:                                     }
17106:                                 } elsif (keys(%usertypes) > 0) {
17107:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
17108:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
17109:                                     } else {
17110:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
17111:                                     }
17112:                                     $chgtext .= '<ul>';
17113:                                     foreach my $case (@{$cancreate{$type}}) {
17114:                                         if ($case eq 'default') {
17115:                                             $chgtext .= '<li>'.$othertitle.'</li>';
17116:                                         } else {
17117:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
17118:                                         }
17119:                                     }
17120:                                     $chgtext .= '</ul>';
17121:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
17122:                                         $chgtext .= '<span class="LC_warning">'.
17123:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
17124:                                                     '</span>';
17125:                                     }
17126:                                 }
17127:                             } else {
17128:                                 if (@{$cancreate{$type}} == 0) {
17129:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
17130:                                 } else {
17131:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
17132:                                 }
17133:                             }
17134:                             $chgtext .= '<br />';
17135:                         }
17136:                     } elsif ($type eq 'selfcreateprocessing') {
17137:                         my %choices = &Apache::lonlocal::texthash (
17138:                                                                     automatic => 'Automatic approval',
17139:                                                                     approval  => 'Queued for approval',
17140:                                                                   );
17141:                         if (@types) {
17142:                             if (@statuses) {
17143:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:'). 
17144:                                             '<ul>';
17145:                                 foreach my $status (@statuses) {
17146:                                     if ($status eq 'default') {
17147:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
17148:                                     } else {
17149:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
17150:                                     }
17151:                                 }
17152:                                 $chgtext .= '</ul>';
17153:                             }
17154:                         } else {
17155:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
17156:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
17157:                         }
17158:                     } elsif ($type eq 'emailverified') {
17159:                         my %options = &Apache::lonlocal::texthash (
17160:                                                                     all   => 'Same as e-mail',
17161:                                                                     first => 'Omit @domain',
17162:                                                                     free  => 'Free to choose',
17163:                                                                   );
17164:                         if (@types) {
17165:                             if (@statuses) {
17166:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
17167:                                             '<ul>';
17168:                                 foreach my $status (@statuses) {
17169:                                     if ($status eq 'default') {
17170:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
17171:                                     } else {
17172:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
17173:                                     }
17174:                                 }
17175:                                 $chgtext .= '</ul>';
17176:                             }
17177:                         } else {
17178:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
17179:                                             $options{$cancreate{'emailverified'}{'default'}});
17180:                         }
17181:                     } elsif ($type eq 'emailoptions') {
17182:                         my %options = &Apache::lonlocal::texthash (
17183:                                                                     any     => 'Any e-mail',
17184:                                                                     inst    => 'Institutional only',
17185:                                                                     noninst => 'Non-institutional only',
17186:                                                                     custom  => 'Custom restrictions',
17187:                                                                   );
17188:                         if (@types) {
17189:                             if (@statuses) {
17190:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
17191:                                             '<ul>';
17192:                                 foreach my $status (@statuses) {
17193:                                     if ($type eq 'default') {
17194:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
17195:                                     } else {
17196:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
17197:                                     }
17198:                                 }
17199:                                 $chgtext .= '</ul>';
17200:                             }
17201:                         } else {
17202:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
17203:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
17204:                             } else {
17205:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
17206:                                                 $options{$cancreate{'emailoptions'}{'default'}});
17207:                             }
17208:                         }
17209:                     } elsif ($type eq 'emaildomain') {
17210:                         my $output;
17211:                         if (@statuses) {
17212:                             foreach my $type (@statuses) {
17213:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
17214:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
17215:                                         if ($type eq 'default') {
17216:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
17217:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
17218:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
17219:                                             } else {
17220:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
17221:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
17222:                                             }
17223:                                         } else {
17224:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
17225:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
17226:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
17227:                                             } else {
17228:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
17229:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>'; 
17230:                                             }
17231:                                         }
17232:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
17233:                                         if ($type eq 'default') {
17234:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
17235:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
17236:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
17237:                                             } else {
17238:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
17239:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
17240:                                             }
17241:                                         } else {
17242:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
17243:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
17244:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
17245:                                             } else {
17246:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
17247:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';   
17248:                                             }
17249:                                         }
17250:                                     }
17251:                                 }
17252:                             }
17253:                         }
17254:                         if ($output ne '') {
17255:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
17256:                                         '<ul>'.$output.'</ul>';
17257:                         }
17258:                     } elsif ($type eq 'captcha') {
17259:                         if ($savecaptcha{$type} eq 'notused') {
17260:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
17261:                         } else {
17262:                             my %captchas = &captcha_phrases();
17263:                             if ($captchas{$savecaptcha{$type}}) {
17264:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
17265:                             } else {
17266:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
17267:                             }
17268:                         }
17269:                     } elsif ($type eq 'recaptchakeys') {
17270:                         my ($privkey,$pubkey);
17271:                         if (ref($savecaptcha{$type}) eq 'HASH') {
17272:                             $pubkey = $savecaptcha{$type}{'public'};
17273:                             $privkey = $savecaptcha{$type}{'private'};
17274:                         }
17275:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
17276:                         if (!$pubkey) {
17277:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
17278:                         } else {
17279:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
17280:                         }
17281:                         if (!$privkey) {
17282:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
17283:                         } else {
17284:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
17285:                         }
17286:                         $chgtext .= '</ul>';
17287:                     } elsif ($type eq 'recaptchaversion') {
17288:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
17289:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
17290:                         }
17291:                     } elsif ($type eq 'emailusername') {
17292:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
17293:                             if (@statuses) {
17294:                                 foreach my $type (@statuses) {
17295:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
17296:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
17297:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
17298:                                                     '<ul>';
17299:                                             foreach my $field (@{$infofields}) {
17300:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
17301:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
17302:                                                 }
17303:                                             }
17304:                                             $chgtext .= '</ul>';
17305:                                         } else {
17306:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
17307:                                         }
17308:                                     } else {
17309:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
17310:                                     }
17311:                                 }
17312:                             }
17313:                         }
17314:                     } elsif ($type eq 'notify') {
17315:                         my $numapprove = 0;
17316:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
17317:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
17318:                                 if ($cancreate{'notify'}{'approval'}) {
17319:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
17320:                                     $numapprove ++;
17321:                                 }
17322:                             }
17323:                         }
17324:                         unless ($numapprove) {
17325:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
17326:                         }
17327:                     }
17328:                     if ($chgtext) {
17329:                         $resulttext .= '<li>'.$chgtext.'</li>';
17330:                     }
17331:                 }
17332:             }
17333:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
17334:                 my ($emailrules,$emailruleorder) =
17335:                     &Apache::lonnet::inst_userrules($dom,'email');
17336:                 foreach my $type (@{$changes{'email_rule'}}) {
17337:                     if (ref($email_rule{$type}) eq 'ARRAY') {
17338:                         my $chgtext = '<ul>';
17339:                         foreach my $rule (@{$email_rule{$type}}) {
17340:                             if (ref($emailrules->{$rule}) eq 'HASH') {
17341:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
17342:                             }
17343:                         }
17344:                         $chgtext .= '</ul>';
17345:                         my $typename;
17346:                         if (@types) {
17347:                             if ($type eq 'default') {
17348:                                 $typename = $othertitle;
17349:                             } else {
17350:                                 $typename = $usertypes{$type};
17351:                             } 
17352:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
17353:                         }
17354:                         if (@{$email_rule{$type}} > 0) {
17355:                             $resulttext .= '<li>'.
17356:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
17357:                                                $usertypes{$type}).
17358:                                            $chgtext.
17359:                                            '</li>';
17360:                         } else {
17361:                             $resulttext .= '<li>'.
17362:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
17363:                                            '</li>'.
17364:                                            &mt('(Affiliation: [_1])',$typename);
17365:                         }
17366:                     }
17367:                 }
17368:             }
17369:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
17370:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
17371:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
17372:                         my $chgtext = '<ul>';
17373:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
17374:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
17375:                         }
17376:                         $chgtext .= '</ul>';
17377:                         $resulttext .= '<li>'.
17378:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
17379:                                           $chgtext.
17380:                                        '</li>';
17381:                     } else {
17382:                         $resulttext .= '<li>'.
17383:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
17384:                                        '</li>';
17385:                     }
17386:                 }
17387:             }
17388:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
17389:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
17390:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
17391:                 foreach my $type (@{$changes{'selfcreate'}}) {
17392:                     my $typename = $type;
17393:                     if (keys(%usertypes) > 0) {
17394:                         if ($usertypes{$type} ne '') {
17395:                             $typename = $usertypes{$type};
17396:                         }
17397:                     }
17398:                     my @modifiable;
17399:                     $resulttext .= '<li>'.
17400:                                     &mt('Self-creation of account by users with status: [_1]',
17401:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
17402:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
17403:                     foreach my $field (@fields) {
17404:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
17405:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
17406:                         }
17407:                     }
17408:                     if (@modifiable > 0) {
17409:                         $resulttext .= join(', ',@modifiable);
17410:                     } else {
17411:                         $resulttext .= &mt('none');
17412:                     }
17413:                     $resulttext .= '</li>';
17414:                 }
17415:                 $resulttext .= '</ul></li>';
17416:             }
17417:             $resulttext .= '</ul>';
17418:             my $cachetime = 24*60*60;
17419:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
17420:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
17421:             if (ref($lastactref) eq 'HASH') {
17422:                 $lastactref->{'domdefaults'} = 1;
17423:             }
17424:         } else {
17425:             $resulttext = &mt('No changes made to self-creation settings');
17426:         }
17427:     } else {
17428:         $resulttext = '<span class="LC_error">'.
17429:             &mt('An error occurred: [_1]',$putresult).'</span>';
17430:     }
17431:     if ($warningmsg ne '') {
17432:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
17433:     }
17434:     return $resulttext;
17435: }
17436: 
17437: sub process_captcha {
17438:     my ($container,$changes,$newsettings,$currsettings) = @_;
17439:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH'));
17440:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
17441:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
17442:         $newsettings->{'captcha'} = 'original';
17443:     }
17444:     my %current;
17445:     if (ref($currsettings) eq 'HASH') {
17446:         %current = %{$currsettings};
17447:     }
17448:     if ($current{'captcha'} ne $newsettings->{'captcha'}) {
17449:         if ($container eq 'cancreate') {
17450:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
17451:                 push(@{$changes->{'cancreate'}},'captcha');
17452:             } elsif (!defined($changes->{'cancreate'})) {
17453:                 $changes->{'cancreate'} = ['captcha'];
17454:             }
17455:         } elsif ($container eq 'passwords') {
17456:             $changes->{'reset'} = 1;
17457:         } else {
17458:             $changes->{'captcha'} = 1;
17459:         }
17460:     }
17461:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
17462:     if ($newsettings->{'captcha'} eq 'recaptcha') {
17463:         $newpub = $env{'form.'.$container.'_recaptchapub'};
17464:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
17465:         $newpub =~ s/[^\w\-]//g;
17466:         $newpriv =~ s/[^\w\-]//g;
17467:         $newsettings->{'recaptchakeys'} = {
17468:                                              public  => $newpub,
17469:                                              private => $newpriv,
17470:                                           };
17471:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
17472:         $newversion =~ s/\D//g;
17473:         if ($newversion ne '2') {
17474:             $newversion = 1;
17475:         }
17476:         $newsettings->{'recaptchaversion'} = $newversion;
17477:     }
17478:     if (ref($current{'recaptchakeys'}) eq 'HASH') {
17479:         $currpub = $current{'recaptchakeys'}{'public'};
17480:         $currpriv = $current{'recaptchakeys'}{'private'};
17481:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
17482:             $newsettings->{'recaptchakeys'} = {
17483:                                                  public  => '',
17484:                                                  private => '',
17485:                                               }
17486:         }
17487:     }
17488:     if ($current{'captcha'} eq 'recaptcha') {
17489:         $currversion = $current{'recaptchaversion'};
17490:         if ($currversion ne '2') {
17491:             $currversion = 1;
17492:         }
17493:     }
17494:     if ($currversion ne $newversion) {
17495:         if ($container eq 'cancreate') {
17496:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
17497:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
17498:             } elsif (!defined($changes->{'cancreate'})) {
17499:                 $changes->{'cancreate'} = ['recaptchaversion'];
17500:             }
17501:         } elsif ($container eq 'passwords') {
17502:             $changes->{'reset'} = 1;
17503:         } else {
17504:             $changes->{'recaptchaversion'} = 1;
17505:         }
17506:     }
17507:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
17508:         if ($container eq 'cancreate') {
17509:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
17510:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
17511:             } elsif (!defined($changes->{'cancreate'})) {
17512:                 $changes->{'cancreate'} = ['recaptchakeys'];
17513:             }
17514:         } elsif ($container eq 'passwords') {
17515:             $changes->{'reset'} = 1;
17516:         } else {
17517:             $changes->{'recaptchakeys'} = 1;
17518:         }
17519:     }
17520:     return;
17521: }
17522: 
17523: sub modify_usermodification {
17524:     my ($dom,%domconfig) = @_;
17525:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
17526:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
17527:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
17528:             if ($key eq 'selfcreate') {
17529:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
17530:             } else {  
17531:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
17532:             }
17533:         }
17534:     }
17535:     my @contexts = ('author','course');
17536:     my %context_title = (
17537:                            author => 'In author context',
17538:                            course => 'In course context',
17539:                         );
17540:     my @fields = ('lastname','firstname','middlename','generation',
17541:                   'permanentemail','id');
17542:     my %roles = (
17543:                   author => ['ca','aa'],
17544:                   course => ['st','ep','ta','in','cr'],
17545:                 );
17546:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
17547:     foreach my $context (@contexts) {
17548:         foreach my $role (@{$roles{$context}}) {
17549:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
17550:             foreach my $item (@fields) {
17551:                 if (grep(/^\Q$item\E$/,@modifiable)) {
17552:                     $modifyhash{$context}{$role}{$item} = 1;
17553:                 } else {
17554:                     $modifyhash{$context}{$role}{$item} = 0;
17555:                 }
17556:             }
17557:         }
17558:         if (ref($curr_usermodification{$context}) eq 'HASH') {
17559:             foreach my $role (@{$roles{$context}}) {
17560:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
17561:                     foreach my $field (@fields) {
17562:                         if ($modifyhash{$context}{$role}{$field} ne 
17563:                                 $curr_usermodification{$context}{$role}{$field}) {
17564:                             push(@{$changes{$context}},$role);
17565:                             last;
17566:                         }
17567:                     }
17568:                 }
17569:             }
17570:         } else {
17571:             foreach my $context (@contexts) {
17572:                 foreach my $role (@{$roles{$context}}) {
17573:                     push(@{$changes{$context}},$role);
17574:                 }
17575:             }
17576:         }
17577:     }
17578:     my %usermodification_hash =  (
17579:                                    usermodification => \%modifyhash,
17580:                                  );
17581:     my $putresult = &Apache::lonnet::put_dom('configuration',
17582:                                              \%usermodification_hash,$dom);
17583:     if ($putresult eq 'ok') {
17584:         if (keys(%changes) > 0) {
17585:             $resulttext = &mt('Changes made: ').'<ul>';
17586:             foreach my $context (@contexts) {
17587:                 if (ref($changes{$context}) eq 'ARRAY') {
17588:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
17589:                     if (ref($changes{$context}) eq 'ARRAY') {
17590:                         foreach my $role (@{$changes{$context}}) {
17591:                             my $rolename;
17592:                             if ($role eq 'cr') {
17593:                                 $rolename = &mt('Custom');
17594:                             } else {
17595:                                 $rolename = &Apache::lonnet::plaintext($role);
17596:                             }
17597:                             my @modifiable;
17598:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
17599:                             foreach my $field (@fields) {
17600:                                 if ($modifyhash{$context}{$role}{$field}) {
17601:                                     push(@modifiable,$fieldtitles{$field});
17602:                                 }
17603:                             }
17604:                             if (@modifiable > 0) {
17605:                                 $resulttext .= join(', ',@modifiable);
17606:                             } else {
17607:                                 $resulttext .= &mt('none'); 
17608:                             }
17609:                             $resulttext .= '</li>';
17610:                         }
17611:                         $resulttext .= '</ul></li>';
17612:                     }
17613:                 }
17614:             }
17615:             $resulttext .= '</ul>';
17616:         } else {
17617:             $resulttext = &mt('No changes made to user modification settings');
17618:         }
17619:     } else {
17620:         $resulttext = '<span class="LC_error">'.
17621:             &mt('An error occurred: [_1]',$putresult).'</span>';
17622:     }
17623:     return $resulttext;
17624: }
17625: 
17626: sub modify_defaults {
17627:     my ($dom,$lastactref,%domconfig) = @_;
17628:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
17629:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
17630:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
17631:                  'portal_def');
17632:     my @authtypes = ('internal','krb4','krb5','localauth','lti');
17633:     foreach my $item (@items) {
17634:         $newvalues{$item} = $env{'form.'.$item};
17635:         if ($item eq 'auth_def') {
17636:             if ($newvalues{$item} ne '') {
17637:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
17638:                     push(@errors,$item);
17639:                 }
17640:             }
17641:         } elsif ($item eq 'lang_def') {
17642:             if ($newvalues{$item} ne '') {
17643:                 if ($newvalues{$item} =~ /^(\w+)/) {
17644:                     my $langcode = $1;
17645:                     if ($langcode ne 'x_chef') {
17646:                         if (code2language($langcode) eq '') {
17647:                             push(@errors,$item);
17648:                         }
17649:                     }
17650:                 } else {
17651:                     push(@errors,$item);
17652:                 }
17653:             }
17654:         } elsif ($item eq 'timezone_def') {
17655:             if ($newvalues{$item} ne '') {
17656:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
17657:                     push(@errors,$item);   
17658:                 }
17659:             }
17660:         } elsif ($item eq 'datelocale_def') {
17661:             if ($newvalues{$item} ne '') {
17662:                 my @datelocale_ids = DateTime::Locale->ids();
17663:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
17664:                     push(@errors,$item);
17665:                 }
17666:             }
17667:         } elsif ($item eq 'portal_def') {
17668:             if ($newvalues{$item} ne '') {
17669:                 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])\/?$/) {
17670:                     push(@errors,$item);
17671:                 }
17672:             }
17673:         }
17674:         if (grep(/^\Q$item\E$/,@errors)) {
17675:             $newvalues{$item} = $domdefaults{$item};
17676:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
17677:             $changes{$item} = 1;
17678:         }
17679:         $domdefaults{$item} = $newvalues{$item};
17680:     }
17681:     my %staticdefaults = (
17682:                            'intauth_cost'   => 10,
17683:                            'intauth_check'  => 0,
17684:                            'intauth_switch' => 0,
17685:                          );
17686:     foreach my $item ('intauth_cost','intauth_check','intauth_switch') {
17687:         if (exists($domdefaults{$item})) {
17688:             $newvalues{$item} = $domdefaults{$item};
17689:         } else {
17690:             $newvalues{$item} = $staticdefaults{$item};
17691:         }
17692:     }
17693:     my %defaults_hash = (
17694:                          defaults => \%newvalues,
17695:                         );
17696:     my $title = &defaults_titles();
17697: 
17698:     my $currinststatus;
17699:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
17700:         $currinststatus = $domconfig{'inststatus'};
17701:     } else {
17702:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
17703:         $currinststatus = {
17704:                              inststatustypes => $usertypes,
17705:                              inststatusorder => $types,
17706:                              inststatusguest => [],
17707:                           };
17708:     }
17709:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
17710:     my @allpos;
17711:     my %alltypes;
17712:     my @inststatusguest;
17713:     if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
17714:         foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
17715:             unless (grep(/^\Q$type\E$/,@todelete)) {
17716:                 push(@inststatusguest,$type);
17717:             }
17718:         }
17719:     }
17720:     my ($currtitles,$currorder);
17721:     if (ref($currinststatus) eq 'HASH') {
17722:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
17723:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
17724:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
17725:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
17726:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
17727:                     }
17728:                 }
17729:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
17730:                     my $position = $env{'form.inststatus_pos_'.$type};
17731:                     $position =~ s/\D+//g;
17732:                     $allpos[$position] = $type;
17733:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
17734:                     $alltypes{$type} =~ s/`//g;
17735:                 }
17736:             }
17737:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
17738:             $currtitles =~ s/,$//;
17739:         }
17740:     }
17741:     if ($env{'form.addinststatus'}) {
17742:         my $newtype = $env{'form.addinststatus'};
17743:         $newtype =~ s/\W//g;
17744:         unless (exists($alltypes{$newtype})) {
17745:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
17746:             $alltypes{$newtype} =~ s/`//g; 
17747:             my $position = $env{'form.addinststatus_pos'};
17748:             $position =~ s/\D+//g;
17749:             if ($position ne '') {
17750:                 $allpos[$position] = $newtype;
17751:             }
17752:         }
17753:     }
17754:     my @orderedstatus;
17755:     foreach my $type (@allpos) {
17756:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
17757:             push(@orderedstatus,$type);
17758:         }
17759:     }
17760:     foreach my $type (keys(%alltypes)) {
17761:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
17762:             delete($alltypes{$type});
17763:         }
17764:     }
17765:     $defaults_hash{'inststatus'} = {
17766:                                      inststatustypes => \%alltypes,
17767:                                      inststatusorder => \@orderedstatus,
17768:                                      inststatusguest => \@inststatusguest,
17769:                                    };
17770:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
17771:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
17772:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
17773:         }
17774:     }
17775:     if ($currorder ne join(',',@orderedstatus)) {
17776:         $changes{'inststatus'}{'inststatusorder'} = 1;
17777:     }
17778:     my $newtitles;
17779:     foreach my $item (@orderedstatus) {
17780:         $newtitles .= $alltypes{$item}.',';
17781:     }
17782:     $newtitles =~ s/,$//;
17783:     if ($currtitles ne $newtitles) {
17784:         $changes{'inststatus'}{'inststatustypes'} = 1;
17785:     }
17786:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
17787:                                              $dom);
17788:     if ($putresult eq 'ok') {
17789:         if (keys(%changes) > 0) {
17790:             $resulttext = &mt('Changes made:').'<ul>';
17791:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
17792:             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";
17793:             foreach my $item (sort(keys(%changes))) {
17794:                 if ($item eq 'inststatus') {
17795:                     if (ref($changes{'inststatus'}) eq 'HASH') {
17796:                         if (@orderedstatus) {
17797:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
17798:                             foreach my $type (@orderedstatus) { 
17799:                                 $resulttext .= $alltypes{$type}.', ';
17800:                             }
17801:                             $resulttext =~ s/, $//;
17802:                             $resulttext .= '</li>';
17803:                         } else {
17804:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>'; 
17805:                         }
17806:                     }
17807:                 } else {
17808:                     my $value = $env{'form.'.$item};
17809:                     if ($value eq '') {
17810:                         $value = &mt('none');
17811:                     } elsif ($item eq 'auth_def') {
17812:                         my %authnames = &authtype_names();
17813:                         my %shortauth = (
17814:                                           internal   => 'int',
17815:                                           krb4       => 'krb4',
17816:                                           krb5       => 'krb5',
17817:                                           localauth  => 'loc',
17818:                                           lti        => 'lti',
17819:                         );
17820:                         $value = $authnames{$shortauth{$value}};
17821:                     }
17822:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
17823:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
17824:                 }
17825:             }
17826:             $resulttext .= '</ul>';
17827:             $mailmsgtext .= "\n";
17828:             my $cachetime = 24*60*60;
17829:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
17830:             if (ref($lastactref) eq 'HASH') {
17831:                 $lastactref->{'domdefaults'} = 1;
17832:             }
17833:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
17834:                 my $notify = 1;
17835:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
17836:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
17837:                         $notify = 0;
17838:                     }
17839:                 }
17840:                 if ($notify) {
17841:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
17842:                                                "LON-CAPA Domain Settings Change - $dom",
17843:                                                $mailmsgtext);
17844:                 }
17845:             }
17846:         } else {
17847:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
17848:         }
17849:     } else {
17850:         $resulttext = '<span class="LC_error">'.
17851:             &mt('An error occurred: [_1]',$putresult).'</span>';
17852:     }
17853:     if (@errors > 0) {
17854:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
17855:         foreach my $item (@errors) {
17856:             $resulttext .= ' "'.$title->{$item}.'",';
17857:         }
17858:         $resulttext =~ s/,$//;
17859:     }
17860:     return $resulttext;
17861: }
17862: 
17863: sub modify_scantron {
17864:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
17865:     my ($resulttext,%confhash,%changes,$errors);
17866:     my $custom = 'custom.tab';
17867:     my $default = 'default.tab';
17868:     my $servadm = $r->dir_config('lonAdmEMail');
17869:     my ($configuserok,$author_ok,$switchserver) =
17870:         &config_check($dom,$confname,$servadm);
17871:     if ($env{'form.scantronformat.filename'} ne '') {
17872:         my $error;
17873:         if ($configuserok eq 'ok') {
17874:             if ($switchserver) {
17875:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
17876:             } else {
17877:                 if ($author_ok eq 'ok') {
17878:                     my ($result,$scantronurl) =
17879:                         &publishlogo($r,'upload','scantronformat',$dom,
17880:                                      $confname,'scantron','','',$custom);
17881:                     if ($result eq 'ok') {
17882:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
17883:                         $changes{'scantronformat'} = 1;
17884:                     } else {
17885:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
17886:                     }
17887:                 } else {
17888:                     $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);
17889:                 }
17890:             }
17891:         } else {
17892:             $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);
17893:         }
17894:         if ($error) {
17895:             &Apache::lonnet::logthis($error);
17896:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
17897:         }
17898:     }
17899:     if (ref($domconfig{'scantron'}) eq 'HASH') {
17900:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
17901:             if ($env{'form.scantronformat_del'}) {
17902:                 $confhash{'scantron'}{'scantronformat'} = '';
17903:                 $changes{'scantronformat'} = 1;
17904:             } else {
17905:                 $confhash{'scantron'}{'scantronformat'} = $domconfig{'scantron'}{'scantronformat'};
17906:             }
17907:         }
17908:     }
17909:     my @options = ('hdr','pad','rem');
17910:     my @fields = &scantroncsv_fields();
17911:     my %titles = &scantronconfig_titles();
17912:     my @formats = &Apache::loncommon::get_env_multiple('form.scantronconfig');
17913:     my ($newdat,$currdat,%newcol,%currcol);
17914:     if (grep(/^dat$/,@formats)) {
17915:         $confhash{'scantron'}{config}{dat} = 1;
17916:         $newdat = 1;
17917:     } else {
17918:         $newdat = 0;
17919:     }
17920:     if (grep(/^csv$/,@formats)) {
17921:         my %bynum;
17922:         foreach my $field (@fields) {
17923:             if ($env{'form.scantronconfig_csv_'.$field} =~ /^(\d+)$/) {
17924:                 my $posscol = $1;
17925:                 if (($posscol < 20) && (!$bynum{$posscol})) {
17926:                     $confhash{'scantron'}{config}{csv}{fields}{$field} = $posscol;
17927:                     $bynum{$posscol} = $field;
17928:                     $newcol{$field} = $posscol;
17929:                 }
17930:             }
17931:         }
17932:         if (keys(%newcol)) {
17933:             foreach my $option (@options) {
17934:                 if ($env{'form.scantroncsv_'.$option}) {
17935:                     $confhash{'scantron'}{config}{csv}{options}{$option} = 1;
17936:                 }
17937:             }
17938:         }
17939:     }
17940:     $currdat = 1;
17941:     if (ref($domconfig{'scantron'}) eq 'HASH') {
17942:         if (ref($domconfig{'scantron'}{'config'}) eq 'HASH') {
17943:             unless (exists($domconfig{'scantron'}{'config'}{'dat'})) {
17944:                 $currdat = 0;
17945:             }
17946:             if (ref($domconfig{'scantron'}{'config'}{'csv'}) eq 'HASH') {
17947:                 if (ref($domconfig{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
17948:                     %currcol = %{$domconfig{'scantron'}{'config'}{'csv'}{'fields'}};
17949:                 }
17950:             }
17951:         }
17952:     }
17953:     if ($currdat != $newdat) {
17954:         $changes{'config'} = 1;
17955:     } else {
17956:         foreach my $field (@fields) {
17957:             if ($currcol{$field} ne '') {
17958:                 if ($currcol{$field} ne $newcol{$field}) {
17959:                     $changes{'config'} = 1;
17960:                     last;
17961:                 }
17962:             } elsif ($newcol{$field} ne '') {
17963:                 $changes{'config'} = 1;
17964:                 last;
17965:             }
17966:         }
17967:     }
17968:     if (keys(%confhash) > 0) {
17969:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
17970:                                                  $dom);
17971:         if ($putresult eq 'ok') {
17972:             if (keys(%changes) > 0) {
17973:                 if (ref($confhash{'scantron'}) eq 'HASH') {
17974:                     $resulttext = &mt('Changes made:').'<ul>';
17975:                     if ($changes{'scantronformat'}) {
17976:                         if ($confhash{'scantron'}{'scantronformat'} eq '') {
17977:                             $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
17978:                         } else {
17979:                             $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
17980:                         }
17981:                     }
17982:                     if ($changes{'config'}) {
17983:                         if (ref($confhash{'scantron'}{'config'}) eq 'HASH') {
17984:                             if ($confhash{'scantron'}{'config'}{'dat'}) {
17985:                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .dat format').'</li>';
17986:                             }
17987:                             if (ref($confhash{'scantron'}{'config'}{'csv'}) eq 'HASH') {
17988:                                 if (ref($confhash{'scantron'}{'config'}{'csv'}{'fields'}) eq 'HASH') {
17989:                                     if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'fields'}})) {
17990:                                         $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following fields/column numbers supported:').'<ul>';
17991:                                         foreach my $field (@fields) {
17992:                                             if ($confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} ne '') {
17993:                                                 my $showcol = $confhash{'scantron'}{'config'}{'csv'}{'fields'}{$field} + 1;
17994:                                                 $resulttext .= '<li>'.$titles{$field}.': '.$showcol.'</li>';
17995:                                             }
17996:                                         }
17997:                                         $resulttext .= '</ul></li>';
17998:                                         if (ref($confhash{'scantron'}{'config'}{'csv'}{'options'}) eq 'HASH') {
17999:                                             if (keys(%{$confhash{'scantron'}{'config'}{'csv'}{'options'}})) {
18000:                                                 $resulttext .= '<li>'.&mt('Bubblesheet data upload formats includes .csv format, with following options:').'<ul>';
18001:                                                 foreach my $option (@options) {
18002:                                                     if ($confhash{'scantron'}{'config'}{'csv'}{'options'}{$option} ne '') {
18003:                                                         $resulttext .= '<li>'.$titles{$option}.'</li>';
18004:                                                     }
18005:                                                 }
18006:                                                 $resulttext .= '</ul></li>';
18007:                                             }
18008:                                         }
18009:                                     }
18010:                                 }
18011:                             }
18012:                         } else {
18013:                             $resulttext .= '<li>'.&mt('No bubblesheet data upload formats set -- will default to assuming .dat format').'</li>';
18014:                         }
18015:                     }
18016:                     $resulttext .= '</ul>';
18017:                 } else {
18018:                     $resulttext = &mt('Changes made to bubblesheet format file.');
18019:                 }
18020:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
18021:                 if (ref($lastactref) eq 'HASH') {
18022:                     $lastactref->{'domainconfig'} = 1;
18023:                 }
18024:             } else {
18025:                 $resulttext = &mt('No changes made to bubblesheet format settings');
18026:             }
18027:         } else {
18028:             $resulttext = '<span class="LC_error">'.
18029:                 &mt('An error occurred: [_1]',$putresult).'</span>';
18030:         }
18031:     } else {
18032:         $resulttext = &mt('No changes made to bubblesheet format file'); 
18033:     }
18034:     if ($errors) {
18035:         $resulttext .= '<p>'.&mt('The following errors occurred: ').'<ul>'.
18036:                        $errors.'</ul></p>';
18037:     }
18038:     return $resulttext;
18039: }
18040: 
18041: sub modify_coursecategories {
18042:     my ($dom,$lastactref,%domconfig) = @_;
18043:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
18044:         $cathash);
18045:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
18046:     my @catitems = ('unauth','auth');
18047:     my @cattypes = ('std','domonly','codesrch','none');
18048:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
18049:         $cathash = $domconfig{'coursecategories'}{'cats'};
18050:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
18051:             $changes{'togglecats'} = 1;
18052:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
18053:         }
18054:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
18055:             $changes{'categorize'} = 1;
18056:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
18057:         }
18058:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
18059:             $changes{'togglecatscomm'} = 1;
18060:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
18061:         }
18062:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
18063:             $changes{'categorizecomm'} = 1;
18064:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
18065: 
18066:         }
18067:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
18068:             $changes{'togglecatsplace'} = 1;
18069:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
18070:         }
18071:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
18072:             $changes{'categorizeplace'} = 1;
18073:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
18074:         }
18075:         foreach my $item (@catitems) {
18076:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
18077:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
18078:                     $changes{$item} = 1;
18079:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
18080:                 }
18081:             }
18082:         }
18083:     } else {
18084:         $changes{'togglecats'} = 1;
18085:         $changes{'categorize'} = 1;
18086:         $changes{'togglecatscomm'} = 1;
18087:         $changes{'categorizecomm'} = 1;
18088:         $changes{'togglecatsplace'} = 1;
18089:         $changes{'categorizeplace'} = 1;
18090:         $domconfig{'coursecategories'} = {
18091:                                              togglecats => $env{'form.togglecats'},
18092:                                              categorize => $env{'form.categorize'},
18093:                                              togglecatscomm => $env{'form.togglecatscomm'},
18094:                                              categorizecomm => $env{'form.categorizecomm'},
18095:                                              togglecatsplace => $env{'form.togglecatsplace'},
18096:                                              categorizeplace => $env{'form.categorizeplace'},
18097:                                          };
18098:         foreach my $item (@catitems) {
18099:             if ($env{'form.coursecat_'.$item} ne 'std') {
18100:                 $changes{$item} = 1;
18101:             }
18102:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
18103:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
18104:             }
18105:         }
18106:     }
18107:     if (ref($cathash) eq 'HASH') {
18108:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
18109:             push (@deletecategory,'instcode::0');
18110:         }
18111:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
18112:             push(@deletecategory,'communities::0');
18113:         }
18114:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
18115:             push(@deletecategory,'placement::0');
18116:         }
18117:     }
18118:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
18119:     if (ref($cathash) eq 'HASH') {
18120:         if (@deletecategory > 0) {
18121:             #FIXME Need to remove category from all courses using a deleted category 
18122:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
18123:             foreach my $item (@deletecategory) {
18124:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
18125:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
18126:                     $deletions{$item} = 1;
18127:                     &recurse_cat_deletes($item,$cathash,\%deletions);
18128:                 }
18129:             }
18130:         }
18131:         foreach my $item (keys(%{$cathash})) {
18132:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
18133:             if ($cathash->{$item} ne $env{'form.'.$item}) {
18134:                 $reorderings{$item} = 1;
18135:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
18136:             }
18137:             if ($env{'form.addcategory_name_'.$item} ne '') {
18138:                 my $newcat = $env{'form.addcategory_name_'.$item};
18139:                 my $newdepth = $depth+1;
18140:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
18141:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
18142:                 $adds{$newitem} = 1; 
18143:             }
18144:             if ($env{'form.subcat_'.$item} ne '') {
18145:                 my $newcat = $env{'form.subcat_'.$item};
18146:                 my $newdepth = $depth+1;
18147:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
18148:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
18149:                 $adds{$newitem} = 1;
18150:             }
18151:         }
18152:     }
18153:     if ($env{'form.instcode'} eq '1') {
18154:         if (ref($cathash) eq 'HASH') {
18155:             my $newitem = 'instcode::0';
18156:             if ($cathash->{$newitem} eq '') {  
18157:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
18158:                 $adds{$newitem} = 1;
18159:             }
18160:         } else {
18161:             my $newitem = 'instcode::0';
18162:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
18163:             $adds{$newitem} = 1;
18164:         }
18165:     }
18166:     if ($env{'form.communities'} eq '1') {
18167:         if (ref($cathash) eq 'HASH') {
18168:             my $newitem = 'communities::0';
18169:             if ($cathash->{$newitem} eq '') {
18170:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
18171:                 $adds{$newitem} = 1;
18172:             }
18173:         } else {
18174:             my $newitem = 'communities::0';
18175:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
18176:             $adds{$newitem} = 1;
18177:         }
18178:     }
18179:     if ($env{'form.placement'} eq '1') {
18180:         if (ref($cathash) eq 'HASH') {
18181:             my $newitem = 'placement::0';
18182:             if ($cathash->{$newitem} eq '') {
18183:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
18184:                 $adds{$newitem} = 1;
18185:             }
18186:         } else {
18187:             my $newitem = 'placement::0';
18188:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
18189:             $adds{$newitem} = 1;
18190:         }
18191:     }
18192:     if ($env{'form.addcategory_name'} ne '') {
18193:         if (($env{'form.addcategory_name'} ne 'instcode') &&
18194:             ($env{'form.addcategory_name'} ne 'communities') &&
18195:             ($env{'form.addcategory_name'} ne 'placement')) {
18196:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
18197:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
18198:             $adds{$newitem} = 1;
18199:         }
18200:     }
18201:     my $putresult;
18202:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
18203:         if (keys(%deletions) > 0) {
18204:             foreach my $key (keys(%deletions)) {
18205:                 if ($predelallitems{$key} ne '') {
18206:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
18207:                 }
18208:             }
18209:         }
18210:         my (@chkcats,@chktrails,%chkallitems);
18211:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
18212:         if (ref($chkcats[0]) eq 'ARRAY') {
18213:             my $depth = 0;
18214:             my $chg = 0;
18215:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
18216:                 my $name = $chkcats[0][$i];
18217:                 my $item;
18218:                 if ($name eq '') {
18219:                     $chg ++;
18220:                 } else {
18221:                     $item = &escape($name).'::0';
18222:                     if ($chg) {
18223:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
18224:                     }
18225:                     $depth ++; 
18226:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
18227:                     $depth --;
18228:                 }
18229:             }
18230:         }
18231:     }
18232:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
18233:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
18234:         if ($putresult eq 'ok') {
18235:             my %title = (
18236:                          togglecats     => 'Show/Hide a course in catalog',
18237:                          categorize     => 'Assign a category to a course',
18238:                          togglecatscomm => 'Show/Hide a community in catalog',
18239:                          categorizecomm => 'Assign a category to a community',
18240:                         );
18241:             my %level = (
18242:                          dom  => 'set in Domain ("Modify Course/Community")',
18243:                          crs  => 'set in Course ("Course Configuration")',
18244:                          comm => 'set in Community ("Community Configuration")',
18245:                          none     => 'No catalog',
18246:                          std      => 'Standard catalog',
18247:                          domonly  => 'Domain-only catalog',
18248:                          codesrch => 'Code search form',
18249:                         );
18250:             $resulttext = &mt('Changes made:').'<ul>';
18251:             if ($changes{'togglecats'}) {
18252:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
18253:             }
18254:             if ($changes{'categorize'}) {
18255:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
18256:             }
18257:             if ($changes{'togglecatscomm'}) {
18258:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
18259:             }
18260:             if ($changes{'categorizecomm'}) {
18261:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
18262:             }
18263:             if ($changes{'unauth'}) {
18264:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
18265:             }
18266:             if ($changes{'auth'}) {
18267:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
18268:             }
18269:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
18270:                 my $cathash;
18271:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
18272:                     $cathash = $domconfig{'coursecategories'}{'cats'};
18273:                 } else {
18274:                     $cathash = {};
18275:                 } 
18276:                 my (@cats,@trails,%allitems);
18277:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
18278:                 if (keys(%deletions) > 0) {
18279:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
18280:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
18281:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
18282:                     }
18283:                     $resulttext .= '</ul></li>';
18284:                 }
18285:                 if (keys(%reorderings) > 0) {
18286:                     my %sort_by_trail;
18287:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
18288:                     foreach my $key (keys(%reorderings)) {
18289:                         if ($allitems{$key} ne '') {
18290:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
18291:                         }
18292:                     }
18293:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
18294:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
18295:                     }
18296:                     $resulttext .= '</ul></li>';
18297:                 }
18298:                 if (keys(%adds) > 0) {
18299:                     my %sort_by_trail;
18300:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
18301:                     foreach my $key (keys(%adds)) {
18302:                         if ($allitems{$key} ne '') {
18303:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
18304:                         }
18305:                     }
18306:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
18307:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
18308:                     }
18309:                     $resulttext .= '</ul></li>';
18310:                 }
18311:                 &Apache::lonnet::do_cache_new('cats',$dom,$cathash,3600);
18312:                 if (ref($lastactref) eq 'HASH') {
18313:                     $lastactref->{'cats'} = 1;
18314:                 }
18315:             }
18316:             $resulttext .= '</ul>';
18317:             if ($changes{'unauth'} || $changes{'auth'}) {
18318:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
18319:                 if ($changes{'auth'}) {
18320:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
18321:                 }
18322:                 if ($changes{'unauth'}) {
18323:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
18324:                 }
18325:                 my $cachetime = 24*60*60;
18326:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18327:                 if (ref($lastactref) eq 'HASH') {
18328:                     $lastactref->{'domdefaults'} = 1;
18329:                 }
18330:             }
18331:         } else {
18332:             $resulttext = '<span class="LC_error">'.
18333:                           &mt('An error occurred: [_1]',$putresult).'</span>';
18334:         }
18335:     } else {
18336:         $resulttext = &mt('No changes made to course and community categories');
18337:     }
18338:     return $resulttext;
18339: }
18340: 
18341: sub modify_serverstatuses {
18342:     my ($dom,%domconfig) = @_;
18343:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
18344:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
18345:         %currserverstatus = %{$domconfig{'serverstatuses'}};
18346:     }
18347:     my @pages = &serverstatus_pages();
18348:     foreach my $type (@pages) {
18349:         $newserverstatus{$type}{'namedusers'} = '';
18350:         $newserverstatus{$type}{'machines'} = '';
18351:         if (defined($env{'form.'.$type.'_namedusers'})) {
18352:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
18353:             my @okusers;
18354:             foreach my $user (@users) {
18355:                 my ($uname,$udom) = split(/:/,$user);
18356:                 if (($udom =~ /^$match_domain$/) &&   
18357:                     (&Apache::lonnet::domain($udom)) &&
18358:                     ($uname =~ /^$match_username$/)) {
18359:                     if (!grep(/^\Q$user\E/,@okusers)) {
18360:                         push(@okusers,$user);
18361:                     }
18362:                 }
18363:             }
18364:             if (@okusers > 0) {
18365:                  @okusers = sort(@okusers);
18366:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
18367:             }
18368:         }
18369:         if (defined($env{'form.'.$type.'_machines'})) {
18370:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
18371:             my @okmachines;
18372:             foreach my $ip (@machines) {
18373:                 my @parts = split(/\./,$ip);
18374:                 next if (@parts < 4);
18375:                 my $badip = 0;
18376:                 for (my $i=0; $i<4; $i++) {
18377:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
18378:                         $badip = 1;
18379:                         last;
18380:                     }
18381:                 }
18382:                 if (!$badip) {
18383:                     push(@okmachines,$ip);     
18384:                 }
18385:             }
18386:             @okmachines = sort(@okmachines);
18387:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
18388:         }
18389:     }
18390:     my %serverstatushash =  (
18391:                                 serverstatuses => \%newserverstatus,
18392:                             );
18393:     foreach my $type (@pages) {
18394:         foreach my $setting ('namedusers','machines') {
18395:             my (@current,@new);
18396:             if (ref($currserverstatus{$type}) eq 'HASH') {
18397:                 if ($currserverstatus{$type}{$setting} ne '') { 
18398:                     @current = split(/,/,$currserverstatus{$type}{$setting});
18399:                 }
18400:             }
18401:             if ($newserverstatus{$type}{$setting} ne '') {
18402:                 @new = split(/,/,$newserverstatus{$type}{$setting});
18403:             }
18404:             if (@current > 0) {
18405:                 if (@new > 0) {
18406:                     foreach my $item (@current) {
18407:                         if (!grep(/^\Q$item\E$/,@new)) {
18408:                             $changes{$type}{$setting} = 1;
18409:                             last;
18410:                         }
18411:                     }
18412:                     foreach my $item (@new) {
18413:                         if (!grep(/^\Q$item\E$/,@current)) {
18414:                             $changes{$type}{$setting} = 1;
18415:                             last;
18416:                         }
18417:                     }
18418:                 } else {
18419:                     $changes{$type}{$setting} = 1;
18420:                 }
18421:             } elsif (@new > 0) {
18422:                 $changes{$type}{$setting} = 1;
18423:             }
18424:         }
18425:     }
18426:     if (keys(%changes) > 0) {
18427:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
18428:         my $putresult = &Apache::lonnet::put_dom('configuration',
18429:                                                  \%serverstatushash,$dom);
18430:         if ($putresult eq 'ok') {
18431:             $resulttext .= &mt('Changes made:').'<ul>';
18432:             foreach my $type (@pages) {
18433:                 if (ref($changes{$type}) eq 'HASH') {
18434:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
18435:                     if ($changes{$type}{'namedusers'}) {
18436:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
18437:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
18438:                         } else {
18439:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
18440:                         }
18441:                     }
18442:                     if ($changes{$type}{'machines'}) {
18443:                         if ($newserverstatus{$type}{'machines'} eq '') {
18444:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
18445:                         } else {
18446:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
18447:                         }
18448: 
18449:                     }
18450:                     $resulttext .= '</ul></li>';
18451:                 }
18452:             }
18453:             $resulttext .= '</ul>';
18454:         } else {
18455:             $resulttext = '<span class="LC_error">'.
18456:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
18457: 
18458:         }
18459:     } else {
18460:         $resulttext = &mt('No changes made to access to server status pages');
18461:     }
18462:     return $resulttext;
18463: }
18464: 
18465: sub modify_helpsettings {
18466:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
18467:     my ($resulttext,$errors,%changes,%helphash);
18468:     my %defaultchecked = ('submitbugs' => 'on');
18469:     my @offon = ('off','on');
18470:     my @toggles = ('submitbugs');
18471:     my %current = ('submitbugs' => '',
18472:                    'adhoc'      => {},
18473:                   );
18474:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
18475:         %current = %{$domconfig{'helpsettings'}};
18476:     }
18477:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
18478:     foreach my $item (@toggles) {
18479:         if ($defaultchecked{$item} eq 'on') { 
18480:             if ($current{$item} eq '') {
18481:                 if ($env{'form.'.$item} eq '0') {
18482:                     $changes{$item} = 1;
18483:                 }
18484:             } elsif ($current{$item} ne $env{'form.'.$item}) {
18485:                 $changes{$item} = 1;
18486:             }
18487:         } elsif ($defaultchecked{$item} eq 'off') {
18488:             if ($current{$item} eq '') {
18489:                 if ($env{'form.'.$item} eq '1') {
18490:                     $changes{$item} = 1;
18491:                 }
18492:             } elsif ($current{$item} ne $env{'form.'.$item}) {
18493:                 $changes{$item} = 1;
18494:             }
18495:         }
18496:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
18497:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
18498:         }
18499:     }
18500:     my $maxnum = $env{'form.helproles_maxnum'};
18501:     my $confname = $dom.'-domainconfig';
18502:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
18503:     my (@allpos,%newsettings,%changedprivs,$newrole);
18504:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
18505:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
18506:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
18507:     my %lt = &Apache::lonlocal::texthash(
18508:                     s      => 'system',
18509:                     d      => 'domain',
18510:                     order  => 'Display order',
18511:                     access => 'Role usage',
18512:                     all    => 'All with domain helpdesk or helpdesk assistant role',
18513:                     dh     => 'All with domain helpdesk role',
18514:                     da     => 'All with domain helpdesk assistant role',
18515:                     none   => 'None',
18516:                     status => 'Determined based on institutional status',
18517:                     inc    => 'Include all, but exclude specific personnel',
18518:                     exc    => 'Exclude all, but include specific personnel',
18519:     );
18520:     for (my $num=0; $num<=$maxnum; $num++) {
18521:         my ($prefix,$identifier,$rolename,%curr);
18522:         if ($num == $maxnum) {
18523:             next unless ($env{'form.newcusthelp'} == $maxnum);
18524:             $identifier = 'custhelp'.$num;
18525:             $prefix = 'helproles_'.$num;
18526:             $rolename = $env{'form.custhelpname'.$num};
18527:             $rolename=~s/[^A-Za-z0-9]//gs;
18528:             next if ($rolename eq '');
18529:             next if (exists($existing{'rolesdef_'.$rolename}));
18530:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
18531:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
18532:                                                      $newprivs{'c'},$confname,$dom);
18533:             if ($result ne 'ok') {
18534:                 $errors .= '<li><span class="LC_error">'.
18535:                            &mt('An error occurred storing the new custom role: [_1]',
18536:                            $result).'</span></li>';
18537:                 next;
18538:             } else {
18539:                 $changedprivs{$rolename} = \%newprivs;
18540:                 $newrole = $rolename;
18541:             }
18542:         } else {
18543:             $prefix = 'helproles_'.$num;
18544:             $rolename = $env{'form.'.$prefix};
18545:             next if ($rolename eq '');
18546:             next unless (exists($existing{'rolesdef_'.$rolename}));
18547:             $identifier = 'custhelp'.$num;
18548:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
18549:             my %currprivs;
18550:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
18551:                 split(/\_/,$existing{'rolesdef_'.$rolename});
18552:             foreach my $level ('c','d','s') {
18553:                 if ($newprivs{$level} ne $currprivs{$level}) {
18554:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
18555:                                                              $newprivs{'c'},$confname,$dom);
18556:                     if ($result ne 'ok') {
18557:                         $errors .= '<li><span class="LC_error">'.
18558:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
18559:                                        $rolename,$result).'</span></li>';
18560:                     } else {
18561:                         $changedprivs{$rolename} = \%newprivs;
18562:                     }
18563:                     last;
18564:                 }
18565:             }
18566:             if (ref($current{'adhoc'}) eq 'HASH') {
18567:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
18568:                     %curr = %{$current{'adhoc'}{$rolename}};
18569:                 }
18570:             }
18571:         }
18572:         my $newpos = $env{'form.'.$prefix.'_pos'};
18573:         $newpos =~ s/\D+//g;
18574:         $allpos[$newpos] = $rolename;
18575:         my $newdesc = $env{'form.'.$prefix.'_desc'};
18576:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
18577:         if ($curr{'desc'}) {
18578:             if ($curr{'desc'} ne $newdesc) {
18579:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
18580:                 $newsettings{$rolename}{'desc'} = $newdesc;
18581:             }
18582:         } elsif ($newdesc ne '') {
18583:             $changes{'customrole'}{$rolename}{'desc'} = 1;
18584:             $newsettings{$rolename}{'desc'} = $newdesc;
18585:         }
18586:         my $access = $env{'form.'.$prefix.'_access'};
18587:         if (grep(/^\Q$access\E$/,@accesstypes)) {
18588:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
18589:             if ($access eq 'status') {
18590:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
18591:                 if (scalar(@statuses) == 0) {
18592:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
18593:                 } else {
18594:                     my (@shownstatus,$numtypes);
18595:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
18596:                     if (ref($types) eq 'ARRAY') {
18597:                         $numtypes = scalar(@{$types});
18598:                         foreach my $type (sort(@statuses)) {
18599:                             if ($type eq 'default') {
18600:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
18601:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
18602:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
18603:                                 push(@shownstatus,$usertypes->{$type});
18604:                             }
18605:                         }
18606:                     }
18607:                     if (grep(/^default$/,@statuses)) {
18608:                         push(@shownstatus,$othertitle);
18609:                     }
18610:                     if (scalar(@shownstatus) == 1+$numtypes) {
18611:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
18612:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
18613:                     } else {
18614:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
18615:                         if (ref($curr{'status'}) eq 'ARRAY') {
18616:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
18617:                             if (@diffs) {
18618:                                 $changes{'customrole'}{$rolename}{$access} = 1;
18619:                             }
18620:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
18621:                             $changes{'customrole'}{$rolename}{$access} = 1;
18622:                         }
18623:                     }
18624:                 }
18625:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
18626:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
18627:                 my @newspecstaff;
18628:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
18629:                 foreach my $person (sort(@personnel)) {
18630:                     if ($domhelpdesk{$person}) {
18631:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
18632:                     }
18633:                 }
18634:                 if (ref($curr{$access}) eq 'ARRAY') {
18635:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
18636:                     if (@diffs) {
18637:                         $changes{'customrole'}{$rolename}{$access} = 1;
18638:                     }
18639:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
18640:                     $changes{'customrole'}{$rolename}{$access} = 1;
18641:                 }
18642:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
18643:                     my ($uname,$udom) = split(/:/,$person);
18644:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
18645:                 }
18646:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
18647:             }
18648:         } else {
18649:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
18650:         }
18651:         unless ($curr{'access'} eq $access) {
18652:             $changes{'customrole'}{$rolename}{'access'} = 1;
18653:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
18654:         }
18655:     }
18656:     if (@allpos > 0) {
18657:         my $idx = 0;
18658:         foreach my $rolename (@allpos) {
18659:             if ($rolename ne '') {
18660:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
18661:                 if (ref($current{'adhoc'}) eq 'HASH') {
18662:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
18663:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
18664:                             $changes{'customrole'}{$rolename}{'order'} = 1;
18665:                             $newsettings{$rolename}{'order'} = $idx+1;
18666:                         }
18667:                     }
18668:                 }
18669:                 $idx ++;
18670:             }
18671:         }
18672:     }
18673:     my $putresult;
18674:     if (keys(%changes) > 0) {
18675:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
18676:         if ($putresult eq 'ok') {
18677:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
18678:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
18679:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
18680:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
18681:                 }
18682:             }
18683:             my $cachetime = 24*60*60;
18684:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
18685:             if (ref($lastactref) eq 'HASH') {
18686:                 $lastactref->{'domdefaults'} = 1;
18687:             }
18688:         } else {
18689:             $errors .= '<li><span class="LC_error">'.
18690:                        &mt('An error occurred storing the settings: [_1]',
18691:                            $putresult).'</span></li>';
18692:         }
18693:     }
18694:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
18695:         $resulttext = &mt('Changes made:').'<ul>';
18696:         my (%shownprivs,@levelorder);
18697:         @levelorder = ('c','d','s');
18698:         if ((keys(%changes)) && ($putresult eq 'ok')) {
18699:             foreach my $item (sort(keys(%changes))) {
18700:                 if ($item eq 'submitbugs') {
18701:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
18702:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
18703:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
18704:                 } elsif ($item eq 'customrole') {
18705:                     if (ref($changes{'customrole'}) eq 'HASH') {
18706:                         my @keyorder = ('order','desc','access','status','exc','inc');
18707:                         my %keytext = &Apache::lonlocal::texthash(
18708:                                                                    order  => 'Order',
18709:                                                                    desc   => 'Role description',
18710:                                                                    access => 'Role usage',
18711:                                                                    status => 'Allowed institutional types',
18712:                                                                    exc    => 'Allowed personnel',
18713:                                                                    inc    => 'Disallowed personnel',
18714:                         );
18715:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
18716:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
18717:                                 if ($role eq $newrole) {
18718:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
18719:                                                               $role).'<ul>';
18720:                                 } else {
18721:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
18722:                                                               $role).'<ul>';
18723:                                 }
18724:                                 foreach my $key (@keyorder) {
18725:                                     if ($changes{'customrole'}{$role}{$key}) {
18726:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
18727:                                                                   $keytext{$key},$newsettings{$role}{$key}).
18728:                                                        '</li>';
18729:                                     }
18730:                                 }
18731:                                 if (ref($changedprivs{$role}) eq 'HASH') {
18732:                                     $shownprivs{$role} = 1;
18733:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
18734:                                     foreach my $level (@levelorder) {
18735:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
18736:                                             next if ($item eq '');
18737:                                             my ($priv) = split(/\&/,$item,2);
18738:                                             if (&Apache::lonnet::plaintext($priv)) {
18739:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
18740:                                                 unless ($level eq 'c') {
18741:                                                     $resulttext .= ' ('.$lt{$level}.')';
18742:                                                 }
18743:                                                 $resulttext .= '</li>';
18744:                                             }
18745:                                         }
18746:                                     }
18747:                                     $resulttext .= '</ul>';
18748:                                 }
18749:                                 $resulttext .= '</ul></li>';
18750:                             }
18751:                         }
18752:                     }
18753:                 }
18754:             }
18755:         }
18756:         if (keys(%changedprivs)) {
18757:             foreach my $role (sort(keys(%changedprivs))) {
18758:                 unless ($shownprivs{$role}) {
18759:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
18760:                                               $role).'<ul>'.
18761:                                    '<li>'.&mt('Privileges set to :').'<ul>';
18762:                     foreach my $level (@levelorder) {
18763:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
18764:                             next if ($item eq '');
18765:                             my ($priv) = split(/\&/,$item,2);
18766:                             if (&Apache::lonnet::plaintext($priv)) {
18767:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
18768:                                 unless ($level eq 'c') {
18769:                                     $resulttext .= ' ('.$lt{$level}.')';
18770:                                 }
18771:                                 $resulttext .= '</li>';
18772:                             }
18773:                         }
18774:                     }
18775:                     $resulttext .= '</ul></li></ul></li>';
18776:                 }
18777:             }
18778:         }
18779:         $resulttext .= '</ul>';
18780:     } else {
18781:         $resulttext = &mt('No changes made to help settings');
18782:     }
18783:     if ($errors) {
18784:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
18785:                                     $errors.'</ul>';
18786:     }
18787:     return $resulttext;
18788: }
18789: 
18790: sub modify_coursedefaults {
18791:     my ($dom,$lastactref,%domconfig) = @_;
18792:     my ($resulttext,$errors,%changes,%defaultshash);
18793:     my %defaultchecked = (
18794:                            'canuse_pdfforms' => 'off',
18795:                            'uselcmath'       => 'on',
18796:                            'usejsme'         => 'on'
18797:                          );
18798:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme');
18799:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
18800:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
18801:                    'mysqltables_official','mysqltables_unofficial','mysqltables_community',
18802:                    'mysqltables_textbook','mysqltables_placement');
18803:     my @types = ('official','unofficial','community','textbook','placement');
18804:     my %staticdefaults = (
18805:                            anonsurvey_threshold => 10,
18806:                            uploadquota          => 500,
18807:                            postsubmit           => 60,
18808:                            mysqltables          => 172800,
18809:                          );
18810:     my %texoptions = (
18811:                         MathJax  => 'MathJax',
18812:                         mimetex  => &mt('Convert to Images'),
18813:                         tth      => &mt('TeX to HTML'),
18814:                      );
18815:     $defaultshash{'coursedefaults'} = {};
18816: 
18817:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
18818:         if ($domconfig{'coursedefaults'} eq '') {
18819:             $domconfig{'coursedefaults'} = {};
18820:         }
18821:     }
18822: 
18823:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
18824:         foreach my $item (@toggles) {
18825:             if ($defaultchecked{$item} eq 'on') {
18826:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
18827:                     ($env{'form.'.$item} eq '0')) {
18828:                     $changes{$item} = 1;
18829:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
18830:                     $changes{$item} = 1;
18831:                 }
18832:             } elsif ($defaultchecked{$item} eq 'off') {
18833:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
18834:                     ($env{'form.'.$item} eq '1')) {
18835:                     $changes{$item} = 1;
18836:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
18837:                     $changes{$item} = 1;
18838:                 }
18839:             }
18840:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
18841:         }
18842:         foreach my $item (@numbers) {
18843:             my ($currdef,$newdef);
18844:             $newdef = $env{'form.'.$item};
18845:             if ($item eq 'anonsurvey_threshold') {
18846:                 $currdef = $domconfig{'coursedefaults'}{$item};
18847:                 $newdef =~ s/\D//g;
18848:                 if ($newdef eq '' || $newdef < 1) {
18849:                     $newdef = 1;
18850:                 }
18851:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
18852:             } else {
18853:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
18854:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
18855:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
18856:                 }
18857:                 $newdef =~ s/[^\w.\-]//g;
18858:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
18859:             }
18860:             if ($currdef ne $newdef) {
18861:                 if ($item eq 'anonsurvey_threshold') {
18862:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
18863:                         $changes{$item} = 1;
18864:                     }
18865:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
18866:                     my $setting = $1;
18867:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
18868:                         $changes{$setting} = 1;
18869:                     }
18870:                 }
18871:             }
18872:         }
18873:         my $texengine;
18874:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
18875:             $texengine = $env{'form.texengine'};
18876:             my $currdef = $domconfig{'coursedefaults'}{'texengine'};
18877:             if ($currdef eq '') {
18878:                 unless ($texengine eq $Apache::lonnet::deftex) {
18879:                     $changes{'texengine'} = 1;
18880:                 }
18881:             } elsif ($currdef ne $texengine) {
18882:                 $changes{'texengine'} = 1;
18883:             }
18884:         }
18885:         if ($texengine ne '') {
18886:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
18887:         }
18888:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
18889:         my @currclonecode;
18890:         if (ref($currclone) eq 'HASH') {
18891:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
18892:                 @currclonecode = @{$currclone->{'instcode'}};
18893:             }
18894:         }
18895:         my $newclone;
18896:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
18897:             $newclone = $env{'form.canclone'};
18898:         }
18899:         if ($newclone eq 'instcode') {
18900:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
18901:             my (%codedefaults,@code_order,@clonecode);
18902:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
18903:                                                     \@code_order);
18904:             foreach my $item (@code_order) {
18905:                 if (grep(/^\Q$item\E$/,@newcodes)) {
18906:                     push(@clonecode,$item);
18907:                 }
18908:             }
18909:             if (@clonecode) {
18910:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
18911:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
18912:                 if (@diffs) {
18913:                     $changes{'canclone'} = 1;
18914:                 }
18915:             } else {
18916:                 $newclone eq '';
18917:             }
18918:         } elsif ($newclone ne '') {
18919:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
18920:         }
18921:         if ($newclone ne $currclone) {
18922:             $changes{'canclone'} = 1;
18923:         }
18924:         my %credits;
18925:         foreach my $type (@types) {
18926:             unless ($type eq 'community') {
18927:                 $credits{$type} = $env{'form.'.$type.'_credits'};
18928:                 $credits{$type} =~ s/[^\d.]+//g;
18929:             }
18930:         }
18931:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
18932:             ($env{'form.coursecredits'} eq '1')) {
18933:             $changes{'coursecredits'} = 1;
18934:             foreach my $type (keys(%credits)) {
18935:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
18936:             }
18937:         } else {
18938:             if ($env{'form.coursecredits'} eq '1') {
18939:                 foreach my $type (@types) {
18940:                     unless ($type eq 'community') {
18941:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
18942:                             $changes{'coursecredits'} = 1;
18943:                         }
18944:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
18945:                     }
18946:                 }
18947:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
18948:                 foreach my $type (@types) {
18949:                     unless ($type eq 'community') {
18950:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
18951:                             $changes{'coursecredits'} = 1;
18952:                             last;
18953:                         }
18954:                     }
18955:                 }
18956:             }
18957:         }
18958:         if ($env{'form.postsubmit'} eq '1') {
18959:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
18960:             my %currtimeout;
18961:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
18962:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
18963:                     $changes{'postsubmit'} = 1;
18964:                 }
18965:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
18966:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
18967:                 }
18968:             } else {
18969:                 $changes{'postsubmit'} = 1;
18970:             }
18971:             foreach my $type (@types) {
18972:                 my $timeout = $env{'form.'.$type.'_timeout'};
18973:                 $timeout =~ s/\D//g;
18974:                 if ($timeout == $staticdefaults{'postsubmit'}) {
18975:                     $timeout = '';
18976:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
18977:                     $timeout = '0';
18978:                 }
18979:                 unless ($timeout eq '') {
18980:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
18981:                 }
18982:                 if (exists($currtimeout{$type})) {
18983:                     if ($timeout ne $currtimeout{$type}) {
18984:                         $changes{'postsubmit'} = 1;
18985:                     }
18986:                 } elsif ($timeout ne '') {
18987:                     $changes{'postsubmit'} = 1;
18988:                 }
18989:             }
18990:         } else {
18991:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
18992:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
18993:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
18994:                     $changes{'postsubmit'} = 1;
18995:                 }
18996:             } else {
18997:                 $changes{'postsubmit'} = 1;
18998:             }
18999:         }
19000:     }
19001:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
19002:                                              $dom);
19003:     if ($putresult eq 'ok') {
19004:         if (keys(%changes) > 0) {
19005:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
19006:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
19007:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
19008:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'})) {
19009:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme','texengine') {
19010:                     if ($changes{$item}) {
19011:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
19012:                     }
19013:                 }
19014:                 if ($changes{'coursecredits'}) {
19015:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
19016:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
19017:                             $domdefaults{$type.'credits'} =
19018:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
19019:                         }
19020:                     }
19021:                 }
19022:                 if ($changes{'postsubmit'}) {
19023:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
19024:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
19025:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
19026:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
19027:                                 $domdefaults{$type.'postsubtimeout'} =
19028:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
19029:                             }
19030:                         }
19031:                     }
19032:                 }
19033:                 if ($changes{'uploadquota'}) {
19034:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
19035:                         foreach my $type (@types) {
19036:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
19037:                         }
19038:                     }
19039:                 }
19040:                 if ($changes{'canclone'}) {
19041:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
19042:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
19043:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
19044:                             if (@clonecodes) {
19045:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
19046:                             }
19047:                         }
19048:                     } else {
19049:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
19050:                     }
19051:                 }
19052:                 my $cachetime = 24*60*60;
19053:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19054:                 if (ref($lastactref) eq 'HASH') {
19055:                     $lastactref->{'domdefaults'} = 1;
19056:                 }
19057:             }
19058:             $resulttext = &mt('Changes made:').'<ul>';
19059:             foreach my $item (sort(keys(%changes))) {
19060:                 if ($item eq 'canuse_pdfforms') {
19061:                     if ($env{'form.'.$item} eq '1') {
19062:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
19063:                     } else {
19064:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
19065:                     }
19066:                 } elsif ($item eq 'uselcmath') {
19067:                     if ($env{'form.'.$item} eq '1') {
19068:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
19069:                     } else {
19070:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
19071:                     }
19072:                 } elsif ($item eq 'usejsme') {
19073:                     if ($env{'form.'.$item} eq '1') {
19074:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
19075:                     } else {
19076:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
19077:                     }
19078:                 } elsif ($item eq 'texengine') {
19079:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
19080:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
19081:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
19082:                     }
19083:                 } elsif ($item eq 'anonsurvey_threshold') {
19084:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
19085:                 } elsif ($item eq 'uploadquota') {
19086:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
19087:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
19088:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
19089:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
19090:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
19091:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
19092:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
19093:                                        '</ul>'.
19094:                                        '</li>';
19095:                     } else {
19096:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
19097:                     }
19098:                 } elsif ($item eq 'mysqltables') {
19099:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
19100:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
19101:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
19102:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
19103:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
19104:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
19105:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
19106:                                        '</ul>'.
19107:                                        '</li>';
19108:                     } else {
19109:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
19110:                     }
19111:                 } elsif ($item eq 'postsubmit') {
19112:                     if ($domdefaults{'postsubmit'} eq 'off') {
19113:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
19114:                     } else {
19115:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
19116:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
19117:                             $resulttext .= &mt('durations:').'<ul>';
19118:                             foreach my $type (@types) {
19119:                                 $resulttext .= '<li>';
19120:                                 my $timeout;
19121:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
19122:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
19123:                                 }
19124:                                 my $display;
19125:                                 if ($timeout eq '0') {
19126:                                     $display = &mt('unlimited');
19127:                                 } elsif ($timeout eq '') {
19128:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
19129:                                 } else {
19130:                                     $display = &mt('[quant,_1,second]',$timeout);
19131:                                 }
19132:                                 if ($type eq 'community') {
19133:                                     $resulttext .= &mt('Communities');
19134:                                 } elsif ($type eq 'official') {
19135:                                     $resulttext .= &mt('Official courses');
19136:                                 } elsif ($type eq 'unofficial') {
19137:                                     $resulttext .= &mt('Unofficial courses');
19138:                                 } elsif ($type eq 'textbook') {
19139:                                     $resulttext .= &mt('Textbook courses');
19140:                                 } elsif ($type eq 'placement') {
19141:                                     $resulttext .= &mt('Placement tests');
19142:                                 }
19143:                                 $resulttext .= ' -- '.$display.'</li>';
19144:                             }
19145:                             $resulttext .= '</ul>';
19146:                         }
19147:                         $resulttext .= '</li>';
19148:                     }
19149:                 } elsif ($item eq 'coursecredits') {
19150:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
19151:                         if (($domdefaults{'officialcredits'} eq '') &&
19152:                             ($domdefaults{'unofficialcredits'} eq '') &&
19153:                             ($domdefaults{'textbookcredits'} eq '')) {
19154:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
19155:                         } else {
19156:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
19157:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
19158:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
19159:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
19160:                                            '</ul>'.
19161:                                            '</li>';
19162:                         }
19163:                     } else {
19164:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
19165:                     }
19166:                 } elsif ($item eq 'canclone') {
19167:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
19168:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
19169:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
19170:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
19171:                         }
19172:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
19173:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
19174:                     } else {
19175:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
19176:                     }
19177:                 }
19178:             }
19179:             $resulttext .= '</ul>';
19180:         } else {
19181:             $resulttext = &mt('No changes made to course defaults');
19182:         }
19183:     } else {
19184:         $resulttext = '<span class="LC_error">'.
19185:             &mt('An error occurred: [_1]',$putresult).'</span>';
19186:     }
19187:     return $resulttext;
19188: }
19189: 
19190: sub modify_selfenrollment {
19191:     my ($dom,$lastactref,%domconfig) = @_;
19192:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
19193:     my @types = ('official','unofficial','community','textbook','placement');
19194:     my %titles = &tool_titles();
19195:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
19196:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
19197:     $ordered{'default'} = ['types','registered','approval','limit'];
19198: 
19199:     my (%roles,%shown,%toplevel);
19200:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
19201: 
19202:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
19203:         if ($domconfig{'selfenrollment'} eq '') {
19204:             $domconfig{'selfenrollment'} = {};
19205:         }
19206:     }
19207:     %toplevel = (
19208:                   admin      => 'Configuration Rights',
19209:                   default    => 'Default settings',
19210:                   validation => 'Validation of self-enrollment requests',
19211:                 );
19212:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
19213: 
19214:     if (ref($ordered{'admin'}) eq 'ARRAY') {
19215:         foreach my $item (@{$ordered{'admin'}}) {
19216:             foreach my $type (@types) {
19217:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
19218:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
19219:                 } else {
19220:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
19221:                 }
19222:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
19223:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
19224:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
19225:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
19226:                             push(@{$changes{'admin'}{$type}},$item);
19227:                         }
19228:                     } else {
19229:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
19230:                             push(@{$changes{'admin'}{$type}},$item);
19231:                         }
19232:                     }
19233:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
19234:                     push(@{$changes{'admin'}{$type}},$item);
19235:                 }
19236:             }
19237:         }
19238:     }
19239: 
19240:     foreach my $item (@{$ordered{'default'}}) {
19241:         foreach my $type (@types) {
19242:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
19243:             if ($item eq 'types') {
19244:                 unless (($value eq 'all') || ($value eq 'dom')) {
19245:                     $value = '';
19246:                 }
19247:             } elsif ($item eq 'registered') {
19248:                 unless ($value eq '1') {
19249:                     $value = 0;
19250:                 }
19251:             } elsif ($item eq 'approval') {
19252:                 unless ($value =~ /^[012]$/) {
19253:                     $value = 0;
19254:                 }
19255:             } else {
19256:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
19257:                     $value = 'none';
19258:                 }
19259:             }
19260:             $selfenrollhash{'default'}{$type}{$item} = $value;
19261:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
19262:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
19263:                     if ($selfenrollhash{'default'}{$type}{$item} ne
19264:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
19265:                          push(@{$changes{'default'}{$type}},$item);
19266:                     }
19267:                 } else {
19268:                     push(@{$changes{'default'}{$type}},$item);
19269:                 }
19270:             } else {
19271:                 push(@{$changes{'default'}{$type}},$item);
19272:             }
19273:             if ($item eq 'limit') {
19274:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
19275:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
19276:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
19277:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
19278:                     }
19279:                 } else {
19280:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
19281:                 }
19282:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
19283:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
19284:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
19285:                          push(@{$changes{'default'}{$type}},'cap');
19286:                     }
19287:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
19288:                     push(@{$changes{'default'}{$type}},'cap');
19289:                 }
19290:             }
19291:         }
19292:     }
19293: 
19294:     foreach my $item (@{$itemsref}) {
19295:         if ($item eq 'fields') {
19296:             my @changed;
19297:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
19298:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
19299:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
19300:             }
19301:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
19302:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
19303:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
19304:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
19305:                 } else {
19306:                     @changed = @{$selfenrollhash{'validation'}{$item}};
19307:                 }
19308:             } else {
19309:                 @changed = @{$selfenrollhash{'validation'}{$item}};
19310:             }
19311:             if (@changed) {
19312:                 if ($selfenrollhash{'validation'}{$item}) { 
19313:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
19314:                 } else {
19315:                     $changes{'validation'}{$item} = &mt('None');
19316:                 }
19317:             }
19318:         } else {
19319:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
19320:             if ($item eq 'markup') {
19321:                if ($env{'form.selfenroll_validation_'.$item}) {
19322:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
19323:                }
19324:             }
19325:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
19326:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
19327:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
19328:                 }
19329:             }
19330:         }
19331:     }
19332: 
19333:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
19334:                                              $dom);
19335:     if ($putresult eq 'ok') {
19336:         if (keys(%changes) > 0) {
19337:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
19338:             $resulttext = &mt('Changes made:').'<ul>';
19339:             foreach my $key ('admin','default','validation') {
19340:                 if (ref($changes{$key}) eq 'HASH') {
19341:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
19342:                     if ($key eq 'validation') {
19343:                         foreach my $item (@{$itemsref}) {
19344:                             if (exists($changes{$key}{$item})) {
19345:                                 if ($item eq 'markup') {
19346:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
19347:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
19348:                                 } else {  
19349:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
19350:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
19351:                                 }
19352:                             }
19353:                         }
19354:                     } else {
19355:                         foreach my $type (@types) {
19356:                             if ($type eq 'community') {
19357:                                 $roles{'1'} = &mt('Community personnel');
19358:                             } else {
19359:                                 $roles{'1'} = &mt('Course personnel');
19360:                             }
19361:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
19362:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
19363:                                     if ($key eq 'admin') {
19364:                                         my @mgrdc = ();
19365:                                         if (ref($ordered{$key}) eq 'ARRAY') {
19366:                                             foreach my $item (@{$ordered{'admin'}}) {
19367:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
19368:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
19369:                                                         push(@mgrdc,$item);
19370:                                                     }
19371:                                                 }
19372:                                             }
19373:                                             if (@mgrdc) {
19374:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
19375:                                             } else {
19376:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
19377:                                             }
19378:                                         }
19379:                                     } else {
19380:                                         if (ref($ordered{$key}) eq 'ARRAY') {
19381:                                             foreach my $item (@{$ordered{$key}}) {
19382:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
19383:                                                     $domdefaults{$type.'selfenroll'.$item} =
19384:                                                         $selfenrollhash{$key}{$type}{$item};
19385:                                                 }
19386:                                             }
19387:                                         }
19388:                                     }
19389:                                 }
19390:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
19391:                                 foreach my $item (@{$ordered{$key}}) {
19392:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
19393:                                         $resulttext .= '<li>';
19394:                                         if ($key eq 'admin') {
19395:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
19396:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
19397:                                         } else {
19398:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
19399:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
19400:                                         }
19401:                                         $resulttext .= '</li>';
19402:                                     }
19403:                                 }
19404:                                 $resulttext .= '</ul></li>';
19405:                             }
19406:                         }
19407:                         $resulttext .= '</ul></li>'; 
19408:                     }
19409:                 }
19410:             }
19411:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
19412:                 my $cachetime = 24*60*60;
19413:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19414:                 if (ref($lastactref) eq 'HASH') {
19415:                     $lastactref->{'domdefaults'} = 1;
19416:                 }
19417:             }
19418:             $resulttext .= '</ul>';
19419:         } else {
19420:             $resulttext = &mt('No changes made to self-enrollment settings');
19421:         }
19422:     } else {
19423:         $resulttext = '<span class="LC_error">'.
19424:             &mt('An error occurred: [_1]',$putresult).'</span>';
19425:     }
19426:     return $resulttext;
19427: }
19428: 
19429: sub modify_usersessions {
19430:     my ($dom,$lastactref,%domconfig) = @_;
19431:     my @hostingtypes = ('version','excludedomain','includedomain');
19432:     my @offloadtypes = ('primary','default');
19433:     my %types = (
19434:                   remote => \@hostingtypes,
19435:                   hosted => \@hostingtypes,
19436:                   spares => \@offloadtypes,
19437:                 );
19438:     my @prefixes = ('remote','hosted','spares');
19439:     my @lcversions = &Apache::lonnet::all_loncaparevs();
19440:     my (%by_ip,%by_location,@intdoms,@instdoms);
19441:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
19442:     my @locations = sort(keys(%by_location));
19443:     my (%defaultshash,%changes);
19444:     foreach my $prefix (@prefixes) {
19445:         $defaultshash{'usersessions'}{$prefix} = {};
19446:     }
19447:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
19448:     my $resulttext;
19449:     my %iphost = &Apache::lonnet::get_iphost();
19450:     foreach my $prefix (@prefixes) {
19451:         next if ($prefix eq 'spares');
19452:         foreach my $type (@{$types{$prefix}}) {
19453:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
19454:             if ($type eq 'version') {
19455:                 my $value = $env{'form.'.$prefix.'_'.$type};
19456:                 my $okvalue;
19457:                 if ($value ne '') {
19458:                     if (grep(/^\Q$value\E$/,@lcversions)) {
19459:                         $okvalue = $value;
19460:                     }
19461:                 }
19462:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
19463:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
19464:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
19465:                             if ($inuse == 0) {
19466:                                 $changes{$prefix}{$type} = 1;
19467:                             } else {
19468:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
19469:                                     $changes{$prefix}{$type} = 1;
19470:                                 }
19471:                                 if ($okvalue ne '') {
19472:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
19473:                                 } 
19474:                             }
19475:                         } else {
19476:                             if (($inuse == 1) && ($okvalue ne '')) {
19477:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
19478:                                 $changes{$prefix}{$type} = 1;
19479:                             }
19480:                         }
19481:                     } else {
19482:                         if (($inuse == 1) && ($okvalue ne '')) {
19483:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
19484:                             $changes{$prefix}{$type} = 1;
19485:                         }
19486:                     }
19487:                 } else {
19488:                     if (($inuse == 1) && ($okvalue ne '')) {
19489:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
19490:                         $changes{$prefix}{$type} = 1;
19491:                     }
19492:                 }
19493:             } else {
19494:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
19495:                 my @okvals;
19496:                 foreach my $val (@vals) {
19497:                     if ($val =~ /:/) {
19498:                         my @items = split(/:/,$val);
19499:                         foreach my $item (@items) {
19500:                             if (ref($by_location{$item}) eq 'ARRAY') {
19501:                                 push(@okvals,$item);
19502:                             }
19503:                         }
19504:                     } else {
19505:                         if (ref($by_location{$val}) eq 'ARRAY') {
19506:                             push(@okvals,$val);
19507:                         }
19508:                     }
19509:                 }
19510:                 @okvals = sort(@okvals);
19511:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
19512:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
19513:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
19514:                             if ($inuse == 0) {
19515:                                 $changes{$prefix}{$type} = 1; 
19516:                             } else {
19517:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
19518:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
19519:                                 if (@changed > 0) {
19520:                                     $changes{$prefix}{$type} = 1;
19521:                                 }
19522:                             }
19523:                         } else {
19524:                             if ($inuse == 1) {
19525:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
19526:                                 $changes{$prefix}{$type} = 1;
19527:                             }
19528:                         } 
19529:                     } else {
19530:                         if ($inuse == 1) {
19531:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
19532:                             $changes{$prefix}{$type} = 1;
19533:                         }
19534:                     }
19535:                 } else {
19536:                     if ($inuse == 1) {
19537:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
19538:                         $changes{$prefix}{$type} = 1;
19539:                     }
19540:                 }
19541:             }
19542:         }
19543:     }
19544: 
19545:     my @alldoms = &Apache::lonnet::all_domains();
19546:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
19547:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
19548:     my $savespares;
19549: 
19550:     foreach my $lonhost (sort(keys(%servers))) {
19551:         my $serverhomeID =
19552:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
19553:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
19554:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
19555:         my %spareschg;
19556:         foreach my $type (@{$types{'spares'}}) {
19557:             my @okspares;
19558:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
19559:             foreach my $server (@checked) {
19560:                 if (&Apache::lonnet::hostname($server) ne '') {
19561:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
19562:                         unless (grep(/^\Q$server\E$/,@okspares)) {
19563:                             push(@okspares,$server);
19564:                         }
19565:                     }
19566:                 }
19567:             }
19568:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
19569:             my $newspare;
19570:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
19571:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
19572:                     $newspare = $new;
19573:                 }
19574:             }
19575:             my @spares;
19576:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
19577:                 @spares = sort(@okspares,$newspare);
19578:             } else {
19579:                 @spares = sort(@okspares);
19580:             }
19581:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
19582:             if (ref($spareid{$lonhost}) eq 'HASH') {
19583:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
19584:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
19585:                     if (@diffs > 0) {
19586:                         $spareschg{$type} = 1;
19587:                     }
19588:                 }
19589:             }
19590:         }
19591:         if (keys(%spareschg) > 0) {
19592:             $changes{'spares'}{$lonhost} = \%spareschg;
19593:         }
19594:     }
19595:     $defaultshash{'usersessions'}{'offloadnow'} = {};
19596:     $defaultshash{'usersessions'}{'offloadoth'} = {};
19597:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
19598:     my @okoffload;
19599:     if (@offloadnow) {
19600:         foreach my $server (@offloadnow) {
19601:             if (&Apache::lonnet::hostname($server) ne '') {
19602:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
19603:                     push(@okoffload,$server);
19604:                 }
19605:             }
19606:         }
19607:         if (@okoffload) {
19608:             foreach my $lonhost (@okoffload) {
19609:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
19610:             }
19611:         }
19612:     }
19613:     my @offloadoth = &Apache::loncommon::get_env_multiple('form.offloadoth');
19614:     my @okoffloadoth;
19615:     if (@offloadoth) {
19616:         foreach my $server (@offloadoth) {
19617:             if (&Apache::lonnet::hostname($server) ne '') {
19618:                 unless (grep(/^\Q$server\E$/,@okoffloadoth)) {
19619:                     push(@okoffloadoth,$server);
19620:                 }
19621:             }
19622:         }
19623:         if (@okoffloadoth) {
19624:             foreach my $lonhost (@okoffloadoth) {
19625:                 $defaultshash{'usersessions'}{'offloadoth'}{$lonhost} = 1;
19626:             }
19627:         }
19628:     }
19629:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
19630:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
19631:             if (ref($changes{'spares'}) eq 'HASH') {
19632:                 if (keys(%{$changes{'spares'}}) > 0) {
19633:                     $savespares = 1;
19634:                 }
19635:             }
19636:         } else {
19637:             $savespares = 1;
19638:         }
19639:         foreach my $offload ('offloadnow','offloadoth') {
19640:             if (ref($domconfig{'usersessions'}{$offload}) eq 'HASH') {
19641:                 foreach my $lonhost (keys(%{$domconfig{'usersessions'}{$offload}})) {
19642:                     unless ($defaultshash{'usersessions'}{$offload}{$lonhost}) {
19643:                         $changes{$offload} = 1;
19644:                         last;
19645:                     }
19646:                 }
19647:                 unless ($changes{$offload}) {
19648:                     foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{$offload}})) {
19649:                         unless ($domconfig{'usersessions'}{$offload}{$lonhost}) {
19650:                             $changes{$offload} = 1;
19651:                             last;
19652:                         }
19653:                     }
19654:                 }
19655:             } else {
19656:                 if (($offload eq 'offloadnow') && (@okoffload)) {
19657:                      $changes{'offloadnow'} = 1;
19658:                 }
19659:                 if (($offload eq 'offloadoth') && (@okoffloadoth)) {
19660:                     $changes{'offloadoth'} = 1;
19661:                 }
19662:             } 
19663:         }
19664:     } else {
19665:         if (@okoffload) {
19666:             $changes{'offloadnow'} = 1;
19667:         }
19668:         if (@okoffloadoth) {
19669:             $changes{'offloadoth'} = 1;
19670:         }
19671:     }
19672:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
19673:     if ((keys(%changes) > 0) || ($savespares)) {
19674:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
19675:                                                  $dom);
19676:         if ($putresult eq 'ok') {
19677:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
19678:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
19679:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
19680:                 }
19681:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
19682:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
19683:                 }
19684:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
19685:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
19686:                 }
19687:                 if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
19688:                     $domdefaults{'offloadoth'} = $defaultshash{'usersessions'}{'offloadoth'};
19689:                 }
19690:             }
19691:             my $cachetime = 24*60*60;
19692:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19693:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
19694:             if (ref($lastactref) eq 'HASH') {
19695:                 $lastactref->{'domdefaults'} = 1;
19696:                 $lastactref->{'usersessions'} = 1;
19697:             }
19698:             if (keys(%changes) > 0) {
19699:                 my %lt = &usersession_titles();
19700:                 $resulttext = &mt('Changes made:').'<ul>';
19701:                 foreach my $prefix (@prefixes) {
19702:                     if (ref($changes{$prefix}) eq 'HASH') {
19703:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
19704:                         if ($prefix eq 'spares') {
19705:                             if (ref($changes{$prefix}) eq 'HASH') {
19706:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
19707:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
19708:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
19709:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
19710:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
19711:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
19712:                                         foreach my $type (@{$types{$prefix}}) {
19713:                                             if ($changes{$prefix}{$lonhost}{$type}) {
19714:                                                 my $offloadto = &mt('None');
19715:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
19716:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
19717:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
19718:                                                     }
19719:                                                 }
19720:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
19721:                                             }
19722:                                         }
19723:                                     }
19724:                                     $resulttext .= '</li>';
19725:                                 }
19726:                             }
19727:                         } else {
19728:                             foreach my $type (@{$types{$prefix}}) {
19729:                                 if (defined($changes{$prefix}{$type})) {
19730:                                     my ($newvalue,$notinuse);
19731:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
19732:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
19733:                                             if ($type eq 'version') {
19734:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
19735:                                             } else {
19736:                                                 if (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
19737:                                                     if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
19738:                                                         $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
19739:                                                     }
19740:                                                 } else {
19741:                                                     $notinuse = 1;
19742:                                                 }
19743:                                             }
19744:                                         }
19745:                                     }
19746:                                     if ($newvalue eq '') {
19747:                                         if ($type eq 'version') {
19748:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
19749:                                         } elsif ($notinuse) {
19750:                                             $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
19751:                                         } else {
19752:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
19753:                                         }
19754:                                     } else {
19755:                                         if ($type eq 'version') {
19756:                                             $newvalue .= ' '.&mt('(or later)');
19757:                                         }
19758:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
19759:                                     }
19760:                                 }
19761:                             }
19762:                         }
19763:                         $resulttext .= '</ul>';
19764:                     }
19765:                 }
19766:                 if ($changes{'offloadnow'}) {
19767:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
19768:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
19769:                             $resulttext .= '<li>'.&mt('Switch any active user on next access, for server(s):').'<ul>';
19770:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
19771:                                 $resulttext .= '<li>'.$lonhost.'</li>';
19772:                             }
19773:                             $resulttext .= '</ul>';
19774:                         } else {
19775:                             $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.');
19776:                         }
19777:                     } else {
19778:                         $resulttext .= '<li>'.&mt('No servers now set to switch any active user on next access.').'</li>';
19779:                     }
19780:                 }
19781:                 if ($changes{'offloadoth'}) {
19782:                     if (ref($defaultshash{'usersessions'}{'offloadoth'}) eq 'HASH') {
19783:                         if (keys(%{$defaultshash{'usersessions'}{'offloadoth'}}) > 0) {
19784:                             $resulttext .= '<li>'.&mt('Switch other institutions on next access, for server(s):').'<ul>';
19785:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadoth'}}))) {
19786:                                 $resulttext .= '<li>'.$lonhost.'</li>';
19787:                             }
19788:                             $resulttext .= '</ul>';
19789:                         } else {
19790:                             $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.');
19791:                         }
19792:                     } else {
19793:                         $resulttext .= '<li>'.&mt('No servers now set to switch other institutions on next access.').'</li>';
19794:                     }
19795:                 }
19796:                 $resulttext .= '</ul>';
19797:             } else {
19798:                 $resulttext = $nochgmsg;
19799:             }
19800:         } else {
19801:             $resulttext = '<span class="LC_error">'.
19802:                           &mt('An error occurred: [_1]',$putresult).'</span>';
19803:         }
19804:     } else {
19805:         $resulttext = $nochgmsg;
19806:     }
19807:     return $resulttext;
19808: }
19809: 
19810: sub modify_ssl {
19811:     my ($dom,$lastactref,%domconfig) = @_;
19812:     my (%by_ip,%by_location,@intdoms,@instdoms);
19813:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
19814:     my @locations = sort(keys(%by_location));
19815:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
19816:     my (%defaultshash,%changes);
19817:     my $action = 'ssl';
19818:     my @prefixes = ('connto','connfrom','replication');
19819:     foreach my $prefix (@prefixes) {
19820:         $defaultshash{$action}{$prefix} = {};
19821:     }
19822:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
19823:     my $resulttext;
19824:     my %iphost = &Apache::lonnet::get_iphost();
19825:     my @reptypes = ('certreq','nocertreq');
19826:     my @connecttypes = ('dom','intdom','other');
19827:     my %types = (
19828:                   connto      => \@connecttypes,
19829:                   connfrom    => \@connecttypes,
19830:                   replication => \@reptypes,
19831:                 );
19832:     foreach my $prefix (sort(keys(%types))) {
19833:         foreach my $type (@{$types{$prefix}}) {
19834:             if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
19835:                 my $value = 'yes';
19836:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
19837:                     $value = $env{'form.'.$prefix.'_'.$type};
19838:                 }
19839:                 if (ref($domconfig{$action}) eq 'HASH') {
19840:                     if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
19841:                         if ($domconfig{$action}{$prefix}{$type} ne '') {
19842:                             if ($value ne $domconfig{$action}{$prefix}{$type}) {
19843:                                 $changes{$prefix}{$type} = 1;
19844:                             }
19845:                             $defaultshash{$action}{$prefix}{$type} = $value;
19846:                         } else {
19847:                             $defaultshash{$action}{$prefix}{$type} = $value;
19848:                             $changes{$prefix}{$type} = 1;
19849:                         }
19850:                     } else {
19851:                         $defaultshash{$action}{$prefix}{$type} = $value;
19852:                         $changes{$prefix}{$type} = 1;
19853:                     }
19854:                 } else {
19855:                     $defaultshash{$action}{$prefix}{$type} = $value;
19856:                     $changes{$prefix}{$type} = 1;
19857:                 }
19858:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
19859:                     delete($changes{$prefix}{$type});
19860:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
19861:                     delete($changes{$prefix}{$type});
19862:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
19863:                     delete($changes{$prefix}{$type});
19864:                 }
19865:             } elsif ($prefix eq 'replication') {
19866:                 if (@locations > 0) {
19867:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
19868:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
19869:                     my @okvals;
19870:                     foreach my $val (@vals) {
19871:                         if ($val =~ /:/) {
19872:                             my @items = split(/:/,$val);
19873:                             foreach my $item (@items) {
19874:                                 if (ref($by_location{$item}) eq 'ARRAY') {
19875:                                     push(@okvals,$item);
19876:                                 }
19877:                             }
19878:                         } else {
19879:                             if (ref($by_location{$val}) eq 'ARRAY') {
19880:                                 push(@okvals,$val);
19881:                             }
19882:                         }
19883:                     }
19884:                     @okvals = sort(@okvals);
19885:                     if (ref($domconfig{$action}) eq 'HASH') {
19886:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
19887:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
19888:                                 if ($inuse == 0) {
19889:                                     $changes{$prefix}{$type} = 1;
19890:                                 } else {
19891:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
19892:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
19893:                                     if (@changed > 0) {
19894:                                         $changes{$prefix}{$type} = 1;
19895:                                     }
19896:                                 }
19897:                             } else {
19898:                                 if ($inuse == 1) {
19899:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
19900:                                     $changes{$prefix}{$type} = 1;
19901:                                 }
19902:                             }
19903:                         } else {
19904:                             if ($inuse == 1) {
19905:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
19906:                                 $changes{$prefix}{$type} = 1;
19907:                             }
19908:                         }
19909:                     } else {
19910:                         if ($inuse == 1) {
19911:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
19912:                             $changes{$prefix}{$type} = 1;
19913:                         }
19914:                     }
19915:                 }
19916:             }
19917:         }
19918:     }
19919:     if (keys(%changes)) {
19920:         foreach my $prefix (keys(%changes)) {
19921:             if (ref($changes{$prefix}) eq 'HASH') {
19922:                 if (scalar(keys(%{$changes{$prefix}})) == 0) {
19923:                     delete($changes{$prefix});
19924:                 }
19925:             } else {
19926:                 delete($changes{$prefix});
19927:             }
19928:         }
19929:     }
19930:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
19931:     if (keys(%changes) > 0) {
19932:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
19933:                                                  $dom);
19934:         if ($putresult eq 'ok') {
19935:             if (ref($defaultshash{$action}) eq 'HASH') {
19936:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
19937:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
19938:                 }
19939:                 if (ref($defaultshash{$action}{'connto'}) eq 'HASH') {
19940:                     $domdefaults{'connto'} = $defaultshash{$action}{'connto'};
19941:                 }
19942:                 if (ref($defaultshash{$action}{'connfrom'}) eq 'HASH') {
19943:                     $domdefaults{'connfrom'} = $defaultshash{$action}{'connfrom'};
19944:                 }
19945:             }
19946:             my $cachetime = 24*60*60;
19947:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
19948:             if (ref($lastactref) eq 'HASH') {
19949:                 $lastactref->{'domdefaults'} = 1;
19950:             }
19951:             if (keys(%changes) > 0) {
19952:                 my %titles = &ssl_titles();
19953:                 $resulttext = &mt('Changes made:').'<ul>';
19954:                 foreach my $prefix (@prefixes) {
19955:                     if (ref($changes{$prefix}) eq 'HASH') {
19956:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
19957:                         foreach my $type (@{$types{$prefix}}) {
19958:                             if (defined($changes{$prefix}{$type})) {
19959:                                 my ($newvalue,$notinuse);
19960:                                 if (ref($defaultshash{$action}) eq 'HASH') {
19961:                                     if (ref($defaultshash{$action}{$prefix})) {
19962:                                         if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
19963:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
19964:                                         } else {
19965:                                             if (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
19966:                                                 if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
19967:                                                     $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
19968:                                                 }
19969:                                             } else {
19970:                                                 $notinuse = 1;
19971:                                             }
19972:                                         }
19973:                                     }
19974:                                     if ($notinuse) {
19975:                                         $resulttext .= '<li>'.&mt('[_1] set to: not in use',$titles{$type}).'</li>';
19976:                                     } elsif ($newvalue eq '') {
19977:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
19978:                                     } else {
19979:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
19980:                                     }
19981:                                 }
19982:                             }
19983:                         }
19984:                         $resulttext .= '</ul>';
19985:                     }
19986:                 }
19987:             } else {
19988:                 $resulttext = $nochgmsg;
19989:             }
19990:         } else {
19991:             $resulttext = '<span class="LC_error">'.
19992:                           &mt('An error occurred: [_1]',$putresult).'</span>';
19993:         }
19994:     } else {
19995:         $resulttext = $nochgmsg;
19996:     }
19997:     return $resulttext;
19998: }
19999: 
20000: sub modify_trust {
20001:     my ($dom,$lastactref,%domconfig) = @_;
20002:     my (%by_ip,%by_location,@intdoms,@instdoms);
20003:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
20004:     my @locations = sort(keys(%by_location));
20005:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
20006:     my @types = ('exc','inc');
20007:     my (%defaultshash,%changes);
20008:     foreach my $prefix (@prefixes) {
20009:         $defaultshash{'trust'}{$prefix} = {};
20010:     }
20011:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
20012:     my $resulttext;
20013:     foreach my $prefix (@prefixes) {
20014:         foreach my $type (@types) {
20015:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
20016:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
20017:             my @okvals;
20018:             foreach my $val (@vals) {
20019:                 if ($val =~ /:/) {
20020:                     my @items = split(/:/,$val);
20021:                     foreach my $item (@items) {
20022:                         if (ref($by_location{$item}) eq 'ARRAY') {
20023:                             push(@okvals,$item);
20024:                         }
20025:                     }
20026:                 } else {
20027:                     if (ref($by_location{$val}) eq 'ARRAY') {
20028:                         push(@okvals,$val);
20029:                     }
20030:                 }
20031:             }
20032:             @okvals = sort(@okvals);
20033:             if (ref($domconfig{'trust'}) eq 'HASH') {
20034:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
20035:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
20036:                         if ($inuse == 0) {
20037:                             $changes{$prefix}{$type} = 1;
20038:                         } else {
20039:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
20040:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
20041:                             if (@changed > 0) {
20042:                                 $changes{$prefix}{$type} = 1;
20043:                             }
20044:                         }
20045:                     } else {
20046:                         if ($inuse == 1) {
20047:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
20048:                             $changes{$prefix}{$type} = 1;
20049:                         }
20050:                     }
20051:                 } else {
20052:                     if ($inuse == 1) {
20053:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
20054:                         $changes{$prefix}{$type} = 1;
20055:                     }
20056:                 }
20057:             } else {
20058:                 if ($inuse == 1) {
20059:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
20060:                     $changes{$prefix}{$type} = 1;
20061:                 }
20062:             }
20063:         }
20064:     }
20065:     my $nochgmsg = &mt('No changes made to trust settings.');
20066:     if (keys(%changes) > 0) {
20067:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
20068:                                                  $dom);
20069:         if ($putresult eq 'ok') {
20070:             if (ref($defaultshash{'trust'}) eq 'HASH') {
20071:                 foreach my $prefix (@prefixes) {
20072:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
20073:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
20074:                     }
20075:                 }
20076:             }
20077:             my $cachetime = 24*60*60;
20078:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
20079:             if (ref($lastactref) eq 'HASH') {
20080:                 $lastactref->{'domdefaults'} = 1;
20081:             }
20082:             if (keys(%changes) > 0) {
20083:                 my %lt = &trust_titles();
20084:                 $resulttext = &mt('Changes made:').'<ul>';
20085:                 foreach my $prefix (@prefixes) {
20086:                     if (ref($changes{$prefix}) eq 'HASH') {
20087:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
20088:                         foreach my $type (@types) {
20089:                             if (defined($changes{$prefix}{$type})) {
20090:                                 my ($newvalue,$notinuse);
20091:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
20092:                                     if (ref($defaultshash{'trust'}{$prefix})) {
20093:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
20094:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
20095:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
20096:                                             }
20097:                                         } else {
20098:                                             $notinuse = 1;
20099:                                         }
20100:                                     }
20101:                                 }
20102:                                 if ($notinuse) {
20103:                                     $resulttext .= '<li>'.&mt('[_1] set to: not in use',$lt{$type}).'</li>';
20104:                                 } elsif ($newvalue eq '') {
20105:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
20106:                                 } else {
20107:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
20108:                                 }
20109:                             }
20110:                         }
20111:                         $resulttext .= '</ul>';
20112:                     }
20113:                 }
20114:                 $resulttext .= '</ul>';
20115:             } else {
20116:                 $resulttext = $nochgmsg;
20117:             }
20118:         } else {
20119:             $resulttext = '<span class="LC_error">'.
20120:                           &mt('An error occurred: [_1]',$putresult).'</span>';
20121:         }
20122:     } else {
20123:         $resulttext = $nochgmsg;
20124:     }
20125:     return $resulttext;
20126: }
20127: 
20128: sub modify_loadbalancing {
20129:     my ($dom,%domconfig) = @_;
20130:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
20131:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
20132:     my ($othertitle,$usertypes,$types) =
20133:         &Apache::loncommon::sorted_inst_types($dom);
20134:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
20135:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
20136:     my @sparestypes = ('primary','default');
20137:     my %typetitles = &sparestype_titles();
20138:     my $resulttext;
20139:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
20140:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
20141:         %existing = %{$domconfig{'loadbalancing'}};
20142:     }
20143:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
20144:                               \%currtargets,\%currrules,\%currcookies);
20145:     my ($saveloadbalancing,%defaultshash,%changes);
20146:     my ($alltypes,$othertypes,$titles) =
20147:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
20148:     my %ruletitles = &offloadtype_text();
20149:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
20150:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
20151:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
20152:         if ($balancer eq '') {
20153:             next;
20154:         }
20155:         if (!exists($servers{$balancer})) {
20156:             if (exists($currbalancer{$balancer})) {
20157:                 push(@{$changes{'delete'}},$balancer);
20158:             }
20159:             next;
20160:         }
20161:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
20162:             push(@{$changes{'delete'}},$balancer);
20163:             next;
20164:         }
20165:         if (!exists($currbalancer{$balancer})) {
20166:             push(@{$changes{'add'}},$balancer);
20167:         }
20168:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
20169:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
20170:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
20171:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
20172:             $saveloadbalancing = 1;
20173:         }
20174:         foreach my $sparetype (@sparestypes) {
20175:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
20176:             my @offloadto;
20177:             foreach my $target (@targets) {
20178:                 if (($servers{$target}) && ($target ne $balancer)) {
20179:                     if ($sparetype eq 'default') {
20180:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
20181:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
20182:                         }
20183:                     }
20184:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
20185:                         push(@offloadto,$target);
20186:                     }
20187:                 }
20188:             }
20189:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
20190:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
20191:                     push(@offloadto,$balancer);
20192:                 }
20193:             }
20194:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
20195:         }
20196:         if ($env{'form.loadbalancing_cookie_'.$i}) {
20197:             $defaultshash{'loadbalancing'}{$balancer}{'cookie'} = 1;
20198:             if (exists($currbalancer{$balancer})) { 
20199:                 unless ($currcookies{$balancer}) {
20200:                     $changes{'curr'}{$balancer}{'cookie'} = 1;
20201:                 }
20202:             }
20203:         } elsif (exists($currbalancer{$balancer})) {
20204:             if ($currcookies{$balancer}) {
20205:                 $changes{'curr'}{$balancer}{'cookie'} = 1;
20206:             }
20207:         }
20208:         if (ref($currtargets{$balancer}) eq 'HASH') {
20209:             foreach my $sparetype (@sparestypes) {
20210:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
20211:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
20212:                     if (@targetdiffs > 0) {
20213:                         $changes{'curr'}{$balancer}{'targets'} = 1;
20214:                     }
20215:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
20216:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
20217:                         $changes{'curr'}{$balancer}{'targets'} = 1;
20218:                     }
20219:                 }
20220:             }
20221:         } else {
20222:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
20223:                 foreach my $sparetype (@sparestypes) {
20224:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
20225:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
20226:                             $changes{'curr'}{$balancer}{'targets'} = 1;
20227:                         }
20228:                     }
20229:                 }
20230:             }
20231:         }
20232:         my $ishomedom;
20233:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
20234:             $ishomedom = 1;
20235:         }
20236:         if (ref($alltypes) eq 'ARRAY') {
20237:             foreach my $type (@{$alltypes}) {
20238:                 my $rule;
20239:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
20240:                          (!$ishomedom)) {
20241:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
20242:                 }
20243:                 if ($rule eq 'specific') {
20244:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
20245:                     if (exists($servers{$specifiedhost})) {
20246:                         $rule = $specifiedhost;
20247:                     }
20248:                 }
20249:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
20250:                 if (ref($currrules{$balancer}) eq 'HASH') {
20251:                     if ($rule ne $currrules{$balancer}{$type}) {
20252:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
20253:                     }
20254:                 } elsif ($rule ne '') {
20255:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
20256:                 }
20257:             }
20258:         }
20259:     }
20260:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
20261:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
20262:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
20263:             $defaultshash{'loadbalancing'} = {};
20264:         }
20265:         my $putresult = &Apache::lonnet::put_dom('configuration',
20266:                                                  \%defaultshash,$dom);
20267:         if ($putresult eq 'ok') {
20268:             if (keys(%changes) > 0) {
20269:                 my %toupdate;
20270:                 if (ref($changes{'delete'}) eq 'ARRAY') {
20271:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
20272:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
20273:                         $toupdate{$balancer} = 1;
20274:                     }
20275:                 }
20276:                 if (ref($changes{'add'}) eq 'ARRAY') {
20277:                     foreach my $balancer (sort(@{$changes{'add'}})) {
20278:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
20279:                         $toupdate{$balancer} = 1;
20280:                     }
20281:                 }
20282:                 if (ref($changes{'curr'}) eq 'HASH') {
20283:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
20284:                         $toupdate{$balancer} = 1;
20285:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
20286:                             if ($changes{'curr'}{$balancer}{'targets'}) {
20287:                                 my %offloadstr;
20288:                                 foreach my $sparetype (@sparestypes) {
20289:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
20290:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
20291:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
20292:                                         }
20293:                                     }
20294:                                 }
20295:                                 if (keys(%offloadstr) == 0) {
20296:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
20297:                                 } else {
20298:                                     my $showoffload;
20299:                                     foreach my $sparetype (@sparestypes) {
20300:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
20301:                                         if (defined($offloadstr{$sparetype})) {
20302:                                             $showoffload .= $offloadstr{$sparetype};
20303:                                         } else {
20304:                                             $showoffload .= &mt('None');
20305:                                         }
20306:                                         $showoffload .= ('&nbsp;'x3);
20307:                                     }
20308:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
20309:                                 }
20310:                             }
20311:                         }
20312:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
20313:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
20314:                                 foreach my $type (@{$alltypes}) {
20315:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
20316:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
20317:                                         my $balancetext;
20318:                                         if ($rule eq '') {
20319:                                             $balancetext =  $ruletitles{'default'};
20320:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
20321:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
20322:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
20323:                                                 foreach my $sparetype (@sparestypes) {
20324:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
20325:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
20326:                                                     }
20327:                                                 }
20328:                                                 foreach my $item (@{$alltypes}) {
20329:                                                     next if ($item =~  /^_LC_ipchange/);
20330:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
20331:                                                     if ($hasrule eq 'homeserver') {
20332:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
20333:                                                     } else {
20334:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
20335:                                                             if ($servers{$hasrule}) {
20336:                                                                 $toupdate{$hasrule} = 1;
20337:                                                             }
20338:                                                         }
20339:                                                     }
20340:                                                 }
20341:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
20342:                                                     $balancetext =  $ruletitles{$rule};
20343:                                                 } else {
20344:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
20345:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
20346:                                                     if ($receiver) {
20347:                                                         $toupdate{$receiver};
20348:                                                     }
20349:                                                 }
20350:                                             } else {
20351:                                                 $balancetext =  $ruletitles{$rule};
20352:                                             }
20353:                                         } else {
20354:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
20355:                                         }
20356:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
20357:                                     }
20358:                                 }
20359:                             }
20360:                         }
20361:                         if ($changes{'curr'}{$balancer}{'cookie'}) {
20362:                             $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- cookie use enabled',
20363:                                                       $balancer).'</li>'; 
20364:                         }
20365:                         if (keys(%toupdate)) {
20366:                             my %thismachine;
20367:                             my $updatedhere;
20368:                             my $cachetime = 60*60*24;
20369:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
20370:                             foreach my $lonhost (keys(%toupdate)) {
20371:                                 if ($thismachine{$lonhost}) {
20372:                                     unless ($updatedhere) {
20373:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
20374:                                                                       $defaultshash{'loadbalancing'},
20375:                                                                       $cachetime);
20376:                                         $updatedhere = 1;
20377:                                     }
20378:                                 } else {
20379:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
20380:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
20381:                                 }
20382:                             }
20383:                         }
20384:                     }
20385:                 }
20386:                 if ($resulttext ne '') {
20387:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
20388:                 } else {
20389:                     $resulttext = $nochgmsg;
20390:                 }
20391:             } else {
20392:                 $resulttext = $nochgmsg;
20393:             }
20394:         } else {
20395:             $resulttext = '<span class="LC_error">'.
20396:                           &mt('An error occurred: [_1]',$putresult).'</span>';
20397:         }
20398:     } else {
20399:         $resulttext = $nochgmsg;
20400:     }
20401:     return $resulttext;
20402: }
20403: 
20404: sub recurse_check {
20405:     my ($chkcats,$categories,$depth,$name) = @_;
20406:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
20407:         my $chg = 0;
20408:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
20409:             my $category = $chkcats->[$depth]{$name}[$j];
20410:             my $item;
20411:             if ($category eq '') {
20412:                 $chg ++;
20413:             } else {
20414:                 my $deeper = $depth + 1;
20415:                 $item = &escape($category).':'.&escape($name).':'.$depth;
20416:                 if ($chg) {
20417:                     $categories->{$item} -= $chg;
20418:                 }
20419:                 &recurse_check($chkcats,$categories,$deeper,$category);
20420:                 $deeper --;
20421:             }
20422:         }
20423:     }
20424:     return;
20425: }
20426: 
20427: sub recurse_cat_deletes {
20428:     my ($item,$coursecategories,$deletions) = @_;
20429:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
20430:     my $subdepth = $depth + 1;
20431:     if (ref($coursecategories) eq 'HASH') {
20432:         foreach my $subitem (keys(%{$coursecategories})) {
20433:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
20434:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
20435:                 delete($coursecategories->{$subitem});
20436:                 $deletions->{$subitem} = 1;
20437:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
20438:             }
20439:         }
20440:     }
20441:     return;
20442: }
20443: 
20444: sub active_dc_picker {
20445:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
20446:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
20447:     my @domcoord = keys(%domcoords);
20448:     if (keys(%currhash)) {
20449:         foreach my $dc (keys(%currhash)) {
20450:             unless (exists($domcoords{$dc})) {
20451:                 push(@domcoord,$dc);
20452:             }
20453:         }
20454:     }
20455:     @domcoord = sort(@domcoord);
20456:     my $numdcs = scalar(@domcoord);
20457:     my $rows = 0;
20458:     my $table;
20459:     if ($numdcs > 1) {
20460:         $table = '<table>';
20461:         for (my $i=0; $i<@domcoord; $i++) {
20462:             my $rem = $i%($numinrow);
20463:             if ($rem == 0) {
20464:                 if ($i > 0) {
20465:                     $table .= '</tr>';
20466:                 }
20467:                 $table .= '<tr>';
20468:                 $rows ++;
20469:             }
20470:             my $check = '';
20471:             if ($inputtype eq 'radio') {
20472:                 if (keys(%currhash) == 0) {
20473:                     if (!$i) {
20474:                         $check = ' checked="checked"';
20475:                     }
20476:                 } elsif (exists($currhash{$domcoord[$i]})) {
20477:                     $check = ' checked="checked"';
20478:                 }
20479:             } else {
20480:                 if (exists($currhash{$domcoord[$i]})) {
20481:                     $check = ' checked="checked"';
20482:                 }
20483:             }
20484:             if ($i == @domcoord - 1) {
20485:                 my $colsleft = $numinrow - $rem;
20486:                 if ($colsleft > 1) {
20487:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
20488:                 } else {
20489:                     $table .= '<td class="LC_left_item">';
20490:                 }
20491:             } else {
20492:                 $table .= '<td class="LC_left_item">';
20493:             }
20494:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
20495:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
20496:             $table .= '<span class="LC_nobreak"><label>'.
20497:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
20498:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
20499:             if ($user ne $dcname.':'.$dcdom) {
20500:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
20501:             }
20502:             $table .= '</label></span></td>';
20503:         }
20504:         $table .= '</tr></table>';
20505:     } elsif ($numdcs == 1) {
20506:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
20507:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
20508:         if ($inputtype eq 'radio') {
20509:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
20510:             if ($user ne $dcname.':'.$dcdom) {
20511:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
20512:             }
20513:         } else {
20514:             my $check;
20515:             if (exists($currhash{$domcoord[0]})) {
20516:                 $check = ' checked="checked"';
20517:             }
20518:             $table = '<span class="LC_nobreak"><label>'.
20519:                      '<input type="checkbox" name="'.$name.'" '.
20520:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
20521:             if ($user ne $dcname.':'.$dcdom) {
20522:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
20523:             }
20524:             $table .= '</label></span>';
20525:             $rows ++;
20526:         }
20527:     }
20528:     return ($numdcs,$table,$rows);
20529: }
20530: 
20531: sub usersession_titles {
20532:     return &Apache::lonlocal::texthash(
20533:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
20534:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
20535:                spares => 'Servers offloaded to, when busy',
20536:                version => 'LON-CAPA version requirement',
20537:                excludedomain => 'Allow all, but exclude specific domains',
20538:                includedomain => 'Deny all, but include specific domains',
20539:                primary => 'Primary (checked first)',
20540:                default => 'Default',
20541:            );
20542: }
20543: 
20544: sub id_for_thisdom {
20545:     my (%servers) = @_;
20546:     my %altids;
20547:     foreach my $server (keys(%servers)) {
20548:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
20549:         if ($serverhome ne $server) {
20550:             $altids{$serverhome} = $server;
20551:         }
20552:     }
20553:     return %altids;
20554: }
20555: 
20556: sub count_servers {
20557:     my ($currbalancer,%servers) = @_;
20558:     my (@spares,$numspares);
20559:     foreach my $lonhost (sort(keys(%servers))) {
20560:         next if ($currbalancer eq $lonhost);
20561:         push(@spares,$lonhost);
20562:     }
20563:     if ($currbalancer) {
20564:         $numspares = scalar(@spares);
20565:     } else {
20566:         $numspares = scalar(@spares) - 1;
20567:     }
20568:     return ($numspares,@spares);
20569: }
20570: 
20571: sub lonbalance_targets_js {
20572:     my ($dom,$types,$servers,$settings) = @_;
20573:     my $select = &mt('Select');
20574:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
20575:     if (ref($servers) eq 'HASH') {
20576:         $alltargets = join("','",sort(keys(%{$servers})));
20577:         my @homedoms;
20578:         foreach my $server (sort(keys(%{$servers}))) {
20579:             if (&Apache::lonnet::host_domain($server) eq $dom) {
20580:                 push(@homedoms,'1');
20581:             } else {
20582:                 push(@homedoms,'0');
20583:             }
20584:         }
20585:         $allishome = join("','",@homedoms);
20586:     }
20587:     if (ref($types) eq 'ARRAY') {
20588:         if (@{$types} > 0) {
20589:             @alltypes = @{$types};
20590:         }
20591:     }
20592:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
20593:     $allinsttypes = join("','",@alltypes);
20594:     my (%currbalancer,%currtargets,%currrules,%existing,%currcookies);
20595:     if (ref($settings) eq 'HASH') {
20596:         %existing = %{$settings};
20597:     }
20598:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
20599:                               \%currtargets,\%currrules,\%currcookies);
20600:     my $balancers = join("','",sort(keys(%currbalancer)));
20601:     return <<"END";
20602: 
20603: <script type="text/javascript">
20604: // <![CDATA[
20605: 
20606: currBalancers = new Array('$balancers');
20607: 
20608: function toggleTargets(balnum) {
20609:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
20610:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
20611:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
20612:     var prevbalancer = prevhostitem.value;
20613:     var baltotal = document.getElementById('loadbalancing_total').value;
20614:     prevhostitem.value = balancer;
20615:     if (prevbalancer != '') {
20616:         var prevIdx = currBalancers.indexOf(prevbalancer);
20617:         if (prevIdx != -1) {
20618:             currBalancers.splice(prevIdx,1);
20619:         }
20620:     }
20621:     if (balancer == '') {
20622:         hideSpares(balnum);
20623:     } else {
20624:         var currIdx = currBalancers.indexOf(balancer);
20625:         if (currIdx == -1) {
20626:             currBalancers.push(balancer);
20627:         }
20628:         var homedoms = new Array('$allishome');
20629:         var ishomedom = homedoms[lonhostitem.selectedIndex];
20630:         showSpares(balancer,ishomedom,balnum);
20631:     }
20632:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
20633:     return;
20634: }
20635: 
20636: function showSpares(balancer,ishomedom,balnum) {
20637:     var alltargets = new Array('$alltargets');
20638:     var insttypes = new Array('$allinsttypes');
20639:     var offloadtypes = new Array('primary','default');
20640: 
20641:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
20642:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
20643:  
20644:     for (var i=0; i<offloadtypes.length; i++) {
20645:         var count = 0;
20646:         for (var j=0; j<alltargets.length; j++) {
20647:             if (alltargets[j] != balancer) {
20648:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
20649:                 item.value = alltargets[j];
20650:                 item.style.textAlign='left';
20651:                 item.style.textFace='normal';
20652:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
20653:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
20654:                     item.disabled = '';
20655:                 } else {
20656:                     item.disabled = 'disabled';
20657:                     item.checked = false;
20658:                 }
20659:                 count ++;
20660:             }
20661:         }
20662:     }
20663:     for (var k=0; k<insttypes.length; k++) {
20664:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
20665:             if (ishomedom == 1) {
20666:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
20667:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
20668:             } else {
20669:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
20670:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
20671:             }
20672:         } else {
20673:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
20674:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
20675:         }
20676:         if ((insttypes[k] != '_LC_external') && 
20677:             ((insttypes[k] != '_LC_internetdom') ||
20678:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
20679:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
20680:             item.options.length = 0;
20681:             item.options[0] = new Option("","",true,true);
20682:             var idx = 0;
20683:             for (var m=0; m<alltargets.length; m++) {
20684:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
20685:                     idx ++;
20686:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
20687:                 }
20688:             }
20689:         }
20690:     }
20691:     return;
20692: }
20693: 
20694: function hideSpares(balnum) {
20695:     var alltargets = new Array('$alltargets');
20696:     var insttypes = new Array('$allinsttypes');
20697:     var offloadtypes = new Array('primary','default');
20698: 
20699:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
20700:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
20701: 
20702:     var total = alltargets.length - 1;
20703:     for (var i=0; i<offloadtypes; i++) {
20704:         for (var j=0; j<total; j++) {
20705:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
20706:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
20707:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
20708:         }
20709:     }
20710:     for (var k=0; k<insttypes.length; k++) {
20711:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
20712:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
20713:         if (insttypes[k] != '_LC_external') {
20714:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
20715:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
20716:         }
20717:     }
20718:     return;
20719: }
20720: 
20721: function checkOffloads(item,balnum,type) {
20722:     var alltargets = new Array('$alltargets');
20723:     var offloadtypes = new Array('primary','default');
20724:     if (item.checked) {
20725:         var total = alltargets.length - 1;
20726:         var other;
20727:         if (type == offloadtypes[0]) {
20728:             other = offloadtypes[1];
20729:         } else {
20730:             other = offloadtypes[0];
20731:         }
20732:         for (var i=0; i<total; i++) {
20733:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
20734:             if (server == item.value) {
20735:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
20736:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
20737:                 }
20738:             }
20739:         }
20740:     }
20741:     return;
20742: }
20743: 
20744: function singleServerToggle(balnum,type) {
20745:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
20746:     if (offloadtoSelIdx == 0) {
20747:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
20748:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
20749: 
20750:     } else {
20751:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
20752:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
20753:     }
20754:     return;
20755: }
20756: 
20757: function balanceruleChange(formname,balnum,type) {
20758:     if (type == '_LC_external') {
20759:         return;
20760:     }
20761:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
20762:     for (var i=0; i<typesRules.length; i++) {
20763:         if (formname.elements[typesRules[i]].checked) {
20764:             if (formname.elements[typesRules[i]].value != 'specific') {
20765:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
20766:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
20767:             } else {
20768:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
20769:             }
20770:         }
20771:     }
20772:     return;
20773: }
20774: 
20775: function balancerDeleteChange(balnum) {
20776:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
20777:     var baltotal = document.getElementById('loadbalancing_total').value;
20778:     var addtarget;
20779:     var removetarget;
20780:     var action = 'delete';
20781:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
20782:         var lonhost = hostitem.value;
20783:         var currIdx = currBalancers.indexOf(lonhost);
20784:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
20785:             if (currIdx != -1) {
20786:                 currBalancers.splice(currIdx,1);
20787:             }
20788:             addtarget = lonhost;
20789:         } else {
20790:             if (currIdx == -1) {
20791:                 currBalancers.push(lonhost);
20792:             }
20793:             removetarget = lonhost;
20794:             action = 'undelete';
20795:         }
20796:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
20797:     }
20798:     return;
20799: }
20800: 
20801: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
20802:     if (baltotal > 1) {
20803:         var offloadtypes = new Array('primary','default');
20804:         var alltargets = new Array('$alltargets');
20805:         var insttypes = new Array('$allinsttypes');
20806:         for (var i=0; i<baltotal; i++) {
20807:             if (i != balnum) {
20808:                 for (var j=0; j<offloadtypes.length; j++) {
20809:                     var total = alltargets.length - 1;
20810:                     for (var k=0; k<total; k++) {
20811:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
20812:                         var server = serveritem.value;
20813:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
20814:                             if (server == addtarget) {
20815:                                 serveritem.disabled = '';
20816:                             }
20817:                         }
20818:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
20819:                             if (server == removetarget) {
20820:                                 serveritem.disabled = 'disabled';
20821:                                 serveritem.checked = false;
20822:                             }
20823:                         }
20824:                     }
20825:                 }
20826:                 for (var j=0; j<insttypes.length; j++) {
20827:                     if (insttypes[j] != '_LC_external') {
20828:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
20829:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
20830:                             var currSel = singleserver.selectedIndex;
20831:                             var currVal = singleserver.options[currSel].value;
20832:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
20833:                                 var numoptions = singleserver.options.length;
20834:                                 var needsnew = 1;
20835:                                 for (var k=0; k<numoptions; k++) {
20836:                                     if (singleserver.options[k] == addtarget) {
20837:                                         needsnew = 0;
20838:                                         break;
20839:                                     }
20840:                                 }
20841:                                 if (needsnew == 1) {
20842:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
20843:                                 }
20844:                             }
20845:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
20846:                                 singleserver.options.length = 0;
20847:                                 if ((currVal) && (currVal != removetarget)) {
20848:                                     singleserver.options[0] = new Option("","",false,false);
20849:                                 } else {
20850:                                     singleserver.options[0] = new Option("","",true,true);
20851:                                 }
20852:                                 var idx = 0;
20853:                                 for (var m=0; m<alltargets.length; m++) {
20854:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
20855:                                         idx ++;
20856:                                         if (currVal == alltargets[m]) {
20857:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
20858:                                         } else {
20859:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
20860:                                         }
20861:                                     }
20862:                                 }
20863:                             }
20864:                         }
20865:                     }
20866:                 }
20867:             }
20868:         }
20869:     }
20870:     return;
20871: }
20872: 
20873: // ]]>
20874: </script>
20875: 
20876: END
20877: }
20878: 
20879: 
20880: sub new_spares_js {
20881:     my @sparestypes = ('primary','default');
20882:     my $types = join("','",@sparestypes);
20883:     my $select = &mt('Select');
20884:     return <<"END";
20885: 
20886: <script type="text/javascript">
20887: // <![CDATA[
20888: 
20889: function updateNewSpares(formname,lonhost) {
20890:     var types = new Array('$types');
20891:     var include = new Array();
20892:     var exclude = new Array();
20893:     for (var i=0; i<types.length; i++) {
20894:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
20895:         for (var j=0; j<spareboxes.length; j++) {
20896:             if (formname.elements[spareboxes[j]].checked) {
20897:                 exclude.push(formname.elements[spareboxes[j]].value);
20898:             } else {
20899:                 include.push(formname.elements[spareboxes[j]].value);
20900:             }
20901:         }
20902:     }
20903:     for (var i=0; i<types.length; i++) {
20904:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
20905:         var selIdx = newSpare.selectedIndex;
20906:         var currnew = newSpare.options[selIdx].value;
20907:         var okSpares = new Array();
20908:         for (var j=0; j<newSpare.options.length; j++) {
20909:             var possible = newSpare.options[j].value;
20910:             if (possible != '') {
20911:                 if (exclude.indexOf(possible) == -1) {
20912:                     okSpares.push(possible);
20913:                 } else {
20914:                     if (currnew == possible) {
20915:                         selIdx = 0;
20916:                     }
20917:                 }
20918:             }
20919:         }
20920:         for (var k=0; k<include.length; k++) {
20921:             if (okSpares.indexOf(include[k]) == -1) {
20922:                 okSpares.push(include[k]);
20923:             }
20924:         }
20925:         okSpares.sort();
20926:         newSpare.options.length = 0;
20927:         if (selIdx == 0) {
20928:             newSpare.options[0] = new Option("$select","",true,true);
20929:         } else {
20930:             newSpare.options[0] = new Option("$select","",false,false);
20931:         }
20932:         for (var m=0; m<okSpares.length; m++) {
20933:             var idx = m+1;
20934:             var selThis = 0;
20935:             if (selIdx != 0) {
20936:                 if (okSpares[m] == currnew) {
20937:                     selThis = 1;
20938:                 }
20939:             }
20940:             if (selThis == 1) {
20941:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
20942:             } else {
20943:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
20944:             }
20945:         }
20946:     }
20947:     return;
20948: }
20949: 
20950: function checkNewSpares(lonhost,type) {
20951:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
20952:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
20953:     if (chosen != '') {
20954:         var othertype;
20955:         var othernewSpare;
20956:         if (type == 'primary') {
20957:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
20958:         }
20959:         if (type == 'default') {
20960:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
20961:         }
20962:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
20963:             othernewSpare.selectedIndex = 0;
20964:         }
20965:     }
20966:     return;
20967: }
20968: 
20969: // ]]>
20970: </script>
20971: 
20972: END
20973: 
20974: }
20975: 
20976: sub common_domprefs_js {
20977:     return <<"END";
20978: 
20979: <script type="text/javascript">
20980: // <![CDATA[
20981: 
20982: function getIndicesByName(formname,item) {
20983:     var group = new Array();
20984:     for (var i=0;i<formname.elements.length;i++) {
20985:         if (formname.elements[i].name == item) {
20986:             group.push(formname.elements[i].id);
20987:         }
20988:     }
20989:     return group;
20990: }
20991: 
20992: // ]]>
20993: </script>
20994: 
20995: END
20996: 
20997: }
20998: 
20999: sub recaptcha_js {
21000:     my %lt = &captcha_phrases();
21001:     return <<"END";
21002: 
21003: <script type="text/javascript">
21004: // <![CDATA[
21005: 
21006: function updateCaptcha(caller,context) {
21007:     var privitem;
21008:     var pubitem;
21009:     var privtext;
21010:     var pubtext;
21011:     var versionitem;
21012:     var versiontext;
21013:     if (document.getElementById(context+'_recaptchapub')) {
21014:         pubitem = document.getElementById(context+'_recaptchapub');
21015:     } else {
21016:         return;
21017:     }
21018:     if (document.getElementById(context+'_recaptchapriv')) {
21019:         privitem = document.getElementById(context+'_recaptchapriv');
21020:     } else {
21021:         return;
21022:     }
21023:     if (document.getElementById(context+'_recaptchapubtxt')) {
21024:         pubtext = document.getElementById(context+'_recaptchapubtxt');
21025:     } else {
21026:         return;
21027:     }
21028:     if (document.getElementById(context+'_recaptchaprivtxt')) {
21029:         privtext = document.getElementById(context+'_recaptchaprivtxt');
21030:     } else {
21031:         return;
21032:     }
21033:     if (document.getElementById(context+'_recaptchaversion')) {
21034:         versionitem = document.getElementById(context+'_recaptchaversion');
21035:     } else {
21036:         return;
21037:     }
21038:     if (document.getElementById(context+'_recaptchavertxt')) {
21039:         versiontext = document.getElementById(context+'_recaptchavertxt');
21040:     } else {
21041:         return;
21042:     }
21043:     if (caller.checked) {
21044:         if (caller.value == 'recaptcha') {
21045:             pubitem.type = 'text';
21046:             privitem.type = 'text';
21047:             pubitem.size = '40';
21048:             privitem.size = '40';
21049:             pubtext.innerHTML = "$lt{'pub'}";
21050:             privtext.innerHTML = "$lt{'priv'}";
21051:             versionitem.type = 'text';
21052:             versionitem.size = '3';
21053:             versiontext.innerHTML = "$lt{'ver'}";
21054:         } else {
21055:             pubitem.type = 'hidden';
21056:             privitem.type = 'hidden';
21057:             versionitem.type = 'hidden';
21058:             pubtext.innerHTML = '';
21059:             privtext.innerHTML = '';
21060:             versiontext.innerHTML = '';
21061:         }
21062:     }
21063:     return;
21064: }
21065: 
21066: // ]]>
21067: </script>
21068: 
21069: END
21070: 
21071: }
21072: 
21073: sub toggle_display_js {
21074:     return <<"END";
21075: 
21076: <script type="text/javascript">
21077: // <![CDATA[
21078: 
21079: function toggleDisplay(domForm,caller) {
21080:     if (document.getElementById(caller)) {
21081:         var divitem = document.getElementById(caller);
21082:         var optionsElement = domForm.coursecredits;
21083:         var checkval = 1;
21084:         var dispval = 'block';
21085:         var selfcreateRegExp = /^cancreate_emailverified/;
21086:         if (caller == 'emailoptions') {
21087:             optionsElement = domForm.cancreate_email;
21088:         }
21089:         if (caller == 'studentsubmission') {
21090:             optionsElement = domForm.postsubmit;
21091:         }
21092:         if (caller == 'cloneinstcode') {
21093:             optionsElement = domForm.canclone;
21094:             checkval = 'instcode';
21095:         }
21096:         if (selfcreateRegExp.test(caller)) {
21097:             optionsElement = domForm.elements[caller];
21098:             checkval = 'other';
21099:             dispval = 'inline'
21100:         }
21101:         if (optionsElement.length) {
21102:             var currval;
21103:             for (var i=0; i<optionsElement.length; i++) {
21104:                 if (optionsElement[i].checked) {
21105:                    currval = optionsElement[i].value;
21106:                 }
21107:             }
21108:             if (currval == checkval) {
21109:                 divitem.style.display = dispval;
21110:             } else {
21111:                 divitem.style.display = 'none';
21112:             }
21113:         }
21114:     }
21115:     return;
21116: }
21117: 
21118: // ]]>
21119: </script>
21120: 
21121: END
21122: 
21123: }
21124: 
21125: sub captcha_phrases {
21126:     return &Apache::lonlocal::texthash (
21127:                  priv => 'Private key',
21128:                  pub  => 'Public key',
21129:                  original  => 'original (CAPTCHA)',
21130:                  recaptcha => 'successor (ReCAPTCHA)',
21131:                  notused   => 'unused',
21132:                  ver => 'ReCAPTCHA version (1 or 2)',
21133:     );
21134: }
21135: 
21136: sub devalidate_remote_domconfs {
21137:     my ($dom,$cachekeys) = @_;
21138:     return unless (ref($cachekeys) eq 'HASH');
21139:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
21140:     my %thismachine;
21141:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
21142:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions',
21143:                       'directorysrch','passwdconf','cats');
21144:     if (keys(%servers)) {
21145:         foreach my $server (keys(%servers)) {
21146:             next if ($thismachine{$server});
21147:             my @cached;
21148:             foreach my $name (@posscached) {
21149:                 if ($cachekeys->{$name}) {
21150:                     push(@cached,&escape($name).':'.&escape($dom));
21151:                 }
21152:             }
21153:             if (@cached) {
21154:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
21155:             }
21156:         }
21157:     }
21158:     return;
21159: }
21160: 
21161: 1;

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