File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.311: download - view: text, annotated - select for diffs
Fri Aug 25 00:04:29 2017 UTC (6 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Add missing space.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.311 2017/08/25 00:04:29 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, and placement).  
  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'],$dom);
  223:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
  224:         my %encconfig =
  225:             &Apache::lonnet::get_dom('encconfig',['ltitools'],$dom);
  226:         if (ref($encconfig{'ltitools'}) eq 'HASH') {
  227:             foreach my $id (keys(%{$domconfig{'ltitools'}})) {
  228:                 if (ref($domconfig{'ltitools'}{$id}) eq 'HASH') {
  229:                     foreach my $item ('key','secret') {
  230:                         $domconfig{'ltitools'}{$id}{$item} = $encconfig{'ltitools'}{$id}{$item};
  231:                     }
  232:                 }
  233:             }
  234:         }
  235:     }
  236:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  237:                        'autoupdate','autocreate','directorysrch','contacts',
  238:                        'usercreation','selfcreation','usermodification','scantron',
  239:                        'requestcourses','requestauthor','coursecategories',
  240:                        'serverstatuses','helpsettings','coursedefaults',
  241:                        'ltitools','selfenrollment','usersessions','ssl','trust');
  242:     my %existing;
  243:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  244:         %existing = %{$domconfig{'loadbalancing'}};
  245:     }
  246:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  247:         push(@prefs_order,'loadbalancing');
  248:     }
  249:     my %prefs = (
  250:         'rolecolors' =>
  251:                    { text => 'Default color schemes',
  252:                      help => 'Domain_Configuration_Color_Schemes',
  253:                      header => [{col1 => 'Student Settings',
  254:                                  col2 => '',},
  255:                                 {col1 => 'Coordinator Settings',
  256:                                  col2 => '',},
  257:                                 {col1 => 'Author Settings',
  258:                                  col2 => '',},
  259:                                 {col1 => 'Administrator Settings',
  260:                                  col2 => '',}],
  261:                       print => \&print_rolecolors,
  262:                       modify => \&modify_rolecolors,
  263:                     },
  264:         'login' =>
  265:                     { text => 'Log-in page options',
  266:                       help => 'Domain_Configuration_Login_Page',
  267:                       header => [{col1 => 'Log-in Page Items',
  268:                                   col2 => '',},
  269:                                  {col1 => 'Log-in Help',
  270:                                   col2 => 'Value'},
  271:                                  {col1 => 'Custom HTML in document head',
  272:                                   col2 => 'Value'}],
  273:                       print => \&print_login,
  274:                       modify => \&modify_login,
  275:                     },
  276:         'defaults' => 
  277:                     { text => 'Default authentication/language/timezone/portal/types',
  278:                       help => 'Domain_Configuration_LangTZAuth',
  279:                       header => [{col1 => 'Setting',
  280:                                   col2 => 'Value'},
  281:                                  {col1 => 'Internal Authentication',
  282:                                   col2 => 'Value'},
  283:                                  {col1 => 'Institutional user types',
  284:                                   col2 => 'Name displayed'}],
  285:                       print => \&print_defaults,
  286:                       modify => \&modify_defaults,
  287:                     },
  288:         'quotas' => 
  289:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  290:                       help => 'Domain_Configuration_Quotas',
  291:                       header => [{col1 => 'User affiliation',
  292:                                   col2 => 'Available tools',
  293:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  294:                       print => \&print_quotas,
  295:                       modify => \&modify_quotas,
  296:                     },
  297:         'autoenroll' =>
  298:                    { text => 'Auto-enrollment settings',
  299:                      help => 'Domain_Configuration_Auto_Enrollment',
  300:                      header => [{col1 => 'Configuration setting',
  301:                                  col2 => 'Value(s)'}],
  302:                      print => \&print_autoenroll,
  303:                      modify => \&modify_autoenroll,
  304:                    },
  305:         'autoupdate' => 
  306:                    { text => 'Auto-update settings',
  307:                      help => 'Domain_Configuration_Auto_Updates',
  308:                      header => [{col1 => 'Setting',
  309:                                  col2 => 'Value',},
  310:                                 {col1 => 'Setting',
  311:                                  col2 => 'Affiliation'},
  312:                                 {col1 => 'User population',
  313:                                  col2 => 'Updatable user data'}],
  314:                      print => \&print_autoupdate,
  315:                      modify => \&modify_autoupdate,
  316:                   },
  317:         'autocreate' => 
  318:                   { text => 'Auto-course creation settings',
  319:                      help => 'Domain_Configuration_Auto_Creation',
  320:                      header => [{col1 => 'Configuration Setting',
  321:                                  col2 => 'Value',}],
  322:                      print => \&print_autocreate,
  323:                      modify => \&modify_autocreate,
  324:                   },
  325:         'directorysrch' => 
  326:                   { text => 'Directory searches',
  327:                     help => 'Domain_Configuration_InstDirectory_Search',
  328:                     header => [{col1 => 'Institutional Directory Setting',
  329:                                 col2 => 'Value',},
  330:                                {col1 => 'LON-CAPA Directory Setting',
  331:                                 col2 => 'Value',}],
  332:                     print => \&print_directorysrch,
  333:                     modify => \&modify_directorysrch,
  334:                   },
  335:         'contacts' =>
  336:                   { text => 'E-mail addresses and helpform',
  337:                     help => 'Domain_Configuration_Contact_Info',
  338:                     header => [{col1 => 'Default e-mail addresses',
  339:                                 col2 => 'Value',},
  340:                                {col1 => 'Recipient(s) for notifications',
  341:                                 col2 => 'Value',},
  342:                                {col1 => 'Ask helpdesk form settings',
  343:                                 col2 => 'Value',},],
  344:                     print => \&print_contacts,
  345:                     modify => \&modify_contacts,
  346:                   },
  347:         'usercreation' => 
  348:                   { text => 'User creation',
  349:                     help => 'Domain_Configuration_User_Creation',
  350:                     header => [{col1 => 'Format rule type',
  351:                                 col2 => 'Format rules in force'},
  352:                                {col1 => 'User account creation',
  353:                                 col2 => 'Usernames which may be created',},
  354:                                {col1 => 'Context',
  355:                                 col2 => 'Assignable authentication types'}],
  356:                     print => \&print_usercreation,
  357:                     modify => \&modify_usercreation,
  358:                   },
  359:         'selfcreation' => 
  360:                   { text => 'Users self-creating accounts',
  361:                     help => 'Domain_Configuration_Self_Creation', 
  362:                     header => [{col1 => 'Self-creation with institutional username',
  363:                                 col2 => 'Enabled?'},
  364:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  365:                                 col2 => 'Information user can enter'},
  366:                                {col1 => 'Self-creation with e-mail verification',
  367:                                 col2 => 'Settings'}],
  368:                     print => \&print_selfcreation,
  369:                     modify => \&modify_selfcreation,
  370:                   },
  371:         'usermodification' =>
  372:                   { text => 'User modification',
  373:                     help => 'Domain_Configuration_User_Modification',
  374:                     header => [{col1 => 'Target user has role',
  375:                                 col2 => 'User information updatable in author context'},
  376:                                {col1 => 'Target user has role',
  377:                                 col2 => 'User information updatable in course context'}],
  378:                     print => \&print_usermodification,
  379:                     modify => \&modify_usermodification,
  380:                   },
  381:         'scantron' =>
  382:                   { text => 'Bubblesheet format file',
  383:                     help => 'Domain_Configuration_Scantron_Format',
  384:                     header => [ {col1 => 'Item',
  385:                                  col2 => '',
  386:                               }],
  387:                     print => \&print_scantron,
  388:                     modify => \&modify_scantron,
  389:                   },
  390:         'requestcourses' => 
  391:                  {text => 'Request creation of courses',
  392:                   help => 'Domain_Configuration_Request_Courses',
  393:                   header => [{col1 => 'User affiliation',
  394:                               col2 => 'Availability/Processing of requests',},
  395:                              {col1 => 'Setting',
  396:                               col2 => 'Value'},
  397:                              {col1 => 'Available textbooks',
  398:                               col2 => ''},
  399:                              {col1 => 'Available templates',
  400:                               col2 => ''},
  401:                              {col1 => 'Validation (not official courses)',
  402:                               col2 => 'Value'},],
  403:                   print => \&print_quotas,
  404:                   modify => \&modify_quotas,
  405:                  },
  406:         'requestauthor' =>
  407:                  {text => 'Request Authoring Space',
  408:                   help => 'Domain_Configuration_Request_Author',
  409:                   header => [{col1 => 'User affiliation',
  410:                               col2 => 'Availability/Processing of requests',},
  411:                              {col1 => 'Setting',
  412:                               col2 => 'Value'}],
  413:                   print => \&print_quotas,
  414:                   modify => \&modify_quotas,
  415:                  },
  416:         'coursecategories' =>
  417:                   { text => 'Cataloging of courses/communities',
  418:                     help => 'Domain_Configuration_Cataloging_Courses',
  419:                     header => [{col1 => 'Catalog type/availability',
  420:                                 col2 => '',},
  421:                                {col1 => 'Category settings for standard catalog',
  422:                                 col2 => '',},
  423:                                {col1 => 'Categories',
  424:                                 col2 => '',
  425:                                }],
  426:                     print => \&print_coursecategories,
  427:                     modify => \&modify_coursecategories,
  428:                   },
  429:         'serverstatuses' =>
  430:                  {text   => 'Access to server status pages',
  431:                   help   => 'Domain_Configuration_Server_Status',
  432:                   header => [{col1 => 'Status Page',
  433:                               col2 => 'Other named users',
  434:                               col3 => 'Specific IPs',
  435:                             }],
  436:                   print => \&print_serverstatuses,
  437:                   modify => \&modify_serverstatuses,
  438:                  },
  439:         'helpsettings' =>
  440:                  {text   => 'Support settings',
  441:                   help   => 'Domain_Configuration_Help_Settings',
  442:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  443:                               col2 => 'Value'},
  444:                              {col1 => 'Helpdesk Roles',
  445:                               col2 => 'Settings'},],
  446:                   print  => \&print_helpsettings,
  447:                   modify => \&modify_helpsettings,
  448:                  },
  449:         'coursedefaults' => 
  450:                  {text => 'Course/Community defaults',
  451:                   help => 'Domain_Configuration_Course_Defaults',
  452:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  453:                               col2 => 'Value',},
  454:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  455:                               col2 => 'Value',},],
  456:                   print => \&print_coursedefaults,
  457:                   modify => \&modify_coursedefaults,
  458:                  },
  459:         'selfenrollment' => 
  460:                  {text   => 'Self-enrollment in Course/Community',
  461:                   help   => 'Domain_Configuration_Selfenrollment',
  462:                   header => [{col1 => 'Configuration Rights',
  463:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  464:                              {col1 => 'Defaults',
  465:                               col2 => 'Value'},
  466:                              {col1 => 'Self-enrollment validation (optional)',
  467:                               col2 => 'Value'},],
  468:                   print => \&print_selfenrollment,
  469:                   modify => \&modify_selfenrollment,
  470:                  },
  471:         'privacy' => 
  472:                  {text   => 'User Privacy',
  473:                   help   => 'Domain_Configuration_User_Privacy',
  474:                   header => [{col1 => 'Setting',
  475:                               col2 => 'Value',}],
  476:                   print => \&print_privacy,
  477:                   modify => \&modify_privacy,
  478:                  },
  479:         'usersessions' =>
  480:                  {text  => 'User session hosting/offloading',
  481:                   help  => 'Domain_Configuration_User_Sessions',
  482:                   header => [{col1 => 'Domain server',
  483:                               col2 => 'Servers to offload sessions to when busy'},
  484:                              {col1 => 'Hosting of users from other domains',
  485:                               col2 => 'Rules'},
  486:                              {col1 => "Hosting domain's own users elsewhere",
  487:                               col2 => 'Rules'}],
  488:                   print => \&print_usersessions,
  489:                   modify => \&modify_usersessions,
  490:                  },
  491:         'loadbalancing' =>
  492:                  {text  => 'Dedicated Load Balancer(s)',
  493:                   help  => 'Domain_Configuration_Load_Balancing',
  494:                   header => [{col1 => 'Balancers',
  495:                               col2 => 'Default destinations',
  496:                               col3 => 'User affiliation',
  497:                               col4 => 'Overrides'},
  498:                             ],
  499:                   print => \&print_loadbalancing,
  500:                   modify => \&modify_loadbalancing,
  501:                  },
  502:         'ltitools' => 
  503:                  {text => 'External Tools (LTI)',
  504:                   help => 'Domain_Configuration_LTI_Tools',
  505:                   header => [{col1 => 'Setting',
  506:                               col2 => 'Value',}],
  507:                   print => \&print_ltitools,
  508:                   modify => \&modify_ltitools,
  509:                  },
  510:         'ssl' =>
  511:                  {text  => 'LON-CAPA Network (SSL)',
  512:                   help  => 'Domain_Configuration_Network_SSL',
  513:                   header => [{col1 => 'Server',
  514:                               col2 => 'Certificate Status'},
  515:                              {col1 => 'Connections to other servers',
  516:                               col2 => 'Rules'},
  517:                              {col1 => 'Connections from other servers',
  518:                               col2 => 'Rules'},
  519:                              {col1 => "Replicating domain's published content",
  520:                               col2 => 'Rules'}],
  521:                   print => \&print_ssl,
  522:                   modify => \&modify_ssl,
  523:                  },
  524:         'trust' =>
  525:                  {text   => 'Trust Settings',
  526:                   help   => 'Domain_Configuration_Trust',
  527:                   header => [{col1 => "Access to this domain's content by others",
  528:                               col2 => 'Rules'},
  529:                              {col1 => "Access to other domain's content by this domain",
  530:                               col2 => 'Rules'},
  531:                              {col1 => "Enrollment in this domain's courses by others",
  532:                               col2 => 'Rules',},
  533:                              {col1 => "Co-author roles in this domain for others",
  534:                               col2 => 'Rules',},
  535:                              {col1 => "Co-author roles for this domain's users elsewhere",
  536:                               col2 => 'Rules',},
  537:                              {col1 => "Domain roles in this domain assignable to others",
  538:                               col2 => 'Rules'},
  539:                              {col1 => "Course catalog for this domain displayed elsewhere",
  540:                               col2 => 'Rules'},
  541:                              {col1 => "Requests for creation of courses in this domain by others",
  542:                               col2 => 'Rules'},
  543:                              {col1 => "Users in other domains can send messages to this domain",
  544:                               col2 => 'Rules'},],
  545:                   print => \&print_trust,
  546:                   modify => \&modify_trust,
  547:                  },
  548:     );
  549:     if (keys(%servers) > 1) {
  550:         $prefs{'login'}  = { text   => 'Log-in page options',
  551:                              help   => 'Domain_Configuration_Login_Page',
  552:                             header => [{col1 => 'Log-in Service',
  553:                                         col2 => 'Server Setting',},
  554:                                        {col1 => 'Log-in Page Items',
  555:                                         col2 => ''},
  556:                                        {col1 => 'Log-in Help',
  557:                                         col2 => 'Value'},
  558:                                        {col1 => 'Custom HTML in document head',
  559:                                         col2 => 'Value'}],
  560:                             print => \&print_login,
  561:                             modify => \&modify_login,
  562:                            };
  563:     }
  564: 
  565:     my @roles = ('student','coordinator','author','admin');
  566:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  567:     &Apache::lonhtmlcommon::add_breadcrumb
  568:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  569:       text=>"Settings to display/modify"});
  570:     my $confname = $dom.'-domainconfig';
  571: 
  572:     if ($phase eq 'process') {
  573:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  574:                                                               \%prefs,\%domconfig,$confname,\@roles);
  575:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  576:             $r->rflush();
  577:             &devalidate_remote_domconfs($dom,$result);
  578:         }
  579:     } elsif ($phase eq 'display') {
  580:         my $js = &recaptcha_js().
  581:                  &toggle_display_js();
  582:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  583:             my ($othertitle,$usertypes,$types) =
  584:                 &Apache::loncommon::sorted_inst_types($dom);
  585:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  586:                                           $domconfig{'loadbalancing'}).
  587:                    &new_spares_js().
  588:                    &common_domprefs_js().
  589:                    &Apache::loncommon::javascript_array_indexof();
  590:         }
  591:         if (grep(/^requestcourses$/,@actions)) {
  592:             my $javascript_validations;
  593:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  594:             $js .= <<END;
  595: <script type="text/javascript">
  596: $javascript_validations
  597: </script>
  598: $coursebrowserjs
  599: END
  600:         }
  601:         if (grep(/^selfcreation$/,@actions)) {
  602:             $js .= &selfcreate_javascript();
  603:         }
  604:         if (grep(/^contacts$/,@actions)) {
  605:             $js .= &contacts_javascript();
  606:         }
  607:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  608:     } else {
  609: # check if domconfig user exists for the domain.
  610:         my $servadm = $r->dir_config('lonAdmEMail');
  611:         my ($configuserok,$author_ok,$switchserver) =
  612:             &config_check($dom,$confname,$servadm);
  613:         unless ($configuserok eq 'ok') {
  614:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  615:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  616:                           $confname).
  617:                       '<br />'
  618:             );
  619:             if ($switchserver) {
  620:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  621:                           '<br />'.
  622:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  623:                           '<br />'.
  624:                           &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).
  625:                           '<br />'.
  626:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  627:                 );
  628:             } else {
  629:                 $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.').
  630:                           '<br />'.
  631:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  632:                 );
  633:             }
  634:             $r->print(&Apache::loncommon::end_page());
  635:             return OK;
  636:         }
  637:         if (keys(%domconfig) == 0) {
  638:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  639:             my @ids=&Apache::lonnet::current_machine_ids();
  640:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  641:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  642:                 my @loginimages = ('img','logo','domlogo','login');
  643:                 my $custom_img_count = 0;
  644:                 foreach my $img (@loginimages) {
  645:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  646:                         $custom_img_count ++;
  647:                     }
  648:                 }
  649:                 foreach my $role (@roles) {
  650:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  651:                         $custom_img_count ++;
  652:                     }
  653:                 }
  654:                 if ($custom_img_count > 0) {
  655:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  656:                     my $switch_server = &check_switchserver($dom,$confname);
  657:                     $r->print(
  658:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  659:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  660:     &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 />'.
  661:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  662:                     if ($switch_server) {
  663:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  664:                     }
  665:                     $r->print(&Apache::loncommon::end_page());
  666:                     return OK;
  667:                 }
  668:             }
  669:         }
  670:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  671:     }
  672:     return OK;
  673: }
  674: 
  675: sub process_changes {
  676:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  677:     my %domconfig;
  678:     if (ref($values) eq 'HASH') {
  679:         %domconfig = %{$values};
  680:     }
  681:     my $output;
  682:     if ($action eq 'login') {
  683:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  684:     } elsif ($action eq 'rolecolors') {
  685:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  686:                                      $lastactref,%domconfig);
  687:     } elsif ($action eq 'quotas') {
  688:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  689:     } elsif ($action eq 'autoenroll') {
  690:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  691:     } elsif ($action eq 'autoupdate') {
  692:         $output = &modify_autoupdate($dom,%domconfig);
  693:     } elsif ($action eq 'autocreate') {
  694:         $output = &modify_autocreate($dom,%domconfig);
  695:     } elsif ($action eq 'directorysrch') {
  696:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  697:     } elsif ($action eq 'usercreation') {
  698:         $output = &modify_usercreation($dom,%domconfig);
  699:     } elsif ($action eq 'selfcreation') {
  700:         $output = &modify_selfcreation($dom,$lastactref,%domconfig);
  701:     } elsif ($action eq 'usermodification') {
  702:         $output = &modify_usermodification($dom,%domconfig);
  703:     } elsif ($action eq 'contacts') {
  704:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  705:     } elsif ($action eq 'defaults') {
  706:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  707:     } elsif ($action eq 'scantron') {
  708:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  709:     } elsif ($action eq 'coursecategories') {
  710:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  711:     } elsif ($action eq 'serverstatuses') {
  712:         $output = &modify_serverstatuses($dom,%domconfig);
  713:     } elsif ($action eq 'requestcourses') {
  714:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  715:     } elsif ($action eq 'requestauthor') {
  716:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  717:     } elsif ($action eq 'helpsettings') {
  718:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  719:     } elsif ($action eq 'coursedefaults') {
  720:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  721:     } elsif ($action eq 'selfenrollment') {
  722:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  723:     } elsif ($action eq 'usersessions') {
  724:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  725:     } elsif ($action eq 'loadbalancing') {
  726:         $output = &modify_loadbalancing($dom,%domconfig);
  727:     } elsif ($action eq 'ltitools') {
  728:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  729:     } elsif ($action eq 'ssl') {
  730:         $output = &modify_ssl($dom,$lastactref,%domconfig);
  731:     } elsif ($action eq 'trust') {
  732:         $output = &modify_trust($dom,$lastactref,%domconfig);
  733:     }
  734:     return $output;
  735: }
  736: 
  737: sub print_config_box {
  738:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  739:     my $rowtotal = 0;
  740:     my $output;
  741:     if ($action eq 'coursecategories') {
  742:         $output = &coursecategories_javascript($settings);
  743:     } elsif ($action eq 'defaults') {
  744:         $output = &defaults_javascript($settings); 
  745:     } elsif ($action eq 'helpsettings') {
  746:         my (%privs,%levelscurrent);
  747:         my %full=();
  748:         my %levels=(
  749:                      course => {},
  750:                      domain => {},
  751:                      system => {},
  752:                    );
  753:         my $context = 'domain';
  754:         my $crstype = 'Course';
  755:         my $formname = 'display';
  756:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  757:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  758:         $output =
  759:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full, 
  760:                                                       \@templateroles);
  761:     }
  762:     $output .=
  763:          '<table class="LC_nested_outer">
  764:           <tr>
  765:            <th class="LC_left_item LC_middle"><span class="LC_nobreak">'.
  766:            &mt($item->{text}).'&nbsp;'.
  767:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  768:           '</tr>';
  769:     $rowtotal ++;
  770:     my $numheaders = 1;
  771:     if (ref($item->{'header'}) eq 'ARRAY') {
  772:         $numheaders = scalar(@{$item->{'header'}});
  773:     }
  774:     if ($numheaders > 1) {
  775:         my $colspan = '';
  776:         my $rightcolspan = '';
  777:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  778:             ($action eq 'directorysrch') ||
  779:             (($action eq 'login') && ($numheaders < 4))) {
  780:             $colspan = ' colspan="2"';
  781:         }
  782:         if ($action eq 'usersessions') {
  783:             $rightcolspan = ' colspan="3"'; 
  784:         }
  785:         $output .= '
  786:           <tr>
  787:            <td>
  788:             <table class="LC_nested">
  789:              <tr class="LC_info_row">
  790:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  791:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  792:              </tr>';
  793:         $rowtotal ++;
  794:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  795:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  796:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'ssl') ||
  797:             ($action eq 'directorysrch') || ($action eq 'trust') || ($action eq 'helpsettings') ||
  798:             ($action eq 'contacts')) {
  799:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  800:         } elsif ($action eq 'coursecategories') {
  801:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  802:         } elsif ($action eq 'login') {
  803:             if ($numheaders == 4) {
  804:                 $colspan = ' colspan="2"';
  805:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  806:             } else {
  807:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  808:             }
  809:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  810:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  811:         } elsif ($action eq 'rolecolors') {
  812:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  813:         }
  814:         $output .= '
  815:            </table>
  816:           </td>
  817:          </tr>
  818:          <tr>
  819:            <td>
  820:             <table class="LC_nested">
  821:              <tr class="LC_info_row">
  822:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  823:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  824:              </tr>';
  825:             $rowtotal ++;
  826:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  827:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  828:             ($action eq 'usersessions') || ($action eq 'coursecategories') || 
  829:             ($action eq 'trust') || ($action eq 'contacts') || ($action eq 'defaults')) {
  830:             if ($action eq 'coursecategories') {
  831:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  832:                 $colspan = ' colspan="2"';
  833:             } elsif ($action eq 'trust') {
  834:                 $output .= $item->{'print'}->('shared',$dom,$settings,\$rowtotal);
  835:             } else {
  836:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  837:             }
  838:             if ($action eq 'trust') {
  839:                 $output .= '
  840:             </table>
  841:           </td>
  842:          </tr>';
  843:                 my @trusthdrs = qw(2 3 4 5 6 7);
  844:                 my @prefixes = qw(enroll othcoau coaurem domroles catalog reqcrs);
  845:                 for (my $i=0; $i<@trusthdrs; $i++) {
  846:                     $output .= '
  847:          <tr>
  848:            <td>
  849:             <table class="LC_nested">
  850:              <tr class="LC_info_row">
  851:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col1'}).'</td>
  852:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col2'}).'</td></tr>'.
  853:                            $item->{'print'}->($prefixes[$i],$dom,$settings,\$rowtotal).'
  854:             </table>
  855:           </td>
  856:          </tr>';
  857:                 }
  858:                 $output .= '
  859:          <tr>
  860:            <td>
  861:             <table class="LC_nested">
  862:              <tr class="LC_info_row">
  863:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col1'}).'</td>
  864:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col2'}).'</td></tr>'.
  865:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  866:             } else {
  867:                 $output .= '
  868:            </table>
  869:           </td>
  870:          </tr>
  871:          <tr>
  872:            <td>
  873:             <table class="LC_nested">
  874:              <tr class="LC_info_row">
  875:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  876:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  877:              </tr>'."\n";
  878:                 if ($action eq 'coursecategories') {
  879:                     $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  880:                 } else {
  881:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  882:                 }
  883:             }
  884:             $rowtotal ++;
  885:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  886:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
  887:                  ($action eq 'helpsettings')) {
  888:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  889:         } elsif ($action eq 'ssl') {
  890:             $output .= $item->{'print'}->('connto',$dom,$settings,\$rowtotal).'
  891:             </table>
  892:           </td>
  893:          </tr>
  894:          <tr>
  895:            <td>
  896:             <table class="LC_nested">
  897:              <tr class="LC_info_row">
  898:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  899:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  900:                            $item->{'print'}->('connfrom',$dom,$settings,\$rowtotal).'
  901:             </table>
  902:           </td>
  903:          </tr>
  904:          <tr>
  905:            <td>
  906:             <table class="LC_nested">
  907:              <tr class="LC_info_row">
  908:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  909:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'.
  910:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  911:         } elsif ($action eq 'login') {
  912:             if ($numheaders == 4) {
  913:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  914:            </table>
  915:           </td>
  916:          </tr>
  917:          <tr>
  918:            <td>
  919:             <table class="LC_nested">
  920:              <tr class="LC_info_row">
  921:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  922:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  923:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  924:                 $rowtotal ++;
  925:             } else {
  926:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  927:             }
  928:             $output .= '
  929:            </table>
  930:           </td>
  931:          </tr>
  932:          <tr>
  933:            <td>
  934:             <table class="LC_nested">
  935:              <tr class="LC_info_row">';
  936:             if ($numheaders == 4) {
  937:                 $output .= '
  938:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  939:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  940:              </tr>';
  941:             } else {
  942:                 $output .= '
  943:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  944:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  945:              </tr>';
  946:             }
  947:             $rowtotal ++;
  948:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
  949:         } elsif ($action eq 'requestcourses') {
  950:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  951:             $rowtotal ++;
  952:             $output .= &print_studentcode($settings,\$rowtotal).'
  953:            </table>
  954:           </td>
  955:          </tr>
  956:          <tr>
  957:            <td>
  958:             <table class="LC_nested">
  959:              <tr class="LC_info_row">
  960:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  961:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  962:                        &textbookcourses_javascript($settings).
  963:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  964:             </table>
  965:            </td>
  966:           </tr>
  967:          <tr>
  968:            <td>
  969:             <table class="LC_nested">
  970:              <tr class="LC_info_row">
  971:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  972:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  973:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  974:             </table>
  975:            </td>
  976:           </tr>
  977:           <tr>
  978:            <td>
  979:             <table class="LC_nested">
  980:              <tr class="LC_info_row">
  981:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  982:               <td class="LC_right_item" style="vertical-align: top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  983:              </tr>'.
  984:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  985:         } elsif ($action eq 'requestauthor') {
  986:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  987:             $rowtotal ++;
  988:         } elsif ($action eq 'rolecolors') {
  989:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  990:            </table>
  991:           </td>
  992:          </tr>
  993:          <tr>
  994:            <td>
  995:             <table class="LC_nested">
  996:              <tr class="LC_info_row">
  997:               <td class="LC_left_item"'.$colspan.' style="vertical-align: top">'.
  998:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  999:               <td class="LC_right_item" style="vertical-align: top">'.
 1000:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
 1001:              </tr>'.
 1002:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
 1003:            </table>
 1004:           </td>
 1005:          </tr>
 1006:          <tr>
 1007:            <td>
 1008:             <table class="LC_nested">
 1009:              <tr class="LC_info_row">
 1010:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1011:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1012:              </tr>'.
 1013:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
 1014:             $rowtotal += 2;
 1015:         }
 1016:     } else {
 1017:         $output .= '
 1018:           <tr>
 1019:            <td>
 1020:             <table class="LC_nested">
 1021:              <tr class="LC_info_row">';
 1022:         if ($action eq 'login') {
 1023:             $output .= '  
 1024:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1025:         } elsif ($action eq 'serverstatuses') {
 1026:             $output .= '
 1027:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).
 1028:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
 1029: 
 1030:         } else {
 1031:             $output .= '
 1032:               <td class="LC_left_item" style="vertical-align: top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1033:         }
 1034:         if (defined($item->{'header'}->[0]->{'col3'})) {
 1035:             $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1036:                        &mt($item->{'header'}->[0]->{'col2'});
 1037:             if ($action eq 'serverstatuses') {
 1038:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
 1039:             } 
 1040:         } else {
 1041:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1042:                        &mt($item->{'header'}->[0]->{'col2'});
 1043:         }
 1044:         $output .= '</td>';
 1045:         if ($item->{'header'}->[0]->{'col3'}) {
 1046:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1047:                 $output .= '<td class="LC_left_item" style="vertical-align: top">'.
 1048:                             &mt($item->{'header'}->[0]->{'col3'});
 1049:             } else {
 1050:                 $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1051:                            &mt($item->{'header'}->[0]->{'col3'});
 1052:             }
 1053:             if ($action eq 'serverstatuses') {
 1054:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1055:             }
 1056:             $output .= '</td>';
 1057:         }
 1058:         if ($item->{'header'}->[0]->{'col4'}) {
 1059:             $output .= '<td class="LC_right_item" style="vertical-align: top">'.
 1060:                        &mt($item->{'header'}->[0]->{'col4'});
 1061:         }
 1062:         $output .= '</tr>';
 1063:         $rowtotal ++;
 1064:         if ($action eq 'quotas') {
 1065:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1066:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1067:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') || 
 1068:                  ($action eq 'ltitools')) {
 1069:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1070:         } elsif ($action eq 'scantron') {
 1071:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
 1072:         }
 1073:     }
 1074:     $output .= '
 1075:    </table>
 1076:   </td>
 1077:  </tr>
 1078: </table><br />';
 1079:     return ($output,$rowtotal);
 1080: }
 1081: 
 1082: sub print_login {
 1083:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1084:     my ($css_class,$datatable);
 1085:     my %choices = &login_choices();
 1086: 
 1087:     if ($caller eq 'service') {
 1088:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1089:         my $choice = $choices{'disallowlogin'};
 1090:         $css_class = ' class="LC_odd_row"';
 1091:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1092:                       '<td style="text-align: right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1093:                       '<th>'.$choices{'server'}.'</th>'.
 1094:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1095:                       '<th>'.$choices{'custompath'}.'</th>'.
 1096:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1097:         my %disallowed;
 1098:         if (ref($settings) eq 'HASH') {
 1099:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1100:                %disallowed = %{$settings->{'loginvia'}};
 1101:             }
 1102:         }
 1103:         foreach my $lonhost (sort(keys(%servers))) {
 1104:             my $direct = 'selected="selected"';
 1105:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1106:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1107:                     $direct = '';
 1108:                 }
 1109:             }
 1110:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1111:                           '<td><select name="'.$lonhost.'_server">'.
 1112:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1113:                           '</option>';
 1114:             foreach my $hostid (sort(keys(%servers))) {
 1115:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1116:                 my $selected = '';
 1117:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1118:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1119:                         $selected = 'selected="selected"';
 1120:                     }
 1121:                 }
 1122:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1123:                               $servers{$hostid}.'</option>';
 1124:             }
 1125:             $datatable .= '</select></td>'.
 1126:                           '<td><select name="'.$lonhost.'_serverpath">';
 1127:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1128:                 my $pathname = $path;
 1129:                 if ($path eq 'custom') {
 1130:                     $pathname = &mt('Custom Path').' ->';
 1131:                 }
 1132:                 my $selected = '';
 1133:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1134:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1135:                         $selected = 'selected="selected"';
 1136:                     }
 1137:                 } elsif ($path eq '') {
 1138:                     $selected = 'selected="selected"';
 1139:                 }
 1140:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1141:             }
 1142:             $datatable .= '</select></td>';
 1143:             my ($custom,$exempt);
 1144:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1145:                 $custom = $disallowed{$lonhost}{'custompath'};
 1146:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1147:             }
 1148:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1149:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1150:                           '</tr>';
 1151:         }
 1152:         $datatable .= '</table></td></tr>';
 1153:         return $datatable;
 1154:     } elsif ($caller eq 'page') {
 1155:         my %defaultchecked = ( 
 1156:                                'coursecatalog' => 'on',
 1157:                                'helpdesk'      => 'on',
 1158:                                'adminmail'     => 'off',
 1159:                                'newuser'       => 'off',
 1160:                              );
 1161:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1162:         my (%checkedon,%checkedoff);
 1163:         foreach my $item (@toggles) {
 1164:             if ($defaultchecked{$item} eq 'on') { 
 1165:                 $checkedon{$item} = ' checked="checked" ';
 1166:                 $checkedoff{$item} = ' ';
 1167:             } elsif ($defaultchecked{$item} eq 'off') {
 1168:                 $checkedoff{$item} = ' checked="checked" ';
 1169:                 $checkedon{$item} = ' ';
 1170:             }
 1171:         }
 1172:         my @images = ('img','logo','domlogo','login');
 1173:         my @logintext = ('textcol','bgcol');
 1174:         my @bgs = ('pgbg','mainbg','sidebg');
 1175:         my @links = ('link','alink','vlink');
 1176:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1177:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1178:         my (%is_custom,%designs);
 1179:         my %defaults = (
 1180:                        font => $defaultdesign{'login.font'},
 1181:                        );
 1182:         foreach my $item (@images) {
 1183:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1184:             $defaults{'showlogo'}{$item} = 1;
 1185:         }
 1186:         foreach my $item (@bgs) {
 1187:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1188:         }
 1189:         foreach my $item (@logintext) {
 1190:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1191:         }
 1192:         foreach my $item (@links) {
 1193:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1194:         }
 1195:         if (ref($settings) eq 'HASH') {
 1196:             foreach my $item (@toggles) {
 1197:                 if ($settings->{$item} eq '1') {
 1198:                     $checkedon{$item} =  ' checked="checked" ';
 1199:                     $checkedoff{$item} = ' ';
 1200:                 } elsif ($settings->{$item} eq '0') {
 1201:                     $checkedoff{$item} =  ' checked="checked" ';
 1202:                     $checkedon{$item} = ' ';
 1203:                 }
 1204:             }
 1205:             foreach my $item (@images) {
 1206:                 if (defined($settings->{$item})) {
 1207:                     $designs{$item} = $settings->{$item};
 1208:                     $is_custom{$item} = 1;
 1209:                 }
 1210:                 if (defined($settings->{'showlogo'}{$item})) {
 1211:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1212:                 }
 1213:             }
 1214:             foreach my $item (@logintext) {
 1215:                 if ($settings->{$item} ne '') {
 1216:                     $designs{'logintext'}{$item} = $settings->{$item};
 1217:                     $is_custom{$item} = 1;
 1218:                 }
 1219:             }
 1220:             if ($settings->{'font'} ne '') {
 1221:                 $designs{'font'} = $settings->{'font'};
 1222:                 $is_custom{'font'} = 1;
 1223:             }
 1224:             foreach my $item (@bgs) {
 1225:                 if ($settings->{$item} ne '') {
 1226:                     $designs{'bgs'}{$item} = $settings->{$item};
 1227:                     $is_custom{$item} = 1;
 1228:                 }
 1229:             }
 1230:             foreach my $item (@links) {
 1231:                 if ($settings->{$item} ne '') {
 1232:                     $designs{'links'}{$item} = $settings->{$item};
 1233:                     $is_custom{$item} = 1;
 1234:                 }
 1235:             }
 1236:         } else {
 1237:             if ($designhash{$dom.'.login.font'} ne '') {
 1238:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1239:                 $is_custom{'font'} = 1;
 1240:             }
 1241:             foreach my $item (@images) {
 1242:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1243:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1244:                     $is_custom{$item} = 1;
 1245:                 }
 1246:             }
 1247:             foreach my $item (@bgs) {
 1248:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1249:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1250:                     $is_custom{$item} = 1;
 1251:                 }
 1252:             }
 1253:             foreach my $item (@links) {
 1254:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1255:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1256:                     $is_custom{$item} = 1;
 1257:                 }
 1258:             }
 1259:         }
 1260:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1261:                                                       logo => 'Institution Logo',
 1262:                                                       domlogo => 'Domain Logo',
 1263:                                                       login => 'Login box');
 1264:         my $itemcount = 1;
 1265:         foreach my $item (@toggles) {
 1266:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1267:             $datatable .=  
 1268:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1269:                 '</td><td>'.
 1270:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1271:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1272:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1273:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1274:                 '</tr>';
 1275:             $itemcount ++;
 1276:         }
 1277:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1278:         $datatable .= '</tr></table></td></tr>';
 1279:     } elsif ($caller eq 'help') {
 1280:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1281:         my $switchserver = &check_switchserver($dom,$confname);
 1282:         my $itemcount = 1;
 1283:         $defaulturl = '/adm/loginproblems.html';
 1284:         $defaulttype = 'default';
 1285:         %lt = &Apache::lonlocal::texthash (
 1286:                      del     => 'Delete?',
 1287:                      rep     => 'Replace:',
 1288:                      upl     => 'Upload:',
 1289:                      default => 'Default',
 1290:                      custom  => 'Custom',
 1291:                                              );
 1292:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1293:         my @currlangs;
 1294:         if (ref($settings) eq 'HASH') {
 1295:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1296:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1297:                     next if ($settings->{'helpurl'}{$key} eq '');
 1298:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1299:                     $type{$key} = 'custom';
 1300:                     unless ($key eq 'nolang') {
 1301:                         push(@currlangs,$key);
 1302:                     }
 1303:                 }
 1304:             } elsif ($settings->{'helpurl'} ne '') {
 1305:                 $type{'nolang'} = 'custom';
 1306:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1307:             }
 1308:         }
 1309:         foreach my $lang ('nolang',sort(@currlangs)) {
 1310:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1311:             $datatable .= '<tr'.$css_class.'>';
 1312:             if ($url{$lang} eq '') {
 1313:                 $url{$lang} = $defaulturl;
 1314:             }
 1315:             if ($type{$lang} eq '') {
 1316:                 $type{$lang} = $defaulttype;
 1317:             }
 1318:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1319:             if ($lang eq 'nolang') {
 1320:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1321:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1322:             } else {
 1323:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1324:                                   $langchoices{$lang},
 1325:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1326:             }
 1327:             $datatable .= '</span></td>'."\n".
 1328:                           '<td class="LC_left_item">';
 1329:             if ($type{$lang} eq 'custom') {
 1330:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1331:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1332:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1333:             } else {
 1334:                 $datatable .= $lt{'upl'};
 1335:             }
 1336:             $datatable .='<br />';
 1337:             if ($switchserver) {
 1338:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1339:             } else {
 1340:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1341:             }
 1342:             $datatable .= '</td></tr>';
 1343:             $itemcount ++;
 1344:         }
 1345:         my @addlangs;
 1346:         foreach my $lang (sort(keys(%langchoices))) {
 1347:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1348:             push(@addlangs,$lang);
 1349:         }
 1350:         if (@addlangs > 0) {
 1351:             my %toadd;
 1352:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1353:             $toadd{''} = &mt('Select');
 1354:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1355:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1356:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1357:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1358:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1359:             if ($switchserver) {
 1360:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1361:             } else {
 1362:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1363:             }
 1364:             $datatable .= '</td></tr>';
 1365:             $itemcount ++;
 1366:         }
 1367:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1368:     } elsif ($caller eq 'headtag') {
 1369:         my %domservers = &Apache::lonnet::get_servers($dom);
 1370:         my $choice = $choices{'headtag'};
 1371:         $css_class = ' class="LC_odd_row"';
 1372:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1373:                       '<td style="text-align: left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1374:                       '<th>'.$choices{'current'}.'</th>'.
 1375:                       '<th>'.$choices{'action'}.'</th>'.
 1376:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1377:         my (%currurls,%currexempt);
 1378:         if (ref($settings) eq 'HASH') {
 1379:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1380:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1381:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1382:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1383:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1384:                     }
 1385:                 }
 1386:             }
 1387:         }
 1388:         my %lt = &Apache::lonlocal::texthash(
 1389:                                                del  => 'Delete?',
 1390:                                                rep  => 'Replace:',
 1391:                                                upl  => 'Upload:',
 1392:                                                curr => 'View contents',
 1393:                                                none => 'None',
 1394:         );
 1395:         my $switchserver = &check_switchserver($dom,$confname);
 1396:         foreach my $lonhost (sort(keys(%domservers))) {
 1397:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1398:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1399:             if ($currurls{$lonhost}) {
 1400:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1401:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1402:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1403:                               '">'.$lt{'curr'}.'</a></td>'.
 1404:                               '<td><span class="LC_nobreak"><label>'.
 1405:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1406:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1407:             } else {
 1408:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1409:             }
 1410:             $datatable .='<br />';
 1411:             if ($switchserver) {
 1412:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1413:             } else {
 1414:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1415:             }
 1416:             $datatable .= '</td><td><input type="textbox" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1417:         }
 1418:         $datatable .= '</table></td></tr>';
 1419:     }
 1420:     return $datatable;
 1421: }
 1422: 
 1423: sub login_choices {
 1424:     my %choices =
 1425:         &Apache::lonlocal::texthash (
 1426:             coursecatalog => 'Display Course/Community Catalog link?',
 1427:             adminmail     => "Display Administrator's E-mail Address?",
 1428:             helpdesk      => 'Display "Contact Helpdesk" link',
 1429:             disallowlogin => "Login page requests redirected",
 1430:             hostid        => "Server",
 1431:             server        => "Redirect to:",
 1432:             serverpath    => "Path",
 1433:             custompath    => "Custom", 
 1434:             exempt        => "Exempt IP(s)",
 1435:             directlogin   => "No redirect",
 1436:             newuser       => "Link to create a user account",
 1437:             img           => "Header",
 1438:             logo          => "Main Logo",
 1439:             domlogo       => "Domain Logo",
 1440:             login         => "Log-in Header", 
 1441:             textcol       => "Text color",
 1442:             bgcol         => "Box color",
 1443:             bgs           => "Background colors",
 1444:             links         => "Link colors",
 1445:             font          => "Font color",
 1446:             pgbg          => "Header",
 1447:             mainbg        => "Page",
 1448:             sidebg        => "Login box",
 1449:             link          => "Link",
 1450:             alink         => "Active link",
 1451:             vlink         => "Visited link",
 1452:             headtag       => "Custom markup",
 1453:             action        => "Action",
 1454:             current       => "Current",
 1455:         );
 1456:     return %choices;
 1457: }
 1458: 
 1459: sub print_rolecolors {
 1460:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1461:     my %choices = &color_font_choices();
 1462:     my @bgs = ('pgbg','tabbg','sidebg');
 1463:     my @links = ('link','alink','vlink');
 1464:     my @images = ('img');
 1465:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1466:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1467:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1468:     my (%is_custom,%designs);
 1469:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1470:     if (ref($settings) eq 'HASH') {
 1471:         if (ref($settings->{$role}) eq 'HASH') {
 1472:             if ($settings->{$role}->{'img'} ne '') {
 1473:                 $designs{'img'} = $settings->{$role}->{'img'};
 1474:                 $is_custom{'img'} = 1;
 1475:             }
 1476:             if ($settings->{$role}->{'font'} ne '') {
 1477:                 $designs{'font'} = $settings->{$role}->{'font'};
 1478:                 $is_custom{'font'} = 1;
 1479:             }
 1480:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1481:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1482:                 $is_custom{'fontmenu'} = 1;
 1483:             }
 1484:             foreach my $item (@bgs) {
 1485:                 if ($settings->{$role}->{$item} ne '') {
 1486:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1487:                     $is_custom{$item} = 1;
 1488:                 }
 1489:             }
 1490:             foreach my $item (@links) {
 1491:                 if ($settings->{$role}->{$item} ne '') {
 1492:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1493:                     $is_custom{$item} = 1;
 1494:                 }
 1495:             }
 1496:         }
 1497:     } else {
 1498:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1499:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1500:             $is_custom{'img'} = 1;
 1501:         }
 1502:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1503:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1504:             $is_custom{'fontmenu'} = 1; 
 1505:         }
 1506:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1507:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1508:             $is_custom{'font'} = 1;
 1509:         }
 1510:         foreach my $item (@bgs) {
 1511:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1512:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1513:                 $is_custom{$item} = 1;
 1514:             
 1515:             }
 1516:         }
 1517:         foreach my $item (@links) {
 1518:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1519:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1520:                 $is_custom{$item} = 1;
 1521:             }
 1522:         }
 1523:     }
 1524:     my $itemcount = 1;
 1525:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1526:     $datatable .= '</tr></table></td></tr>';
 1527:     return $datatable;
 1528: }
 1529: 
 1530: sub role_defaults {
 1531:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1532:     my %defaults;
 1533:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1534:         return %defaults;
 1535:     }
 1536:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1537:     if ($role eq 'login') {
 1538:         %defaults = (
 1539:                        font => $defaultdesign{$role.'.font'},
 1540:                     );
 1541:         if (ref($logintext) eq 'ARRAY') {
 1542:             foreach my $item (@{$logintext}) {
 1543:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1544:             }
 1545:         }
 1546:         foreach my $item (@{$images}) {
 1547:             $defaults{'showlogo'}{$item} = 1;
 1548:         }
 1549:     } else {
 1550:         %defaults = (
 1551:                        img => $defaultdesign{$role.'.img'},
 1552:                        font => $defaultdesign{$role.'.font'},
 1553:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1554:                     );
 1555:     }
 1556:     foreach my $item (@{$bgs}) {
 1557:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1558:     }
 1559:     foreach my $item (@{$links}) {
 1560:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1561:     }
 1562:     foreach my $item (@{$images}) {
 1563:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1564:     }
 1565:     return %defaults;
 1566: }
 1567: 
 1568: sub display_color_options {
 1569:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1570:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1571:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1572:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1573:     my $datatable = '<tr'.$css_class.'>'.
 1574:         '<td>'.$choices->{'font'}.'</td>';
 1575:     if (!$is_custom->{'font'}) {
 1576:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1577:     } else {
 1578:         $datatable .= '<td>&nbsp;</td>';
 1579:     }
 1580:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1581: 
 1582:     $datatable .= '<td><span class="LC_nobreak">'.
 1583:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1584:                   ' value="'.$current_color.'" />&nbsp;'.
 1585:                   '&nbsp;</td></tr>';
 1586:     unless ($role eq 'login') { 
 1587:         $datatable .= '<tr'.$css_class.'>'.
 1588:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1589:         if (!$is_custom->{'fontmenu'}) {
 1590:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1591:         } else {
 1592:             $datatable .= '<td>&nbsp;</td>';
 1593:         }
 1594: 	$current_color = $designs->{'fontmenu'} ?
 1595: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1596:         $datatable .= '<td><span class="LC_nobreak">'.
 1597:                       '<input class="colorchooser" type="text" size="10" name="'
 1598: 		      .$role.'_fontmenu"'.
 1599:                       ' value="'.$current_color.'" />&nbsp;'.
 1600:                       '&nbsp;</td></tr>';
 1601:     }
 1602:     my $switchserver = &check_switchserver($dom,$confname);
 1603:     foreach my $img (@{$images}) {
 1604: 	$itemcount ++;
 1605:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1606:         $datatable .= '<tr'.$css_class.'>'.
 1607:                       '<td>'.$choices->{$img};
 1608:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1609:         if ($role eq 'login') {
 1610:             if ($img eq 'login') {
 1611:                 $login_hdr_pick =
 1612:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1613:                 $logincolors =
 1614:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1615:                                        $designs,$defaults);
 1616:             } elsif ($img ne 'domlogo') {
 1617:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1618:             }
 1619:         }
 1620:         $datatable .= '</td>';
 1621:         if ($designs->{$img} ne '') {
 1622:             $imgfile = $designs->{$img};
 1623: 	    $img_import = ($imgfile =~ m{^/adm/});
 1624:         } else {
 1625:             $imgfile = $defaults->{$img};
 1626:         }
 1627:         if ($imgfile) {
 1628:             my ($showfile,$fullsize);
 1629:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1630:                 my $urldir = $1;
 1631:                 my $filename = $2;
 1632:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1633:                 if (@info) {
 1634:                     my $thumbfile = 'tn-'.$filename;
 1635:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1636:                     if (@thumb) {
 1637:                         $showfile = $urldir.'/'.$thumbfile;
 1638:                     } else {
 1639:                         $showfile = $imgfile;
 1640:                     }
 1641:                 } else {
 1642:                     $showfile = '';
 1643:                 }
 1644:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1645:                 $showfile = $imgfile;
 1646:                 my $imgdir = $1;
 1647:                 my $filename = $2;
 1648:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1649:                     $showfile = "/$imgdir/tn-".$filename;
 1650:                 } else {
 1651:                     my $input = $londocroot.$imgfile;
 1652:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1653:                     if (!-e $output) {
 1654:                         my ($width,$height) = &thumb_dimensions();
 1655:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1656:                         if ($fullwidth ne '' && $fullheight ne '') {
 1657:                             if ($fullwidth > $width && $fullheight > $height) { 
 1658:                                 my $size = $width.'x'.$height;
 1659:                                 system("convert -sample $size $input $output");
 1660:                                 $showfile = "/$imgdir/tn-".$filename;
 1661:                             }
 1662:                         }
 1663:                     }
 1664:                 }
 1665:             }
 1666:             if ($showfile) {
 1667:                 if ($showfile =~ m{^/(adm|res)/}) {
 1668:                     if ($showfile =~ m{^/res/}) {
 1669:                         my $local_showfile =
 1670:                             &Apache::lonnet::filelocation('',$showfile);
 1671:                         &Apache::lonnet::repcopy($local_showfile);
 1672:                     }
 1673:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1674:                 }
 1675:                 if ($imgfile) {
 1676:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1677:                         if ($imgfile =~ m{^/res/}) {
 1678:                             my $local_imgfile =
 1679:                                 &Apache::lonnet::filelocation('',$imgfile);
 1680:                             &Apache::lonnet::repcopy($local_imgfile);
 1681:                         }
 1682:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1683:                     } else {
 1684:                         $fullsize = $imgfile;
 1685:                     }
 1686:                 }
 1687:                 $datatable .= '<td>';
 1688:                 if ($img eq 'login') {
 1689:                     $datatable .= $login_hdr_pick;
 1690:                 } 
 1691:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1692:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1693:             } else {
 1694:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1695:                               &mt('Upload:').'<br />';
 1696:             }
 1697:         } else {
 1698:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1699:                           &mt('Upload:').'<br />';
 1700:         }
 1701:         if ($switchserver) {
 1702:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1703:         } else {
 1704:             if ($img ne 'login') { # suppress file selection for Log-in header
 1705:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1706:             }
 1707:         }
 1708:         $datatable .= '</td></tr>';
 1709:     }
 1710:     $itemcount ++;
 1711:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1712:     $datatable .= '<tr'.$css_class.'>'.
 1713:                   '<td>'.$choices->{'bgs'}.'</td>';
 1714:     my $bgs_def;
 1715:     foreach my $item (@{$bgs}) {
 1716:         if (!$is_custom->{$item}) {
 1717:             $bgs_def .= '<td><span class="LC_nobreak">'.$choices->{$item}.'</span>&nbsp;<span id="css_default_'.$role.'_'.$item.'" style="background-color: '.$defaults->{'bgs'}{$item}.';">&nbsp;&nbsp;&nbsp;</span><br />'.$defaults->{'bgs'}{$item}.'</td>';
 1718:         }
 1719:     }
 1720:     if ($bgs_def) {
 1721:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1722:     } else {
 1723:         $datatable .= '<td>&nbsp;</td>';
 1724:     }
 1725:     $datatable .= '<td class="LC_right_item">'.
 1726:                   '<table border="0"><tr>';
 1727: 
 1728:     foreach my $item (@{$bgs}) {
 1729:         $datatable .= '<td style="text-align: center">'.$choices->{$item};
 1730: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1731:         if ($designs->{'bgs'}{$item}) {
 1732:             $datatable .= '&nbsp;';
 1733:         }
 1734:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1735:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1736:     }
 1737:     $datatable .= '</tr></table></td></tr>';
 1738:     $itemcount ++;
 1739:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1740:     $datatable .= '<tr'.$css_class.'>'.
 1741:                   '<td>'.$choices->{'links'}.'</td>';
 1742:     my $links_def;
 1743:     foreach my $item (@{$links}) {
 1744:         if (!$is_custom->{$item}) {
 1745:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1746:         }
 1747:     }
 1748:     if ($links_def) {
 1749:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1750:     } else {
 1751:         $datatable .= '<td>&nbsp;</td>';
 1752:     }
 1753:     $datatable .= '<td class="LC_right_item">'.
 1754:                   '<table border="0"><tr>';
 1755:     foreach my $item (@{$links}) {
 1756: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1757:         $datatable .= '<td style="text-align: center">'.$choices->{$item}."\n";
 1758:         if ($designs->{'links'}{$item}) {
 1759:             $datatable.='&nbsp;';
 1760:         }
 1761:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1762:                       '" /></td>';
 1763:     }
 1764:     $$rowtotal += $itemcount;
 1765:     return $datatable;
 1766: }
 1767: 
 1768: sub logo_display_options {
 1769:     my ($img,$defaults,$designs) = @_;
 1770:     my $checkedon;
 1771:     if (ref($defaults) eq 'HASH') {
 1772:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1773:             if ($defaults->{'showlogo'}{$img}) {
 1774:                 $checkedon = 'checked="checked" ';     
 1775:             }
 1776:         } 
 1777:     }
 1778:     if (ref($designs) eq 'HASH') {
 1779:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1780:             if (defined($designs->{'showlogo'}{$img})) {
 1781:                 if ($designs->{'showlogo'}{$img} == 0) {
 1782:                     $checkedon = '';
 1783:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1784:                     $checkedon = 'checked="checked" ';
 1785:                 }
 1786:             }
 1787:         }
 1788:     }
 1789:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1790:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1791:            &mt('show').'</label>'."\n";
 1792: }
 1793: 
 1794: sub login_header_options  {
 1795:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1796:     my $output = '';
 1797:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1798:         $output .= &mt('Text default(s):').'<br />';
 1799:         if (!$is_custom->{'textcol'}) {
 1800:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1801:                        '&nbsp;&nbsp;&nbsp;';
 1802:         }
 1803:         if (!$is_custom->{'bgcol'}) {
 1804:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1805:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1806:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1807:         }
 1808:         $output .= '<br />';
 1809:     }
 1810:     $output .='<br />';
 1811:     return $output;
 1812: }
 1813: 
 1814: sub login_text_colors {
 1815:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1816:     my $color_menu = '<table border="0"><tr>';
 1817:     foreach my $item (@{$logintext}) {
 1818:         $color_menu .= '<td style="text-align: center">'.$choices->{$item};
 1819:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1820:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1821:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1822:     }
 1823:     $color_menu .= '</tr></table><br />';
 1824:     return $color_menu;
 1825: }
 1826: 
 1827: sub image_changes {
 1828:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1829:     my $output;
 1830:     if ($img eq 'login') {
 1831:             # suppress image for Log-in header
 1832:     } elsif (!$is_custom) {
 1833:         if ($img ne 'domlogo') {
 1834:             $output .= &mt('Default image:').'<br />';
 1835:         } else {
 1836:             $output .= &mt('Default in use:').'<br />';
 1837:         }
 1838:     }
 1839:     if ($img eq 'login') { # suppress image for Log-in header
 1840:         $output .= '<td>'.$logincolors;
 1841:     } else {
 1842:         if ($img_import) {
 1843:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1844:         }
 1845:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1846:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1847:         if ($is_custom) {
 1848:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1849:                        '<input type="checkbox" name="'.
 1850:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1851:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1852:         } else {
 1853:             $output .= '<td class="LC_middle">'.$logincolors.&mt('Upload:').'<br />';
 1854:         }
 1855:     }
 1856:     return $output;
 1857: }
 1858: 
 1859: sub print_quotas {
 1860:     my ($dom,$settings,$rowtotal,$action) = @_;
 1861:     my $context;
 1862:     if ($action eq 'quotas') {
 1863:         $context = 'tools';
 1864:     } else {
 1865:         $context = $action;
 1866:     }
 1867:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1868:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1869:     my $typecount = 0;
 1870:     my ($css_class,%titles);
 1871:     if ($context eq 'requestcourses') {
 1872:         @usertools = ('official','unofficial','community','textbook','placement');
 1873:         @options =('norequest','approval','validate','autolimit');
 1874:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1875:         %titles = &courserequest_titles();
 1876:     } elsif ($context eq 'requestauthor') {
 1877:         @usertools = ('author');
 1878:         @options = ('norequest','approval','automatic');
 1879:         %titles = &authorrequest_titles();
 1880:     } else {
 1881:         @usertools = ('aboutme','blog','webdav','portfolio');
 1882:         %titles = &tool_titles();
 1883:     }
 1884:     if (ref($types) eq 'ARRAY') {
 1885:         foreach my $type (@{$types}) {
 1886:             my ($currdefquota,$currauthorquota);
 1887:             unless (($context eq 'requestcourses') ||
 1888:                     ($context eq 'requestauthor')) {
 1889:                 if (ref($settings) eq 'HASH') {
 1890:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1891:                         $currdefquota = $settings->{defaultquota}->{$type};
 1892:                     } else {
 1893:                         $currdefquota = $settings->{$type};
 1894:                     }
 1895:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1896:                         $currauthorquota = $settings->{authorquota}->{$type};
 1897:                     }
 1898:                 }
 1899:             }
 1900:             if (defined($usertypes->{$type})) {
 1901:                 $typecount ++;
 1902:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1903:                 $datatable .= '<tr'.$css_class.'>'.
 1904:                               '<td>'.$usertypes->{$type}.'</td>'.
 1905:                               '<td class="LC_left_item">';
 1906:                 if ($context eq 'requestcourses') {
 1907:                     $datatable .= '<table><tr>';
 1908:                 }
 1909:                 my %cell;  
 1910:                 foreach my $item (@usertools) {
 1911:                     if ($context eq 'requestcourses') {
 1912:                         my ($curroption,$currlimit);
 1913:                         if (ref($settings) eq 'HASH') {
 1914:                             if (ref($settings->{$item}) eq 'HASH') {
 1915:                                 $curroption = $settings->{$item}->{$type};
 1916:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1917:                                     $currlimit = $1; 
 1918:                                 }
 1919:                             }
 1920:                         }
 1921:                         if (!$curroption) {
 1922:                             $curroption = 'norequest';
 1923:                         }
 1924:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1925:                         foreach my $option (@options) {
 1926:                             my $val = $option;
 1927:                             if ($option eq 'norequest') {
 1928:                                 $val = 0;  
 1929:                             }
 1930:                             if ($option eq 'validate') {
 1931:                                 my $canvalidate = 0;
 1932:                                 if (ref($validations{$item}) eq 'HASH') { 
 1933:                                     if ($validations{$item}{$type}) {
 1934:                                         $canvalidate = 1;
 1935:                                     }
 1936:                                 }
 1937:                                 next if (!$canvalidate);
 1938:                             }
 1939:                             my $checked = '';
 1940:                             if ($option eq $curroption) {
 1941:                                 $checked = ' checked="checked"';
 1942:                             } elsif ($option eq 'autolimit') {
 1943:                                 if ($curroption =~ /^autolimit/) {
 1944:                                     $checked = ' checked="checked"';
 1945:                                 }                       
 1946:                             } 
 1947:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1948:                                   '<input type="radio" name="crsreq_'.$item.
 1949:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1950:                                   $titles{$option}.'</label>';
 1951:                             if ($option eq 'autolimit') {
 1952:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1953:                                                 $item.'_limit_'.$type.'" size="1" '.
 1954:                                                 'value="'.$currlimit.'" />';
 1955:                             }
 1956:                             $cell{$item} .= '</span> ';
 1957:                             if ($option eq 'autolimit') {
 1958:                                 $cell{$item} .= $titles{'unlimited'};
 1959:                             }
 1960:                         }
 1961:                     } elsif ($context eq 'requestauthor') {
 1962:                         my $curroption;
 1963:                         if (ref($settings) eq 'HASH') {
 1964:                             $curroption = $settings->{$type};
 1965:                         }
 1966:                         if (!$curroption) {
 1967:                             $curroption = 'norequest';
 1968:                         }
 1969:                         foreach my $option (@options) {
 1970:                             my $val = $option;
 1971:                             if ($option eq 'norequest') {
 1972:                                 $val = 0;
 1973:                             }
 1974:                             my $checked = '';
 1975:                             if ($option eq $curroption) {
 1976:                                 $checked = ' checked="checked"';
 1977:                             }
 1978:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1979:                                   '<input type="radio" name="authorreq_'.$type.
 1980:                                   '" value="'.$val.'"'.$checked.' />'.
 1981:                                   $titles{$option}.'</label></span>&nbsp; ';
 1982:                         }
 1983:                     } else {
 1984:                         my $checked = 'checked="checked" ';
 1985:                         if (ref($settings) eq 'HASH') {
 1986:                             if (ref($settings->{$item}) eq 'HASH') {
 1987:                                 if ($settings->{$item}->{$type} == 0) {
 1988:                                     $checked = '';
 1989:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1990:                                     $checked =  'checked="checked" ';
 1991:                                 }
 1992:                             }
 1993:                         }
 1994:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1995:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1996:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1997:                                       '</label></span>&nbsp; ';
 1998:                     }
 1999:                 }
 2000:                 if ($context eq 'requestcourses') {
 2001:                     $datatable .= '</tr><tr>';
 2002:                     foreach my $item (@usertools) {
 2003:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 2004:                     }
 2005:                     $datatable .= '</tr></table>';
 2006:                 }
 2007:                 $datatable .= '</td>';
 2008:                 unless (($context eq 'requestcourses') ||
 2009:                         ($context eq 'requestauthor')) {
 2010:                     $datatable .= 
 2011:                               '<td class="LC_right_item">'.
 2012:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2013:                               '<input type="text" name="quota_'.$type.
 2014:                               '" value="'.$currdefquota.
 2015:                               '" size="5" /></span>'.('&nbsp;' x 2).
 2016:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2017:                               '<input type="text" name="authorquota_'.$type.
 2018:                               '" value="'.$currauthorquota.
 2019:                               '" size="5" /></span></td>';
 2020:                 }
 2021:                 $datatable .= '</tr>';
 2022:             }
 2023:         }
 2024:     }
 2025:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2026:         $defaultquota = '20';
 2027:         $authorquota = '500';
 2028:         if (ref($settings) eq 'HASH') {
 2029:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2030:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2031:             } elsif (defined($settings->{'default'})) {
 2032:                 $defaultquota = $settings->{'default'};
 2033:             }
 2034:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 2035:                 $authorquota = $settings->{'authorquota'}->{'default'};
 2036:             }
 2037:         }
 2038:     }
 2039:     $typecount ++;
 2040:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2041:     $datatable .= '<tr'.$css_class.'>'.
 2042:                   '<td>'.$othertitle.'</td>'.
 2043:                   '<td class="LC_left_item">';
 2044:     if ($context eq 'requestcourses') {
 2045:         $datatable .= '<table><tr>';
 2046:     }
 2047:     my %defcell;
 2048:     foreach my $item (@usertools) {
 2049:         if ($context eq 'requestcourses') {
 2050:             my ($curroption,$currlimit);
 2051:             if (ref($settings) eq 'HASH') {
 2052:                 if (ref($settings->{$item}) eq 'HASH') {
 2053:                     $curroption = $settings->{$item}->{'default'};
 2054:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2055:                         $currlimit = $1;
 2056:                     }
 2057:                 }
 2058:             }
 2059:             if (!$curroption) {
 2060:                 $curroption = 'norequest';
 2061:             }
 2062:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2063:             foreach my $option (@options) {
 2064:                 my $val = $option;
 2065:                 if ($option eq 'norequest') {
 2066:                     $val = 0;
 2067:                 }
 2068:                 if ($option eq 'validate') {
 2069:                     my $canvalidate = 0;
 2070:                     if (ref($validations{$item}) eq 'HASH') {
 2071:                         if ($validations{$item}{'default'}) {
 2072:                             $canvalidate = 1;
 2073:                         }
 2074:                     }
 2075:                     next if (!$canvalidate);
 2076:                 }
 2077:                 my $checked = '';
 2078:                 if ($option eq $curroption) {
 2079:                     $checked = ' checked="checked"';
 2080:                 } elsif ($option eq 'autolimit') {
 2081:                     if ($curroption =~ /^autolimit/) {
 2082:                         $checked = ' checked="checked"';
 2083:                     }
 2084:                 }
 2085:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2086:                                   '<input type="radio" name="crsreq_'.$item.
 2087:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2088:                                   $titles{$option}.'</label>';
 2089:                 if ($option eq 'autolimit') {
 2090:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2091:                                        $item.'_limit_default" size="1" '.
 2092:                                        'value="'.$currlimit.'" />';
 2093:                 }
 2094:                 $defcell{$item} .= '</span> ';
 2095:                 if ($option eq 'autolimit') {
 2096:                     $defcell{$item} .= $titles{'unlimited'};
 2097:                 }
 2098:             }
 2099:         } elsif ($context eq 'requestauthor') {
 2100:             my $curroption;
 2101:             if (ref($settings) eq 'HASH') {
 2102:                 $curroption = $settings->{'default'};
 2103:             }
 2104:             if (!$curroption) {
 2105:                 $curroption = 'norequest';
 2106:             }
 2107:             foreach my $option (@options) {
 2108:                 my $val = $option;
 2109:                 if ($option eq 'norequest') {
 2110:                     $val = 0;
 2111:                 }
 2112:                 my $checked = '';
 2113:                 if ($option eq $curroption) {
 2114:                     $checked = ' checked="checked"';
 2115:                 }
 2116:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2117:                               '<input type="radio" name="authorreq_default"'.
 2118:                               ' value="'.$val.'"'.$checked.' />'.
 2119:                               $titles{$option}.'</label></span>&nbsp; ';
 2120:             }
 2121:         } else {
 2122:             my $checked = 'checked="checked" ';
 2123:             if (ref($settings) eq 'HASH') {
 2124:                 if (ref($settings->{$item}) eq 'HASH') {
 2125:                     if ($settings->{$item}->{'default'} == 0) {
 2126:                         $checked = '';
 2127:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2128:                         $checked = 'checked="checked" ';
 2129:                     }
 2130:                 }
 2131:             }
 2132:             $datatable .= '<span class="LC_nobreak"><label>'.
 2133:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2134:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2135:                           '</label></span>&nbsp; ';
 2136:         }
 2137:     }
 2138:     if ($context eq 'requestcourses') {
 2139:         $datatable .= '</tr><tr>';
 2140:         foreach my $item (@usertools) {
 2141:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2142:         }
 2143:         $datatable .= '</tr></table>';
 2144:     }
 2145:     $datatable .= '</td>';
 2146:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2147:         $datatable .= '<td class="LC_right_item">'.
 2148:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2149:                       '<input type="text" name="defaultquota" value="'.
 2150:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2151:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2152:                       '<input type="text" name="authorquota" value="'.
 2153:                       $authorquota.'" size="5" /></span></td>';
 2154:     }
 2155:     $datatable .= '</tr>';
 2156:     $typecount ++;
 2157:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2158:     $datatable .= '<tr'.$css_class.'>'.
 2159:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2160:     if ($context eq 'requestcourses') {
 2161:         $datatable .= &mt('(overrides affiliation, if set)').
 2162:                       '</td>'.
 2163:                       '<td class="LC_left_item">'.
 2164:                       '<table><tr>';
 2165:     } else {
 2166:         $datatable .= &mt('(overrides affiliation, if checked)').
 2167:                       '</td>'.
 2168:                       '<td class="LC_left_item" colspan="2">'.
 2169:                       '<br />';
 2170:     }
 2171:     my %advcell;
 2172:     foreach my $item (@usertools) {
 2173:         if ($context eq 'requestcourses') {
 2174:             my ($curroption,$currlimit);
 2175:             if (ref($settings) eq 'HASH') {
 2176:                 if (ref($settings->{$item}) eq 'HASH') {
 2177:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2178:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2179:                         $currlimit = $1;
 2180:                     }
 2181:                 }
 2182:             }
 2183:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2184:             my $checked = '';
 2185:             if ($curroption eq '') {
 2186:                 $checked = ' checked="checked"';
 2187:             }
 2188:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2189:                                '<input type="radio" name="crsreq_'.$item.
 2190:                                '__LC_adv" value=""'.$checked.' />'.
 2191:                                &mt('No override set').'</label></span>&nbsp; ';
 2192:             foreach my $option (@options) {
 2193:                 my $val = $option;
 2194:                 if ($option eq 'norequest') {
 2195:                     $val = 0;
 2196:                 }
 2197:                 if ($option eq 'validate') {
 2198:                     my $canvalidate = 0;
 2199:                     if (ref($validations{$item}) eq 'HASH') {
 2200:                         if ($validations{$item}{'_LC_adv'}) {
 2201:                             $canvalidate = 1;
 2202:                         }
 2203:                     }
 2204:                     next if (!$canvalidate);
 2205:                 }
 2206:                 my $checked = '';
 2207:                 if ($val eq $curroption) {
 2208:                     $checked = ' checked="checked"';
 2209:                 } elsif ($option eq 'autolimit') {
 2210:                     if ($curroption =~ /^autolimit/) {
 2211:                         $checked = ' checked="checked"';
 2212:                     }
 2213:                 }
 2214:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2215:                                   '<input type="radio" name="crsreq_'.$item.
 2216:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2217:                                   $titles{$option}.'</label>';
 2218:                 if ($option eq 'autolimit') {
 2219:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2220:                                        $item.'_limit__LC_adv" size="1" '.
 2221:                                        'value="'.$currlimit.'" />';
 2222:                 }
 2223:                 $advcell{$item} .= '</span> ';
 2224:                 if ($option eq 'autolimit') {
 2225:                     $advcell{$item} .= $titles{'unlimited'};
 2226:                 }
 2227:             }
 2228:         } elsif ($context eq 'requestauthor') {
 2229:             my $curroption;
 2230:             if (ref($settings) eq 'HASH') {
 2231:                 $curroption = $settings->{'_LC_adv'};
 2232:             }
 2233:             my $checked = '';
 2234:             if ($curroption eq '') {
 2235:                 $checked = ' checked="checked"';
 2236:             }
 2237:             $datatable .= '<span class="LC_nobreak"><label>'.
 2238:                           '<input type="radio" name="authorreq__LC_adv"'.
 2239:                           ' value=""'.$checked.' />'.
 2240:                           &mt('No override set').'</label></span>&nbsp; ';
 2241:             foreach my $option (@options) {
 2242:                 my $val = $option;
 2243:                 if ($option eq 'norequest') {
 2244:                     $val = 0;
 2245:                 }
 2246:                 my $checked = '';
 2247:                 if ($val eq $curroption) {
 2248:                     $checked = ' checked="checked"';
 2249:                 }
 2250:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2251:                               '<input type="radio" name="authorreq__LC_adv"'.
 2252:                               ' value="'.$val.'"'.$checked.' />'.
 2253:                               $titles{$option}.'</label></span>&nbsp; ';
 2254:             }
 2255:         } else {
 2256:             my $checked = 'checked="checked" ';
 2257:             if (ref($settings) eq 'HASH') {
 2258:                 if (ref($settings->{$item}) eq 'HASH') {
 2259:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2260:                         $checked = '';
 2261:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2262:                         $checked = 'checked="checked" ';
 2263:                     }
 2264:                 }
 2265:             }
 2266:             $datatable .= '<span class="LC_nobreak"><label>'.
 2267:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2268:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2269:                           '</label></span>&nbsp; ';
 2270:         }
 2271:     }
 2272:     if ($context eq 'requestcourses') {
 2273:         $datatable .= '</tr><tr>';
 2274:         foreach my $item (@usertools) {
 2275:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2276:         }
 2277:         $datatable .= '</tr></table>';
 2278:     }
 2279:     $datatable .= '</td></tr>';
 2280:     $$rowtotal += $typecount;
 2281:     return $datatable;
 2282: }
 2283: 
 2284: sub print_requestmail {
 2285:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2286:     my ($now,$datatable,%currapp);
 2287:     $now = time;
 2288:     if (ref($settings) eq 'HASH') {
 2289:         if (ref($settings->{'notify'}) eq 'HASH') {
 2290:             if ($settings->{'notify'}{'approval'} ne '') {
 2291:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2292:             }
 2293:         }
 2294:     }
 2295:     my $numinrow = 2;
 2296:     my $css_class;
 2297:     if ($$rowtotal%2) {
 2298:         $css_class = 'LC_odd_row';
 2299:     }
 2300:     if ($customcss) {
 2301:         $css_class .= " $customcss";
 2302:     }
 2303:     $css_class =~ s/^\s+//;
 2304:     if ($css_class) {
 2305:         $css_class = ' class="'.$css_class.'"';
 2306:     }
 2307:     if ($rowstyle) {
 2308:         $css_class .= ' style="'.$rowstyle.'"';
 2309:     }
 2310:     my $text;
 2311:     if ($action eq 'requestcourses') {
 2312:         $text = &mt('Receive notification of course requests requiring approval');
 2313:     } elsif ($action eq 'requestauthor') {
 2314:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2315:     } else {
 2316:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2317:     }
 2318:     $datatable = '<tr'.$css_class.'>'.
 2319:                  ' <td>'.$text.'</td>'.
 2320:                  ' <td class="LC_left_item">';
 2321:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2322:                                                  $action.'notifyapproval',%currapp);
 2323:     if ($numdc > 0) {
 2324:         $datatable .= $table;
 2325:     } else {
 2326:         $datatable .= &mt('There are no active Domain Coordinators');
 2327:     }
 2328:     $datatable .='</td></tr>';
 2329:     return $datatable;
 2330: }
 2331: 
 2332: sub print_studentcode {
 2333:     my ($settings,$rowtotal) = @_;
 2334:     my $rownum = 0; 
 2335:     my ($output,%current);
 2336:     my @crstypes = ('official','unofficial','community','textbook','placement');
 2337:     if (ref($settings) eq 'HASH') {
 2338:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2339:             foreach my $type (@crstypes) {
 2340:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2341:             }
 2342:         }
 2343:     }
 2344:     $output .= '<tr>'.
 2345:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2346:                '<td class="LC_left_item">';
 2347:     foreach my $type (@crstypes) {
 2348:         my $check = ' ';
 2349:         if ($current{$type}) {
 2350:             $check = ' checked="checked" ';
 2351:         }
 2352:         $output .= '<span class="LC_nobreak"><label>'.
 2353:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2354:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2355:     }
 2356:     $output .= '</td></tr>';
 2357:     $$rowtotal ++;
 2358:     return $output;
 2359: }
 2360: 
 2361: sub print_textbookcourses {
 2362:     my ($dom,$type,$settings,$rowtotal) = @_;
 2363:     my $rownum = 0;
 2364:     my $css_class;
 2365:     my $itemcount = 1;
 2366:     my $maxnum = 0;
 2367:     my $bookshash;
 2368:     if (ref($settings) eq 'HASH') {
 2369:         $bookshash = $settings->{$type};
 2370:     }
 2371:     my %ordered;
 2372:     if (ref($bookshash) eq 'HASH') {
 2373:         foreach my $item (keys(%{$bookshash})) {
 2374:             if (ref($bookshash->{$item}) eq 'HASH') {
 2375:                 my $num = $bookshash->{$item}{'order'};
 2376:                 $ordered{$num} = $item;
 2377:             }
 2378:         }
 2379:     }
 2380:     my $confname = $dom.'-domainconfig';
 2381:     my $switchserver = &check_switchserver($dom,$confname);
 2382:     my $maxnum = scalar(keys(%ordered));
 2383:     my $datatable;
 2384:     if (keys(%ordered)) {
 2385:         my @items = sort { $a <=> $b } keys(%ordered);
 2386:         for (my $i=0; $i<@items; $i++) {
 2387:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2388:             my $key = $ordered{$items[$i]};
 2389:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2390:             my $coursetitle = $coursehash{'description'};
 2391:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2392:             if (ref($bookshash->{$key}) eq 'HASH') {
 2393:                 $subject = $bookshash->{$key}->{'subject'};
 2394:                 $title = $bookshash->{$key}->{'title'};
 2395:                 if ($type eq 'textbooks') {
 2396:                     $publisher = $bookshash->{$key}->{'publisher'};
 2397:                     $author = $bookshash->{$key}->{'author'};
 2398:                     $image = $bookshash->{$key}->{'image'};
 2399:                     if ($image ne '') {
 2400:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2401:                         my $imagethumb = "$path/tn-".$imagefile;
 2402:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2403:                     }
 2404:                 }
 2405:             }
 2406:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2407:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2408:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2409:             for (my $k=0; $k<=$maxnum; $k++) {
 2410:                 my $vpos = $k+1;
 2411:                 my $selstr;
 2412:                 if ($k == $i) {
 2413:                     $selstr = ' selected="selected" ';
 2414:                 }
 2415:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2416:             }
 2417:             $datatable .= '</select>'.('&nbsp;'x2).
 2418:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2419:                 &mt('Delete?').'</label></span></td>'.
 2420:                 '<td colspan="2">'.
 2421:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2422:                 ('&nbsp;'x2).
 2423:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2424:             if ($type eq 'textbooks') {
 2425:                 $datatable .= ('&nbsp;'x2).
 2426:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2427:                               ('&nbsp;'x2).
 2428:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2429:                               ('&nbsp;'x2).
 2430:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2431:                 if ($image) {
 2432:                     $datatable .= $imgsrc.
 2433:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2434:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2435:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2436:                 }
 2437:                 if ($switchserver) {
 2438:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2439:                 } else {
 2440:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2441:                 }
 2442:             }
 2443:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2444:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2445:                           $coursetitle.'</span></td></tr>'."\n";
 2446:             $itemcount ++;
 2447:         }
 2448:     }
 2449:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2450:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2451:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2452:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2453:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2454:     for (my $k=0; $k<$maxnum+1; $k++) {
 2455:         my $vpos = $k+1;
 2456:         my $selstr;
 2457:         if ($k == $maxnum) {
 2458:             $selstr = ' selected="selected" ';
 2459:         }
 2460:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2461:     }
 2462:     $datatable .= '</select>&nbsp;'."\n".
 2463:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2464:                   '<td colspan="2">'.
 2465:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2466:                   ('&nbsp;'x2).
 2467:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2468:                   ('&nbsp;'x2);
 2469:     if ($type eq 'textbooks') {
 2470:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2471:                       ('&nbsp;'x2).
 2472:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2473:                       ('&nbsp;'x2).
 2474:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2475:         if ($switchserver) {
 2476:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2477:         } else {
 2478:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2479:         }
 2480:     }
 2481:     $datatable .= '</span>'."\n".
 2482:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2483:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2484:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2485:                   &Apache::loncommon::selectcourse_link
 2486:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course');
 2487:                   '</span></td>'."\n".
 2488:                   '</tr>'."\n";
 2489:     $itemcount ++;
 2490:     return $datatable;
 2491: }
 2492: 
 2493: sub textbookcourses_javascript {
 2494:     my ($settings) = @_;
 2495:     return unless(ref($settings) eq 'HASH');
 2496:     my (%ordered,%total,%jstext);
 2497:     foreach my $type ('textbooks','templates') {
 2498:         $total{$type} = 0;
 2499:         if (ref($settings->{$type}) eq 'HASH') {
 2500:             foreach my $item (keys(%{$settings->{$type}})) {
 2501:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2502:                     my $num = $settings->{$type}->{$item}{'order'};
 2503:                     $ordered{$type}{$num} = $item;
 2504:                 }
 2505:             }
 2506:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2507:         }
 2508:         my @jsarray = ();
 2509:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2510:             push(@jsarray,$ordered{$type}{$item});
 2511:         }
 2512:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2513:     }
 2514:     return <<"ENDSCRIPT";
 2515: <script type="text/javascript">
 2516: // <![CDATA[
 2517: function reorderBooks(form,item,caller) {
 2518:     var changedVal;
 2519: $jstext{'textbooks'};
 2520: $jstext{'templates'};
 2521:     var newpos;
 2522:     var maxh;
 2523:     if (caller == 'textbooks') {  
 2524:         newpos = 'textbooks_addbook_pos';
 2525:         maxh = 1 + $total{'textbooks'};
 2526:     } else {
 2527:         newpos = 'templates_addbook_pos';
 2528:         maxh = 1 + $total{'templates'};
 2529:     }
 2530:     var current = new Array;
 2531:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2532:     if (item == newpos) {
 2533:         changedVal = newitemVal;
 2534:     } else {
 2535:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2536:         current[newitemVal] = newpos;
 2537:     }
 2538:     if (caller == 'textbooks') {
 2539:         for (var i=0; i<textbooks.length; i++) {
 2540:             var elementName = 'textbooks_'+textbooks[i];
 2541:             if (elementName != item) {
 2542:                 if (form.elements[elementName]) {
 2543:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2544:                     current[currVal] = elementName;
 2545:                 }
 2546:             }
 2547:         }
 2548:     }
 2549:     if (caller == 'templates') {
 2550:         for (var i=0; i<templates.length; i++) {
 2551:             var elementName = 'templates_'+templates[i];
 2552:             if (elementName != item) {
 2553:                 if (form.elements[elementName]) {
 2554:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2555:                     current[currVal] = elementName;
 2556:                 }
 2557:             }
 2558:         }
 2559:     }
 2560:     var oldVal;
 2561:     for (var j=0; j<maxh; j++) {
 2562:         if (current[j] == undefined) {
 2563:             oldVal = j;
 2564:         }
 2565:     }
 2566:     if (oldVal < changedVal) {
 2567:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2568:            var elementName = current[k];
 2569:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2570:         }
 2571:     } else {
 2572:         for (var k=changedVal; k<oldVal; k++) {
 2573:             var elementName = current[k];
 2574:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2575:         }
 2576:     }
 2577:     return;
 2578: }
 2579: 
 2580: // ]]>
 2581: </script>
 2582: 
 2583: ENDSCRIPT
 2584: }
 2585: 
 2586: sub ltitools_javascript {
 2587:     my ($settings) = @_;
 2588:     return unless(ref($settings) eq 'HASH');
 2589:     my (%ordered,$total,%jstext);
 2590:     $total = 0;
 2591:     foreach my $item (keys(%{$settings})) {
 2592:         if (ref($settings->{$item}) eq 'HASH') {
 2593:             my $num = $settings->{$item}{'order'};
 2594:             $ordered{$num} = $item;
 2595:         }
 2596:     }
 2597:     $total = scalar(keys(%{$settings}));
 2598:     my @jsarray = ();
 2599:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2600:         push(@jsarray,$ordered{$item});
 2601:     }
 2602:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 2603:     return <<"ENDSCRIPT";
 2604: <script type="text/javascript">
 2605: // <![CDATA[
 2606: function reorderLTI(form,item) {
 2607:     var changedVal;
 2608: $jstext
 2609:     var newpos = 'ltitools_add_pos';
 2610:     var maxh = 1 + $total;
 2611:     var current = new Array;
 2612:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2613:     if (item == newpos) {
 2614:         changedVal = newitemVal;
 2615:     } else {
 2616:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2617:         current[newitemVal] = newpos;
 2618:     }
 2619:     for (var i=0; i<ltitools.length; i++) {
 2620:         var elementName = 'ltitools_'+ltitools[i];
 2621:         if (elementName != item) {
 2622:             if (form.elements[elementName]) {
 2623:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2624:                 current[currVal] = elementName;
 2625:             }
 2626:         }
 2627:     }
 2628:     var oldVal;
 2629:     for (var j=0; j<maxh; j++) {
 2630:         if (current[j] == undefined) {
 2631:             oldVal = j;
 2632:         }
 2633:     }
 2634:     if (oldVal < changedVal) {
 2635:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2636:            var elementName = current[k];
 2637:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2638:         }
 2639:     } else {
 2640:         for (var k=changedVal; k<oldVal; k++) {
 2641:             var elementName = current[k];
 2642:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2643:         }
 2644:     }
 2645:     return;
 2646: }
 2647: 
 2648: // ]]>
 2649: </script>
 2650: 
 2651: ENDSCRIPT
 2652: }
 2653: 
 2654: sub print_autoenroll {
 2655:     my ($dom,$settings,$rowtotal) = @_;
 2656:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2657:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 2658:     if (ref($settings) eq 'HASH') {
 2659:         if (exists($settings->{'run'})) {
 2660:             if ($settings->{'run'} eq '0') {
 2661:                 $runoff = ' checked="checked" ';
 2662:                 $runon = ' ';
 2663:             } else {
 2664:                 $runon = ' checked="checked" ';
 2665:                 $runoff = ' ';
 2666:             }
 2667:         } else {
 2668:             if ($autorun) {
 2669:                 $runon = ' checked="checked" ';
 2670:                 $runoff = ' ';
 2671:             } else {
 2672:                 $runoff = ' checked="checked" ';
 2673:                 $runon = ' ';
 2674:             }
 2675:         }
 2676:         if (exists($settings->{'co-owners'})) {
 2677:             if ($settings->{'co-owners'} eq '0') {
 2678:                 $coownersoff = ' checked="checked" ';
 2679:                 $coownerson = ' ';
 2680:             } else {
 2681:                 $coownerson = ' checked="checked" ';
 2682:                 $coownersoff = ' ';
 2683:             }
 2684:         } else {
 2685:             $coownersoff = ' checked="checked" ';
 2686:             $coownerson = ' ';
 2687:         }
 2688:         if (exists($settings->{'sender_domain'})) {
 2689:             $defdom = $settings->{'sender_domain'};
 2690:         }
 2691:         if (exists($settings->{'autofailsafe'})) {
 2692:             $failsafe = $settings->{'autofailsafe'};
 2693:         }
 2694:     } else {
 2695:         if ($autorun) {
 2696:             $runon = ' checked="checked" ';
 2697:             $runoff = ' ';
 2698:         } else {
 2699:             $runoff = ' checked="checked" ';
 2700:             $runon = ' ';
 2701:         }
 2702:     }
 2703:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2704:     my $notif_sender;
 2705:     if (ref($settings) eq 'HASH') {
 2706:         $notif_sender = $settings->{'sender_uname'};
 2707:     }
 2708:     my $datatable='<tr class="LC_odd_row">'.
 2709:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2710:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2711:                   '<input type="radio" name="autoenroll_run"'.
 2712:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2713:                   '<label><input type="radio" name="autoenroll_run"'.
 2714:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2715:                   '</tr><tr>'.
 2716:                   '<td>'.&mt('Notification messages - sender').
 2717:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2718:                   &mt('username').':&nbsp;'.
 2719:                   '<input type="text" name="sender_uname" value="'.
 2720:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2721:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2722:                   '<tr class="LC_odd_row">'.
 2723:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2724:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2725:                   '<input type="radio" name="autoassign_coowners"'.
 2726:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2727:                   '<label><input type="radio" name="autoassign_coowners"'.
 2728:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2729:                   '</tr><tr>'.
 2730:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 2731:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2732:                   '<input type="text" name="autoenroll_failsafe"'.
 2733:                   ' value="'.$failsafe.'" size="4" /></td></tr>';
 2734:     $$rowtotal += 4;
 2735:     return $datatable;
 2736: }
 2737: 
 2738: sub print_autoupdate {
 2739:     my ($position,$dom,$settings,$rowtotal) = @_;
 2740:     my $datatable;
 2741:     if ($position eq 'top') {
 2742:         my $updateon = ' ';
 2743:         my $updateoff = ' checked="checked" ';
 2744:         my $classlistson = ' ';
 2745:         my $classlistsoff = ' checked="checked" ';
 2746:         if (ref($settings) eq 'HASH') {
 2747:             if ($settings->{'run'} eq '1') {
 2748:                 $updateon = $updateoff;
 2749:                 $updateoff = ' ';
 2750:             }
 2751:             if ($settings->{'classlists'} eq '1') {
 2752:                 $classlistson = $classlistsoff;
 2753:                 $classlistsoff = ' ';
 2754:             }
 2755:         }
 2756:         my %title = (
 2757:                    run => 'Auto-update active?',
 2758:                    classlists => 'Update information in classlists?',
 2759:                     );
 2760:         $datatable = '<tr class="LC_odd_row">'. 
 2761:                   '<td>'.&mt($title{'run'}).'</td>'.
 2762:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2763:                   '<input type="radio" name="autoupdate_run"'.
 2764:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2765:                   '<label><input type="radio" name="autoupdate_run"'.
 2766:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2767:                   '</tr><tr>'.
 2768:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2769:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2770:                   '<label><input type="radio" name="classlists"'.
 2771:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2772:                   '<label><input type="radio" name="classlists"'.
 2773:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2774:                   '</tr>';
 2775:         $$rowtotal += 2;
 2776:     } elsif ($position eq 'middle') {
 2777:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2778:         my $numinrow = 3;
 2779:         my $locknamesettings;
 2780:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2781:                                      $dom,$numinrow,$othertitle,
 2782:                                     'lockablenames',$rowtotal);
 2783:         $$rowtotal ++;
 2784:     } else {
 2785:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2786:         my @fields = ('lastname','firstname','middlename','generation',
 2787:                       'permanentemail','id');
 2788:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2789:         my $numrows = 0;
 2790:         if (ref($types) eq 'ARRAY') {
 2791:             if (@{$types} > 0) {
 2792:                 $datatable = 
 2793:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2794:                                          \@fields,$types,\$numrows);
 2795:                     $$rowtotal += @{$types}; 
 2796:             }
 2797:         }
 2798:         $datatable .= 
 2799:             &usertype_update_row($settings,{'default' => $othertitle},
 2800:                                  \%fieldtitles,\@fields,['default'],
 2801:                                  \$numrows);
 2802:         $$rowtotal ++;     
 2803:     }
 2804:     return $datatable;
 2805: }
 2806: 
 2807: sub print_autocreate {
 2808:     my ($dom,$settings,$rowtotal) = @_;
 2809:     my (%createon,%createoff,%currhash);
 2810:     my @types = ('xml','req');
 2811:     if (ref($settings) eq 'HASH') {
 2812:         foreach my $item (@types) {
 2813:             $createoff{$item} = ' checked="checked" ';
 2814:             $createon{$item} = ' ';
 2815:             if (exists($settings->{$item})) {
 2816:                 if ($settings->{$item}) {
 2817:                     $createon{$item} = ' checked="checked" ';
 2818:                     $createoff{$item} = ' ';
 2819:                 }
 2820:             }
 2821:         }
 2822:         if ($settings->{'xmldc'} ne '') {
 2823:             $currhash{$settings->{'xmldc'}} = 1;
 2824:         }
 2825:     } else {
 2826:         foreach my $item (@types) {
 2827:             $createoff{$item} = ' checked="checked" ';
 2828:             $createon{$item} = ' ';
 2829:         }
 2830:     }
 2831:     $$rowtotal += 2;
 2832:     my $numinrow = 2;
 2833:     my $datatable='<tr class="LC_odd_row">'.
 2834:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2835:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2836:                   '<input type="radio" name="autocreate_xml"'.
 2837:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2838:                   '<label><input type="radio" name="autocreate_xml"'.
 2839:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2840:                   '</td></tr><tr>'.
 2841:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2842:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2843:                   '<input type="radio" name="autocreate_req"'.
 2844:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2845:                   '<label><input type="radio" name="autocreate_req"'.
 2846:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2847:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2848:                                                    'autocreate_xmldc',%currhash);
 2849:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2850:     if ($numdc > 1) {
 2851:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2852:                       '</td><td class="LC_left_item">';
 2853:     } else {
 2854:         $datatable .= &mt('Course creation processed as:').
 2855:                       '</td><td class="LC_right_item">';
 2856:     }
 2857:     $datatable .= $dctable.'</td></tr>';
 2858:     $$rowtotal += $rows;
 2859:     return $datatable;
 2860: }
 2861: 
 2862: sub print_directorysrch {
 2863:     my ($position,$dom,$settings,$rowtotal) = @_;
 2864:     my $datatable;
 2865:     if ($position eq 'top') {
 2866:         my $instsrchon = ' ';
 2867:         my $instsrchoff = ' checked="checked" ';
 2868:         my ($exacton,$containson,$beginson);
 2869:         my $instlocalon = ' ';
 2870:         my $instlocaloff = ' checked="checked" ';
 2871:         if (ref($settings) eq 'HASH') {
 2872:             if ($settings->{'available'} eq '1') {
 2873:                 $instsrchon = $instsrchoff;
 2874:                 $instsrchoff = ' ';
 2875:             }
 2876:             if ($settings->{'localonly'} eq '1') {
 2877:                 $instlocalon = $instlocaloff;
 2878:                 $instlocaloff = ' ';
 2879:             }
 2880:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2881:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 2882:                     if ($type eq 'exact') {
 2883:                         $exacton = ' checked="checked" ';
 2884:                     } elsif ($type eq 'contains') {
 2885:                         $containson = ' checked="checked" ';
 2886:                     } elsif ($type eq 'begins') {
 2887:                         $beginson = ' checked="checked" ';
 2888:                     }
 2889:                 }
 2890:             } else {
 2891:                 if ($settings->{'searchtypes'} eq 'exact') {
 2892:                     $exacton = ' checked="checked" ';
 2893:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 2894:                     $containson = ' checked="checked" ';
 2895:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 2896:                     $exacton = ' checked="checked" ';
 2897:                     $containson = ' checked="checked" ';
 2898:                 }
 2899:             }
 2900:         }
 2901:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2902:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2903: 
 2904:         my $numinrow = 4;
 2905:         my $cansrchrow = 0;
 2906:         $datatable='<tr class="LC_odd_row">'.
 2907:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 2908:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2909:                    '<input type="radio" name="dirsrch_available"'.
 2910:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2911:                    '<label><input type="radio" name="dirsrch_available"'.
 2912:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2913:                    '</tr><tr>'.
 2914:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 2915:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2916:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 2917:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2918:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 2919:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2920:                    '</tr>';
 2921:         $$rowtotal += 2;
 2922:         if (ref($usertypes) eq 'HASH') {
 2923:             if (keys(%{$usertypes}) > 0) {
 2924:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2925:                                              $numinrow,$othertitle,'cansearch',
 2926:                                              $rowtotal);
 2927:                 $cansrchrow = 1;
 2928:             }
 2929:         }
 2930:         if ($cansrchrow) {
 2931:             $$rowtotal ++;
 2932:             $datatable .= '<tr>';
 2933:         } else {
 2934:             $datatable .= '<tr class="LC_odd_row">';
 2935:         }
 2936:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2937:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2938:         foreach my $title (@{$titleorder}) {
 2939:             if (defined($searchtitles->{$title})) {
 2940:                 my $check = ' ';
 2941:                 if (ref($settings) eq 'HASH') {
 2942:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2943:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2944:                             $check = ' checked="checked" ';
 2945:                         }
 2946:                     }
 2947:                 }
 2948:                 $datatable .= '<td class="LC_left_item">'.
 2949:                               '<span class="LC_nobreak"><label>'.
 2950:                               '<input type="checkbox" name="searchby" '.
 2951:                               'value="'.$title.'"'.$check.'/>'.
 2952:                               $searchtitles->{$title}.'</label></span></td>';
 2953:             }
 2954:         }
 2955:         $datatable .= '</tr></table></td></tr>';
 2956:         $$rowtotal ++;
 2957:         if ($cansrchrow) {
 2958:             $datatable .= '<tr class="LC_odd_row">';
 2959:         } else {
 2960:             $datatable .= '<tr>';
 2961:         }
 2962:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2963:                       '<td class="LC_left_item" colspan="2">'.
 2964:                       '<span class="LC_nobreak"><label>'.
 2965:                       '<input type="checkbox" name="searchtypes" '.
 2966:                       $exacton.' value="exact" />'.&mt('Exact match').
 2967:                       '</label>&nbsp;'.
 2968:                       '<label><input type="checkbox" name="searchtypes" '.
 2969:                       $beginson.' value="begins" />'.&mt('Begins with').
 2970:                       '</label>&nbsp;'.
 2971:                       '<label><input type="checkbox" name="searchtypes" '.
 2972:                       $containson.' value="contains" />'.&mt('Contains').
 2973:                       '</label></span></td></tr>';
 2974:         $$rowtotal ++;
 2975:     } else {
 2976:         my $domsrchon = ' checked="checked" ';
 2977:         my $domsrchoff = ' ';
 2978:         my $domlocalon = ' ';
 2979:         my $domlocaloff = ' checked="checked" ';
 2980:         if (ref($settings) eq 'HASH') {
 2981:             if ($settings->{'lclocalonly'} eq '1') {
 2982:                 $domlocalon = $domlocaloff;
 2983:                 $domlocaloff = ' ';
 2984:             }
 2985:             if ($settings->{'lcavailable'} eq '0') {
 2986:                 $domsrchoff = $domsrchon;
 2987:                 $domsrchon = ' ';
 2988:             }
 2989:         }
 2990:         $datatable='<tr class="LC_odd_row">'.
 2991:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 2992:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2993:                       '<input type="radio" name="dirsrch_domavailable"'.
 2994:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2995:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 2996:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2997:                       '</tr><tr>'.
 2998:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 2999:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3000:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 3001:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 3002:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 3003:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 3004:                       '</tr>';
 3005:         $$rowtotal += 2;
 3006:     }
 3007:     return $datatable;
 3008: }
 3009: 
 3010: sub print_contacts {
 3011:     my ($position,$dom,$settings,$rowtotal) = @_;
 3012:     my $datatable;
 3013:     my @contacts = ('adminemail','supportemail');
 3014:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 3015:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings);
 3016:     if ($position eq 'top') {
 3017:         if (ref($settings) eq 'HASH') {
 3018:             foreach my $item (@contacts) {
 3019:                 if (exists($settings->{$item})) {
 3020:                     $to{$item} = $settings->{$item};
 3021:                 }
 3022:             }
 3023:         }
 3024:     } elsif ($position eq 'middle') {
 3025:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 3026:                      'updatesmail','idconflictsmail');
 3027:         foreach my $type (@mailings) {
 3028:             $otheremails{$type} = '';
 3029:         }
 3030:     } else {
 3031:         @mailings = ('helpdeskmail','otherdomsmail');
 3032:         foreach my $type (@mailings) {
 3033:             $otheremails{$type} = '';
 3034:         }
 3035:         $bccemails{'helpdeskmail'} = '';
 3036:         $bccemails{'otherdomsmail'} = '';
 3037:         $includestr{'helpdeskmail'} = '';
 3038:         $includestr{'otherdomsmail'} = '';
 3039:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 3040:     }
 3041:     if (ref($settings) eq 'HASH') {
 3042:         unless ($position eq 'top') {
 3043:             foreach my $type (@mailings) {
 3044:                 if (exists($settings->{$type})) {
 3045:                     if (ref($settings->{$type}) eq 'HASH') {
 3046:                         foreach my $item (@contacts) {
 3047:                             if ($settings->{$type}{$item}) {
 3048:                                 $checked{$type}{$item} = ' checked="checked" ';
 3049:                             }
 3050:                         }
 3051:                         $otheremails{$type} = $settings->{$type}{'others'};
 3052:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3053:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 3054:                             if ($settings->{$type}{'include'} ne '') {
 3055:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3056:                                 $includestr{$type} = &unescape($includestr{$type});
 3057:                             }
 3058:                         }
 3059:                     }
 3060:                 } elsif ($type eq 'lonstatusmail') {
 3061:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3062:                 }
 3063:             }
 3064:         }
 3065:         if ($position eq 'bottom') {
 3066:             foreach my $type (@mailings) {
 3067:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 3068:                 if ($settings->{$type}{'include'} ne '') {
 3069:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3070:                     $includestr{$type} = &unescape($includestr{$type});
 3071:                 }
 3072:             }
 3073:             if (ref($settings->{'helpform'}) eq 'HASH') {
 3074:                 if (ref($fields) eq 'ARRAY') {
 3075:                     foreach my $field (@{$fields}) {
 3076:                         $currfield{$field} = $settings->{'helpform'}{$field};
 3077:                     }
 3078:                 }
 3079:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 3080:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 3081:                 } else {
 3082:                     $maxsize = '1.0';
 3083:                 }
 3084:             } else {
 3085:                 if (ref($fields) eq 'ARRAY') {
 3086:                     foreach my $field (@{$fields}) {
 3087:                         $currfield{$field} = 'yes';
 3088:                     }
 3089:                 }
 3090:                 $maxsize = '1.0';
 3091:             }
 3092:         }
 3093:     } else {
 3094:         if ($position eq 'top') {
 3095:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 3096:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 3097:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 3098:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 3099:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3100:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 3101:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 3102:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 3103:         } elsif ($position eq 'bottom') {
 3104:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 3105:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 3106:             if (ref($fields) eq 'ARRAY') {
 3107:                 foreach my $field (@{$fields}) {
 3108:                     $currfield{$field} = 'yes';
 3109:                 }
 3110:             }
 3111:             $maxsize = '1.0';
 3112:         }
 3113:     }
 3114:     my ($titles,$short_titles) = &contact_titles();
 3115:     my $rownum = 0;
 3116:     my $css_class;
 3117:     if ($position eq 'top') {
 3118:         foreach my $item (@contacts) {
 3119:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3120:             $datatable .= '<tr'.$css_class.'>'. 
 3121:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 3122:                           '</span></td><td class="LC_right_item">'.
 3123:                           '<input type="text" name="'.$item.'" value="'.
 3124:                           $to{$item}.'" /></td></tr>';
 3125:             $rownum ++;
 3126:         }
 3127:     } else {
 3128:         foreach my $type (@mailings) {
 3129:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3130:             $datatable .= '<tr'.$css_class.'>'.
 3131:                           '<td><span class="LC_nobreak">'.
 3132:                           $titles->{$type}.': </span></td>'.
 3133:                           '<td class="LC_left_item">';
 3134:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3135:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 3136:             }
 3137:             $datatable .= '<span class="LC_nobreak">';
 3138:             foreach my $item (@contacts) {
 3139:                 $datatable .= '<label>'.
 3140:                               '<input type="checkbox" name="'.$type.'"'.
 3141:                               $checked{$type}{$item}.
 3142:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 3143:                               '</label>&nbsp;';
 3144:             }
 3145:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3146:                           '<input type="text" name="'.$type.'_others" '.
 3147:                           'value="'.$otheremails{$type}.'"  />';
 3148:             my %locchecked;
 3149:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3150:                 foreach my $loc ('s','b') {
 3151:                     if ($includeloc{$type} eq $loc) {
 3152:                         $locchecked{$loc} = ' checked="checked"';
 3153:                         last;
 3154:                     }
 3155:                 }
 3156:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3157:                               '<input type="text" name="'.$type.'_bcc" '.
 3158:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 3159:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3160:                               &mt('Text automatically added to e-mail:').' '.
 3161:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br >'.
 3162:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3163:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3164:                               ('&nbsp;'x2).
 3165:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3166:                               '</span></fieldset>';
 3167:             }
 3168:             $datatable .= '</td></tr>'."\n";
 3169:             $rownum ++;
 3170:         }
 3171:     }
 3172:     if ($position eq 'middle') {
 3173:         my %choices;
 3174:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 3175:                                        &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3176:                                        &mt('LON-CAPA core group - MSU'),600,500));
 3177:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3178:                                         &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3179:                                         &mt('LON-CAPA core group - MSU'),600,500));
 3180:         my @toggles = ('reporterrors','reportupdates');
 3181:         my %defaultchecked = ('reporterrors'  => 'on',
 3182:                               'reportupdates' => 'on');
 3183:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3184:                                                    \%choices,$rownum);
 3185:         $datatable .= $reports;
 3186:     } elsif ($position eq 'bottom') {
 3187:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3188:         $datatable .= '<tr'.$css_class.'>'.
 3189:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 3190:                       &mt('(e-mail, subject, and description always shown)').
 3191:                       '</td><td class="LC_left_item">';
 3192:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 3193:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 3194:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 3195:             foreach my $field (@{$fields}) {
 3196:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 3197:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 3198:                     $datatable .= ' '.&mt('(logged-in users)');
 3199:                 }
 3200:                 $datatable .='</td><td>';
 3201:                 my $clickaction;
 3202:                 if ($field eq 'screenshot') {
 3203:                     $clickaction = ' onclick="screenshotSize(this);"';
 3204:                 }
 3205:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 3206:                     foreach my $option (@{$possoptions->{$field}}) {
 3207:                         my $checked;
 3208:                         if ($currfield{$field} eq $option) {
 3209:                             $checked = ' checked="checked"';
 3210:                         }
 3211:                         $datatable .= '<span class="LC_nobreak"><label>'.
 3212:                                       '<input type="radio" name="helpform_'.$field.'" '.
 3213:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 3214:                                       '</label></span>'.('&nbsp;'x2);
 3215:                     }
 3216:                 }
 3217:                 if ($field eq 'screenshot') {
 3218:                     my $display;
 3219:                     if ($currfield{$field} eq 'no') {
 3220:                         $display = ' style="display:none"';
 3221:                     }
 3222:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.' />'.
 3223:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3224:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3225:                 }
 3226:                 $datatable .= '</td></tr>';
 3227:             }
 3228:             $datatable .= '</table>';
 3229:         }
 3230:         $datatable .= '</td></tr>'."\n";
 3231:         $rownum ++;
 3232:     }
 3233:     $$rowtotal += $rownum;
 3234:     return $datatable;
 3235: }
 3236: 
 3237: sub contacts_javascript {
 3238:     return <<"ENDSCRIPT";
 3239: 
 3240: <script type="text/javascript">
 3241: // <![CDATA[
 3242: 
 3243: function screenshotSize(field) {
 3244:     if (document.getElementById('help_screenshotsize')) {
 3245:         if (field.value == 'no') {
 3246:             document.getElementById('help_screenshotsize').style.display="none";
 3247:         } else {
 3248:             document.getElementById('help_screenshotsize').style.display="";
 3249:         }
 3250:     }
 3251:     return;
 3252: }
 3253: 
 3254: // ]]>
 3255: </script>
 3256: 
 3257: ENDSCRIPT
 3258: }
 3259: 
 3260: sub print_helpsettings {
 3261:     my ($position,$dom,$settings,$rowtotal) = @_;
 3262:     my $confname = $dom.'-domainconfig';
 3263:     my $formname = 'display';
 3264:     my ($datatable,$itemcount);
 3265:     if ($position eq 'top') {
 3266:         $itemcount = 1;
 3267:         my (%choices,%defaultchecked,@toggles);
 3268:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 3269:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 3270:                                      &mt('LON-CAPA bug tracker'),600,500));
 3271:         %defaultchecked = ('submitbugs' => 'on');
 3272:         @toggles = ('submitbugs');
 3273:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3274:                                                      \%choices,$itemcount);
 3275:         $$rowtotal ++;
 3276:     } else {
 3277:         my $css_class;
 3278:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 3279:         my (%customroles,%ordered,%current);
 3280:         if (ref($settings) eq 'HASH') {
 3281:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 3282:                 %current = %{$settings->{'adhoc'}};
 3283:             }
 3284:         }
 3285:         my $count = 0;
 3286:         foreach my $key (sort(keys(%existing))) {
 3287:             if ($key=~/^rolesdef\_(\w+)$/) {
 3288:                 my $rolename = $1;
 3289:                 my (%privs,$order);
 3290:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3291:                 $customroles{$rolename} = \%privs;
 3292:                 if (ref($current{$rolename}) eq 'HASH') {
 3293:                     $order = $current{$rolename}{'order'};
 3294:                 }
 3295:                 if ($order eq '') {
 3296:                     $order = $count;
 3297:                 }
 3298:                 $ordered{$order} = $rolename;
 3299:                 $count++;
 3300:             }
 3301:         }
 3302:         my $maxnum = scalar(keys(%ordered));
 3303:         my @roles_by_num = ();
 3304:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3305:             push(@roles_by_num,$item);
 3306:         }
 3307:         my $context = 'domprefs';
 3308:         my $crstype = 'Course';
 3309:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3310:         my @accesstypes = ('all','dh','da','none');
 3311:         my ($numstatustypes,@jsarray);
 3312:         if (ref($types) eq 'ARRAY') {
 3313:             if (@{$types} > 0) {
 3314:                 $numstatustypes = scalar(@{$types});
 3315:                 push(@accesstypes,'status');
 3316:                 @jsarray = ('bystatus');
 3317:             }
 3318:         }
 3319:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 3320:         if (keys(%domhelpdesk)) {
 3321:             push(@accesstypes,('inc','exc'));
 3322:             push(@jsarray,('notinc','notexc'));
 3323:         }
 3324:         my $hiddenstr = join("','",@jsarray);
 3325:         $datatable .= &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname);
 3326:         my $context = 'domprefs';
 3327:         my $crstype = 'Course';
 3328:         my $prefix = 'helproles_';
 3329:         my $add_class = 'LC_hidden';
 3330:         foreach my $num (@roles_by_num) {
 3331:             my $role = $ordered{$num};
 3332:             my ($desc,$access,@statuses);
 3333:             if (ref($current{$role}) eq 'HASH') {
 3334:                 $desc = $current{$role}{'desc'};
 3335:                 $access = $current{$role}{'access'};
 3336:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 3337:                     @statuses = @{$current{$role}{'insttypes'}};
 3338:                 }
 3339:             }
 3340:             if ($desc eq '') {
 3341:                 $desc = $role;
 3342:             }
 3343:             my $identifier = 'custhelp'.$num;
 3344:             my %full=();
 3345:             my %levels= (
 3346:                          course => {},
 3347:                          domain => {},
 3348:                          system => {},
 3349:                         );
 3350:             my %levelscurrent=(
 3351:                                course => {},
 3352:                                domain => {},
 3353:                                system => {},
 3354:                               );
 3355:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 3356:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3357:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3358:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 3359:             $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><b>'.$role.'</b><br />'.
 3360:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 3361:             for (my $k=0; $k<=$maxnum; $k++) {
 3362:                 my $vpos = $k+1;
 3363:                 my $selstr;
 3364:                 if ($k == $num) {
 3365:                     $selstr = ' selected="selected" ';
 3366:                 }
 3367:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3368:             }
 3369:             $datatable .= '</select>'.('&nbsp;'x2).
 3370:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 3371:                           '</td>'.
 3372:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3373:                           &mt('Name shown to users:').
 3374:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 3375:                           '</fieldset>'.
 3376:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 3377:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 3378:                           '<fieldset>'.
 3379:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 3380:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 3381:                                                                    \%levelscurrent,$identifier,
 3382:                                                                    'LC_hidden',$prefix.$num.'_privs').
 3383:                           '</fieldset></td>';
 3384:             $itemcount ++;
 3385:         }
 3386:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3387:         my $newcust = 'custhelp'.$count;
 3388:         my (%privs,%levelscurrent);
 3389:         my %full=();
 3390:         my %levels= (
 3391:                      course => {},
 3392:                      domain => {},
 3393:                      system => {},
 3394:                     );
 3395:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 3396:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3397:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 3398:         $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><span class="LC_nobreak"><label>'.
 3399:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 3400:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 3401:         for (my $k=0; $k<$maxnum+1; $k++) {
 3402:             my $vpos = $k+1;
 3403:             my $selstr;
 3404:             if ($k == $maxnum) {
 3405:                 $selstr = ' selected="selected" ';
 3406:             }
 3407:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3408:         }
 3409:         $datatable .= '</select>&nbsp;'."\n".
 3410:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 3411:                       '</label></span></td>'.
 3412:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3413:                       '<span class="LC_nobreak">'.
 3414:                       &mt('Internal name:').
 3415:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 3416:                       '</span>'.('&nbsp;'x4).
 3417:                       '<span class="LC_nobreak">'.
 3418:                       &mt('Name shown to users:').
 3419:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 3420:                       '</span></fieldset>'.
 3421:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 3422:                                              $usertypes,$types,\%domhelpdesk).
 3423:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 3424:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 3425:                                                                 \@templateroles,$newcust).
 3426:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 3427:                                                                \%levelscurrent,$newcust).
 3428:                       '</fieldset></td></tr>';
 3429:         $count ++;
 3430:         $$rowtotal += $count;
 3431:     }
 3432:     return $datatable;
 3433: }
 3434: 
 3435: sub adhocbutton {
 3436:     my ($prefix,$num,$field,$visibility) = @_;
 3437:     my %lt = &Apache::lonlocal::texthash(
 3438:                                           show => 'Show details',
 3439:                                           hide => 'Hide details',
 3440:                                         );
 3441:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 3442:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 3443:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 3444:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 3445: }
 3446: 
 3447: sub helpsettings_javascript {
 3448:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 3449:     return unless(ref($roles_by_num) eq 'ARRAY');
 3450:     my %html_js_lt = &Apache::lonlocal::texthash(
 3451:                                           show => 'Show details',
 3452:                                           hide => 'Hide details',
 3453:                                         );
 3454:     &html_escape(\%html_js_lt);
 3455:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 3456:     return <<"ENDSCRIPT";
 3457: <script type="text/javascript">
 3458: // <![CDATA[
 3459: 
 3460: function reorderHelpRoles(form,item) {
 3461:     var changedVal;
 3462: $jstext
 3463:     var newpos = 'helproles_${total}_pos';
 3464:     var maxh = 1 + $total;
 3465:     var current = new Array();
 3466:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3467:     if (item == newpos) {
 3468:         changedVal = newitemVal;
 3469:     } else {
 3470:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3471:         current[newitemVal] = newpos;
 3472:     }
 3473:     for (var i=0; i<helproles.length; i++) {
 3474:         var elementName = 'helproles_'+helproles[i]+'_pos';
 3475:         if (elementName != item) {
 3476:             if (form.elements[elementName]) {
 3477:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3478:                 current[currVal] = elementName;
 3479:             }
 3480:         }
 3481:     }
 3482:     var oldVal;
 3483:     for (var j=0; j<maxh; j++) {
 3484:         if (current[j] == undefined) {
 3485:             oldVal = j;
 3486:         }
 3487:     }
 3488:     if (oldVal < changedVal) {
 3489:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3490:            var elementName = current[k];
 3491:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3492:         }
 3493:     } else {
 3494:         for (var k=changedVal; k<oldVal; k++) {
 3495:             var elementName = current[k];
 3496:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3497:         }
 3498:     }
 3499:     return;
 3500: }
 3501: 
 3502: function helpdeskAccess(num) {
 3503:     var curraccess = null;
 3504:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 3505:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 3506:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 3507:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 3508:             }
 3509:         }
 3510:     }
 3511:     var shown = Array();
 3512:     var hidden = Array();
 3513:     if (curraccess == 'none') {
 3514:         hidden = Array('$hiddenstr');
 3515:     } else {
 3516:         if (curraccess == 'status') {
 3517:             shown = Array('bystatus');
 3518:             hidden = Array('notinc','notexc');
 3519:         } else {
 3520:             if (curraccess == 'exc') {
 3521:                 shown = Array('notexc');
 3522:                 hidden = Array('notinc','bystatus');
 3523:             }
 3524:             if (curraccess == 'inc') {
 3525:                 shown = Array('notinc');
 3526:                 hidden = Array('notexc','bystatus');
 3527:             }
 3528:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 3529:                 hidden = Array('notinc','notexc','bystatus');
 3530:             }
 3531:         }
 3532:     }
 3533:     if (hidden.length > 0) {
 3534:         for (var i=0; i<hidden.length; i++) {
 3535:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 3536:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 3537:             }
 3538:         }
 3539:     }
 3540:     if (shown.length > 0) {
 3541:         for (var i=0; i<shown.length; i++) {
 3542:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 3543:                 if (shown[i] == 'privs') {
 3544:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 3545:                 } else {
 3546:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 3547:                 }
 3548:             }
 3549:         }
 3550:     }
 3551:     return;
 3552: }
 3553: 
 3554: function toggleHelpdeskItem(num,field) {
 3555:     if (document.getElementById('helproles_'+num+'_'+field)) {
 3556:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 3557:             document.getElementById('helproles_'+num+'_'+field).className =
 3558:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 3559:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3560:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 3561:             }
 3562:         } else {
 3563:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 3564:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3565:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 3566:             }
 3567:         }
 3568:     }
 3569:     return;
 3570: }
 3571: 
 3572: // ]]>
 3573: </script>
 3574: 
 3575: ENDSCRIPT
 3576: }
 3577: 
 3578: sub helpdeskroles_access {
 3579:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 3580:         $usertypes,$types,$domhelpdesk) = @_;
 3581:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 3582:     my %lt = &Apache::lonlocal::texthash(
 3583:                     'rou'    => 'Role usage',
 3584:                     'whi'    => 'Which helpdesk personnel may use this role?',
 3585:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 3586:                     'dh'     => 'All with domain helpdesk role',
 3587:                     'da'     => 'All with domain helpdesk assistant role',
 3588:                     'none'   => 'None',
 3589:                     'status' => 'Determined based on institutional status',
 3590:                     'inc'    => 'Include all, but exclude specific personnel',
 3591:                     'exc'    => 'Exclude all, but include specific personnel',
 3592:                   );
 3593:     my %usecheck = (
 3594:                      all => ' checked="checked"',
 3595:                    );
 3596:     my %displaydiv = (
 3597:                       status => 'none',
 3598:                       inc    => 'none',
 3599:                       exc    => 'none',
 3600:                       priv   => 'block',
 3601:                      );
 3602:     my $output;
 3603:     if (ref($current) eq 'HASH') {
 3604:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 3605:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 3606:                 $usecheck{$current->{access}} = $usecheck{'all'};
 3607:                 delete($usecheck{'all'});
 3608:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 3609:                     my $access = $1;
 3610:                     $displaydiv{$access} = 'inline';
 3611:                 } elsif ($current->{access} eq 'none') {
 3612:                     $displaydiv{'priv'} = 'none';
 3613:                 }
 3614:             }
 3615:         }
 3616:     }
 3617:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 3618:               '<p>'.$lt{'whi'}.'</p>';
 3619:     foreach my $access (@{$accesstypes}) {
 3620:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 3621:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 3622:                    $lt{$access}.'</label>';
 3623:         if ($access eq 'status') {
 3624:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 3625:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 3626:                                                                  $othertitle,$usertypes,$types).
 3627:                        '</div>';
 3628:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 3629:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 3630:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3631:                        '</div>';
 3632:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 3633:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 3634:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3635:                        '</div>';
 3636:         }
 3637:         $output .= '</p>';
 3638:     }
 3639:     $output .= '</fieldset>';
 3640:     return $output;
 3641: }
 3642: 
 3643: sub radiobutton_prefs {
 3644:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 3645:         $additional,$align) = @_;
 3646:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 3647:                    (ref($choices) eq 'HASH'));
 3648: 
 3649:     my (%checkedon,%checkedoff,$datatable,$css_class);
 3650: 
 3651:     foreach my $item (@{$toggles}) {
 3652:         if ($defaultchecked->{$item} eq 'on') {
 3653:             $checkedon{$item} = ' checked="checked" ';
 3654:             $checkedoff{$item} = ' ';
 3655:         } elsif ($defaultchecked->{$item} eq 'off') {
 3656:             $checkedoff{$item} = ' checked="checked" ';
 3657:             $checkedon{$item} = ' ';
 3658:         }
 3659:     }
 3660:     if (ref($settings) eq 'HASH') {
 3661:         foreach my $item (@{$toggles}) {
 3662:             if ($settings->{$item} eq '1') {
 3663:                 $checkedon{$item} =  ' checked="checked" ';
 3664:                 $checkedoff{$item} = ' ';
 3665:             } elsif ($settings->{$item} eq '0') {
 3666:                 $checkedoff{$item} =  ' checked="checked" ';
 3667:                 $checkedon{$item} = ' ';
 3668:             }
 3669:         }
 3670:     }
 3671:     if ($onclick) {
 3672:         $onclick = ' onclick="'.$onclick.'"';
 3673:     }
 3674:     foreach my $item (@{$toggles}) {
 3675:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3676:         $datatable .=
 3677:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 3678:             '<span class="LC_nobreak">'.$choices->{$item}.
 3679:             '</span></td>';
 3680:         if ($align eq 'left') {
 3681:             $datatable .= '<td class="LC_left_item">';
 3682:         } else {
 3683:             $datatable .= '<td class="LC_right_item">';
 3684:         }
 3685:         $datatable .=
 3686:             '<span class="LC_nobreak">'.
 3687:             '<label><input type="radio" name="'.
 3688:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 3689:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 3690:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3691:             '</span>'.$additional.
 3692:             '</td>'.
 3693:             '</tr>';
 3694:         $itemcount ++;
 3695:     }
 3696:     return ($datatable,$itemcount);
 3697: }
 3698: 
 3699: sub print_ltitools {
 3700:     my ($dom,$settings,$rowtotal) = @_;
 3701:     my $rownum = 0;
 3702:     my $css_class;
 3703:     my $itemcount = 1;
 3704:     my $maxnum = 0;
 3705:     my %ordered;
 3706:     if (ref($settings) eq 'HASH') {
 3707:         foreach my $item (keys(%{$settings})) {
 3708:             if (ref($settings->{$item}) eq 'HASH') {
 3709:                 my $num = $settings->{$item}{'order'};
 3710:                 $ordered{$num} = $item;
 3711:             }
 3712:         }
 3713:     }
 3714:     my $confname = $dom.'-domainconfig';
 3715:     my $switchserver = &check_switchserver($dom,$confname);
 3716:     my $maxnum = scalar(keys(%ordered));
 3717:     my $datatable = &ltitools_javascript($settings);
 3718:     my %lt = &ltitools_names();
 3719:     my @courseroles = ('cc','in','ta','ep','st');
 3720:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 3721:     my @fields = ('fullname','firstname','lastname','email','user','roles');
 3722:     if (keys(%ordered)) {
 3723:         my @items = sort { $a <=> $b } keys(%ordered);
 3724:         for (my $i=0; $i<@items; $i++) {
 3725:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3726:             my $item = $ordered{$items[$i]};
 3727:             my ($title,$key,$secret,$url,$imgsrc,$version);
 3728:             if (ref($settings->{$item}) eq 'HASH') {
 3729:                 $title = $settings->{$item}->{'title'};
 3730:                 $url = $settings->{$item}->{'url'};
 3731:                 $key = $settings->{$item}->{'key'};
 3732:                 $secret = $settings->{$item}->{'secret'};
 3733:                 my $image = $settings->{$item}->{'image'};
 3734:                 if ($image ne '') {
 3735:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 3736:                 }
 3737:             }
 3738:             my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_".$item."'".');"';
 3739:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3740:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 3741:             for (my $k=0; $k<=$maxnum; $k++) {
 3742:                 my $vpos = $k+1;
 3743:                 my $selstr;
 3744:                 if ($k == $i) {
 3745:                     $selstr = ' selected="selected" ';
 3746:                 }
 3747:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3748:             }
 3749:             $datatable .= '</select>'.('&nbsp;'x2).
 3750:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 3751:                 &mt('Delete?').'</label></span></td>'.
 3752:                 '<td colspan="2">'.
 3753:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 3754:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 3755:                 ('&nbsp;'x2).
 3756:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 3757:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 3758:                 ('&nbsp;'x2).
 3759:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 3760:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 3761:                 '<br /><br />'.
 3762:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_url_'.$i.'"'.
 3763:                 ' value="'.$url.'" /></span>'.
 3764:                 ('&nbsp;'x2).
 3765:                 '<span class="LC_nobreak">'.$lt{'key'}.
 3766:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 3767:                 ('&nbsp;'x2).
 3768:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 3769:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 3770:                 '<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>'.
 3771:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 3772:                 '</fieldset>'.
 3773:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 3774:                 '<span class="LC_nobreak">'.&mt('Display target:');
 3775:             my %currdisp;
 3776:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 3777:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 3778:                     $currdisp{'window'} = ' checked="checked"';
 3779:                 } elsif ($settings->{$item}->{'display'}->{'target'} eq 'tab') {
 3780:                     $currdisp{'tab'} = ' checked="checked"';
 3781:                 } else {
 3782:                     $currdisp{'iframe'} = ' checked="checked"';
 3783:                 }
 3784:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 3785:                     $currdisp{'width'} = $1;
 3786:                 }
 3787:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 3788:                      $currdisp{'height'} = $1;
 3789:                 }
 3790:                 $currdisp{'linktext'} = $settings->{$item}->{'display'}->{'linktext'};
 3791:                 $currdisp{'explanation'} = $settings->{$item}->{'display'}->{'explanation'};
 3792:             } else {
 3793:                 $currdisp{'iframe'} = ' checked="checked"';
 3794:             }
 3795:             foreach my $disp ('iframe','tab','window') {
 3796:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 3797:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 3798:             }
 3799:             $datatable .= ('&nbsp;'x4);
 3800:             foreach my $dimen ('width','height') {
 3801:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 3802:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 3803:                               ('&nbsp;'x2);
 3804:             }
 3805:             $datatable .= '<br />'.
 3806:                           '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 3807:                           '<input type="text" name="ltitools_linktext_'.$i.'" size="25" value="'.$currdisp{'linktext'}.'" /></label></div>'.
 3808:                           '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 3809:                           '<textarea name="ltitools_explanation_'.$i.'" rows="5" cols="40">'.$currdisp{'explanation'}.
 3810:                           '</textarea></div><div style=""></div><br />';
 3811:             $datatable .= '<br />';
 3812:             foreach my $extra ('passback','roster') {
 3813:                 my $checkedon = '';
 3814:                 my $checkedoff = ' checked="checked"';
 3815:                 if ($settings->{$item}->{$extra}) {
 3816:                     $checkedon = $checkedoff;
 3817:                     $checkedoff = '';
 3818:                 }
 3819:                 $datatable .= $lt{$extra}.'&nbsp;'.
 3820:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.' />'.
 3821:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 3822:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.' />'.
 3823:                               &mt('No').'</label>'.('&nbsp;'x4);
 3824:             }
 3825:             $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 3826:             if ($imgsrc) {
 3827:                 $datatable .= $imgsrc.
 3828:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 3829:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 3830:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 3831:             } else {
 3832:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 3833:             }
 3834:             if ($switchserver) {
 3835:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3836:             } else {
 3837:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 3838:             }
 3839:             $datatable .= '</span></fieldset>';
 3840:             my (%checkedfields,%rolemaps);
 3841:             if (ref($settings->{$item}) eq 'HASH') {
 3842:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 3843:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 3844:                 }
 3845:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 3846:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 3847:                     $checkedfields{'roles'} = 1;
 3848:                 }
 3849:             }
 3850:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 3851:                           '<span class="LC_nobreak">';
 3852:             foreach my $field (@fields) {
 3853:                 my $checked;
 3854:                 if ($checkedfields{$field}) {
 3855:                     $checked = ' checked="checked"';
 3856:                 }
 3857:                 $datatable .= '<label>'.
 3858:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$checked.' />'.
 3859:                               $lt{$field}.'</label>'.('&nbsp;' x2);
 3860:             }
 3861:             $datatable .= '</span></fieldset>'.
 3862:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 3863:             foreach my $role (@courseroles) {
 3864:                 my ($selected,$selectnone);
 3865:                 if (!$rolemaps{$role}) {
 3866:                     $selectnone = ' selected="selected"';
 3867:                 }
 3868:                 $datatable .= '<td style="text-align: center">'. 
 3869:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 3870:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 3871:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 3872:                 foreach my $ltirole (@ltiroles) {
 3873:                     unless ($selectnone) {
 3874:                         if ($rolemaps{$role} eq $ltirole) {
 3875:                             $selected = ' selected="selected"';
 3876:                         } else {
 3877:                             $selected = '';
 3878:                         }
 3879:                     }
 3880:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 3881:                 }
 3882:                 $datatable .= '</select></td>';
 3883:             }
 3884:             $datatable .= '</tr></table></fieldset>';
 3885:             my %courseconfig;
 3886:             if (ref($settings->{$item}) eq 'HASH') {
 3887:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 3888:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 3889:                 }
 3890:             }
 3891:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 3892:             foreach my $item ('label','title','target','linktext','explanation') {
 3893:                 my $checked;
 3894:                 if ($courseconfig{$item}) {
 3895:                     $checked = ' checked="checked"';
 3896:                 }
 3897:                 $datatable .= '<label>'.
 3898:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 3899:                        $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 3900:             }
 3901:             $datatable .= '</span></fieldset>'.
 3902:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 3903:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 3904:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 3905:                 my %custom = %{$settings->{$item}->{'custom'}};
 3906:                 if (keys(%custom) > 0) {
 3907:                     foreach my $key (sort(keys(%custom))) {
 3908:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 3909:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 3910:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 3911:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 3912:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 3913:                     }
 3914:                 }
 3915:             }
 3916:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 3917:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 3918:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 3919:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 3920:             $datatable .= '</table></fieldset></td></tr>'."\n";
 3921:             $itemcount ++;
 3922:         }
 3923:     }
 3924:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3925:     my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_add_pos'".');"';
 3926:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 3927:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 3928:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 3929:     for (my $k=0; $k<$maxnum+1; $k++) {
 3930:         my $vpos = $k+1;
 3931:         my $selstr;
 3932:         if ($k == $maxnum) {
 3933:             $selstr = ' selected="selected" ';
 3934:         }
 3935:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3936:     }
 3937:     $datatable .= '</select>&nbsp;'."\n".
 3938:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</td>'."\n".
 3939:                   '<td colspan="2">'.
 3940:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 3941:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_add_title" value="" /></span> '."\n".
 3942:                   ('&nbsp;'x2).
 3943:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 3944:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 3945:                   ('&nbsp;'x2).
 3946:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 3947:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 3948:                   '<br />'.
 3949:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_add_url" value="" /></span> '."\n".
 3950:                   ('&nbsp;'x2).
 3951:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 3952:                   ('&nbsp;'x2).
 3953:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 3954:                   '<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".
 3955:                   '</fieldset>'.
 3956:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 3957:                   '<span class="LC_nobreak">'.&mt('Display target:');
 3958:     my %defaultdisp;
 3959:     $defaultdisp{'iframe'} = ' checked="checked"';
 3960:     foreach my $disp ('iframe','tab','window') {
 3961:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 3962:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 3963:     }
 3964:     $datatable .= ('&nbsp;'x4);
 3965:     foreach my $dimen ('width','height') {
 3966:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 3967:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 3968:                       ('&nbsp;'x2);
 3969:     }
 3970:     $datatable .= '<br />'.
 3971:                   '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 3972:                   '<input type="text" name="ltitools_add_linktext" size="5" /></label></div>'.
 3973:                   '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 3974:                   '<textarea name=ltitools_add_explanation" rows="5" cols="40"></textarea>'.
 3975:                   '</div><div style=""></div><br />';
 3976:     foreach my $extra ('passback','roster') {
 3977:         $datatable .= $lt{$extra}.'&nbsp;'.
 3978:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="1" />'.
 3979:                       &mt('Yes').'</label>'.('&nbsp;'x2).
 3980:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="0" checked="checked" />'.
 3981:                       &mt('No').'</label>'.('&nbsp;'x4);
 3982:     }
 3983:     $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 3984:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 3985:     if ($switchserver) {
 3986:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3987:     } else {
 3988:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 3989:     }
 3990:     $datatable .= '</span></fieldset>'.
 3991:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 3992:                   '<span class="LC_nobreak">';
 3993:     foreach my $field (@fields) {
 3994:         $datatable .= '<label>'.
 3995:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'" />'.
 3996:                       $lt{$field}.'</label>'.('&nbsp;' x2);
 3997:     }
 3998:     $datatable .= '</span></fieldset>'.
 3999:                   '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4000:     foreach my $role (@courseroles) {
 4001:         my ($checked,$checkednone);
 4002:         $datatable .= '<td style="text-align: center">'.
 4003:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4004:                       '<select name="ltitools_add_roles_'.$role.'">'.
 4005:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 4006:         foreach my $ltirole (@ltiroles) {
 4007:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 4008:         }
 4009:         $datatable .= '</select></td>';
 4010:     }
 4011:     $datatable .= '</tr></table></fieldset>'.
 4012:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4013:     foreach my $item ('label','title','target','linktext','explanation') {
 4014:         $datatable .= '<label>'.
 4015:                       '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 4016:                       $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4017:     }
 4018:     $datatable .= '</span></fieldset>'.
 4019:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4020:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 4021:                   '<tr><td><span class="LC_nobreak">'.
 4022:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 4023:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 4024:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 4025:                   '</table></fieldset></td></tr>'."\n".
 4026:                   '</td>'."\n".
 4027:                   '</tr>'."\n";
 4028:     $itemcount ++;
 4029:     return $datatable;
 4030: }
 4031: 
 4032: sub ltitools_names {
 4033:     my %lt = &Apache::lonlocal::texthash(
 4034:                                           'title'          => 'Title',
 4035:                                           'version'        => 'Version',
 4036:                                           'msgtype'        => 'Message Type',
 4037:                                           'url'            => 'URL',
 4038:                                           'key'            => 'Key',
 4039:                                           'secret'         => 'Secret',
 4040:                                           'icon'           => 'Icon',   
 4041:                                           'user'           => 'Username:domain',
 4042:                                           'fullname'       => 'Full Name',
 4043:                                           'firstname'      => 'First Name',
 4044:                                           'lastname'       => 'Last Name',
 4045:                                           'email'          => 'E-mail',
 4046:                                           'roles'          => 'Role',
 4047:                                           'window'         => 'Window',
 4048:                                           'tab'            => 'Tab',
 4049:                                           'iframe'         => 'iFrame',
 4050:                                           'height'         => 'Height',
 4051:                                           'width'          => 'Width',
 4052:                                           'linktext'       => 'Default Link Text',
 4053:                                           'explanation'    => 'Default Explanation',
 4054:                                           'passback'       => 'Tool can return grades:',
 4055:                                           'roster'         => 'Tool can retrieve roster:',
 4056:                                           'crstarget'      => 'Display target',
 4057:                                           'crslabel'       => 'Course label',
 4058:                                           'crstitle'       => 'Course title', 
 4059:                                           'crslinktext'    => 'Link Text',
 4060:                                           'crsexplanation' => 'Explanation',
 4061:                                         );
 4062:     return %lt;
 4063: }
 4064: 
 4065: sub print_coursedefaults {
 4066:     my ($position,$dom,$settings,$rowtotal) = @_;
 4067:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 4068:     my $itemcount = 1;
 4069:     my %choices =  &Apache::lonlocal::texthash (
 4070:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 4071:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 4072:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 4073:         coursecredits        => 'Credits can be specified for courses',
 4074:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 4075:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 4076:         postsubmit           => 'Disable submit button/keypress following student submission',
 4077:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 4078:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 4079:     );
 4080:     my %staticdefaults = (
 4081:                            anonsurvey_threshold => 10,
 4082:                            uploadquota          => 500,
 4083:                            postsubmit           => 60,
 4084:                            mysqltables          => 172800,
 4085:                          );
 4086:     if ($position eq 'top') {
 4087:         %defaultchecked = (
 4088:                             'canuse_pdfforms' => 'off',
 4089:                             'uselcmath'       => 'on',
 4090:                             'usejsme'         => 'on',
 4091:                             'canclone'        => 'none',
 4092:                           );
 4093:         @toggles = ('canuse_pdfforms','uselcmath','usejsme');
 4094:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4095:                                                      \%choices,$itemcount);
 4096:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4097:         $datatable .=
 4098:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 4099:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 4100:             '</span></td><td class="LC_left_item">';
 4101:         my $currcanclone = 'none';
 4102:         my $onclick;
 4103:         my @cloneoptions = ('none','domain');
 4104:         my %clonetitles = (
 4105:                              none     => 'No additional course requesters',
 4106:                              domain   => "Any course requester in course's domain",
 4107:                              instcode => 'Course requests for official courses ...',
 4108:                           );
 4109:         my (%codedefaults,@code_order,@posscodes);
 4110:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 4111:                                                     \@code_order) eq 'ok') {
 4112:             if (@code_order > 0) {
 4113:                 push(@cloneoptions,'instcode');
 4114:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 4115:             }
 4116:         }
 4117:         if (ref($settings) eq 'HASH') {
 4118:             if ($settings->{'canclone'}) {
 4119:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 4120:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 4121:                         if (@code_order > 0) {
 4122:                             $currcanclone = 'instcode';
 4123:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 4124:                         }
 4125:                     }
 4126:                 } elsif ($settings->{'canclone'} eq 'domain') {
 4127:                     $currcanclone = $settings->{'canclone'};
 4128:                 }
 4129:             }
 4130:         }
 4131:         foreach my $option (@cloneoptions) {
 4132:             my ($checked,$additional);
 4133:             if ($currcanclone eq $option) {
 4134:                 $checked = ' checked="checked"';
 4135:             }
 4136:             if ($option eq 'instcode') {
 4137:                 if (@code_order) {
 4138:                     my $show = 'none';
 4139:                     if ($checked) {
 4140:                         $show = 'block';
 4141:                     }
 4142:                     $additional = '<div id="cloneinstcode" style="display:'.$show.'" />'.
 4143:                                   &mt('Institutional codes for new and cloned course have identical:').
 4144:                                   '<br />';
 4145:                     foreach my $item (@code_order) {
 4146:                         my $codechk;
 4147:                         if ($checked) {
 4148:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 4149:                                 $codechk = ' checked="checked"';
 4150:                             }
 4151:                         }
 4152:                         $additional .= '<label>'.
 4153:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 4154:                                        $item.'</label>';
 4155:                     }
 4156:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 4157:                 }
 4158:             }
 4159:             $datatable .=
 4160:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 4161:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 4162:                 '</label>&nbsp;'.$additional.'</span><br />';
 4163:         }
 4164:         $datatable .= '</td>'.
 4165:                       '</tr>';
 4166:         $itemcount ++;
 4167:     } else {
 4168:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4169:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 4170:         my $currusecredits = 0;
 4171:         my $postsubmitclient = 1;
 4172:         my @types = ('official','unofficial','community','textbook','placement');
 4173:         if (ref($settings) eq 'HASH') {
 4174:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 4175:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 4176:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 4177:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 4178:                 }
 4179:             }
 4180:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 4181:                 foreach my $type (@types) {
 4182:                     next if ($type eq 'community');
 4183:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 4184:                     if ($defcredits{$type} ne '') {
 4185:                         $currusecredits = 1;
 4186:                     }
 4187:                 }
 4188:             }
 4189:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 4190:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 4191:                     $postsubmitclient = 0;
 4192:                     foreach my $type (@types) {
 4193:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4194:                     }
 4195:                 } else {
 4196:                     foreach my $type (@types) {
 4197:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 4198:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 4199:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 4200:                             } else {
 4201:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4202:                             }
 4203:                         } else {
 4204:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4205:                         }
 4206:                     }
 4207:                 }
 4208:             } else {
 4209:                 foreach my $type (@types) {
 4210:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4211:                 }
 4212:             }
 4213:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 4214:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 4215:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 4216:                 }
 4217:             } else {
 4218:                 foreach my $type (@types) {
 4219:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 4220:                 }
 4221:             }
 4222:         } else {
 4223:             foreach my $type (@types) {
 4224:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4225:             }
 4226:         }
 4227:         if (!$currdefresponder) {
 4228:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 4229:         } elsif ($currdefresponder < 1) {
 4230:             $currdefresponder = 1;
 4231:         }
 4232:         foreach my $type (@types) {
 4233:             if ($curruploadquota{$type} eq '') {
 4234:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 4235:             }
 4236:         }
 4237:         $datatable .=
 4238:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4239:                 $choices{'anonsurvey_threshold'}.
 4240:                 '</span></td>'.
 4241:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 4242:                 '<input type="text" name="anonsurvey_threshold"'.
 4243:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 4244:                 '</td></tr>'."\n";
 4245:         $itemcount ++;
 4246:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4247:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4248:                       $choices{'uploadquota'}.
 4249:                       '</span></td>'.
 4250:                       '<td style="text-align: right" class="LC_right_item">'.
 4251:                       '<table><tr>';
 4252:         foreach my $type (@types) {
 4253:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 4254:                            '<input type="text" name="uploadquota_'.$type.'"'.
 4255:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 4256:         }
 4257:         $datatable .= '</tr></table></td></tr>'."\n";
 4258:         $itemcount ++;
 4259:         my $onclick = "toggleDisplay(this.form,'credits');";
 4260:         my $display = 'none';
 4261:         if ($currusecredits) {
 4262:             $display = 'block';
 4263:         }
 4264:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 4265:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 4266:         foreach my $type (@types) {
 4267:             next if ($type eq 'community');
 4268:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 4269:                            '<input type="text" name="'.$type.'_credits"'.
 4270:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 4271:         }
 4272:         $additional .= '</tr></table></div>'."\n";
 4273:         %defaultchecked = ('coursecredits' => 'off');
 4274:         @toggles = ('coursecredits');
 4275:         my $current = {
 4276:                         'coursecredits' => $currusecredits,
 4277:                       };
 4278:         (my $table,$itemcount) =
 4279:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4280:                                \%choices,$itemcount,$onclick,$additional,'left');
 4281:         $datatable .= $table;
 4282:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 4283:         my $display = 'none';
 4284:         if ($postsubmitclient) {
 4285:             $display = 'block';
 4286:         }
 4287:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 4288:                       &mt('Number of seconds submit is disabled').'<br />'.
 4289:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 4290:                       '<table><tr>';
 4291:         foreach my $type (@types) {
 4292:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 4293:                            '<input type="text" name="'.$type.'_timeout" value="'.
 4294:                            $deftimeout{$type}.'" size="5" /></td>';
 4295:         }
 4296:         $additional .= '</tr></table></div>'."\n";
 4297:         %defaultchecked = ('postsubmit' => 'on');
 4298:         @toggles = ('postsubmit');
 4299:         $current = {
 4300:                        'postsubmit' => $postsubmitclient,
 4301:                    };
 4302:         ($table,$itemcount) =
 4303:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4304:                                \%choices,$itemcount,$onclick,$additional,'left');
 4305:         $datatable .= $table;
 4306:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4307:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4308:                       $choices{'mysqltables'}.
 4309:                       '</span></td>'.
 4310:                       '<td style="text-align: right" class="LC_right_item">'.
 4311:                       '<table><tr>';
 4312:         foreach my $type (@types) {
 4313:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 4314:                            '<input type="text" name="mysqltables_'.$type.'"'.
 4315:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 4316:         }
 4317:         $datatable .= '</tr></table></td></tr>'."\n";
 4318:         $itemcount ++;
 4319: 
 4320:     }
 4321:     $$rowtotal += $itemcount;
 4322:     return $datatable;
 4323: }
 4324: 
 4325: sub print_selfenrollment {
 4326:     my ($position,$dom,$settings,$rowtotal) = @_;
 4327:     my ($css_class,$datatable);
 4328:     my $itemcount = 1;
 4329:     my @types = ('official','unofficial','community','textbook','placement');
 4330:     if (($position eq 'top') || ($position eq 'middle')) {
 4331:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 4332:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 4333:         my @rows;
 4334:         my $key;
 4335:         if ($position eq 'top') {
 4336:             $key = 'admin'; 
 4337:             if (ref($rowsref) eq 'ARRAY') {
 4338:                 @rows = @{$rowsref};
 4339:             }
 4340:         } elsif ($position eq 'middle') {
 4341:             $key = 'default';
 4342:             @rows = ('types','registered','approval','limit');
 4343:         }
 4344:         foreach my $row (@rows) {
 4345:             if (defined($titlesref->{$row})) {
 4346:                 $itemcount ++;
 4347:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4348:                 $datatable .= '<tr'.$css_class.'>'.
 4349:                               '<td>'.$titlesref->{$row}.'</td>'.
 4350:                               '<td class="LC_left_item">'.
 4351:                               '<table><tr>';
 4352:                 my (%current,%currentcap);
 4353:                 if (ref($settings) eq 'HASH') {
 4354:                     if (ref($settings->{$key}) eq 'HASH') {
 4355:                         foreach my $type (@types) {
 4356:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4357:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 4358:                             }
 4359:                             if (($row eq 'limit') && ($key eq 'default')) {
 4360:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4361:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 4362:                                 }
 4363:                             }
 4364:                         }
 4365:                     }
 4366:                 }
 4367:                 my %roles = (
 4368:                              '0' => &Apache::lonnet::plaintext('dc'),
 4369:                             ); 
 4370:             
 4371:                 foreach my $type (@types) {
 4372:                     unless (($row eq 'registered') && ($key eq 'default')) {
 4373:                         $datatable .= '<th>'.&mt($type).'</th>';
 4374:                     }
 4375:                 }
 4376:                 unless (($row eq 'registered') && ($key eq 'default')) {
 4377:                     $datatable .= '</tr><tr>';
 4378:                 }
 4379:                 foreach my $type (@types) {
 4380:                     if ($type eq 'community') {
 4381:                         $roles{'1'} = &mt('Community personnel');
 4382:                     } else {
 4383:                         $roles{'1'} = &mt('Course personnel');
 4384:                     }
 4385:                     $datatable .= '<td style="vertical-align: top">';
 4386:                     if ($position eq 'top') {
 4387:                         my %checked;
 4388:                         if ($current{$type} eq '0') {
 4389:                             $checked{'0'} = ' checked="checked"';
 4390:                         } else {
 4391:                             $checked{'1'} = ' checked="checked"';
 4392:                         }
 4393:                         foreach my $role ('1','0') {
 4394:                             $datatable .= '<span class="LC_nobreak"><label>'.
 4395:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 4396:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 4397:                                           $roles{$role}.'</label></span> ';
 4398:                         }
 4399:                     } else {
 4400:                         if ($row eq 'types') {
 4401:                             my %checked;
 4402:                             if ($current{$type} =~ /^(all|dom)$/) {
 4403:                                 $checked{$1} = ' checked="checked"';
 4404:                             } else {
 4405:                                 $checked{''} = ' checked="checked"';
 4406:                             }
 4407:                             foreach my $val ('','dom','all') {
 4408:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4409:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4410:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4411:                             }
 4412:                         } elsif ($row eq 'registered') {
 4413:                             my %checked;
 4414:                             if ($current{$type} eq '1') {
 4415:                                 $checked{'1'} = ' checked="checked"';
 4416:                             } else {
 4417:                                 $checked{'0'} = ' checked="checked"';
 4418:                             }
 4419:                             foreach my $val ('0','1') {
 4420:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4421:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4422:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4423:                             }
 4424:                         } elsif ($row eq 'approval') {
 4425:                             my %checked;
 4426:                             if ($current{$type} =~ /^([12])$/) {
 4427:                                 $checked{$1} = ' checked="checked"';
 4428:                             } else {
 4429:                                 $checked{'0'} = ' checked="checked"';
 4430:                             }
 4431:                             for my $val (0..2) {
 4432:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4433:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4434:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4435:                             }
 4436:                         } elsif ($row eq 'limit') {
 4437:                             my %checked;
 4438:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 4439:                                 $checked{$1} = ' checked="checked"';
 4440:                             } else {
 4441:                                 $checked{'none'} = ' checked="checked"';
 4442:                             }
 4443:                             my $cap;
 4444:                             if ($currentcap{$type} =~ /^\d+$/) {
 4445:                                 $cap = $currentcap{$type};
 4446:                             }
 4447:                             foreach my $val ('none','allstudents','selfenrolled') {
 4448:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4449:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4450:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4451:                             }
 4452:                             $datatable .= '<br />'.
 4453:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 4454:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 4455:                                           '</span>'; 
 4456:                         }
 4457:                     }
 4458:                     $datatable .= '</td>';
 4459:                 }
 4460:                 $datatable .= '</tr>';
 4461:             }
 4462:             $datatable .= '</table></td></tr>';
 4463:         }
 4464:     } elsif ($position eq 'bottom') {
 4465:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 4466:     }
 4467:     $$rowtotal += $itemcount;
 4468:     return $datatable;
 4469: }
 4470: 
 4471: sub print_validation_rows {
 4472:     my ($caller,$dom,$settings,$rowtotal) = @_;
 4473:     my ($itemsref,$namesref,$fieldsref);
 4474:     if ($caller eq 'selfenroll') { 
 4475:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 4476:     } elsif ($caller eq 'requestcourses') {
 4477:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 4478:     }
 4479:     my %currvalidation;
 4480:     if (ref($settings) eq 'HASH') {
 4481:         if (ref($settings->{'validation'}) eq 'HASH') {
 4482:             %currvalidation = %{$settings->{'validation'}};
 4483:         }
 4484:     }
 4485:     my $datatable;
 4486:     my $itemcount = 0;
 4487:     foreach my $item (@{$itemsref}) {
 4488:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4489:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4490:                       $namesref->{$item}.
 4491:                       '</span></td>'.
 4492:                       '<td class="LC_left_item">';
 4493:         if (($item eq 'url') || ($item eq 'button')) {
 4494:             $datatable .= '<span class="LC_nobreak">'.
 4495:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 4496:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 4497:         } elsif ($item eq 'fields') {
 4498:             my @currfields;
 4499:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 4500:                 @currfields = @{$currvalidation{$item}};
 4501:             }
 4502:             foreach my $field (@{$fieldsref}) {
 4503:                 my $check = '';
 4504:                 if (grep(/^\Q$field\E$/,@currfields)) {
 4505:                     $check = ' checked="checked"';
 4506:                 }
 4507:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4508:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 4509:                               ' value="'.$field.'"'.$check.' />'.$field.
 4510:                               '</label></span> ';
 4511:             }
 4512:         } elsif ($item eq 'markup') {
 4513:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 4514:                            $currvalidation{$item}.
 4515:                               '</textarea>';
 4516:         }
 4517:         $datatable .= '</td></tr>'."\n";
 4518:         if (ref($rowtotal)) {
 4519:             $itemcount ++;
 4520:         }
 4521:     }
 4522:     if ($caller eq 'requestcourses') {
 4523:         my %currhash;
 4524:         if (ref($settings) eq 'HASH') {
 4525:             if (ref($settings->{'validation'}) eq 'HASH') {
 4526:                 if ($settings->{'validation'}{'dc'} ne '') {
 4527:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 4528:                 }
 4529:             }
 4530:         }
 4531:         my $numinrow = 2;
 4532:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4533:                                                        'validationdc',%currhash);
 4534:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4535:         $datatable .= '</td></tr><tr'.$css_class.'><td>';
 4536:         if ($numdc > 1) {
 4537:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 4538:         } else {
 4539:             $datatable .=  &mt('Course creation processed as: ');
 4540:         }
 4541:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 4542:         $itemcount ++;
 4543:     }
 4544:     if (ref($rowtotal)) {
 4545:         $$rowtotal += $itemcount;
 4546:     }
 4547:     return $datatable;
 4548: }
 4549: 
 4550: sub print_usersessions {
 4551:     my ($position,$dom,$settings,$rowtotal) = @_;
 4552:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 4553:     my (%by_ip,%by_location,@intdoms,@instdoms);
 4554:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 4555: 
 4556:     my @alldoms = &Apache::lonnet::all_domains();
 4557:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 4558:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4559:     my %altids = &id_for_thisdom(%servers);
 4560:     if ($position eq 'top') {
 4561:         if (keys(%serverhomes) > 1) {
 4562:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 4563:             my $curroffloadnow;
 4564:             if (ref($settings) eq 'HASH') {
 4565:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 4566:                     $curroffloadnow = $settings->{'offloadnow'};
 4567:                 }
 4568:             }
 4569:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 4570:         } else {
 4571:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 4572:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 4573:                           '</td></tr>';
 4574:         }
 4575:     } else {
 4576:         my %titles = &usersession_titles();
 4577:         my ($prefix,@types);
 4578:         if ($position eq 'bottom') {
 4579:             $prefix = 'remote';
 4580:             @types = ('version','excludedomain','includedomain');
 4581:         } else {
 4582:             $prefix = 'hosted';
 4583:             @types = ('excludedomain','includedomain');
 4584:         }
 4585:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 4586:     }
 4587:     $$rowtotal += $itemcount;
 4588:     return $datatable;
 4589: }
 4590: 
 4591: sub rules_by_location {
 4592:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 4593:     my ($datatable,$itemcount,$css_class);
 4594:     if (keys(%{$by_location}) == 0) {
 4595:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4596:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 4597:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 4598:                      '</td></tr>';
 4599:         $itemcount = 1;
 4600:     } else {
 4601:         $itemcount = 0;
 4602:         my $numinrow = 5;
 4603:         my (%current,%checkedon,%checkedoff);
 4604:         my @locations = sort(keys(%{$by_location}));
 4605:         foreach my $type (@{$types}) {
 4606:             $checkedon{$type} = '';
 4607:             $checkedoff{$type} = ' checked="checked"';
 4608:         }
 4609:         if (ref($settings) eq 'HASH') {
 4610:             if (ref($settings->{$prefix}) eq 'HASH') {
 4611:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 4612:                     $current{$key} = $settings->{$prefix}{$key};
 4613:                     if ($key eq 'version') {
 4614:                         if ($current{$key} ne '') {
 4615:                             $checkedon{$key} = ' checked="checked"';
 4616:                             $checkedoff{$key} = '';
 4617:                         }
 4618:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 4619:                         $checkedon{$key} = ' checked="checked"';
 4620:                         $checkedoff{$key} = '';
 4621:                     }
 4622:                 }
 4623:             }
 4624:         }
 4625:         foreach my $type (@{$types}) {
 4626:             next if ($type ne 'version' && !@locations);
 4627:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4628:             $datatable .= '<tr'.$css_class.'>
 4629:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 4630:                            <span class="LC_nobreak">&nbsp;
 4631:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 4632:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 4633:             if ($type eq 'version') {
 4634:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 4635:                 my $selector = '<select name="'.$prefix.'_version">';
 4636:                 foreach my $version (@lcversions) {
 4637:                     my $selected = '';
 4638:                     if ($current{'version'} eq $version) {
 4639:                         $selected = ' selected="selected"';
 4640:                     }
 4641:                     $selector .= ' <option value="'.$version.'"'.
 4642:                                  $selected.'>'.$version.'</option>';
 4643:                 }
 4644:                 $selector .= '</select> ';
 4645:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 4646:             } else {
 4647:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 4648:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 4649:                              ' />'.('&nbsp;'x2).
 4650:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 4651:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 4652:                              "\n".
 4653:                              '</div><div><table>';
 4654:                 my $rem;
 4655:                 for (my $i=0; $i<@locations; $i++) {
 4656:                     my ($showloc,$value,$checkedtype);
 4657:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 4658:                         my $ip = $by_location->{$locations[$i]}->[0];
 4659:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4660:                             $value = join(':',@{$by_ip->{$ip}});
 4661:                             $showloc = join(', ',@{$by_ip->{$ip}});
 4662:                             if (ref($current{$type}) eq 'ARRAY') {
 4663:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 4664:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 4665:                                         $checkedtype = ' checked="checked"';
 4666:                                         last;
 4667:                                     }
 4668:                                 }
 4669:                             }
 4670:                         }
 4671:                     }
 4672:                     $rem = $i%($numinrow);
 4673:                     if ($rem == 0) {
 4674:                         if ($i > 0) {
 4675:                             $datatable .= '</tr>';
 4676:                         }
 4677:                         $datatable .= '<tr>';
 4678:                     }
 4679:                     $datatable .= '<td class="LC_left_item">'.
 4680:                                   '<span class="LC_nobreak"><label>'.
 4681:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 4682:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 4683:                                   '</label></span></td>';
 4684:                 }
 4685:                 $rem = @locations%($numinrow);
 4686:                 my $colsleft = $numinrow - $rem;
 4687:                 if ($colsleft > 1 ) {
 4688:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4689:                                   '&nbsp;</td>';
 4690:                 } elsif ($colsleft == 1) {
 4691:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4692:                 }
 4693:                 $datatable .= '</tr></table>';
 4694:             }
 4695:             $datatable .= '</td></tr>';
 4696:             $itemcount ++;
 4697:         }
 4698:     }
 4699:     return ($datatable,$itemcount);
 4700: }
 4701: 
 4702: sub print_ssl {
 4703:     my ($position,$dom,$settings,$rowtotal) = @_;
 4704:     my ($css_class,$datatable);
 4705:     my $itemcount = 1;
 4706:     if ($position eq 'top') {
 4707:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 4708:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 4709:         my $same_institution;
 4710:         if ($intdom ne '') {
 4711:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 4712:             if (ref($internet_names) eq 'ARRAY') {
 4713:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 4714:                     $same_institution = 1;
 4715:                 }
 4716:             }
 4717:         }
 4718:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4719:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 4720:         if ($same_institution) {
 4721:             my %domservers = &Apache::lonnet::get_servers($dom);
 4722:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 4723:         } else {
 4724:             $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.");
 4725:         }
 4726:         $datatable .= '</td></tr>';
 4727:         $itemcount ++;
 4728:     } else {
 4729:         my %titles = &ssl_titles();
 4730:         my (%by_ip,%by_location,@intdoms,@instdoms);
 4731:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 4732:         my @alldoms = &Apache::lonnet::all_domains();
 4733:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 4734:         my @domservers = &Apache::lonnet::get_servers($dom);
 4735:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4736:         my %altids = &id_for_thisdom(%servers);
 4737:         if (($position eq 'connto') || ($position eq 'connfrom')) {
 4738:             my $legacy;
 4739:             unless (ref($settings) eq 'HASH') {
 4740:                 my $name;
 4741:                 if ($position eq 'connto') {
 4742:                     $name = 'loncAllowInsecure';
 4743:                 } else {
 4744:                     $name = 'londAllowInsecure';
 4745:                 }
 4746:                 my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
 4747:                 my @ids=&Apache::lonnet::current_machine_ids();
 4748:                 if (($primarylibserv ne '') && (!grep(/^\Q$primarylibserv\E$/,@ids))) {
 4749:                     my %what = (
 4750:                                    $name => 1,
 4751:                                );
 4752:                     my ($result,$returnhash) =
 4753:                         &Apache::lonnet::get_remote_globals($primarylibserv,\%what);
 4754:                     if ($result eq 'ok') {
 4755:                         if (ref($returnhash) eq 'HASH') {
 4756:                             $legacy = $returnhash->{$name};
 4757:                         }
 4758:                     }
 4759:                 } else {
 4760:                     $legacy = $Apache::lonnet::perlvar{$name};
 4761:                 }
 4762:             }
 4763:             foreach my $type ('dom','intdom','other') {
 4764:                 my %checked;
 4765:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4766:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 4767:                               '<td class="LC_right_item">';
 4768:                 my $skip; 
 4769:                 if ($type eq 'dom') {
 4770:                     unless (keys(%servers) > 1) {
 4771:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 4772:                         $skip = 1;
 4773:                     }
 4774:                 }
 4775:                 if ($type eq 'intdom') {
 4776:                     unless (@instdoms > 1) {
 4777:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 4778:                         $skip = 1;
 4779:                     } 
 4780:                 } elsif ($type eq 'other') {
 4781:                     if (keys(%by_location) == 0) {
 4782:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 4783:                         $skip = 1;
 4784:                     }
 4785:                 }
 4786:                 unless ($skip) {
 4787:                     $checked{'yes'} = ' checked="checked"'; 
 4788:                     if (ref($settings) eq 'HASH') {
 4789:                         if (ref($settings->{$position}) eq 'HASH') {
 4790:                             if ($settings->{$position}->{$type} =~ /^(no|req)$/) {
 4791:                                 $checked{$1} = $checked{'yes'};
 4792:                                 delete($checked{'yes'}); 
 4793:                             }
 4794:                         }
 4795:                     } else {
 4796:                         if ($legacy == 0) {
 4797:                             $checked{'req'} = $checked{'yes'};
 4798:                             delete($checked{'yes'});    
 4799:                         }
 4800:                     }
 4801:                     foreach my $option ('no','yes','req') {
 4802:                         $datatable .= '<span class="LC_nobreak"><label>'.
 4803:                                       '<input type="radio" name="'.$position.'_'.$type.'" '.
 4804:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 4805:                                       '</label></span>'.('&nbsp;'x2);
 4806:                     }
 4807:                 }
 4808:                 $datatable .= '</td></tr>';
 4809:                 $itemcount ++; 
 4810:             }
 4811:         } else {
 4812:             my $prefix = 'replication';
 4813:             my @types = ('certreq','nocertreq');
 4814:             if (keys(%by_location) == 0) {
 4815:                 $datatable .= '<tr'.$css_class.'><td>'.
 4816:                               &mt('Nothing to set here, as there are no other institutions').
 4817:                               '</td></tr>';
 4818:                 $itemcount ++;
 4819:             } else {
 4820:                 ($datatable,$itemcount) = 
 4821:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 4822:             }
 4823:         }
 4824:     }
 4825:     $$rowtotal += $itemcount;
 4826:     return $datatable;
 4827: }
 4828: 
 4829: sub ssl_titles {
 4830:     return &Apache::lonlocal::texthash (
 4831:                dom           => 'LON-CAPA servers/VMs from same domain',
 4832:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 4833:                other         => 'External LON-CAPA servers/VMs',
 4834:                connto        => 'Connections to other servers',
 4835:                connfrom      => 'Connections from other servers',
 4836:                replication   => 'Replicating content to other institutions',
 4837:                certreq       => 'Client certificate required, but specific domains exempt',
 4838:                nocertreq     => 'No client certificate required, except for specific domains',
 4839:                no            => 'SSL not used',
 4840:                yes           => 'SSL Optional (used if available)',
 4841:                req           => 'SSL Required',
 4842:     );
 4843: }
 4844: 
 4845: sub print_trust {
 4846:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 4847:     my ($css_class,$datatable,%checked,%choices);
 4848:     my (%by_ip,%by_location,@intdoms,@instdoms);
 4849:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 4850:     my $itemcount = 1;
 4851:     my %titles = &trust_titles();
 4852:     my @types = ('exc','inc');
 4853:     if ($prefix eq 'top') {
 4854:         $prefix = 'content';
 4855:     } elsif ($prefix eq 'bottom') {
 4856:         $prefix = 'msg';
 4857:     }
 4858:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 4859:     $$rowtotal += $itemcount;
 4860:     return $datatable;
 4861: }
 4862: 
 4863: sub trust_titles {
 4864:     return &Apache::lonlocal::texthash(
 4865:                content  => "Access to this domain's content by others",
 4866:                shared   => "Access to other domain's content by this domain",
 4867:                enroll   => "Enrollment in this domain's courses by others", 
 4868:                othcoau  => "Co-author roles in this domain for others",
 4869:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 4870:                domroles => "Domain roles in this domain assignable to others",
 4871:                catalog  => "Course Catalog for this domain displayed elsewhere",
 4872:                reqcrs   => "Requests for creation of courses in this domain by others",
 4873:                msg      => "Users in other domains can send messages to this domain",
 4874:                exc      => "Allow all, but exclude specific domains",
 4875:                inc      => "Deny all, but include specific domains",
 4876:            );
 4877: } 
 4878: 
 4879: sub build_location_hashes {
 4880:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 4881:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 4882:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 4883:     my %iphost = &Apache::lonnet::get_iphost();
 4884:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 4885:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 4886:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 4887:         foreach my $id (@{$iphost{$primary_ip}}) {
 4888:             my $intdom = &Apache::lonnet::internet_dom($id);
 4889:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 4890:                 push(@{$intdoms},$intdom);
 4891:             }
 4892:         }
 4893:     }
 4894:     foreach my $ip (keys(%iphost)) {
 4895:         if (ref($iphost{$ip}) eq 'ARRAY') {
 4896:             foreach my $id (@{$iphost{$ip}}) {
 4897:                 my $location = &Apache::lonnet::internet_dom($id);
 4898:                 if ($location) {
 4899:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 4900:                         my $dom = &Apache::lonnet::host_domain($id);
 4901:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 4902:                             push(@{$instdoms},$dom);
 4903:                         }
 4904:                         next;
 4905:                     }
 4906:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4907:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 4908:                             push(@{$by_ip->{$ip}},$location);
 4909:                         }
 4910:                     } else {
 4911:                         $by_ip->{$ip} = [$location];
 4912:                     }
 4913:                 }
 4914:             }
 4915:         }
 4916:     }
 4917:     foreach my $ip (sort(keys(%{$by_ip}))) {
 4918:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4919:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 4920:             my $first = $by_ip->{$ip}->[0];
 4921:             if (ref($by_location->{$first}) eq 'ARRAY') {
 4922:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 4923:                     push(@{$by_location->{$first}},$ip);
 4924:                 }
 4925:             } else {
 4926:                 $by_location->{$first} = [$ip];
 4927:             }
 4928:         }
 4929:     }
 4930:     return;
 4931: }
 4932: 
 4933: sub current_offloads_to {
 4934:     my ($dom,$settings,$servers) = @_;
 4935:     my (%spareid,%otherdomconfigs);
 4936:     if (ref($servers) eq 'HASH') {
 4937:         foreach my $lonhost (sort(keys(%{$servers}))) {
 4938:             my $gotspares;
 4939:             if (ref($settings) eq 'HASH') {
 4940:                 if (ref($settings->{'spares'}) eq 'HASH') {
 4941:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 4942:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 4943:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 4944:                         $gotspares = 1;
 4945:                     }
 4946:                 }
 4947:             }
 4948:             unless ($gotspares) {
 4949:                 my $gotspares;
 4950:                 my $serverhomeID =
 4951:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 4952:                 my $serverhomedom =
 4953:                     &Apache::lonnet::host_domain($serverhomeID);
 4954:                 if ($serverhomedom ne $dom) {
 4955:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 4956:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4957:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4958:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4959:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4960:                                 $gotspares = 1;
 4961:                             }
 4962:                         }
 4963:                     } else {
 4964:                         $otherdomconfigs{$serverhomedom} =
 4965:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 4966:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 4967:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4968:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4969:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 4970:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4971:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4972:                                         $gotspares = 1;
 4973:                                     }
 4974:                                 }
 4975:                             }
 4976:                         }
 4977:                     }
 4978:                 }
 4979:             }
 4980:             unless ($gotspares) {
 4981:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4982:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4983:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4984:                } else {
 4985:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 4986:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 4987:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4988:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4989:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4990:                     } else {
 4991:                         my %what = (
 4992:                              spareid => 1,
 4993:                         );
 4994:                         my ($result,$returnhash) = 
 4995:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 4996:                         if ($result eq 'ok') { 
 4997:                             if (ref($returnhash) eq 'HASH') {
 4998:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 4999:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 5000:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 5001:                                 }
 5002:                             }
 5003:                         }
 5004:                     }
 5005:                 }
 5006:             }
 5007:         }
 5008:     }
 5009:     return %spareid;
 5010: }
 5011: 
 5012: sub spares_row {
 5013:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 5014:     my $css_class;
 5015:     my $numinrow = 4;
 5016:     my $itemcount = 1;
 5017:     my $datatable;
 5018:     my %typetitles = &sparestype_titles();
 5019:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 5020:         foreach my $server (sort(keys(%{$servers}))) {
 5021:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 5022:             my ($othercontrol,$serverdom);
 5023:             if ($serverhome ne $server) {
 5024:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 5025:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 5026:             } else {
 5027:                 $serverdom = &Apache::lonnet::host_domain($server);
 5028:                 if ($serverdom ne $dom) {
 5029:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 5030:                 }
 5031:             }
 5032:             next unless (ref($spareid->{$server}) eq 'HASH');
 5033:             my $checkednow;
 5034:             if (ref($curroffloadnow) eq 'HASH') {
 5035:                 if ($curroffloadnow->{$server}) {
 5036:                     $checkednow = ' checked="checked"';
 5037:                 }
 5038:             }
 5039:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5040:             $datatable .= '<tr'.$css_class.'>
 5041:                            <td rowspan="2">
 5042:                             <span class="LC_nobreak">'.
 5043:                           &mt('[_1] when busy, offloads to:'
 5044:                               ,'<b>'.$server.'</b>').'</span><br />'.
 5045:                           '<span class="LC_nobreak">'."\n".
 5046:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 5047:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 5048:                           "\n";
 5049:             my (%current,%canselect);
 5050:             my @choices = 
 5051:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 5052:             foreach my $type ('primary','default') {
 5053:                 if (ref($spareid->{$server}) eq 'HASH') {
 5054:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 5055:                         my @spares = @{$spareid->{$server}{$type}};
 5056:                         if (@spares > 0) {
 5057:                             if ($othercontrol) {
 5058:                                 $current{$type} = join(', ',@spares);
 5059:                             } else {
 5060:                                 $current{$type} .= '<table>';
 5061:                                 my $numspares = scalar(@spares);
 5062:                                 for (my $i=0;  $i<@spares; $i++) {
 5063:                                     my $rem = $i%($numinrow);
 5064:                                     if ($rem == 0) {
 5065:                                         if ($i > 0) {
 5066:                                             $current{$type} .= '</tr>';
 5067:                                         }
 5068:                                         $current{$type} .= '<tr>';
 5069:                                     }
 5070:                                     $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;'.
 5071:                                                        $spareid->{$server}{$type}[$i].
 5072:                                                        '</label></td>'."\n";
 5073:                                 }
 5074:                                 my $rem = @spares%($numinrow);
 5075:                                 my $colsleft = $numinrow - $rem;
 5076:                                 if ($colsleft > 1 ) {
 5077:                                     $current{$type} .= '<td colspan="'.$colsleft.
 5078:                                                        '" class="LC_left_item">'.
 5079:                                                        '&nbsp;</td>';
 5080:                                 } elsif ($colsleft == 1) {
 5081:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 5082:                                 }
 5083:                                 $current{$type} .= '</tr></table>';
 5084:                             }
 5085:                         }
 5086:                     }
 5087:                     if ($current{$type} eq '') {
 5088:                         $current{$type} = &mt('None specified');
 5089:                     }
 5090:                     if ($othercontrol) {
 5091:                         if ($type eq 'primary') {
 5092:                             $canselect{$type} = $othercontrol;
 5093:                         }
 5094:                     } else {
 5095:                         $canselect{$type} = 
 5096:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 5097:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 5098:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 5099:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 5100:                         if (@choices > 0) {
 5101:                             foreach my $lonhost (@choices) {
 5102:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 5103:                             }
 5104:                         }
 5105:                         $canselect{$type} .= '</select>'."\n";
 5106:                     }
 5107:                 } else {
 5108:                     $current{$type} = &mt('Could not be determined');
 5109:                     if ($type eq 'primary') {
 5110:                         $canselect{$type} =  $othercontrol;
 5111:                     }
 5112:                 }
 5113:                 if ($type eq 'default') {
 5114:                     $datatable .= '<tr'.$css_class.'>';
 5115:                 }
 5116:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 5117:                               '<td>'.$current{$type}.'</td>'."\n".
 5118:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 5119:             }
 5120:             $itemcount ++;
 5121:         }
 5122:     }
 5123:     $$rowtotal += $itemcount;
 5124:     return $datatable;
 5125: }
 5126: 
 5127: sub possible_newspares {
 5128:     my ($server,$currspares,$serverhomes,$altids) = @_;
 5129:     my $serverhostname = &Apache::lonnet::hostname($server);
 5130:     my %excluded;
 5131:     if ($serverhostname ne '') {
 5132:         %excluded = (
 5133:                        $serverhostname => 1,
 5134:                     );
 5135:     }
 5136:     if (ref($currspares) eq 'HASH') {
 5137:         foreach my $type (keys(%{$currspares})) {
 5138:             if (ref($currspares->{$type}) eq 'ARRAY') {
 5139:                 if (@{$currspares->{$type}} > 0) {
 5140:                     foreach my $curr (@{$currspares->{$type}}) {
 5141:                         my $hostname = &Apache::lonnet::hostname($curr);
 5142:                         $excluded{$hostname} = 1;
 5143:                     }
 5144:                 }
 5145:             }
 5146:         }
 5147:     }
 5148:     my @choices;
 5149:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 5150:         if (keys(%{$serverhomes}) > 1) {
 5151:             foreach my $name (sort(keys(%{$serverhomes}))) {
 5152:                 unless ($excluded{$name}) {
 5153:                     if (exists($altids->{$serverhomes->{$name}})) {
 5154:                         push(@choices,$altids->{$serverhomes->{$name}});
 5155:                     } else {
 5156:                         push(@choices,$serverhomes->{$name});
 5157:                     }
 5158:                 }
 5159:             }
 5160:         }
 5161:     }
 5162:     return sort(@choices);
 5163: }
 5164: 
 5165: sub print_loadbalancing {
 5166:     my ($dom,$settings,$rowtotal) = @_;
 5167:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 5168:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 5169:     my $numinrow = 1;
 5170:     my $datatable;
 5171:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5172:     my (%currbalancer,%currtargets,%currrules,%existing);
 5173:     if (ref($settings) eq 'HASH') {
 5174:         %existing = %{$settings};
 5175:     }
 5176:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 5177:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 5178:                                   \%currtargets,\%currrules);
 5179:     } else {
 5180:         return;
 5181:     }
 5182:     my ($othertitle,$usertypes,$types) =
 5183:         &Apache::loncommon::sorted_inst_types($dom);
 5184:     my $rownum = 8;
 5185:     if (ref($types) eq 'ARRAY') {
 5186:         $rownum += scalar(@{$types});
 5187:     }
 5188:     my @css_class = ('LC_odd_row','LC_even_row');
 5189:     my $balnum = 0;
 5190:     my $islast;
 5191:     my (@toshow,$disabledtext);
 5192:     if (keys(%currbalancer) > 0) {
 5193:         @toshow = sort(keys(%currbalancer));
 5194:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 5195:             push(@toshow,'');
 5196:         }
 5197:     } else {
 5198:         @toshow = ('');
 5199:         $disabledtext = &mt('No existing load balancer');
 5200:     }
 5201:     foreach my $lonhost (@toshow) {
 5202:         if ($balnum == scalar(@toshow)-1) {
 5203:             $islast = 1;
 5204:         } else {
 5205:             $islast = 0;
 5206:         }
 5207:         my $cssidx = $balnum%2;
 5208:         my $targets_div_style = 'display: none';
 5209:         my $disabled_div_style = 'display: block';
 5210:         my $homedom_div_style = 'display: none';
 5211:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 5212:                       '<td rowspan="'.$rownum.'" style="vertical-align: top">'.
 5213:                       '<p>';
 5214:         if ($lonhost eq '') {
 5215:             $datatable .= '<span class="LC_nobreak">';
 5216:             if (keys(%currbalancer) > 0) {
 5217:                 $datatable .= &mt('Add balancer:');
 5218:             } else {
 5219:                 $datatable .= &mt('Enable balancer:');
 5220:             }
 5221:             $datatable .= '&nbsp;'.
 5222:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 5223:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 5224:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 5225:                           '<option value="" selected="selected">'.&mt('None').
 5226:                           '</option>'."\n";
 5227:             foreach my $server (sort(keys(%servers))) {
 5228:                 next if ($currbalancer{$server});
 5229:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 5230:             }
 5231:             $datatable .=
 5232:                 '</select>'."\n".
 5233:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 5234:         } else {
 5235:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 5236:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 5237:                            &mt('Stop balancing').'</label>'.
 5238:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 5239:             $targets_div_style = 'display: block';
 5240:             $disabled_div_style = 'display: none';
 5241:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 5242:                 $homedom_div_style = 'display: block';
 5243:             }
 5244:         }
 5245:         $datatable .= '</p></td><td rowspan="'.$rownum.'" style="vertical-align: top">'.
 5246:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 5247:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 5248:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 5249:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 5250:         my @sparestypes = ('primary','default');
 5251:         my %typetitles = &sparestype_titles();
 5252:         my %hostherechecked = (
 5253:                                   no => ' checked="checked"',
 5254:                               );
 5255:         foreach my $sparetype (@sparestypes) {
 5256:             my $targettable;
 5257:             for (my $i=0; $i<$numspares; $i++) {
 5258:                 my $checked;
 5259:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 5260:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5261:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5262:                             $checked = ' checked="checked"';
 5263:                         }
 5264:                     }
 5265:                 }
 5266:                 my ($chkboxval,$disabled);
 5267:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 5268:                     $chkboxval = $spares[$i];
 5269:                 }
 5270:                 if (exists($currbalancer{$spares[$i]})) {
 5271:                     $disabled = ' disabled="disabled"';
 5272:                 }
 5273:                 $targettable .=
 5274:                     '<td><span class="LC_nobreak"><label>'.
 5275:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 5276:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 5277:                     '</span></label></span></td>';
 5278:                 my $rem = $i%($numinrow);
 5279:                 if ($rem == 0) {
 5280:                     if (($i > 0) && ($i < $numspares-1)) {
 5281:                         $targettable .= '</tr>';
 5282:                     }
 5283:                     if ($i < $numspares-1) {
 5284:                         $targettable .= '<tr>';
 5285:                     }
 5286:                 }
 5287:             }
 5288:             if ($targettable ne '') {
 5289:                 my $rem = $numspares%($numinrow);
 5290:                 my $colsleft = $numinrow - $rem;
 5291:                 if ($colsleft > 1 ) {
 5292:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5293:                                     '&nbsp;</td>';
 5294:                 } elsif ($colsleft == 1) {
 5295:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 5296:                 }
 5297:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 5298:                                '<table><tr>'.$targettable.'</tr></table><br />';
 5299:             }
 5300:             $hostherechecked{$sparetype} = '';
 5301:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 5302:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5303:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5304:                         $hostherechecked{$sparetype} = ' checked="checked"';
 5305:                         $hostherechecked{'no'} = '';
 5306:                     }
 5307:                 }
 5308:             }
 5309:         }
 5310:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 5311:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 5312:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 5313:         foreach my $sparetype (@sparestypes) {
 5314:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 5315:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 5316:                           '</i></label><br />';
 5317:         }
 5318:         $datatable .= '</div></td></tr>'.
 5319:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 5320:                                            $othertitle,$usertypes,$types,\%servers,
 5321:                                            \%currbalancer,$lonhost,
 5322:                                            $targets_div_style,$homedom_div_style,
 5323:                                            $css_class[$cssidx],$balnum,$islast);
 5324:         $$rowtotal += $rownum;
 5325:         $balnum ++;
 5326:     }
 5327:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 5328:     return $datatable;
 5329: }
 5330: 
 5331: sub get_loadbalancers_config {
 5332:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 5333:     return unless ((ref($servers) eq 'HASH') &&
 5334:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 5335:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 5336:     if (keys(%{$existing}) > 0) {
 5337:         my $oldlonhost;
 5338:         foreach my $key (sort(keys(%{$existing}))) {
 5339:             if ($key eq 'lonhost') {
 5340:                 $oldlonhost = $existing->{'lonhost'};
 5341:                 $currbalancer->{$oldlonhost} = 1;
 5342:             } elsif ($key eq 'targets') {
 5343:                 if ($oldlonhost) {
 5344:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 5345:                 }
 5346:             } elsif ($key eq 'rules') {
 5347:                 if ($oldlonhost) {
 5348:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 5349:                 }
 5350:             } elsif (ref($existing->{$key}) eq 'HASH') {
 5351:                 $currbalancer->{$key} = 1;
 5352:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 5353:                 $currrules->{$key} = $existing->{$key}{'rules'};
 5354:             }
 5355:         }
 5356:     } else {
 5357:         my ($balancerref,$targetsref) =
 5358:                 &Apache::lonnet::get_lonbalancer_config($servers);
 5359:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 5360:             foreach my $server (sort(keys(%{$balancerref}))) {
 5361:                 $currbalancer->{$server} = 1;
 5362:                 $currtargets->{$server} = $targetsref->{$server};
 5363:             }
 5364:         }
 5365:     }
 5366:     return;
 5367: }
 5368: 
 5369: sub loadbalancing_rules {
 5370:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 5371:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 5372:         $css_class,$balnum,$islast) = @_;
 5373:     my $output;
 5374:     my $num = 0;
 5375:     my ($alltypes,$othertypes,$titles) =
 5376:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 5377:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 5378:         foreach my $type (@{$alltypes}) {
 5379:             $num ++;
 5380:             my $current;
 5381:             if (ref($currrules) eq 'HASH') {
 5382:                 $current = $currrules->{$type};
 5383:             }
 5384:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5385:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 5386:                     $current = '';
 5387:                 }
 5388:             }
 5389:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 5390:                                              $servers,$currbalancer,$lonhost,$dom,
 5391:                                              $targets_div_style,$homedom_div_style,
 5392:                                              $css_class,$balnum,$num,$islast);
 5393:         }
 5394:     }
 5395:     return $output;
 5396: }
 5397: 
 5398: sub loadbalancing_titles {
 5399:     my ($dom,$intdom,$usertypes,$types) = @_;
 5400:     my %othertypes = (
 5401:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 5402:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 5403:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 5404:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 5405:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 5406:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 5407:                      );
 5408:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 5409:     my @available;
 5410:     if (ref($types) eq 'ARRAY') {
 5411:         @available = @{$types};
 5412:     }
 5413:     unless (grep(/^default$/,@available)) {
 5414:         push(@available,'default');
 5415:     }
 5416:     unshift(@alltypes,@available);
 5417:     my %titles;
 5418:     foreach my $type (@alltypes) {
 5419:         if ($type =~ /^_LC_/) {
 5420:             $titles{$type} = $othertypes{$type};
 5421:         } elsif ($type eq 'default') {
 5422:             $titles{$type} = &mt('All users from [_1]',$dom);
 5423:             if (ref($types) eq 'ARRAY') {
 5424:                 if (@{$types} > 0) {
 5425:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 5426:                 }
 5427:             }
 5428:         } elsif (ref($usertypes) eq 'HASH') {
 5429:             $titles{$type} = $usertypes->{$type};
 5430:         }
 5431:     }
 5432:     return (\@alltypes,\%othertypes,\%titles);
 5433: }
 5434: 
 5435: sub loadbalance_rule_row {
 5436:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 5437:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 5438:     my @rulenames;
 5439:     my %ruletitles = &offloadtype_text();
 5440:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 5441:         @rulenames = ('balancer','offloadedto','specific');
 5442:     } else {
 5443:         @rulenames = ('default','homeserver');
 5444:         if ($type eq '_LC_external') {
 5445:             push(@rulenames,'externalbalancer');
 5446:         } else {
 5447:             push(@rulenames,'specific');
 5448:         }
 5449:         push(@rulenames,'none');
 5450:     }
 5451:     my $style = $targets_div_style;
 5452:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5453:         $style = $homedom_div_style;
 5454:     }
 5455:     my $space;
 5456:     if ($islast && $num == 1) {
 5457:         $space = '<div display="inline-block">&nbsp;</div>';
 5458:     }
 5459:     my $output =
 5460:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td style="vertical-align: top">'.$space.
 5461:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 5462:         '<td valaign="top">'.$space.
 5463:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 5464:     for (my $i=0; $i<@rulenames; $i++) {
 5465:         my $rule = $rulenames[$i];
 5466:         my ($checked,$extra);
 5467:         if ($rulenames[$i] eq 'default') {
 5468:             $rule = '';
 5469:         }
 5470:         if ($rulenames[$i] eq 'specific') {
 5471:             if (ref($servers) eq 'HASH') {
 5472:                 my $default;
 5473:                 if (($current ne '') && (exists($servers->{$current}))) {
 5474:                     $checked = ' checked="checked"';
 5475:                 }
 5476:                 unless ($checked) {
 5477:                     $default = ' selected="selected"';
 5478:                 }
 5479:                 $extra =
 5480:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5481:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5482:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 5483:                     '<option value=""'.$default.'></option>'."\n";
 5484:                 foreach my $server (sort(keys(%{$servers}))) {
 5485:                     if (ref($currbalancer) eq 'HASH') {
 5486:                         next if (exists($currbalancer->{$server}));
 5487:                     }
 5488:                     my $selected;
 5489:                     if ($server eq $current) {
 5490:                         $selected = ' selected="selected"';
 5491:                     }
 5492:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 5493:                 }
 5494:                 $extra .= '</select>';
 5495:             }
 5496:         } elsif ($rule eq $current) {
 5497:             $checked = ' checked="checked"';
 5498:         }
 5499:         $output .= '<span class="LC_nobreak"><label>'.
 5500:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 5501:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 5502:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 5503:                    ')"'.$checked.' />&nbsp;';
 5504:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 5505:             $output .= $ruletitles{'particular'};
 5506:         } else {
 5507:             $output .= $ruletitles{$rulenames[$i]};
 5508:         }
 5509:         $output .= '</label>'.$extra.'</span><br />'."\n";
 5510:     }
 5511:     $output .= '</div></td></tr>'."\n";
 5512:     return $output;
 5513: }
 5514: 
 5515: sub offloadtype_text {
 5516:     my %ruletitles = &Apache::lonlocal::texthash (
 5517:            'default'          => 'Offloads to default destinations',
 5518:            'homeserver'       => "Offloads to user's home server",
 5519:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 5520:            'specific'         => 'Offloads to specific server',
 5521:            'none'             => 'No offload',
 5522:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 5523:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 5524:            'particular'       => 'Session hosted (after re-auth) on server:',
 5525:     );
 5526:     return %ruletitles;
 5527: }
 5528: 
 5529: sub sparestype_titles {
 5530:     my %typestitles = &Apache::lonlocal::texthash (
 5531:                           'primary' => 'primary',
 5532:                           'default' => 'default',
 5533:                       );
 5534:     return %typestitles;
 5535: }
 5536: 
 5537: sub contact_titles {
 5538:     my %titles = &Apache::lonlocal::texthash (
 5539:                    'supportemail'    => 'Support E-mail address',
 5540:                    'adminemail'      => 'Default Server Admin E-mail address',
 5541:                    'errormail'       => 'Error reports to be e-mailed to',
 5542:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 5543:                    'helpdeskmail'    => "Helpdesk requests for this domain's users",
 5544:                    'otherdomsmail'   => 'Helpdesk requests for other (unconfigured) domains',
 5545:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 5546:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 5547:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 5548:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 5549:                  );
 5550:     my %short_titles = &Apache::lonlocal::texthash (
 5551:                            adminemail   => 'Admin E-mail address',
 5552:                            supportemail => 'Support E-mail',
 5553:                        );   
 5554:     return (\%titles,\%short_titles);
 5555: }
 5556: 
 5557: sub helpform_fields {
 5558:     my %titles =  &Apache::lonlocal::texthash (
 5559:                        'username'   => 'Name',
 5560:                        'user'       => 'Username/domain',
 5561:                        'phone'      => 'Phone',
 5562:                        'cc'         => 'Cc e-mail',
 5563:                        'course'     => 'Course Details',
 5564:                        'section'    => 'Sections',
 5565:                        'screenshot' => 'File upload',
 5566:     );
 5567:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 5568:     my %possoptions = (
 5569:                         username     => ['yes','no','req'],
 5570:                         phone        => ['yes','no','req'],
 5571:                         user         => ['yes','no'],
 5572:                         cc           => ['yes','no'],
 5573:                         course       => ['yes','no'],
 5574:                         section      => ['yes','no'],
 5575:                         screenshot   => ['yes','no'],
 5576:                       );
 5577:     my %fieldoptions = &Apache::lonlocal::texthash (
 5578:                          'yes'  => 'Optional',
 5579:                          'req'  => 'Required',
 5580:                          'no'   => "Not shown",
 5581:     );
 5582:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 5583: }
 5584: 
 5585: sub tool_titles {
 5586:     my %titles = &Apache::lonlocal::texthash (
 5587:                      aboutme    => 'Personal web page',
 5588:                      blog       => 'Blog',
 5589:                      webdav     => 'WebDAV',
 5590:                      portfolio  => 'Portfolio',
 5591:                      official   => 'Official courses (with institutional codes)',
 5592:                      unofficial => 'Unofficial courses',
 5593:                      community  => 'Communities',
 5594:                      textbook   => 'Textbook courses',
 5595:                      placement  => 'Placement tests',
 5596:                  );
 5597:     return %titles;
 5598: }
 5599: 
 5600: sub courserequest_titles {
 5601:     my %titles = &Apache::lonlocal::texthash (
 5602:                                    official   => 'Official',
 5603:                                    unofficial => 'Unofficial',
 5604:                                    community  => 'Communities',
 5605:                                    textbook   => 'Textbook',
 5606:                                    placement  => 'Placement tests',
 5607:                                    norequest  => 'Not allowed',
 5608:                                    approval   => 'Approval by Dom. Coord.',
 5609:                                    validate   => 'With validation',
 5610:                                    autolimit  => 'Numerical limit',
 5611:                                    unlimited  => '(blank for unlimited)',
 5612:                  );
 5613:     return %titles;
 5614: }
 5615: 
 5616: sub authorrequest_titles {
 5617:     my %titles = &Apache::lonlocal::texthash (
 5618:                                    norequest  => 'Not allowed',
 5619:                                    approval   => 'Approval by Dom. Coord.',
 5620:                                    automatic  => 'Automatic approval',
 5621:                  );
 5622:     return %titles;
 5623: }
 5624: 
 5625: sub courserequest_conditions {
 5626:     my %conditions = &Apache::lonlocal::texthash (
 5627:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 5628:        validate   => '(Processing of request subject to institutional validation).',
 5629:                  );
 5630:     return %conditions;
 5631: }
 5632: 
 5633: 
 5634: sub print_usercreation {
 5635:     my ($position,$dom,$settings,$rowtotal) = @_;
 5636:     my $numinrow = 4;
 5637:     my $datatable;
 5638:     if ($position eq 'top') {
 5639:         $$rowtotal ++;
 5640:         my $rowcount = 0;
 5641:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 5642:         if (ref($rules) eq 'HASH') {
 5643:             if (keys(%{$rules}) > 0) {
 5644:                 $datatable .= &user_formats_row('username',$settings,$rules,
 5645:                                                 $ruleorder,$numinrow,$rowcount);
 5646:                 $$rowtotal ++;
 5647:                 $rowcount ++;
 5648:             }
 5649:         }
 5650:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 5651:         if (ref($idrules) eq 'HASH') {
 5652:             if (keys(%{$idrules}) > 0) {
 5653:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 5654:                                                 $idruleorder,$numinrow,$rowcount);
 5655:                 $$rowtotal ++;
 5656:                 $rowcount ++;
 5657:             }
 5658:         }
 5659:         if ($rowcount == 0) {
 5660:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 5661:             $$rowtotal ++;
 5662:             $rowcount ++;
 5663:         }
 5664:     } elsif ($position eq 'middle') {
 5665:         my @creators = ('author','course','requestcrs');
 5666:         my ($rules,$ruleorder) =
 5667:             &Apache::lonnet::inst_userrules($dom,'username');
 5668:         my %lt = &usercreation_types();
 5669:         my %checked;
 5670:         if (ref($settings) eq 'HASH') {
 5671:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 5672:                 foreach my $item (@creators) {
 5673:                     $checked{$item} = $settings->{'cancreate'}{$item};
 5674:                 }
 5675:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 5676:                 foreach my $item (@creators) {
 5677:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 5678:                         $checked{$item} = 'none';
 5679:                     }
 5680:                 }
 5681:             }
 5682:         }
 5683:         my $rownum = 0;
 5684:         foreach my $item (@creators) {
 5685:             $rownum ++;
 5686:             if ($checked{$item} eq '') {
 5687:                 $checked{$item} = 'any';
 5688:             }
 5689:             my $css_class;
 5690:             if ($rownum%2) {
 5691:                 $css_class = '';
 5692:             } else {
 5693:                 $css_class = ' class="LC_odd_row" ';
 5694:             }
 5695:             $datatable .= '<tr'.$css_class.'>'.
 5696:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 5697:                          '</span></td><td style="text-align: right">';
 5698:             my @options = ('any');
 5699:             if (ref($rules) eq 'HASH') {
 5700:                 if (keys(%{$rules}) > 0) {
 5701:                     push(@options,('official','unofficial'));
 5702:                 }
 5703:             }
 5704:             push(@options,'none');
 5705:             foreach my $option (@options) {
 5706:                 my $type = 'radio';
 5707:                 my $check = ' ';
 5708:                 if ($checked{$item} eq $option) {
 5709:                     $check = ' checked="checked" ';
 5710:                 } 
 5711:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5712:                               '<input type="'.$type.'" name="can_createuser_'.
 5713:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 5714:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 5715:             }
 5716:             $datatable .= '</td></tr>';
 5717:         }
 5718:     } else {
 5719:         my @contexts = ('author','course','domain');
 5720:         my @authtypes = ('int','krb4','krb5','loc');
 5721:         my %checked;
 5722:         if (ref($settings) eq 'HASH') {
 5723:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 5724:                 foreach my $item (@contexts) {
 5725:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 5726:                         foreach my $auth (@authtypes) {
 5727:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 5728:                                 $checked{$item}{$auth} = ' checked="checked" ';
 5729:                             }
 5730:                         }
 5731:                     }
 5732:                 }
 5733:             }
 5734:         } else {
 5735:             foreach my $item (@contexts) {
 5736:                 foreach my $auth (@authtypes) {
 5737:                     $checked{$item}{$auth} = ' checked="checked" ';
 5738:                 }
 5739:             }
 5740:         }
 5741:         my %title = &context_names();
 5742:         my %authname = &authtype_names();
 5743:         my $rownum = 0;
 5744:         my $css_class; 
 5745:         foreach my $item (@contexts) {
 5746:             if ($rownum%2) {
 5747:                 $css_class = '';
 5748:             } else {
 5749:                 $css_class = ' class="LC_odd_row" ';
 5750:             }
 5751:             $datatable .=   '<tr'.$css_class.'>'.
 5752:                             '<td>'.$title{$item}.
 5753:                             '</td><td class="LC_left_item">'.
 5754:                             '<span class="LC_nobreak">';
 5755:             foreach my $auth (@authtypes) {
 5756:                 $datatable .= '<label>'. 
 5757:                               '<input type="checkbox" name="'.$item.'_auth" '.
 5758:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 5759:                               $authname{$auth}.'</label>&nbsp;';
 5760:             }
 5761:             $datatable .= '</span></td></tr>';
 5762:             $rownum ++;
 5763:         }
 5764:         $$rowtotal += $rownum;
 5765:     }
 5766:     return $datatable;
 5767: }
 5768: 
 5769: sub print_selfcreation {
 5770:     my ($position,$dom,$settings,$rowtotal) = @_;
 5771:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 5772:         $emaildomain,$datatable);
 5773:     if (ref($settings) eq 'HASH') {
 5774:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 5775:             $createsettings = $settings->{'cancreate'};
 5776:             if (ref($createsettings) eq 'HASH') {
 5777:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 5778:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 5779:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 5780:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 5781:                         @selfcreate = ('email','login','sso');
 5782:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 5783:                         @selfcreate = ($createsettings->{'selfcreate'});
 5784:                     }
 5785:                 }
 5786:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 5787:                     $processing = $createsettings->{'selfcreateprocessing'};
 5788:                 }
 5789:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 5790:                     $emailoptions = $createsettings->{'emailoptions'};
 5791:                 }
 5792:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 5793:                     $emailverified = $createsettings->{'emailverified'};
 5794:                 }
 5795:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 5796:                     $emaildomain = $createsettings->{'emaildomain'};
 5797:                 }
 5798:             }
 5799:         }
 5800:     }
 5801:     my %radiohash;
 5802:     my $numinrow = 4;
 5803:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 5804:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5805:     if ($position eq 'top') {
 5806:         my %choices = &Apache::lonlocal::texthash (
 5807:                                                       cancreate_login      => 'Institutional Login',
 5808:                                                       cancreate_sso        => 'Institutional Single Sign On',
 5809:                                                   );
 5810:         my @toggles = sort(keys(%choices));
 5811:         my %defaultchecked = (
 5812:                                'cancreate_login' => 'off',
 5813:                                'cancreate_sso'   => 'off',
 5814:                              );
 5815:         my ($onclick,$itemcount);
 5816:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5817:                                                      \%choices,$itemcount,$onclick);
 5818:         $$rowtotal += $itemcount;
 5819:         
 5820:         if (ref($usertypes) eq 'HASH') {
 5821:             if (keys(%{$usertypes}) > 0) {
 5822:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 5823:                                              $dom,$numinrow,$othertitle,
 5824:                                              'statustocreate',$rowtotal);
 5825:                 $$rowtotal ++;
 5826:             }
 5827:         }
 5828:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 5829:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5830:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 5831:         my $rem;
 5832:         my $numperrow = 2;
 5833:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 5834:         $datatable .= '<tr'.$css_class.'>'.
 5835:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 5836:                      '<td class="LC_left_item">'."\n".
 5837:                      '<table><tr><td>'."\n";
 5838:         for (my $i=0; $i<@fields; $i++) {
 5839:             $rem = $i%($numperrow);
 5840:             if ($rem == 0) {
 5841:                 if ($i > 0) {
 5842:                     $datatable .= '</tr>';
 5843:                 }
 5844:                 $datatable .= '<tr>';
 5845:             }
 5846:             my $currval;
 5847:             if (ref($createsettings) eq 'HASH') {
 5848:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 5849:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 5850:                 }
 5851:             }
 5852:             $datatable .= '<td class="LC_left_item">'.
 5853:                           '<span class="LC_nobreak">'.
 5854:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 5855:                           'value="'.$currval.'" size="10" />&nbsp;'.
 5856:                           $fieldtitles{$fields[$i]}.'</span></td>';
 5857:         }
 5858:         my $colsleft = $numperrow - $rem;
 5859:         if ($colsleft > 1 ) {
 5860:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5861:                          '&nbsp;</td>';
 5862:         } elsif ($colsleft == 1) {
 5863:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5864:         }
 5865:         $datatable .= '</tr></table></td></tr>';
 5866:         $$rowtotal ++;
 5867:     } elsif ($position eq 'middle') {
 5868:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 5869:         my @posstypes;
 5870:         if (ref($types) eq 'ARRAY') {
 5871:             @posstypes = @{$types};
 5872:         }
 5873:         unless (grep(/^default$/,@posstypes)) {
 5874:             push(@posstypes,'default');
 5875:         }
 5876:         my %usertypeshash;
 5877:         if (ref($usertypes) eq 'HASH') {
 5878:             %usertypeshash = %{$usertypes};
 5879:         }
 5880:         $usertypeshash{'default'} = $othertitle;
 5881:         foreach my $status (@posstypes) {
 5882:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 5883:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 5884:             $$rowtotal ++;
 5885:         }
 5886:     } else {
 5887:         my %choices = &Apache::lonlocal::texthash (
 5888:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 5889:                                                   );
 5890:         my @toggles = sort(keys(%choices));
 5891:         my %defaultchecked = (
 5892:                                'cancreate_email' => 'off',
 5893:                              );
 5894:         my $customclass = 'LC_selfcreate_email';
 5895:         my $classprefix = 'LC_canmodify_emailusername_';
 5896:         my $optionsprefix = 'LC_options_emailusername_';
 5897:         my $display = 'none';
 5898:         my $rowstyle = 'display:none';
 5899:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 5900:             $display = 'block';
 5901:             $rowstyle = 'display:table-row';
 5902:         }
 5903:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 5904:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5905:                                                      \%choices,$$rowtotal,$onclick);
 5906:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 5907:                                          $rowstyle);
 5908:         $$rowtotal ++;
 5909:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 5910:                                       $rowstyle);
 5911:         $$rowtotal ++;
 5912:         my (@ordered,@posstypes,%usertypeshash);
 5913:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5914:         my ($emailrules,$emailruleorder) =
 5915:             &Apache::lonnet::inst_userrules($dom,'email');
 5916:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 5917:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 5918:         if (ref($types) eq 'ARRAY') {
 5919:             @posstypes = @{$types};
 5920:         }
 5921:         if (@posstypes) {
 5922:             unless (grep(/^default$/,@posstypes)) {
 5923:                 push(@posstypes,'default');
 5924:             }
 5925:             if (ref($usertypes) eq 'HASH') {
 5926:                 %usertypeshash = %{$usertypes};
 5927:             }
 5928:             my $currassign;
 5929:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 5930:                 $currassign = {
 5931:                                   selfassign => $domdefaults{'inststatusguest'},
 5932:                               };
 5933:                 @ordered = @{$domdefaults{'inststatusguest'}};
 5934:             } else {
 5935:                 $currassign = { selfassign => [] };
 5936:             }
 5937:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 5938:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 5939:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 5940:                                          $numinrow,$othertitle,'selfassign',
 5941:                                          $rowtotal,$onclicktypes,$customclass,
 5942:                                          $rowstyle);
 5943:             $$rowtotal ++;
 5944:             $usertypeshash{'default'} = $othertitle;
 5945:             foreach my $status (@posstypes) {
 5946:                 my $css_class;
 5947:                 if ($$rowtotal%2) {
 5948:                     $css_class = 'LC_odd_row ';
 5949:                 }
 5950:                 $css_class .= $customclass;
 5951:                 my $rowid = $optionsprefix.$status;
 5952:                 my $hidden = 1;
 5953:                 my $currstyle = 'display:none';
 5954:                 if (grep(/^\Q$status\E$/,@ordered)) {
 5955:                     $currstyle = $rowstyle;
 5956:                     $hidden = 0; 
 5957:                 }
 5958:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 5959:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 5960:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 5961:                 unless ($hidden) {
 5962:                     $$rowtotal ++;
 5963:                 }
 5964:             }
 5965:         } else {
 5966:             my $css_class;
 5967:             if ($$rowtotal%2) {
 5968:                 $css_class = 'LC_odd_row ';
 5969:             }
 5970:             $css_class .= $customclass;
 5971:             $usertypeshash{'default'} = $othertitle;
 5972:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 5973:                                          $emailrules,$emailruleorder,$settings,'default','',
 5974:                                          $othertitle,$css_class,$rowstyle,$intdom);
 5975:             $$rowtotal ++;
 5976:         }
 5977:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 5978:         $numinrow = 1;
 5979:         if (@posstypes) {
 5980:             foreach my $status (@posstypes) {
 5981:                 my $rowid = $classprefix.$status;
 5982:                 my $datarowstyle = 'display:none';
 5983:                 if (grep(/^\Q$status\E$/,@ordered)) { 
 5984:                     $datarowstyle = $rowstyle; 
 5985:                 }
 5986:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 5987:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 5988:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 5989:                 unless ($datarowstyle eq 'display:none') {
 5990:                     $$rowtotal ++;
 5991:                 }
 5992:             }
 5993:         } else {
 5994:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 5995:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 5996:                                                    $infotitles,'',$customclass,$rowstyle);
 5997:         }
 5998:     }
 5999:     return $datatable;
 6000: }
 6001: 
 6002: sub selfcreate_javascript {
 6003:     return <<"ENDSCRIPT";
 6004: 
 6005: <script type="text/javascript">
 6006: // <![CDATA[
 6007: 
 6008: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 6009:     var x = document.getElementsByClassName(target);
 6010:     var insttypes = 0;
 6011:     var insttypeRegExp = new RegExp(prefix);
 6012:     if ((x.length != undefined) && (x.length > 0)) {
 6013:         if (form.elements[radio].length != undefined) {
 6014:             for (var i=0; i<form.elements[radio].length; i++) {
 6015:                 if (form.elements[radio][i].checked) {
 6016:                     if (form.elements[radio][i].value == 1) {
 6017:                         for (var j=0; j<x.length; j++) {
 6018:                             if (x[j].id == 'undefined') {
 6019:                                 x[j].style.display = 'table-row';
 6020:                             } else if (insttypeRegExp.test(x[j].id)) {
 6021:                                 insttypes ++;
 6022:                             } else {
 6023:                                 x[j].style.display = 'table-row';
 6024:                             }
 6025:                         }
 6026:                     } else {
 6027:                         for (var j=0; j<x.length; j++) {
 6028:                             x[j].style.display = 'none';
 6029:                         }
 6030:                     }
 6031:                     break;
 6032:                 }
 6033:             }
 6034:             if (insttypes > 0) {
 6035:                 toggleDataRow(form,checkbox,target,altprefix);
 6036:                 toggleDataRow(form,checkbox,target,prefix,1);
 6037:             }
 6038:         }
 6039:     }
 6040:     return;
 6041: }
 6042: 
 6043: function toggleDataRow(form,checkbox,target,prefix,docount) {
 6044:     if (form.elements[checkbox].length != undefined) {
 6045:         var count = 0;
 6046:         if (docount) {
 6047:             for (var i=0; i<form.elements[checkbox].length; i++) {
 6048:                 if (form.elements[checkbox][i].checked) {
 6049:                     count ++;
 6050:                 }
 6051:             }
 6052:         }
 6053:         for (var i=0; i<form.elements[checkbox].length; i++) {
 6054:             var type = form.elements[checkbox][i].value;
 6055:             if (document.getElementById(prefix+type)) {
 6056:                 if (form.elements[checkbox][i].checked) {
 6057:                     document.getElementById(prefix+type).style.display = 'table-row';
 6058:                     if (count % 2 == 1) {
 6059:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 6060:                     } else {
 6061:                         document.getElementById(prefix+type).className = target;
 6062:                     }
 6063:                     count ++;
 6064:                 } else {
 6065:                     document.getElementById(prefix+type).style.display = 'none';
 6066:                 }
 6067:             }
 6068:         }
 6069:     }
 6070:     return;
 6071: }
 6072: 
 6073: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 6074:     var caller = radio+'_'+status;
 6075:     if (form.elements[caller].length != undefined) {
 6076:         for (var i=0; i<form.elements[caller].length; i++) {
 6077:             if (form.elements[caller][i].checked) {
 6078:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 6079:                     var curr = form.elements[caller][i].value;
 6080:                     if (prefix) {
 6081:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 6082:                     }
 6083:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 6084:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 6085:                     if (curr == 'custom') {
 6086:                         if (prefix) { 
 6087:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 6088:                         }
 6089:                     } else if (curr == 'inst') {
 6090:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 6091:                     } else if (curr == 'noninst') {
 6092:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 6093:                     }
 6094:                     break;
 6095:                 }
 6096:             }
 6097:         }
 6098:     }
 6099: }
 6100: 
 6101: // ]]>
 6102: </script>
 6103: 
 6104: ENDSCRIPT
 6105: }
 6106: 
 6107: sub noninst_users {
 6108:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 6109:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_; 
 6110:     my $class = 'LC_left_item';
 6111:     if ($css_class) {
 6112:         $css_class = ' class="'.$css_class.'"'; 
 6113:     }
 6114:     if ($rowid) {
 6115:         $rowid = ' id="'.$rowid.'"';
 6116:     }
 6117:     if ($rowstyle) {
 6118:         $rowstyle = ' style="'.$rowstyle.'"';
 6119:     }
 6120:     my ($output,$description);
 6121:     if ($type eq 'default') {
 6122:         $description = &mt('Requests for: [_1]',$typetitle);
 6123:     } else {
 6124:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 6125:     }
 6126:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 6127:               "<td>$description</td>\n".           
 6128:               '<td class="'.$class.'" colspan="2">'.
 6129:               '<table><tr>';
 6130:     my %headers = &Apache::lonlocal::texthash( 
 6131:               approve  => 'Processing',
 6132:               email    => 'E-mail',
 6133:               username => 'Username',
 6134:     );
 6135:     foreach my $item ('approve','email','username') {
 6136:         $output .= '<th>'.$headers{$item}.'</th>';
 6137:     }
 6138:     $output .= '</tr><tr>';
 6139:     foreach my $item ('approve','email','username') {
 6140:         $output .= '<td style="vertical-align: top">';
 6141:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 6142:         if ($item eq 'approve') {
 6143:             %choices = &Apache::lonlocal::texthash (
 6144:                                                      automatic => 'Automatically approved',
 6145:                                                      approval  => 'Queued for approval',
 6146:                                                    );
 6147:             @options = ('automatic','approval');
 6148:             $hashref = $processing;
 6149:             $defoption = 'automatic';
 6150:             $name = 'cancreate_emailprocess_'.$type;
 6151:         } elsif ($item eq 'email') {
 6152:             %choices = &Apache::lonlocal::texthash (
 6153:                                                      any     => 'Any e-mail',
 6154:                                                      inst    => 'Institutional only',
 6155:                                                      noninst => 'Non-institutional only',
 6156:                                                      custom  => 'Custom restrictions',
 6157:                                                    );
 6158:             @options = ('any','inst','noninst');
 6159:             my $showcustom;
 6160:             if (ref($emailrules) eq 'HASH') {
 6161:                 if (keys(%{$emailrules}) > 0) {
 6162:                     push(@options,'custom');
 6163:                     $showcustom = 'cancreate_emailrule';
 6164:                     if (ref($settings) eq 'HASH') {
 6165:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 6166:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 6167:                                 if (exists($emailrules->{$rule})) {
 6168:                                     $hascustom ++;
 6169:                                 }
 6170:                             }
 6171:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 6172:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 6173:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 6174:                                     if (exists($emailrules->{$rule})) {
 6175:                                         $hascustom ++;
 6176:                                     }
 6177:                                 }
 6178:                             }
 6179:                         }
 6180:                     }
 6181:                 }
 6182:             }
 6183:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 6184:                                                      "'cancreate_emaildomain','$type'".');"';
 6185:             $hashref = $emailoptions;
 6186:             $defoption = 'any';
 6187:             $name = 'cancreate_emailoptions_'.$type;
 6188:         } elsif ($item eq 'username') {
 6189:             %choices = &Apache::lonlocal::texthash (
 6190:                                                      all    => 'Same as e-mail',
 6191:                                                      first  => 'Omit @domain',
 6192:                                                      free   => 'Free to choose',
 6193:                                                    );
 6194:             @options = ('all','first','free');
 6195:             $hashref = $emailverified;
 6196:             $defoption = 'all';
 6197:             $name = 'cancreate_usernameoptions_'.$type;
 6198:         }
 6199:         foreach my $option (@options) {
 6200:             my $checked;
 6201:             if (ref($hashref) eq 'HASH') {
 6202:                 if ($type eq '') {
 6203:                     if (!exists($hashref->{'default'})) {
 6204:                         if ($option eq $defoption) {
 6205:                             $checked = ' checked="checked"';
 6206:                         }
 6207:                     } else {
 6208:                         if ($hashref->{'default'} eq $option) {
 6209:                             $checked = ' checked="checked"';
 6210:                         }
 6211:                     }
 6212:                 } else {
 6213:                     if (!exists($hashref->{$type})) {
 6214:                         if ($option eq $defoption) {
 6215:                             $checked = ' checked="checked"';
 6216:                         }
 6217:                     } else {
 6218:                         if ($hashref->{$type} eq $option) {
 6219:                             $checked = ' checked="checked"';
 6220:                         }
 6221:                     }
 6222:                 }
 6223:             } elsif (($item eq 'email') && ($hascustom)) {
 6224:                 if ($option eq 'custom') {
 6225:                     $checked = ' checked="checked"';
 6226:                 }
 6227:             } elsif ($option eq $defoption) {
 6228:                 $checked = ' checked="checked"';
 6229:             }
 6230:             $output .= '<span class="LC_nobreak"><label>'.
 6231:                        '<input type="radio" name="'.$name.'"'.
 6232:                        $checked.' value="'.$option.'"'.$onclick.' />'.
 6233:                        $choices{$option}.'</label></span><br />';
 6234:             if ($item eq 'email') {
 6235:                 if ($option eq 'custom') {
 6236:                     my $id = 'cancreate_emailrule_'.$type;
 6237:                     my $display = 'none';
 6238:                     if ($checked) {
 6239:                         $display = 'inline';
 6240:                     }
 6241:                     my $numinrow = 2;
 6242:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
 6243:                                '<legend>'.&mt('Disallow').'</legend><table>'.
 6244:                                &user_formats_row('email',$settings,$emailrules,
 6245:                                                  $emailruleorder,$numinrow,'',$type);
 6246:                               '</table></fieldset>';
 6247:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
 6248:                     my %text = &Apache::lonlocal::texthash (
 6249:                                                              inst    => 'must end:',
 6250:                                                              noninst => 'cannot end:',
 6251:                                                            );
 6252:                     my $value;
 6253:                     if (ref($emaildomain) eq 'HASH') {
 6254:                         if (ref($emaildomain->{$type}) eq 'HASH') {
 6255:                             $value = $emaildomain->{$type}->{$option}; 
 6256:                         }
 6257:                     }
 6258:                     if ($value eq '') {
 6259:                         $value = '@'.$intdom;
 6260:                     }
 6261:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
 6262:                     my $display = 'none';
 6263:                     if ($checked) {
 6264:                         $display = 'inline';
 6265:                     }
 6266:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
 6267:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
 6268:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
 6269:                                '</div>';
 6270:                 }
 6271:             }
 6272:         }
 6273:         $output .= '</td>'."\n";
 6274:     }
 6275:     $output .= "</tr></table></td></tr>\n";
 6276:     return $output;
 6277: }
 6278: 
 6279: sub captcha_choice {
 6280:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
 6281:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 6282:         $vertext,$currver);
 6283:     my %lt = &captcha_phrases();
 6284:     $keyentry = 'hidden';
 6285:     if ($context eq 'cancreate') {
 6286:         $rowname = &mt('CAPTCHA validation');
 6287:     } elsif ($context eq 'login') {
 6288:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 6289:     }
 6290:     if (ref($settings) eq 'HASH') {
 6291:         if ($settings->{'captcha'}) {
 6292:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 6293:         } else {
 6294:             $checked{'original'} = ' checked="checked"';
 6295:         }
 6296:         if ($settings->{'captcha'} eq 'recaptcha') {
 6297:             $pubtext = $lt{'pub'};
 6298:             $privtext = $lt{'priv'};
 6299:             $keyentry = 'text';
 6300:             $vertext = $lt{'ver'};
 6301:             $currver = $settings->{'recaptchaversion'};
 6302:             if ($currver ne '2') {
 6303:                 $currver = 1;
 6304:             }
 6305:         }
 6306:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 6307:             $currpub = $settings->{'recaptchakeys'}{'public'};
 6308:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 6309:         }
 6310:     } else {
 6311:         $checked{'original'} = ' checked="checked"';
 6312:     }
 6313:     my $css_class;
 6314:     if ($itemcount%2) {
 6315:         $css_class = 'LC_odd_row';
 6316:     }
 6317:     if ($customcss) {
 6318:         $css_class .= " $customcss";
 6319:     }
 6320:     $css_class =~ s/^\s+//;
 6321:     if ($css_class) {
 6322:         $css_class = ' class="'.$css_class.'"';
 6323:     }
 6324:     if ($rowstyle) {
 6325:         $css_class .= ' style="'.$rowstyle.'"';
 6326:     }
 6327:     my $output = '<tr'.$css_class.'>'.
 6328:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 6329:                  '<table><tr><td>'."\n";
 6330:     foreach my $option ('original','recaptcha','notused') {
 6331:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 6332:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 6333:                    $lt{$option}.'</label></span>';
 6334:         unless ($option eq 'notused') {
 6335:             $output .= ('&nbsp;'x2)."\n";
 6336:         }
 6337:     }
 6338: #
 6339: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 6340: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 6341: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 6342: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 6343: #
 6344:     $output .= '</td></tr>'."\n".
 6345:                '<tr><td class="LC_zero_height">'."\n".
 6346:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 6347:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 6348:                $currpub.'" size="40" /></span><br />'."\n".
 6349:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 6350:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 6351:                $currpriv.'" size="40" /></span><br />'.
 6352:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 6353:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 6354:                $currver.'" size="3" /></span><br />'.
 6355:                '</td></tr></table>'."\n".
 6356:                '</td></tr>';
 6357:     return $output;
 6358: }
 6359: 
 6360: sub user_formats_row {
 6361:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
 6362:     my $output;
 6363:     my %text = (
 6364:                    'username' => 'new usernames',
 6365:                    'id'       => 'IDs',
 6366:                );
 6367:     unless ($type eq 'email') {
 6368:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 6369:         $output = '<tr '.$css_class.'>'.
 6370:                   '<td><span class="LC_nobreak">'.
 6371:                   &mt("Format rules to check for $text{$type}: ").
 6372:                   '</td><td class="LC_left_item" colspan="2"><table>';
 6373:     }
 6374:     my $rem;
 6375:     if (ref($ruleorder) eq 'ARRAY') {
 6376:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 6377:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 6378:                 my $rem = $i%($numinrow);
 6379:                 if ($rem == 0) {
 6380:                     if ($i > 0) {
 6381:                         $output .= '</tr>';
 6382:                     }
 6383:                     $output .= '<tr>';
 6384:                 }
 6385:                 my $check = ' ';
 6386:                 if (ref($settings) eq 'HASH') {
 6387:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 6388:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 6389:                             $check = ' checked="checked" ';
 6390:                         }
 6391:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
 6392:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
 6393:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
 6394:                                 $check = ' checked="checked" ';
 6395:                             }
 6396:                         }
 6397:                     }
 6398:                 }
 6399:                 my $name = $type.'_rule';
 6400:                 if ($type eq 'email') {
 6401:                     $name .= '_'.$status;
 6402:                 }
 6403:                 $output .= '<td class="LC_left_item">'.
 6404:                            '<span class="LC_nobreak"><label>'.
 6405:                            '<input type="checkbox" name="'.$name.'" '.
 6406:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 6407:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 6408:             }
 6409:         }
 6410:         $rem = @{$ruleorder}%($numinrow);
 6411:     }
 6412:     my $colsleft;
 6413:     if ($rem) {
 6414:         $colsleft = $numinrow - $rem;
 6415:     }
 6416:     if ($colsleft > 1 ) {
 6417:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6418:                    '&nbsp;</td>';
 6419:     } elsif ($colsleft == 1) {
 6420:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 6421:     }
 6422:     $output .= '</tr></table>';
 6423:     unless ($type eq 'email') {
 6424:         $output .= '</td></tr>';
 6425:     }
 6426:     return $output;
 6427: }
 6428: 
 6429: sub usercreation_types {
 6430:     my %lt = &Apache::lonlocal::texthash (
 6431:                     author     => 'When adding a co-author',
 6432:                     course     => 'When adding a user to a course',
 6433:                     requestcrs => 'When requesting a course',
 6434:                     any        => 'Any',
 6435:                     official   => 'Institutional only ',
 6436:                     unofficial => 'Non-institutional only',
 6437:                     none       => 'None',
 6438:     );
 6439:     return %lt;
 6440: }
 6441: 
 6442: sub selfcreation_types {
 6443:     my %lt = &Apache::lonlocal::texthash (
 6444:                     selfcreate => 'User creates own account',
 6445:                     any        => 'Any',
 6446:                     official   => 'Institutional only ',
 6447:                     unofficial => 'Non-institutional only',
 6448:                     email      => 'E-mail address',
 6449:                     login      => 'Institutional Login',
 6450:                     sso        => 'SSO',
 6451:              );
 6452: }
 6453: 
 6454: sub authtype_names {
 6455:     my %lt = &Apache::lonlocal::texthash(
 6456:                       int    => 'Internal',
 6457:                       krb4   => 'Kerberos 4',
 6458:                       krb5   => 'Kerberos 5',
 6459:                       loc    => 'Local',
 6460:                   );
 6461:     return %lt;
 6462: }
 6463: 
 6464: sub context_names {
 6465:     my %context_title = &Apache::lonlocal::texthash(
 6466:        author => 'Creating users when an Author',
 6467:        course => 'Creating users when in a course',
 6468:        domain => 'Creating users when a Domain Coordinator',
 6469:     );
 6470:     return %context_title;
 6471: }
 6472: 
 6473: sub print_usermodification {
 6474:     my ($position,$dom,$settings,$rowtotal) = @_;
 6475:     my $numinrow = 4;
 6476:     my ($context,$datatable,$rowcount);
 6477:     if ($position eq 'top') {
 6478:         $rowcount = 0;
 6479:         $context = 'author'; 
 6480:         foreach my $role ('ca','aa') {
 6481:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6482:                                                    $numinrow,$rowcount);
 6483:             $$rowtotal ++;
 6484:             $rowcount ++;
 6485:         }
 6486:     } elsif ($position eq 'bottom') {
 6487:         $context = 'course';
 6488:         $rowcount = 0;
 6489:         foreach my $role ('st','ep','ta','in','cr') {
 6490:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6491:                                                    $numinrow,$rowcount);
 6492:             $$rowtotal ++;
 6493:             $rowcount ++;
 6494:         }
 6495:     }
 6496:     return $datatable;
 6497: }
 6498: 
 6499: sub print_defaults {
 6500:     my ($position,$dom,$settings,$rowtotal) = @_;
 6501:     my $rownum = 0;
 6502:     my ($datatable,$css_class,$titles);
 6503:     unless ($position eq 'bottom') {
 6504:         $titles = &defaults_titles($dom);
 6505:     }
 6506:     if ($position eq 'top') {
 6507:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 6508:                      'datelocale_def','portal_def');
 6509:         my %defaults;
 6510:         if (ref($settings) eq 'HASH') {
 6511:             %defaults = %{$settings};
 6512:         } else {
 6513:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6514:             foreach my $item (@items) {
 6515:                 $defaults{$item} = $domdefaults{$item};
 6516:             }
 6517:         }
 6518:         foreach my $item (@items) {
 6519:             if ($rownum%2) {
 6520:                 $css_class = '';
 6521:             } else {
 6522:                 $css_class = ' class="LC_odd_row" ';
 6523:             }
 6524:             $datatable .= '<tr'.$css_class.'>'.
 6525:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 6526:                           '</span></td><td class="LC_right_item" colspan="3">';
 6527:             if ($item eq 'auth_def') {
 6528:                 my @authtypes = ('internal','krb4','krb5','localauth');
 6529:                 my %shortauth = (
 6530:                                  internal => 'int',
 6531:                                  krb4 => 'krb4',
 6532:                                  krb5 => 'krb5',
 6533:                                  localauth  => 'loc'
 6534:                                 );
 6535:                 my %authnames = &authtype_names();
 6536:                 foreach my $auth (@authtypes) {
 6537:                     my $checked = ' ';
 6538:                     if ($defaults{$item} eq $auth) {
 6539:                         $checked = ' checked="checked" ';
 6540:                     }
 6541:                     $datatable .= '<label><input type="radio" name="'.$item.
 6542:                                   '" value="'.$auth.'"'.$checked.'/>'.
 6543:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 6544:                 }
 6545:             } elsif ($item eq 'timezone_def') {
 6546:                 my $includeempty = 1;
 6547:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 6548:             } elsif ($item eq 'datelocale_def') {
 6549:                 my $includeempty = 1;
 6550:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 6551:             } elsif ($item eq 'lang_def') {
 6552:                 my $includeempty = 1;
 6553:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 6554:             } else {
 6555:                 my $size;
 6556:                 if ($item eq 'portal_def') {
 6557:                     $size = ' size="25"';
 6558:                 }
 6559:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6560:                               $defaults{$item}.'"'.$size.' />';
 6561:             }
 6562:             $datatable .= '</td></tr>';
 6563:             $rownum ++;
 6564:         }
 6565:     } elsif ($position eq 'middle') {
 6566:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 6567:         my %defaults;
 6568:         if (ref($settings) eq 'HASH') {
 6569:             %defaults = %{$settings};
 6570:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 6571:                 $defaults{'intauth_cost'} = 10;
 6572:             }
 6573:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 6574:                 $defaults{'intauth_check'} = 0;
 6575:             }
 6576:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 6577:                 $defaults{'intauth_switch'} = 0;
 6578:             }
 6579:         } else {
 6580:             %defaults = (
 6581:                           'intauth_cost'   => 10,
 6582:                           'intauth_check'  => 0,
 6583:                           'intauth_switch' => 0,
 6584:                         );
 6585:         }
 6586:         foreach my $item (@items) {
 6587:             if ($rownum%2) {
 6588:                 $css_class = '';
 6589:             } else {
 6590:                 $css_class = ' class="LC_odd_row" ';
 6591:             }
 6592:             $datatable .= '<tr'.$css_class.'>'.
 6593:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 6594:                           '</span></td><td class="LC_left_item" colspan="3">';
 6595:             if ($item eq 'intauth_switch') {
 6596:                 my @options = (0,1,2);
 6597:                 my %optiondesc = &Apache::lonlocal::texthash (
 6598:                                    0 => 'No',
 6599:                                    1 => 'Yes',
 6600:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 6601:                                  );
 6602:                 $datatable .= '<table width="100%">';
 6603:                 foreach my $option (@options) {
 6604:                     my $checked = ' ';
 6605:                     if ($defaults{$item} eq $option) {
 6606:                         $checked = ' checked="checked"';
 6607:                     }
 6608:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6609:                                   '<label><input type="radio" name="'.$item.
 6610:                                   '" value="'.$option.'"'.$checked.' />'.
 6611:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6612:                 }
 6613:                 $datatable .= '</table>';
 6614:             } elsif ($item eq 'intauth_check') {
 6615:                 my @options = (0,1,2);
 6616:                 my %optiondesc = &Apache::lonlocal::texthash (
 6617:                                    0 => 'No',
 6618:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 6619:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 6620:                                  );
 6621:                 $datatable .= '<table wisth="100%">';
 6622:                 foreach my $option (@options) {
 6623:                     my $checked = ' ';
 6624:                     my $onclick;
 6625:                     if ($defaults{$item} eq $option) {
 6626:                         $checked = ' checked="checked"';
 6627:                     }
 6628:                     if ($option == 2) {
 6629:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 6630:                     }
 6631:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6632:                                   '<label><input type="radio" name="'.$item.
 6633:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 6634:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6635:                 }
 6636:                 $datatable .= '</table>';
 6637:             } else {
 6638:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6639:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />'; 
 6640:             }
 6641:             $datatable .= '</td></tr>';
 6642:             $rownum ++;
 6643:         }
 6644:     } else {
 6645:         my %defaults;
 6646:         if (ref($settings) eq 'HASH') {
 6647:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 6648:                 my $maxnum = @{$settings->{'inststatusorder'}};
 6649:                 for (my $i=0; $i<$maxnum; $i++) {
 6650:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 6651:                     my $item = $settings->{'inststatusorder'}->[$i];
 6652:                     my $title = $settings->{'inststatustypes'}->{$item};
 6653:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 6654:                     $datatable .= '<tr'.$css_class.'>'.
 6655:                                   '<td><span class="LC_nobreak">'.
 6656:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 6657:                     for (my $k=0; $k<=$maxnum; $k++) {
 6658:                         my $vpos = $k+1;
 6659:                         my $selstr;
 6660:                         if ($k == $i) {
 6661:                             $selstr = ' selected="selected" ';
 6662:                         }
 6663:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6664:                     }
 6665:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 6666:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 6667:                                   &mt('delete').'</span></td>'.
 6668:                                   '<td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.&mt('Name displayed:').
 6669:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 6670:                                   '</span></td></tr>';
 6671:                 }
 6672:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 6673:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 6674:                 $datatable .= '<tr '.$css_class.'>'.
 6675:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 6676:                 for (my $k=0; $k<=$maxnum; $k++) {
 6677:                     my $vpos = $k+1;
 6678:                     my $selstr;
 6679:                     if ($k == $maxnum) {
 6680:                         $selstr = ' selected="selected" ';
 6681:                     }
 6682:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6683:                 }
 6684:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 6685:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 6686:                               '&nbsp;'.&mt('(new)').
 6687:                               '</span></td><td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.
 6688:                               &mt('Name displayed:').
 6689:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 6690:                               '</tr>'."\n";
 6691:                 $rownum ++;
 6692:             }
 6693:         }
 6694:     }
 6695:     $$rowtotal += $rownum;
 6696:     return $datatable;
 6697: }
 6698: 
 6699: sub get_languages_hash {
 6700:     my %langchoices;
 6701:     foreach my $id (&Apache::loncommon::languageids()) {
 6702:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 6703:         if ($code ne '') {
 6704:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 6705:         }
 6706:     }
 6707:     return %langchoices;
 6708: }
 6709: 
 6710: sub defaults_titles {
 6711:     my ($dom) = @_;
 6712:     my %titles = &Apache::lonlocal::texthash (
 6713:                    'auth_def'      => 'Default authentication type',
 6714:                    'auth_arg_def'  => 'Default authentication argument',
 6715:                    'lang_def'      => 'Default language',
 6716:                    'timezone_def'  => 'Default timezone',
 6717:                    'datelocale_def' => 'Default locale for dates',
 6718:                    'portal_def'     => 'Portal/Default URL',
 6719:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 6720:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 6721:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 6722:                  );
 6723:     if ($dom) {
 6724:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 6725:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 6726:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 6727:         $protocol = 'http' if ($protocol ne 'https');
 6728:         if ($uint_dom) {
 6729:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 6730:                                          $uint_dom);
 6731:         }
 6732:     }
 6733:     return (\%titles);
 6734: }
 6735: 
 6736: sub print_scantronformat {
 6737:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 6738:     my $itemcount = 1;
 6739:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 6740:         %confhash);
 6741:     my $switchserver = &check_switchserver($dom,$confname);
 6742:     my %lt = &Apache::lonlocal::texthash (
 6743:                 default => 'Default bubblesheet format file error',
 6744:                 custom  => 'Custom bubblesheet format file error',
 6745:              );
 6746:     my %scantronfiles = (
 6747:         default => 'default.tab',
 6748:         custom => 'custom.tab',
 6749:     );
 6750:     foreach my $key (keys(%scantronfiles)) {
 6751:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 6752:                               .$scantronfiles{$key};
 6753:     }
 6754:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 6755:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 6756:         if (!$switchserver) {
 6757:             my $servadm = $r->dir_config('lonAdmEMail');
 6758:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 6759:             if ($configuserok eq 'ok') {
 6760:                 if ($author_ok eq 'ok') {
 6761:                     my %legacyfile = (
 6762:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 6763:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 6764:                     );
 6765:                     my %md5chk;
 6766:                     foreach my $type (keys(%legacyfile)) {
 6767:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 6768:                         chomp($md5chk{$type});
 6769:                     }
 6770:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 6771:                         foreach my $type (keys(%legacyfile)) {
 6772:                             ($scantronurls{$type},my $error) = 
 6773:                                 &legacy_scantronformat($r,$dom,$confname,
 6774:                                                  $type,$legacyfile{$type},
 6775:                                                  $scantronurls{$type},
 6776:                                                  $scantronfiles{$type});
 6777:                             if ($error ne '') {
 6778:                                 $error{$type} = $error;
 6779:                             }
 6780:                         }
 6781:                         if (keys(%error) == 0) {
 6782:                             $is_custom = 1;
 6783:                             $confhash{'scantron'}{'scantronformat'} = 
 6784:                                 $scantronurls{'custom'};
 6785:                             my $putresult = 
 6786:                                 &Apache::lonnet::put_dom('configuration',
 6787:                                                          \%confhash,$dom);
 6788:                             if ($putresult ne 'ok') {
 6789:                                 $error{'custom'} = 
 6790:                                     '<span class="LC_error">'.
 6791:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6792:                             }
 6793:                         }
 6794:                     } else {
 6795:                         ($scantronurls{'default'},my $error) =
 6796:                             &legacy_scantronformat($r,$dom,$confname,
 6797:                                           'default',$legacyfile{'default'},
 6798:                                           $scantronurls{'default'},
 6799:                                           $scantronfiles{'default'});
 6800:                         if ($error eq '') {
 6801:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 6802:                             my $putresult =
 6803:                                 &Apache::lonnet::put_dom('configuration',
 6804:                                                          \%confhash,$dom);
 6805:                             if ($putresult ne 'ok') {
 6806:                                 $error{'default'} =
 6807:                                     '<span class="LC_error">'.
 6808:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6809:                             }
 6810:                         } else {
 6811:                             $error{'default'} = $error;
 6812:                         }
 6813:                     }
 6814:                 }
 6815:             }
 6816:         } else {
 6817:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 6818:         }
 6819:     }
 6820:     if (ref($settings) eq 'HASH') {
 6821:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 6822:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 6823:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 6824:                 $scantronurl = '';
 6825:             } else {
 6826:                 $scantronurl = $settings->{'scantronformat'};
 6827:             }
 6828:             $is_custom = 1;
 6829:         } else {
 6830:             $scantronurl = $scantronurls{'default'};
 6831:         }
 6832:     } else {
 6833:         if ($is_custom) {
 6834:             $scantronurl = $scantronurls{'custom'};
 6835:         } else {
 6836:             $scantronurl = $scantronurls{'default'};
 6837:         }
 6838:     }
 6839:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6840:     $datatable .= '<tr'.$css_class.'>';
 6841:     if (!$is_custom) {
 6842:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 6843:                       '<span class="LC_nobreak">';
 6844:         if ($scantronurl) {
 6845:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 6846:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 6847:         } else {
 6848:             $datatable = &mt('File unavailable for display');
 6849:         }
 6850:         $datatable .= '</span></td>';
 6851:         if (keys(%error) == 0) { 
 6852:             $datatable .= '<td style="vertical-align: bottom">';
 6853:             if (!$switchserver) {
 6854:                 $datatable .= &mt('Upload:').'<br />';
 6855:             }
 6856:         } else {
 6857:             my $errorstr;
 6858:             foreach my $key (sort(keys(%error))) {
 6859:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6860:             }
 6861:             $datatable .= '<td>'.$errorstr;
 6862:         }
 6863:     } else {
 6864:         if (keys(%error) > 0) {
 6865:             my $errorstr;
 6866:             foreach my $key (sort(keys(%error))) {
 6867:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6868:             } 
 6869:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 6870:         } elsif ($scantronurl) {
 6871:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 6872:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 6873:             $datatable .= '<td><span class="LC_nobreak">'.
 6874:                           $link.
 6875:                           '<label><input type="checkbox" name="scantronformat_del"'.
 6876:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 6877:                           '<td><span class="LC_nobreak">&nbsp;'.
 6878:                           &mt('Replace:').'</span><br />';
 6879:         }
 6880:     }
 6881:     if (keys(%error) == 0) {
 6882:         if ($switchserver) {
 6883:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 6884:         } else {
 6885:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 6886:                          '<input type="file" name="scantronformat" /></span>';
 6887:         }
 6888:     }
 6889:     $datatable .= '</td></tr>';
 6890:     $$rowtotal ++;
 6891:     return $datatable;
 6892: }
 6893: 
 6894: sub legacy_scantronformat {
 6895:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 6896:     my ($url,$error);
 6897:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 6898:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 6899:         (my $result,$url) =
 6900:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 6901:                          '','',$newfile);
 6902:         if ($result ne 'ok') {
 6903:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 6904:         }
 6905:     }
 6906:     return ($url,$error);
 6907: }
 6908: 
 6909: sub print_coursecategories {
 6910:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 6911:     my $datatable;
 6912:     if ($position eq 'top') {
 6913:         my (%checked);
 6914:         my @catitems = ('unauth','auth');
 6915:         my @cattypes = ('std','domonly','codesrch','none');
 6916:         $checked{'unauth'} = 'std';
 6917:         $checked{'auth'} = 'std';
 6918:         if (ref($settings) eq 'HASH') {
 6919:             foreach my $type (@cattypes) {
 6920:                 if ($type eq $settings->{'unauth'}) {
 6921:                     $checked{'unauth'} = $type;
 6922:                 }
 6923:                 if ($type eq $settings->{'auth'}) {
 6924:                     $checked{'auth'} = $type;
 6925:                 }
 6926:             }
 6927:         }
 6928:         my %lt = &Apache::lonlocal::texthash (
 6929:                                                unauth   => 'Catalog type for unauthenticated users',
 6930:                                                auth     => 'Catalog type for authenticated users',
 6931:                                                none     => 'No catalog',
 6932:                                                std      => 'Standard catalog',
 6933:                                                domonly  => 'Domain-only catalog',
 6934:                                                codesrch => "Code search form",
 6935:                                              );
 6936:        my $itemcount = 0;
 6937:        foreach my $item (@catitems) {
 6938:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 6939:            $datatable .= '<tr '.$css_class.'>'.
 6940:                          '<td>'.$lt{$item}.'</td>'.
 6941:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 6942:            foreach my $type (@cattypes) {
 6943:                my $ischecked;
 6944:                if ($checked{$item} eq $type) {
 6945:                    $ischecked=' checked="checked"';
 6946:                }
 6947:                $datatable .= '<label>'.
 6948:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 6949:                              ' />'.$lt{$type}.'</label>&nbsp;';
 6950:            }
 6951:            $datatable .= '</td></tr>';
 6952:            $itemcount ++;
 6953:         }
 6954:         $$rowtotal += $itemcount;
 6955:     } elsif ($position eq 'middle') {
 6956:         my $toggle_cats_crs = ' ';
 6957:         my $toggle_cats_dom = ' checked="checked" ';
 6958:         my $can_cat_crs = ' ';
 6959:         my $can_cat_dom = ' checked="checked" ';
 6960:         my $toggle_catscomm_comm = ' ';
 6961:         my $toggle_catscomm_dom = ' checked="checked" ';
 6962:         my $can_catcomm_comm = ' ';
 6963:         my $can_catcomm_dom = ' checked="checked" ';
 6964:         my $toggle_catsplace_place = ' ';
 6965:         my $toggle_catsplace_dom = ' checked="checked" ';
 6966:         my $can_catplace_place = ' ';
 6967:         my $can_catplace_dom = ' checked="checked" ';
 6968: 
 6969:         if (ref($settings) eq 'HASH') {
 6970:             if ($settings->{'togglecats'} eq 'crs') {
 6971:                 $toggle_cats_crs = $toggle_cats_dom;
 6972:                 $toggle_cats_dom = ' ';
 6973:             }
 6974:             if ($settings->{'categorize'} eq 'crs') {
 6975:                 $can_cat_crs = $can_cat_dom;
 6976:                 $can_cat_dom = ' ';
 6977:             }
 6978:             if ($settings->{'togglecatscomm'} eq 'comm') {
 6979:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 6980:                 $toggle_catscomm_dom = ' ';
 6981:             }
 6982:             if ($settings->{'categorizecomm'} eq 'comm') {
 6983:                 $can_catcomm_comm = $can_catcomm_dom;
 6984:                 $can_catcomm_dom = ' ';
 6985:             }
 6986:             if ($settings->{'togglecatsplace'} eq 'place') {
 6987:                 $toggle_catsplace_place = $toggle_catsplace_dom;
 6988:                 $toggle_catsplace_dom = ' ';
 6989:             }
 6990:             if ($settings->{'categorizeplace'} eq 'place') {
 6991:                 $can_catplace_place = $can_catplace_dom;
 6992:                 $can_catplace_dom = ' ';
 6993:             }
 6994:         }
 6995:         my %title = &Apache::lonlocal::texthash (
 6996:                      togglecats      => 'Show/Hide a course in catalog',
 6997:                      togglecatscomm  => 'Show/Hide a community in catalog',
 6998:                      togglecatsplace => 'Show/Hide a placement test in catalog',
 6999:                      categorize      => 'Assign a category to a course',
 7000:                      categorizecomm  => 'Assign a category to a community',
 7001:                      categorizeplace => 'Assign a category to a placement test',
 7002:                     );
 7003:         my %level = &Apache::lonlocal::texthash (
 7004:                      dom   => 'Set in Domain',
 7005:                      crs   => 'Set in Course',
 7006:                      comm  => 'Set in Community',
 7007:                      place => 'Set in Placement Test',
 7008:                     );
 7009:         $datatable = '<tr class="LC_odd_row">'.
 7010:                   '<td>'.$title{'togglecats'}.'</td>'.
 7011:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 7012:                   '<input type="radio" name="togglecats"'.
 7013:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7014:                   '<label><input type="radio" name="togglecats"'.
 7015:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 7016:                   '</tr><tr>'.
 7017:                   '<td>'.$title{'categorize'}.'</td>'.
 7018:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 7019:                   '<label><input type="radio" name="categorize"'.
 7020:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7021:                   '<label><input type="radio" name="categorize"'.
 7022:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 7023:                   '</tr><tr class="LC_odd_row">'.
 7024:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 7025:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 7026:                   '<input type="radio" name="togglecatscomm"'.
 7027:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7028:                   '<label><input type="radio" name="togglecatscomm"'.
 7029:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 7030:                   '</tr><tr>'.
 7031:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 7032:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 7033:                   '<label><input type="radio" name="categorizecomm"'.
 7034:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7035:                   '<label><input type="radio" name="categorizecomm"'.
 7036:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 7037:                   '</tr><tr>'.
 7038:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
 7039:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 7040:                   '<input type="radio" name="togglecatsplace"'.
 7041:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7042:                   '<label><input type="radio" name="togglecatscomm"'.
 7043:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
 7044:                   '</tr><tr>'.
 7045:                   '<td>'.$title{'categorizeplace'}.'</td>'.
 7046:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 7047:                   '<label><input type="radio" name="categorizeplace"'.
 7048:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7049:                   '<label><input type="radio" name="categorizeplace"'.
 7050:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
 7051:                   '</tr>';
 7052:         $$rowtotal += 6;
 7053:     } else {
 7054:         my $css_class;
 7055:         my $itemcount = 1;
 7056:         my $cathash; 
 7057:         if (ref($settings) eq 'HASH') {
 7058:             $cathash = $settings->{'cats'};
 7059:         }
 7060:         if (ref($cathash) eq 'HASH') {
 7061:             my (@cats,@trails,%allitems,%idx,@jsarray);
 7062:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 7063:                                                    \%allitems,\%idx,\@jsarray);
 7064:             my $maxdepth = scalar(@cats);
 7065:             my $colattrib = '';
 7066:             if ($maxdepth > 2) {
 7067:                 $colattrib = ' colspan="2" ';
 7068:             }
 7069:             my @path;
 7070:             if (@cats > 0) {
 7071:                 if (ref($cats[0]) eq 'ARRAY') {
 7072:                     my $numtop = @{$cats[0]};
 7073:                     my $maxnum = $numtop;
 7074:                     my %default_names = (
 7075:                           instcode    => &mt('Official courses'),
 7076:                           communities => &mt('Communities'),
 7077:                           placement   => &mt('Placement Tests'),
 7078:                     );
 7079: 
 7080:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 7081:                         ($cathash->{'instcode::0'} eq '') ||
 7082:                         (!grep(/^communities$/,@{$cats[0]})) || 
 7083:                         ($cathash->{'communities::0'} eq '') ||
 7084:                         (!grep(/^placement$/,@{$cats[0]})) ||
 7085:                         ($cathash->{'placement::0'} eq '')) {
 7086:                         $maxnum ++;
 7087:                     }
 7088:                     my $lastidx;
 7089:                     for (my $i=0; $i<$numtop; $i++) {
 7090:                         my $parent = $cats[0][$i];
 7091:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7092:                         my $item = &escape($parent).'::0';
 7093:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 7094:                         $lastidx = $idx{$item};
 7095:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7096:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 7097:                         for (my $k=0; $k<=$maxnum; $k++) {
 7098:                             my $vpos = $k+1;
 7099:                             my $selstr;
 7100:                             if ($k == $i) {
 7101:                                 $selstr = ' selected="selected" ';
 7102:                             }
 7103:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7104:                         }
 7105:                         $datatable .= '</select></span></td><td>';
 7106:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
 7107:                             $datatable .=  '<span class="LC_nobreak">'
 7108:                                            .$default_names{$parent}.'</span>';
 7109:                             if ($parent eq 'instcode') {
 7110:                                 $datatable .= '<br /><span class="LC_nobreak">('
 7111:                                               .&mt('with institutional codes')
 7112:                                               .')</span></td><td'.$colattrib.'>';
 7113:                             } else {
 7114:                                 $datatable .= '<table><tr><td>';
 7115:                             }
 7116:                             $datatable .= '<span class="LC_nobreak">'
 7117:                                           .'<label><input type="radio" name="'
 7118:                                           .$parent.'" value="1" checked="checked" />'
 7119:                                           .&mt('Display').'</label>';
 7120:                             if ($parent eq 'instcode') {
 7121:                                 $datatable .= '&nbsp;';
 7122:                             } else {
 7123:                                 $datatable .= '</span></td></tr><tr><td>'
 7124:                                               .'<span class="LC_nobreak">';
 7125:                             }
 7126:                             $datatable .= '<label><input type="radio" name="'
 7127:                                           .$parent.'" value="0" />'
 7128:                                           .&mt('Do not display').'</label></span>';
 7129:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
 7130:                                 $datatable .= '</td></tr></table>';
 7131:                             }
 7132:                             $datatable .= '</td>';
 7133:                         } else {
 7134:                             $datatable .= $parent
 7135:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 7136:                                           .'<input type="checkbox" name="deletecategory" '
 7137:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 7138:                         }
 7139:                         my $depth = 1;
 7140:                         push(@path,$parent);
 7141:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 7142:                         pop(@path);
 7143:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 7144:                         $itemcount ++;
 7145:                     }
 7146:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7147:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 7148:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 7149:                     for (my $k=0; $k<=$maxnum; $k++) {
 7150:                         my $vpos = $k+1;
 7151:                         my $selstr;
 7152:                         if ($k == $numtop) {
 7153:                             $selstr = ' selected="selected" ';
 7154:                         }
 7155:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7156:                     }
 7157:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 7158:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 7159:                                   .'</tr>'."\n";
 7160:                     $itemcount ++;
 7161:                     foreach my $default ('instcode','communities','placement') {
 7162:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 7163:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7164:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 7165:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 7166:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 7167:                             for (my $k=0; $k<=$maxnum; $k++) {
 7168:                                 my $vpos = $k+1;
 7169:                                 my $selstr;
 7170:                                 if ($k == $maxnum) {
 7171:                                     $selstr = ' selected="selected" ';
 7172:                                 }
 7173:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7174:                             }
 7175:                             $datatable .= '</select></span></td>'.
 7176:                                           '<td><span class="LC_nobreak">'.
 7177:                                           $default_names{$default}.'</span>';
 7178:                             if ($default eq 'instcode') {
 7179:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 7180:                                               .&mt('with institutional codes').')</span>';
 7181:                             }
 7182:                             $datatable .= '</td>'
 7183:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 7184:                                           .&mt('Display').'</label>&nbsp;'
 7185:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 7186:                                           .&mt('Do not display').'</label></span></td></tr>';
 7187:                         }
 7188:                     }
 7189:                 }
 7190:             } else {
 7191:                 $datatable .= &initialize_categories($itemcount);
 7192:             }
 7193:         } else {
 7194:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 7195:                           .&initialize_categories($itemcount);
 7196:         }
 7197:         $$rowtotal += $itemcount;
 7198:     }
 7199:     return $datatable;
 7200: }
 7201: 
 7202: sub print_serverstatuses {
 7203:     my ($dom,$settings,$rowtotal) = @_;
 7204:     my $datatable;
 7205:     my @pages = &serverstatus_pages();
 7206:     my (%namedaccess,%machineaccess);
 7207:     foreach my $type (@pages) {
 7208:         $namedaccess{$type} = '';
 7209:         $machineaccess{$type}= '';
 7210:     }
 7211:     if (ref($settings) eq 'HASH') {
 7212:         foreach my $type (@pages) {
 7213:             if (exists($settings->{$type})) {
 7214:                 if (ref($settings->{$type}) eq 'HASH') {
 7215:                     foreach my $key (keys(%{$settings->{$type}})) {
 7216:                         if ($key eq 'namedusers') {
 7217:                             $namedaccess{$type} = $settings->{$type}->{$key};
 7218:                         } elsif ($key eq 'machines') {
 7219:                             $machineaccess{$type} = $settings->{$type}->{$key};
 7220:                         }
 7221:                     }
 7222:                 }
 7223:             }
 7224:         }
 7225:     }
 7226:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 7227:     my $rownum = 0;
 7228:     my $css_class;
 7229:     foreach my $type (@pages) {
 7230:         $rownum ++;
 7231:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 7232:         $datatable .= '<tr'.$css_class.'>'.
 7233:                       '<td><span class="LC_nobreak">'.
 7234:                       $titles->{$type}.'</span></td>'.
 7235:                       '<td class="LC_left_item">'.
 7236:                       '<input type="text" name="'.$type.'_namedusers" '.
 7237:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 7238:                       '<td class="LC_right_item">'.
 7239:                       '<span class="LC_nobreak">'.
 7240:                       '<input type="text" name="'.$type.'_machines" '.
 7241:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 7242:                       '</td></tr>'."\n";
 7243:     }
 7244:     $$rowtotal += $rownum;
 7245:     return $datatable;
 7246: }
 7247: 
 7248: sub serverstatus_pages {
 7249:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 7250:             'checksums','clusterstatus','certstatus','metadata_keywords',
 7251:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
 7252:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
 7253: }
 7254: 
 7255: sub defaults_javascript {
 7256:     my ($settings) = @_;
 7257:     my $intauthcheck = &mt('Warning: disallowing login for an authenticated user if the stored cost is less than the default will require a password reset by/for the user.');
 7258:     my $intauthcost = &mt('Warning: bcrypt encryption cost for internal authentication must be an integer.');
 7259:     &js_escape(\$intauthcheck);
 7260:     &js_escape(\$intauthcost);
 7261:     my $intauthjs = <<"ENDSCRIPT";
 7262: 
 7263: function warnIntAuth(field) {
 7264:     if (field.name == 'intauth_check') {
 7265:         if (field.value == '2') {
 7266:             alert('$intauthcheck');
 7267:         }
 7268:     }
 7269:     if (field.name == 'intauth_cost') {
 7270:         field.value.replace(/\s/g,'');
 7271:         if (field.value != '') {
 7272:             var regexdigit=/^\\d+\$/;
 7273:             if (!regexdigit.test(field.value)) {
 7274:                 alert('$intauthcost');
 7275:             }
 7276:         }
 7277:     }
 7278:     return;
 7279: }
 7280: 
 7281: ENDSCRIPT
 7282: 
 7283:     if (ref($settings) ne 'HASH') {
 7284:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 7285:     }
 7286:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 7287:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 7288:         if ($maxnum eq '') {
 7289:             $maxnum = 0;
 7290:         }
 7291:         $maxnum ++;
 7292:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 7293:         return <<"ENDSCRIPT";
 7294: <script type="text/javascript">
 7295: // <![CDATA[
 7296: function reorderTypes(form,caller) {
 7297:     var changedVal;
 7298: $jstext 
 7299:     var newpos = 'addinststatus_pos';
 7300:     var current = new Array;
 7301:     var maxh = $maxnum;
 7302:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 7303:     var oldVal;
 7304:     if (caller == newpos) {
 7305:         changedVal = newitemVal;
 7306:     } else {
 7307:         var curritem = 'inststatus_pos_'+caller;
 7308:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 7309:         current[newitemVal] = newpos;
 7310:     }
 7311:     for (var i=0; i<inststatuses.length; i++) {
 7312:         if (inststatuses[i] != caller) {
 7313:             var elementName = 'inststatus_pos_'+inststatuses[i];
 7314:             if (form.elements[elementName]) {
 7315:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 7316:                 current[currVal] = elementName;
 7317:             }
 7318:         }
 7319:     }
 7320:     for (var j=0; j<maxh; j++) {
 7321:         if (current[j] == undefined) {
 7322:             oldVal = j;
 7323:         }
 7324:     }
 7325:     if (oldVal < changedVal) {
 7326:         for (var k=oldVal+1; k<=changedVal ; k++) {
 7327:            var elementName = current[k];
 7328:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 7329:         }
 7330:     } else {
 7331:         for (var k=changedVal; k<oldVal; k++) {
 7332:             var elementName = current[k];
 7333:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 7334:         }
 7335:     }
 7336:     return;
 7337: }
 7338: 
 7339: $intauthjs
 7340: 
 7341: // ]]>
 7342: </script>
 7343: 
 7344: ENDSCRIPT
 7345:     } else {
 7346:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 7347:     }
 7348: }
 7349: 
 7350: sub coursecategories_javascript {
 7351:     my ($settings) = @_;
 7352:     my ($output,$jstext,$cathash);
 7353:     if (ref($settings) eq 'HASH') {
 7354:         $cathash = $settings->{'cats'};
 7355:     }
 7356:     if (ref($cathash) eq 'HASH') {
 7357:         my (@cats,@jsarray,%idx);
 7358:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 7359:         if (@jsarray > 0) {
 7360:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 7361:             for (my $i=0; $i<@jsarray; $i++) {
 7362:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 7363:                     my $catstr = join('","',@{$jsarray[$i]});
 7364:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 7365:                 }
 7366:             }
 7367:         }
 7368:     } else {
 7369:         $jstext  = '    var categories = Array(1);'."\n".
 7370:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 7371:     }
 7372:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 7373:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 7374:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
 7375:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 7376:     &js_escape(\$instcode_reserved);
 7377:     &js_escape(\$communities_reserved);
 7378:     &js_escape(\$placement_reserved);
 7379:     &js_escape(\$choose_again);
 7380:     $output = <<"ENDSCRIPT";
 7381: <script type="text/javascript">
 7382: // <![CDATA[
 7383: function reorderCats(form,parent,item,idx) {
 7384:     var changedVal;
 7385: $jstext
 7386:     var newpos = 'addcategory_pos';
 7387:     if (parent == '') {
 7388:         var has_instcode = 0;
 7389:         var maxtop = categories[idx].length;
 7390:         for (var j=0; j<maxtop; j++) {
 7391:             if (categories[idx][j] == 'instcode::0') {
 7392:                 has_instcode == 1;
 7393:             }
 7394:         }
 7395:         if (has_instcode == 0) {
 7396:             categories[idx][maxtop] = 'instcode_pos';
 7397:         }
 7398:     } else {
 7399:         newpos += '_'+parent;
 7400:     }
 7401:     var maxh = 1 + categories[idx].length;
 7402:     var current = new Array;
 7403:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 7404:     if (item == newpos) {
 7405:         changedVal = newitemVal;
 7406:     } else {
 7407:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 7408:         current[newitemVal] = newpos;
 7409:     }
 7410:     for (var i=0; i<categories[idx].length; i++) {
 7411:         var elementName = categories[idx][i];
 7412:         if (elementName != item) {
 7413:             if (form.elements[elementName]) {
 7414:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 7415:                 current[currVal] = elementName;
 7416:             }
 7417:         }
 7418:     }
 7419:     var oldVal;
 7420:     for (var j=0; j<maxh; j++) {
 7421:         if (current[j] == undefined) {
 7422:             oldVal = j;
 7423:         }
 7424:     }
 7425:     if (oldVal < changedVal) {
 7426:         for (var k=oldVal+1; k<=changedVal ; k++) {
 7427:            var elementName = current[k];
 7428:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 7429:         }
 7430:     } else {
 7431:         for (var k=changedVal; k<oldVal; k++) {
 7432:             var elementName = current[k];
 7433:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 7434:         }
 7435:     }
 7436:     return;
 7437: }
 7438: 
 7439: function categoryCheck(form) {
 7440:     if (form.elements['addcategory_name'].value == 'instcode') {
 7441:         alert('$instcode_reserved\\n$choose_again');
 7442:         return false;
 7443:     }
 7444:     if (form.elements['addcategory_name'].value == 'communities') {
 7445:         alert('$communities_reserved\\n$choose_again');
 7446:         return false;
 7447:     }
 7448:     if (form.elements['addcategory_name'].value == 'placement') {
 7449:         alert('$placement_reserved\\n$choose_again');
 7450:         return false;
 7451:     }
 7452:     return true;
 7453: }
 7454: 
 7455: // ]]>
 7456: </script>
 7457: 
 7458: ENDSCRIPT
 7459:     return $output;
 7460: }
 7461: 
 7462: sub initialize_categories {
 7463:     my ($itemcount) = @_;
 7464:     my ($datatable,$css_class,$chgstr);
 7465:     my %default_names = (
 7466:                       instcode    => 'Official courses (with institutional codes)',
 7467:                       communities => 'Communities',
 7468:                       placement   => 'Placement Tests',
 7469:                         );
 7470:     my $select0 = ' selected="selected"';
 7471:     my $select1 = '';
 7472:     foreach my $default ('instcode','communities','placement') {
 7473:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7474:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 7475:         if (($default eq 'communities') || ($default eq 'placement')) {
 7476:             $select1 = $select0;
 7477:             $select0 = '';
 7478:         }
 7479:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7480:                      .'<select name="'.$default.'_pos">'
 7481:                      .'<option value="0"'.$select0.'>1</option>'
 7482:                      .'<option value="1"'.$select1.'>2</option>'
 7483:                      .'<option value="2">3</option></select>&nbsp;'
 7484:                      .$default_names{$default}
 7485:                      .'</span></td><td><span class="LC_nobreak">'
 7486:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 7487:                      .&mt('Display').'</label>&nbsp;<label>'
 7488:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 7489:                  .'</label></span></td></tr>';
 7490:         $itemcount ++;
 7491:     }
 7492:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7493:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 7494:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7495:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 7496:                   .'<option value="0">1</option>'
 7497:                   .'<option value="1">2</option>'
 7498:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 7499:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 7500:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 7501:     return $datatable;
 7502: }
 7503: 
 7504: sub build_category_rows {
 7505:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 7506:     my ($text,$name,$item,$chgstr);
 7507:     if (ref($cats) eq 'ARRAY') {
 7508:         my $maxdepth = scalar(@{$cats});
 7509:         if (ref($cats->[$depth]) eq 'HASH') {
 7510:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 7511:                 my $numchildren = @{$cats->[$depth]{$parent}};
 7512:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7513:                 $text .= '<td><table class="LC_data_table">';
 7514:                 my ($idxnum,$parent_name,$parent_item);
 7515:                 my $higher = $depth - 1;
 7516:                 if ($higher == 0) {
 7517:                     $parent_name = &escape($parent).'::'.$higher;
 7518:                 } else {
 7519:                     if (ref($path) eq 'ARRAY') {
 7520:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7521:                     }
 7522:                 }
 7523:                 $parent_item = 'addcategory_pos_'.$parent_name;
 7524:                 for (my $j=0; $j<=$numchildren; $j++) {
 7525:                     if ($j < $numchildren) {
 7526:                         $name = $cats->[$depth]{$parent}[$j];
 7527:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 7528:                         $idxnum = $idx->{$item};
 7529:                     } else {
 7530:                         $name = $parent_name;
 7531:                         $item = $parent_item;
 7532:                     }
 7533:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 7534:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 7535:                     for (my $i=0; $i<=$numchildren; $i++) {
 7536:                         my $vpos = $i+1;
 7537:                         my $selstr;
 7538:                         if ($j == $i) {
 7539:                             $selstr = ' selected="selected" ';
 7540:                         }
 7541:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 7542:                     }
 7543:                     $text .= '</select>&nbsp;';
 7544:                     if ($j < $numchildren) {
 7545:                         my $deeper = $depth+1;
 7546:                         $text .= $name.'&nbsp;'
 7547:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 7548:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 7549:                         if(ref($path) eq 'ARRAY') {
 7550:                             push(@{$path},$name);
 7551:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 7552:                             pop(@{$path});
 7553:                         }
 7554:                     } else {
 7555:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 7556:                         if ($j == $numchildren) {
 7557:                             $text .= $name;
 7558:                         } else {
 7559:                             $text .= $item;
 7560:                         }
 7561:                         $text .= '" value="" />';
 7562:                     }
 7563:                     $text .= '</td></tr>';
 7564:                 }
 7565:                 $text .= '</table></td>';
 7566:             } else {
 7567:                 my $higher = $depth-1;
 7568:                 if ($higher == 0) {
 7569:                     $name = &escape($parent).'::'.$higher;
 7570:                 } else {
 7571:                     if (ref($path) eq 'ARRAY') {
 7572:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7573:                     }
 7574:                 }
 7575:                 my $colspan;
 7576:                 if ($parent ne 'instcode') {
 7577:                     $colspan = $maxdepth - $depth - 1;
 7578:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 7579:                 }
 7580:             }
 7581:         }
 7582:     }
 7583:     return $text;
 7584: }
 7585: 
 7586: sub modifiable_userdata_row {
 7587:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
 7588:         $rowid,$customcss,$rowstyle) = @_;
 7589:     my ($role,$rolename,$statustype);
 7590:     $role = $item;
 7591:     if ($context eq 'cancreate') {
 7592:         if ($item =~ /^(emailusername)_(.+)$/) {
 7593:             $role = $1;
 7594:             $statustype = $2;
 7595:             if (ref($usertypes) eq 'HASH') {
 7596:                 if ($usertypes->{$statustype}) {
 7597:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 7598:                 } else {
 7599:                     $rolename = &mt('Data provided by user');
 7600:                 }
 7601:             }
 7602:         }
 7603:     } elsif ($context eq 'selfcreate') {
 7604:         if (ref($usertypes) eq 'HASH') {
 7605:             $rolename = $usertypes->{$role};
 7606:         } else {
 7607:             $rolename = $role;
 7608:         }
 7609:     } else {
 7610:         if ($role eq 'cr') {
 7611:             $rolename = &mt('Custom role');
 7612:         } else {
 7613:             $rolename = &Apache::lonnet::plaintext($role);
 7614:         }
 7615:     }
 7616:     my (@fields,%fieldtitles);
 7617:     if (ref($fieldsref) eq 'ARRAY') {
 7618:         @fields = @{$fieldsref};
 7619:     } else {
 7620:         @fields = ('lastname','firstname','middlename','generation',
 7621:                    'permanentemail','id');
 7622:     }
 7623:     if ((ref($titlesref) eq 'HASH')) {
 7624:         %fieldtitles = %{$titlesref};
 7625:     } else {
 7626:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7627:     }
 7628:     my $output;
 7629:     my $css_class;
 7630:     if ($rowcount%2) {
 7631:         $css_class = 'LC_odd_row';
 7632:     }
 7633:     if ($customcss) {
 7634:         $css_class .= " $customcss";
 7635:     }
 7636:     $css_class =~ s/^\s+//;
 7637:     if ($css_class) {
 7638:         $css_class = ' class="'.$css_class.'"';
 7639:     }
 7640:     if ($rowstyle) {
 7641:         $css_class .= ' style="'.$rowstyle.'"';
 7642:     }
 7643:     if ($rowid) {
 7644:         $rowid = ' id="'.$rowid.'"';
 7645:     }
 7646:     $output = '<tr '.$css_class.$rowid.'>'.
 7647:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 7648:               '<td class="LC_left_item" colspan="2"><table>';
 7649:     my $rem;
 7650:     my %checks;
 7651:     if (ref($settings) eq 'HASH') {
 7652:         if (ref($settings->{$context}) eq 'HASH') {
 7653:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 7654:                 my $hashref = $settings->{$context}->{$role};
 7655:                 if ($role eq 'emailusername') {
 7656:                     if ($statustype) {
 7657:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 7658:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 7659:                             if (ref($hashref) eq 'HASH') { 
 7660:                                 foreach my $field (@fields) {
 7661:                                     if ($hashref->{$field}) {
 7662:                                         $checks{$field} = $hashref->{$field};
 7663:                                     }
 7664:                                 }
 7665:                             }
 7666:                         }
 7667:                     }
 7668:                 } else {
 7669:                     if (ref($hashref) eq 'HASH') {
 7670:                         foreach my $field (@fields) {
 7671:                             if ($hashref->{$field}) {
 7672:                                 $checks{$field} = ' checked="checked" ';
 7673:                             }
 7674:                         }
 7675:                     }
 7676:                 }
 7677:             }
 7678:         }
 7679:     }
 7680:  
 7681:     my $total = scalar(@fields);
 7682:     for (my $i=0; $i<$total; $i++) {
 7683:         $rem = $i%($numinrow);
 7684:         if ($rem == 0) {
 7685:             if ($i > 0) {
 7686:                 $output .= '</tr>';
 7687:             }
 7688:             $output .= '<tr>';
 7689:         }
 7690:         my $check = ' ';
 7691:         unless ($role eq 'emailusername') {
 7692:             if (exists($checks{$fields[$i]})) {
 7693:                 $check = $checks{$fields[$i]}
 7694:             } else {
 7695:                 if ($role eq 'st') {
 7696:                     if (ref($settings) ne 'HASH') {
 7697:                         $check = ' checked="checked" '; 
 7698:                     }
 7699:                 }
 7700:             }
 7701:         }
 7702:         $output .= '<td class="LC_left_item">'.
 7703:                    '<span class="LC_nobreak">';
 7704:         if ($role eq 'emailusername') {
 7705:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 7706:                 $checks{$fields[$i]} = 'omit';
 7707:             }
 7708:             foreach my $option ('required','optional','omit') {
 7709:                 my $checked='';
 7710:                 if ($checks{$fields[$i]} eq $option) {
 7711:                     $checked='checked="checked" ';
 7712:                 }
 7713:                 $output .= '<label>'.
 7714:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 7715:                            &mt($option).'</label>'.('&nbsp;' x2);
 7716:             }
 7717:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 7718:         } else {
 7719:             $output .= '<label>'.
 7720:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 7721:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 7722:                        '</label>';
 7723:         }
 7724:         $output .= '</span></td>';
 7725:     }
 7726:     $rem = $total%$numinrow;
 7727:     my $colsleft;
 7728:     if ($rem) {
 7729:         $colsleft = $numinrow - $rem;
 7730:     }
 7731:     if ($colsleft > 1) {
 7732:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7733:                    '&nbsp;</td>';
 7734:     } elsif ($colsleft == 1) {
 7735:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 7736:     }
 7737:     $output .= '</tr></table></td></tr>';
 7738:     return $output;
 7739: }
 7740: 
 7741: sub insttypes_row {
 7742:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
 7743:         $customcss,$rowstyle) = @_;
 7744:     my %lt = &Apache::lonlocal::texthash (
 7745:                       cansearch => 'Users allowed to search',
 7746:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 7747:                       lockablenames => 'User preference to lock name',
 7748:                       selfassign    => 'Self-reportable affiliations',
 7749:              );
 7750:     my $showdom;
 7751:     if ($context eq 'cansearch') {
 7752:         $showdom = ' ('.$dom.')';
 7753:     }
 7754:     my $class = 'LC_left_item';
 7755:     if ($context eq 'statustocreate') {
 7756:         $class = 'LC_right_item';
 7757:     }
 7758:     my $css_class;
 7759:     if ($$rowtotal%2) {
 7760:         $css_class = 'LC_odd_row';
 7761:     }
 7762:     if ($customcss) {
 7763:         $css_class .= ' '.$customcss;
 7764:     }
 7765:     $css_class =~ s/^\s+//;
 7766:     if ($css_class) {
 7767:         $css_class = ' class="'.$css_class.'"';
 7768:     }
 7769:     if ($rowstyle) {
 7770:         $css_class .= ' style="'.$rowstyle.'"';
 7771:     }
 7772:     if ($onclick) {
 7773:         $onclick = 'onclick="'.$onclick.'" ';
 7774:     }
 7775:     my $output = '<tr'.$css_class.'>'.
 7776:                  '<td>'.$lt{$context}.$showdom.
 7777:                  '</td><td class="'.$class.'" colspan="2"><table>';
 7778:     my $rem;
 7779:     if (ref($types) eq 'ARRAY') {
 7780:         for (my $i=0; $i<@{$types}; $i++) {
 7781:             if (defined($usertypes->{$types->[$i]})) {
 7782:                 my $rem = $i%($numinrow);
 7783:                 if ($rem == 0) {
 7784:                     if ($i > 0) {
 7785:                         $output .= '</tr>';
 7786:                     }
 7787:                     $output .= '<tr>';
 7788:                 }
 7789:                 my $check = ' ';
 7790:                 if (ref($settings) eq 'HASH') {
 7791:                     if (ref($settings->{$context}) eq 'ARRAY') {
 7792:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 7793:                             $check = ' checked="checked" ';
 7794:                         }
 7795:                     } elsif ($context eq 'statustocreate') {
 7796:                         $check = ' checked="checked" ';
 7797:                     }
 7798:                 }
 7799:                 $output .= '<td class="LC_left_item">'.
 7800:                            '<span class="LC_nobreak"><label>'.
 7801:                            '<input type="checkbox" name="'.$context.'" '.
 7802:                            'value="'.$types->[$i].'"'.$check.$onclick.' />'.
 7803:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 7804:             }
 7805:         }
 7806:         $rem = @{$types}%($numinrow);
 7807:     }
 7808:     my $colsleft = $numinrow - $rem;
 7809:     if (($rem == 0) && (@{$types} > 0)) {
 7810:         $output .= '<tr>';
 7811:     }
 7812:     if ($colsleft > 1) {
 7813:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 7814:     } else {
 7815:         $output .= '<td class="LC_left_item">';
 7816:     }
 7817:     my $defcheck = ' ';
 7818:     if (ref($settings) eq 'HASH') {  
 7819:         if (ref($settings->{$context}) eq 'ARRAY') {
 7820:             if (grep(/^default$/,@{$settings->{$context}})) {
 7821:                 $defcheck = ' checked="checked" ';
 7822:             }
 7823:         } elsif ($context eq 'statustocreate') {
 7824:             $defcheck = ' checked="checked" ';
 7825:         }
 7826:     }
 7827:     $output .= '<span class="LC_nobreak"><label>'.
 7828:                '<input type="checkbox" name="'.$context.'" '.
 7829:                'value="default"'.$defcheck.$onclick.' />'.
 7830:                $othertitle.'</label></span></td>'.
 7831:                '</tr></table></td></tr>';
 7832:     return $output;
 7833: }
 7834: 
 7835: sub sorted_searchtitles {
 7836:     my %searchtitles = &Apache::lonlocal::texthash(
 7837:                          'uname' => 'username',
 7838:                          'lastname' => 'last name',
 7839:                          'lastfirst' => 'last name, first name',
 7840:                      );
 7841:     my @titleorder = ('uname','lastname','lastfirst');
 7842:     return (\%searchtitles,\@titleorder);
 7843: }
 7844: 
 7845: sub sorted_searchtypes {
 7846:     my %srchtypes_desc = (
 7847:                            exact    => 'is exact match',
 7848:                            contains => 'contains ..',
 7849:                            begins   => 'begins with ..',
 7850:                          );
 7851:     my @srchtypeorder = ('exact','begins','contains');
 7852:     return (\%srchtypes_desc,\@srchtypeorder);
 7853: }
 7854: 
 7855: sub usertype_update_row {
 7856:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 7857:     my $datatable;
 7858:     my $numinrow = 4;
 7859:     foreach my $type (@{$types}) {
 7860:         if (defined($usertypes->{$type})) {
 7861:             $$rownums ++;
 7862:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 7863:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 7864:                           '</td><td class="LC_left_item"><table>';
 7865:             for (my $i=0; $i<@{$fields}; $i++) {
 7866:                 my $rem = $i%($numinrow);
 7867:                 if ($rem == 0) {
 7868:                     if ($i > 0) {
 7869:                         $datatable .= '</tr>';
 7870:                     }
 7871:                     $datatable .= '<tr>';
 7872:                 }
 7873:                 my $check = ' ';
 7874:                 if (ref($settings) eq 'HASH') {
 7875:                     if (ref($settings->{'fields'}) eq 'HASH') {
 7876:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 7877:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 7878:                                 $check = ' checked="checked" ';
 7879:                             }
 7880:                         }
 7881:                     }
 7882:                 }
 7883: 
 7884:                 if ($i == @{$fields}-1) {
 7885:                     my $colsleft = $numinrow - $rem;
 7886:                     if ($colsleft > 1) {
 7887:                         $datatable .= '<td colspan="'.$colsleft.'">';
 7888:                     } else {
 7889:                         $datatable .= '<td>';
 7890:                     }
 7891:                 } else {
 7892:                     $datatable .= '<td>';
 7893:                 }
 7894:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7895:                               '<input type="checkbox" name="updateable_'.$type.
 7896:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 7897:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 7898:             }
 7899:             $datatable .= '</tr></table></td></tr>';
 7900:         }
 7901:     }
 7902:     return $datatable;
 7903: }
 7904: 
 7905: sub modify_login {
 7906:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 7907:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 7908:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 7909:     %title = ( coursecatalog => 'Display course catalog',
 7910:                adminmail => 'Display administrator E-mail address',
 7911:                helpdesk  => 'Display "Contact Helpdesk" link',
 7912:                newuser => 'Link for visitors to create a user account',
 7913:                loginheader => 'Log-in box header');
 7914:     @offon = ('off','on');
 7915:     if (ref($domconfig{login}) eq 'HASH') {
 7916:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 7917:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 7918:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 7919:             }
 7920:         }
 7921:     }
 7922:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 7923:                                            \%domconfig,\%loginhash);
 7924:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7925:     foreach my $item (@toggles) {
 7926:         $loginhash{login}{$item} = $env{'form.'.$item};
 7927:     }
 7928:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 7929:     if (ref($colchanges{'login'}) eq 'HASH') {  
 7930:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 7931:                                          \%loginhash);
 7932:     }
 7933: 
 7934:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7935:     my %domservers = &Apache::lonnet::get_servers($dom);
 7936:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 7937:     if (keys(%servers) > 1) {
 7938:         foreach my $lonhost (keys(%servers)) {
 7939:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 7940:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 7941:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 7942:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 7943:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 7944:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7945:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7946:                         $changes{'loginvia'}{$lonhost} = 1;
 7947:                     } else {
 7948:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 7949:                         $changes{'loginvia'}{$lonhost} = 1;
 7950:                     }
 7951:                 } else {
 7952:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7953:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7954:                         $changes{'loginvia'}{$lonhost} = 1;
 7955:                     }
 7956:                 }
 7957:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 7958:                     foreach my $item (@loginvia_attribs) {
 7959:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 7960:                     }
 7961:                 } else {
 7962:                     foreach my $item (@loginvia_attribs) {
 7963:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7964:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7965:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 7966:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7967:                                 $new = '/';
 7968:                             }
 7969:                         }
 7970:                         if (($item eq 'custompath') && 
 7971:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 7972:                             $new = '';
 7973:                         }
 7974:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 7975:                             $changes{'loginvia'}{$lonhost} = 1;
 7976:                         }
 7977:                         if ($item eq 'exempt') {
 7978:                             $new = &check_exempt_addresses($new);
 7979:                         }
 7980:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 7981:                     }
 7982:                 }
 7983:             } else {
 7984:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7985:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7986:                     $changes{'loginvia'}{$lonhost} = 1;
 7987:                     foreach my $item (@loginvia_attribs) {
 7988:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7989:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7990:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7991:                                 $new = '/';
 7992:                             }
 7993:                         }
 7994:                         if (($item eq 'custompath') && 
 7995:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 7996:                             $new = '';
 7997:                         }
 7998:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 7999:                     }
 8000:                 }
 8001:             }
 8002:         }
 8003:     }
 8004: 
 8005:     my $servadm = $r->dir_config('lonAdmEMail');
 8006:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 8007:     if (ref($domconfig{'login'}) eq 'HASH') {
 8008:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 8009:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 8010:                 if ($lang eq 'nolang') {
 8011:                     push(@currlangs,$lang);
 8012:                 } elsif (defined($langchoices{$lang})) {
 8013:                     push(@currlangs,$lang);
 8014:                 } else {
 8015:                     next;
 8016:                 }
 8017:             }
 8018:         }
 8019:     }
 8020:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 8021:     if (@currlangs > 0) {
 8022:         foreach my $lang (@currlangs) {
 8023:             if (grep(/^\Q$lang\E$/,@delurls)) {
 8024:                 $changes{'helpurl'}{$lang} = 1;
 8025:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 8026:                 $changes{'helpurl'}{$lang} = 1;
 8027:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 8028:                 push(@newlangs,$lang);
 8029:             } else {
 8030:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 8031:             }
 8032:         }
 8033:     }
 8034:     unless (grep(/^nolang$/,@currlangs)) {
 8035:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 8036:             $changes{'helpurl'}{'nolang'} = 1;
 8037:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 8038:             push(@newlangs,'nolang');
 8039:         }
 8040:     }
 8041:     if ($env{'form.loginhelpurl_add_lang'}) {
 8042:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 8043:             ($env{'form.loginhelpurl_add_file.filename'})) {
 8044:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 8045:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 8046:         }
 8047:     }
 8048:     if ((@newlangs > 0) || ($addedfile)) {
 8049:         my $error;
 8050:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8051:         if ($configuserok eq 'ok') {
 8052:             if ($switchserver) {
 8053:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 8054:             } elsif ($author_ok eq 'ok') {
 8055:                 my @allnew = @newlangs;
 8056:                 if ($addedfile ne '') {
 8057:                     push(@allnew,$addedfile);
 8058:                 }
 8059:                 foreach my $lang (@allnew) {
 8060:                     my $formelem = 'loginhelpurl_'.$lang;
 8061:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 8062:                         $formelem = 'loginhelpurl_add_file';
 8063:                     }
 8064:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 8065:                                                                "help/$lang",'','',$newfile{$lang});
 8066:                     if ($result eq 'ok') {
 8067:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 8068:                         $changes{'helpurl'}{$lang} = 1;
 8069:                     } else {
 8070:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 8071:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 8072:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 8073:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 8074:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 8075:                         }
 8076:                     }
 8077:                 }
 8078:             } else {
 8079:                 $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);
 8080:             }
 8081:         } else {
 8082:             $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);
 8083:         }
 8084:         if ($error) {
 8085:             &Apache::lonnet::logthis($error);
 8086:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8087:         }
 8088:     }
 8089: 
 8090:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 8091:     if (ref($domconfig{'login'}) eq 'HASH') {
 8092:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 8093:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 8094:                 if ($domservers{$lonhost}) {
 8095:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 8096:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 8097:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 8098:                     }
 8099:                 }
 8100:             }
 8101:         }
 8102:     }
 8103:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 8104:     foreach my $lonhost (sort(keys(%domservers))) {
 8105:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 8106:             $changes{'headtag'}{$lonhost} = 1;
 8107:         } else {
 8108:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 8109:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 8110:             }
 8111:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 8112:                 push(@newhosts,$lonhost);
 8113:             } elsif ($currheadtagurls{$lonhost}) {
 8114:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 8115:                 if ($currexempt{$lonhost}) {
 8116:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 8117:                         $changes{'headtag'}{$lonhost} = 1;
 8118:                     }
 8119:                 } elsif ($possexempt{$lonhost}) {
 8120:                     $changes{'headtag'}{$lonhost} = 1;
 8121:                 }
 8122:                 if ($possexempt{$lonhost}) {
 8123:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 8124:                 }
 8125:             }
 8126:         }
 8127:     }
 8128:     if (@newhosts) {
 8129:         my $error;
 8130:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8131:         if ($configuserok eq 'ok') {
 8132:             if ($switchserver) {
 8133:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 8134:             } elsif ($author_ok eq 'ok') {
 8135:                 foreach my $lonhost (@newhosts) {
 8136:                     my $formelem = 'loginheadtag_'.$lonhost;
 8137:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 8138:                                                                           "login/headtag/$lonhost",'','',
 8139:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 8140:                     if ($result eq 'ok') {
 8141:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 8142:                         $changes{'headtag'}{$lonhost} = 1;
 8143:                         if ($possexempt{$lonhost}) {
 8144:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 8145:                         }
 8146:                     } else {
 8147:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 8148:                                            $newheadtagurls{$lonhost},$result);
 8149:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 8150:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 8151:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 8152:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 8153:                         }
 8154:                     }
 8155:                 }
 8156:             } else {
 8157:                 $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);
 8158:             }
 8159:         } else {
 8160:             $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);
 8161:         }
 8162:         if ($error) {
 8163:             &Apache::lonnet::logthis($error);
 8164:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8165:         }
 8166:     }
 8167:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 8168: 
 8169:     my $defaulthelpfile = '/adm/loginproblems.html';
 8170:     my $defaulttext = &mt('Default in use');
 8171: 
 8172:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 8173:                                              $dom);
 8174:     if ($putresult eq 'ok') {
 8175:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 8176:         my %defaultchecked = (
 8177:                     'coursecatalog' => 'on',
 8178:                     'helpdesk'      => 'on',
 8179:                     'adminmail'     => 'off',
 8180:                     'newuser'       => 'off',
 8181:         );
 8182:         if (ref($domconfig{'login'}) eq 'HASH') {
 8183:             foreach my $item (@toggles) {
 8184:                 if ($defaultchecked{$item} eq 'on') { 
 8185:                     if (($domconfig{'login'}{$item} eq '0') &&
 8186:                         ($env{'form.'.$item} eq '1')) {
 8187:                         $changes{$item} = 1;
 8188:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 8189:                               $domconfig{'login'}{$item} eq '1') &&
 8190:                              ($env{'form.'.$item} eq '0')) {
 8191:                         $changes{$item} = 1;
 8192:                     }
 8193:                 } elsif ($defaultchecked{$item} eq 'off') {
 8194:                     if (($domconfig{'login'}{$item} eq '1') &&
 8195:                         ($env{'form.'.$item} eq '0')) {
 8196:                         $changes{$item} = 1;
 8197:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 8198:                               $domconfig{'login'}{$item} eq '0') &&
 8199:                              ($env{'form.'.$item} eq '1')) {
 8200:                         $changes{$item} = 1;
 8201:                     }
 8202:                 }
 8203:             }
 8204:         }
 8205:         if (keys(%changes) > 0 || $colchgtext) {
 8206:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8207:             if (ref($lastactref) eq 'HASH') {
 8208:                 $lastactref->{'domainconfig'} = 1;
 8209:             }
 8210:             $resulttext = &mt('Changes made:').'<ul>';
 8211:             foreach my $item (sort(keys(%changes))) {
 8212:                 if ($item eq 'loginvia') {
 8213:                     if (ref($changes{$item}) eq 'HASH') {
 8214:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 8215:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 8216:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 8217:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 8218:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 8219:                                     $protocol = 'http' if ($protocol ne 'https');
 8220:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 8221: 
 8222:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 8223:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 8224:                                     } else {
 8225:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 8226:                                     }
 8227:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 8228:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 8229:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 8230:                                     }
 8231:                                     $resulttext .= '</li>';
 8232:                                 } else {
 8233:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 8234:                                 }
 8235:                             } else {
 8236:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 8237:                             }
 8238:                         }
 8239:                         $resulttext .= '</ul></li>';
 8240:                     }
 8241:                 } elsif ($item eq 'helpurl') {
 8242:                     if (ref($changes{$item}) eq 'HASH') {
 8243:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 8244:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 8245:                                 my ($chg,$link);
 8246:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 8247:                                 if ($lang eq 'nolang') {
 8248:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 8249:                                 } else {
 8250:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 8251:                                 }
 8252:                                 $resulttext .= '<li>'.$chg.'</li>';
 8253:                             } else {
 8254:                                 my $chg;
 8255:                                 if ($lang eq 'nolang') {
 8256:                                     $chg = &mt('custom log-in help file for no preferred language');
 8257:                                 } else {
 8258:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 8259:                                 }
 8260:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 8261:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 8262:                                                       '?inhibitmenu=yes',$chg,600,500).
 8263:                                                '</li>';
 8264:                             }
 8265:                         }
 8266:                     }
 8267:                 } elsif ($item eq 'headtag') {
 8268:                     if (ref($changes{$item}) eq 'HASH') {
 8269:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 8270:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 8271:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 8272:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 8273:                                 $resulttext .= '<li><a href="'.
 8274:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 8275:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 8276:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 8277:                                 if ($possexempt{$lonhost}) {
 8278:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 8279:                                 } else {
 8280:                                     $resulttext .= &mt('included for any client IP');
 8281:                                 }
 8282:                                 $resulttext .= '</li>';
 8283:                             }
 8284:                         }
 8285:                     }
 8286:                 } elsif ($item eq 'captcha') {
 8287:                     if (ref($loginhash{'login'}) eq 'HASH') {
 8288:                         my $chgtxt;
 8289:                         if ($loginhash{'login'}{$item} eq 'notused') {
 8290:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 8291:                         } else {
 8292:                             my %captchas = &captcha_phrases();
 8293:                             if ($captchas{$loginhash{'login'}{$item}}) {
 8294:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 8295:                             } else {
 8296:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 8297:                             }
 8298:                         }
 8299:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 8300:                     }
 8301:                 } elsif ($item eq 'recaptchakeys') {
 8302:                     if (ref($loginhash{'login'}) eq 'HASH') {
 8303:                         my ($privkey,$pubkey);
 8304:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 8305:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 8306:                             $privkey = $loginhash{'login'}{$item}{'private'};
 8307:                         }
 8308:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 8309:                         if (!$pubkey) {
 8310:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 8311:                         } else {
 8312:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 8313:                         }
 8314:                         if (!$privkey) {
 8315:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 8316:                         } else {
 8317:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 8318:                         }
 8319:                         $chgtxt .= '</ul>';
 8320:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 8321:                     }
 8322:                 } elsif ($item eq 'recaptchaversion') {
 8323:                     if (ref($loginhash{'login'}) eq 'HASH') {
 8324:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 8325:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 8326:                                            '</li>';
 8327:                         }
 8328:                     }
 8329:                 } else {
 8330:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 8331:                 }
 8332:             }
 8333:             $resulttext .= $colchgtext.'</ul>';
 8334:         } else {
 8335:             $resulttext = &mt('No changes made to log-in page settings');
 8336:         }
 8337:     } else {
 8338:         $resulttext = '<span class="LC_error">'.
 8339: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8340:     }
 8341:     if ($errors) {
 8342:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 8343:                        $errors.'</ul>';
 8344:     }
 8345:     return $resulttext;
 8346: }
 8347: 
 8348: sub check_exempt_addresses {
 8349:     my ($iplist) = @_;
 8350:     $iplist =~ s/^\s+//;
 8351:     $iplist =~ s/\s+$//;
 8352:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 8353:     my (@okips,$new);
 8354:     foreach my $ip (@poss_ips) {
 8355:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 8356:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 8357:                 push(@okips,$ip);
 8358:             }
 8359:         }
 8360:     }
 8361:     if (@okips > 0) {
 8362:         $new = join(',',@okips);
 8363:     } else {
 8364:         $new = '';
 8365:     }
 8366:     return $new;
 8367: }
 8368: 
 8369: sub color_font_choices {
 8370:     my %choices =
 8371:         &Apache::lonlocal::texthash (
 8372:             img => "Header",
 8373:             bgs => "Background colors",
 8374:             links => "Link colors",
 8375:             images => "Images",
 8376:             font => "Font color",
 8377:             fontmenu => "Font menu",
 8378:             pgbg => "Page",
 8379:             tabbg => "Header",
 8380:             sidebg => "Border",
 8381:             link => "Link",
 8382:             alink => "Active link",
 8383:             vlink => "Visited link",
 8384:         );
 8385:     return %choices;
 8386: }
 8387: 
 8388: sub modify_rolecolors {
 8389:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 8390:     my ($resulttext,%rolehash);
 8391:     $rolehash{'rolecolors'} = {};
 8392:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 8393:         if ($domconfig{'rolecolors'} eq '') {
 8394:             $domconfig{'rolecolors'} = {};
 8395:         }
 8396:     }
 8397:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 8398:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 8399:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 8400:                                              $dom);
 8401:     if ($putresult eq 'ok') {
 8402:         if (keys(%changes) > 0) {
 8403:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8404:             if (ref($lastactref) eq 'HASH') {
 8405:                 $lastactref->{'domainconfig'} = 1;
 8406:             }
 8407:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 8408:                                              $rolehash{'rolecolors'});
 8409:         } else {
 8410:             $resulttext = &mt('No changes made to default color schemes');
 8411:         }
 8412:     } else {
 8413:         $resulttext = '<span class="LC_error">'.
 8414: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8415:     }
 8416:     if ($errors) {
 8417:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 8418:                        $errors.'</ul>';
 8419:     }
 8420:     return $resulttext;
 8421: }
 8422: 
 8423: sub modify_colors {
 8424:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 8425:     my (%changes,%choices);
 8426:     my @bgs;
 8427:     my @links = ('link','alink','vlink');
 8428:     my @logintext;
 8429:     my @images;
 8430:     my $servadm = $r->dir_config('lonAdmEMail');
 8431:     my $errors;
 8432:     my %defaults;
 8433:     foreach my $role (@{$roles}) {
 8434:         if ($role eq 'login') {
 8435:             %choices = &login_choices();
 8436:             @logintext = ('textcol','bgcol');
 8437:         } else {
 8438:             %choices = &color_font_choices();
 8439:         }
 8440:         if ($role eq 'login') {
 8441:             @images = ('img','logo','domlogo','login');
 8442:             @bgs = ('pgbg','mainbg','sidebg');
 8443:         } else {
 8444:             @images = ('img');
 8445:             @bgs = ('pgbg','tabbg','sidebg');
 8446:         }
 8447:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 8448:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 8449:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 8450:         }
 8451:         if ($role eq 'login') {
 8452:             foreach my $item (@logintext) {
 8453:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8454:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8455:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8456:                 }
 8457:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 8458:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8459:                 }
 8460:             }
 8461:         } else {
 8462:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 8463:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 8464:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 8465:             }
 8466:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 8467:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 8468:             }
 8469:         }
 8470:         foreach my $item (@bgs) {
 8471:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8472:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8473:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8474:             }
 8475:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 8476:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8477:             }
 8478:         }
 8479:         foreach my $item (@links) {
 8480:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8481:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8482:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8483:             }
 8484:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 8485:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8486:             }
 8487:         }
 8488:         my ($configuserok,$author_ok,$switchserver) = 
 8489:             &config_check($dom,$confname,$servadm);
 8490:         my ($width,$height) = &thumb_dimensions();
 8491:         if (ref($domconfig->{$role}) ne 'HASH') {
 8492:             $domconfig->{$role} = {};
 8493:         }
 8494:         foreach my $img (@images) {
 8495:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 8496:                 if (defined($env{'form.login_showlogo_'.$img})) {
 8497:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 8498:                 } else { 
 8499:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 8500:                 }
 8501:             } 
 8502: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 8503: 		 && !defined($domconfig->{$role}{$img})
 8504: 		 && !$env{'form.'.$role.'_del_'.$img}
 8505: 		 && $env{'form.'.$role.'_import_'.$img}) {
 8506: 		# import the old configured image from the .tab setting
 8507: 		# if they haven't provided a new one 
 8508: 		$domconfig->{$role}{$img} = 
 8509: 		    $env{'form.'.$role.'_import_'.$img};
 8510: 	    }
 8511:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 8512:                 my $error;
 8513:                 if ($configuserok eq 'ok') {
 8514:                     if ($switchserver) {
 8515:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 8516:                     } else {
 8517:                         if ($author_ok eq 'ok') {
 8518:                             my ($result,$logourl) = 
 8519:                                 &publishlogo($r,'upload',$role.'_'.$img,
 8520:                                            $dom,$confname,$img,$width,$height);
 8521:                             if ($result eq 'ok') {
 8522:                                 $confhash->{$role}{$img} = $logourl;
 8523:                                 $changes{$role}{'images'}{$img} = 1;
 8524:                             } else {
 8525:                                 $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);
 8526:                             }
 8527:                         } else {
 8528:                             $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);
 8529:                         }
 8530:                     }
 8531:                 } else {
 8532:                     $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);
 8533:                 }
 8534:                 if ($error) {
 8535:                     &Apache::lonnet::logthis($error);
 8536:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8537:                 }
 8538:             } elsif ($domconfig->{$role}{$img} ne '') {
 8539:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 8540:                     my $error;
 8541:                     if ($configuserok eq 'ok') {
 8542: # is confname an author?
 8543:                         if ($switchserver eq '') {
 8544:                             if ($author_ok eq 'ok') {
 8545:                                 my ($result,$logourl) = 
 8546:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 8547:                                             $dom,$confname,$img,$width,$height);
 8548:                                 if ($result eq 'ok') {
 8549:                                     $confhash->{$role}{$img} = $logourl;
 8550: 				    $changes{$role}{'images'}{$img} = 1;
 8551:                                 }
 8552:                             }
 8553:                         }
 8554:                     }
 8555:                 }
 8556:             }
 8557:         }
 8558:         if (ref($domconfig) eq 'HASH') {
 8559:             if (ref($domconfig->{$role}) eq 'HASH') {
 8560:                 foreach my $img (@images) {
 8561:                     if ($domconfig->{$role}{$img} ne '') {
 8562:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8563:                             $confhash->{$role}{$img} = '';
 8564:                             $changes{$role}{'images'}{$img} = 1;
 8565:                         } else {
 8566:                             if ($confhash->{$role}{$img} eq '') {
 8567:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 8568:                             }
 8569:                         }
 8570:                     } else {
 8571:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8572:                             $confhash->{$role}{$img} = '';
 8573:                             $changes{$role}{'images'}{$img} = 1;
 8574:                         } 
 8575:                     }
 8576:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 8577:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 8578:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 8579:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 8580:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 8581:                             }
 8582:                         } else {
 8583:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8584:                                 $changes{$role}{'showlogo'}{$img} = 1;
 8585:                             }
 8586:                         }
 8587:                     }
 8588:                 }
 8589:                 if ($domconfig->{$role}{'font'} ne '') {
 8590:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 8591:                         $changes{$role}{'font'} = 1;
 8592:                     }
 8593:                 } else {
 8594:                     if ($confhash->{$role}{'font'}) {
 8595:                         $changes{$role}{'font'} = 1;
 8596:                     }
 8597:                 }
 8598:                 if ($role ne 'login') {
 8599:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 8600:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 8601:                             $changes{$role}{'fontmenu'} = 1;
 8602:                         }
 8603:                     } else {
 8604:                         if ($confhash->{$role}{'fontmenu'}) {
 8605:                             $changes{$role}{'fontmenu'} = 1;
 8606:                         }
 8607:                     }
 8608:                 }
 8609:                 foreach my $item (@bgs) {
 8610:                     if ($domconfig->{$role}{$item} ne '') {
 8611:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8612:                             $changes{$role}{'bgs'}{$item} = 1;
 8613:                         } 
 8614:                     } else {
 8615:                         if ($confhash->{$role}{$item}) {
 8616:                             $changes{$role}{'bgs'}{$item} = 1;
 8617:                         }
 8618:                     }
 8619:                 }
 8620:                 foreach my $item (@links) {
 8621:                     if ($domconfig->{$role}{$item} ne '') {
 8622:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8623:                             $changes{$role}{'links'}{$item} = 1;
 8624:                         }
 8625:                     } else {
 8626:                         if ($confhash->{$role}{$item}) {
 8627:                             $changes{$role}{'links'}{$item} = 1;
 8628:                         }
 8629:                     }
 8630:                 }
 8631:                 foreach my $item (@logintext) {
 8632:                     if ($domconfig->{$role}{$item} ne '') {
 8633:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8634:                             $changes{$role}{'logintext'}{$item} = 1;
 8635:                         }
 8636:                     } else {
 8637:                         if ($confhash->{$role}{$item}) {
 8638:                             $changes{$role}{'logintext'}{$item} = 1;
 8639:                         }
 8640:                     }
 8641:                 }
 8642:             } else {
 8643:                 &default_change_checker($role,\@images,\@links,\@bgs,
 8644:                                         \@logintext,$confhash,\%changes); 
 8645:             }
 8646:         } else {
 8647:             &default_change_checker($role,\@images,\@links,\@bgs,
 8648:                                     \@logintext,$confhash,\%changes); 
 8649:         }
 8650:     }
 8651:     return ($errors,%changes);
 8652: }
 8653: 
 8654: sub config_check {
 8655:     my ($dom,$confname,$servadm) = @_;
 8656:     my ($configuserok,$author_ok,$switchserver,%currroles);
 8657:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 8658:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 8659:                                                    $confname,$servadm);
 8660:     if ($configuserok eq 'ok') {
 8661:         $switchserver = &check_switchserver($dom,$confname);
 8662:         if ($switchserver eq '') {
 8663:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 8664:         }
 8665:     }
 8666:     return ($configuserok,$author_ok,$switchserver);
 8667: }
 8668: 
 8669: sub default_change_checker {
 8670:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 8671:     foreach my $item (@{$links}) {
 8672:         if ($confhash->{$role}{$item}) {
 8673:             $changes->{$role}{'links'}{$item} = 1;
 8674:         }
 8675:     }
 8676:     foreach my $item (@{$bgs}) {
 8677:         if ($confhash->{$role}{$item}) {
 8678:             $changes->{$role}{'bgs'}{$item} = 1;
 8679:         }
 8680:     }
 8681:     foreach my $item (@{$logintext}) {
 8682:         if ($confhash->{$role}{$item}) {
 8683:             $changes->{$role}{'logintext'}{$item} = 1;
 8684:         }
 8685:     }
 8686:     foreach my $img (@{$images}) {
 8687:         if ($env{'form.'.$role.'_del_'.$img}) {
 8688:             $confhash->{$role}{$img} = '';
 8689:             $changes->{$role}{'images'}{$img} = 1;
 8690:         }
 8691:         if ($role eq 'login') {
 8692:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8693:                 $changes->{$role}{'showlogo'}{$img} = 1;
 8694:             }
 8695:         }
 8696:     }
 8697:     if ($confhash->{$role}{'font'}) {
 8698:         $changes->{$role}{'font'} = 1;
 8699:     }
 8700: }
 8701: 
 8702: sub display_colorchgs {
 8703:     my ($dom,$changes,$roles,$confhash) = @_;
 8704:     my (%choices,$resulttext);
 8705:     if (!grep(/^login$/,@{$roles})) {
 8706:         $resulttext = &mt('Changes made:').'<br />';
 8707:     }
 8708:     foreach my $role (@{$roles}) {
 8709:         if ($role eq 'login') {
 8710:             %choices = &login_choices();
 8711:         } else {
 8712:             %choices = &color_font_choices();
 8713:         }
 8714:         if (ref($changes->{$role}) eq 'HASH') {
 8715:             if ($role ne 'login') {
 8716:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 8717:             }
 8718:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 8719:                 if ($role ne 'login') {
 8720:                     $resulttext .= '<ul>';
 8721:                 }
 8722:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 8723:                     if ($role ne 'login') {
 8724:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 8725:                     }
 8726:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 8727:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 8728:                             if ($confhash->{$role}{$key}{$item}) {
 8729:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 8730:                             } else {
 8731:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 8732:                             }
 8733:                         } elsif ($confhash->{$role}{$item} eq '') {
 8734:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 8735:                         } else {
 8736:                             my $newitem = $confhash->{$role}{$item};
 8737:                             if ($key eq 'images') {
 8738:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" style="vertical-align: bottom" />';
 8739:                             }
 8740:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 8741:                         }
 8742:                     }
 8743:                     if ($role ne 'login') {
 8744:                         $resulttext .= '</ul></li>';
 8745:                     }
 8746:                 } else {
 8747:                     if ($confhash->{$role}{$key} eq '') {
 8748:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 8749:                     } else {
 8750:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 8751:                     }
 8752:                 }
 8753:                 if ($role ne 'login') {
 8754:                     $resulttext .= '</ul>';
 8755:                 }
 8756:             }
 8757:         }
 8758:     }
 8759:     return $resulttext;
 8760: }
 8761: 
 8762: sub thumb_dimensions {
 8763:     return ('200','50');
 8764: }
 8765: 
 8766: sub check_dimensions {
 8767:     my ($inputfile) = @_;
 8768:     my ($fullwidth,$fullheight);
 8769:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 8770:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 8771:             my $imageinfo = <PIPE>;
 8772:             if (!close(PIPE)) {
 8773:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 8774:             }
 8775:             chomp($imageinfo);
 8776:             my ($fullsize) = 
 8777:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 8778:             if ($fullsize) {
 8779:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 8780:             }
 8781:         }
 8782:     }
 8783:     return ($fullwidth,$fullheight);
 8784: }
 8785: 
 8786: sub check_configuser {
 8787:     my ($uhome,$dom,$confname,$servadm) = @_;
 8788:     my ($configuserok,%currroles);
 8789:     if ($uhome eq 'no_host') {
 8790:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 8791:         my $configpass = &LONCAPA::Enrollment::create_password();
 8792:         $configuserok = 
 8793:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 8794:                              $configpass,'','','','','',undef,$servadm);
 8795:     } else {
 8796:         $configuserok = 'ok';
 8797:         %currroles = 
 8798:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 8799:     }
 8800:     return ($configuserok,%currroles);
 8801: }
 8802: 
 8803: sub check_authorstatus {
 8804:     my ($dom,$confname,%currroles) = @_;
 8805:     my $author_ok;
 8806:     if (!$currroles{':'.$dom.':au'}) {
 8807:         my $start = time;
 8808:         my $end = 0;
 8809:         $author_ok = 
 8810:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 8811:                                         'au',$end,$start,'','','domconfig');
 8812:     } else {
 8813:         $author_ok = 'ok';
 8814:     }
 8815:     return $author_ok;
 8816: }
 8817: 
 8818: sub publishlogo {
 8819:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 8820:     my ($output,$fname,$logourl,$madethumb);
 8821:     if ($action eq 'upload') {
 8822:         $fname=$env{'form.'.$formname.'.filename'};
 8823:         chop($env{'form.'.$formname});
 8824:     } else {
 8825:         ($fname) = ($formname =~ /([^\/]+)$/);
 8826:     }
 8827:     if ($savefileas ne '') {
 8828:         $fname = $savefileas;
 8829:     }
 8830:     $fname=&Apache::lonnet::clean_filename($fname);
 8831: # See if there is anything left
 8832:     unless ($fname) { return ('error: no uploaded file'); }
 8833:     $fname="$subdir/$fname";
 8834:     my $docroot=$r->dir_config('lonDocRoot');
 8835:     my $filepath="$docroot/priv";
 8836:     my $relpath = "$dom/$confname";
 8837:     my ($fnamepath,$file,$fetchthumb);
 8838:     $file=$fname;
 8839:     if ($fname=~m|/|) {
 8840:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 8841:     }
 8842:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 8843:     my $count;
 8844:     for ($count=5;$count<=$#parts;$count++) {
 8845:         $filepath.="/$parts[$count]";
 8846:         if ((-e $filepath)!=1) {
 8847:             mkdir($filepath,02770);
 8848:         }
 8849:     }
 8850:     # Check for bad extension and disallow upload
 8851:     if ($file=~/\.(\w+)$/ &&
 8852:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 8853:         $output = 
 8854:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 8855:     } elsif ($file=~/\.(\w+)$/ &&
 8856:         !defined(&Apache::loncommon::fileembstyle($1))) {
 8857:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 8858:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 8859:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 8860:     } elsif (-d "$filepath/$file") {
 8861:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 8862:     } else {
 8863:         my $source = $filepath.'/'.$file;
 8864:         my $logfile;
 8865:         if (!open($logfile,">>$source".'.log')) {
 8866:             return (&mt('No write permission to Authoring Space'));
 8867:         }
 8868:         print $logfile
 8869: "\n================= Publish ".localtime()." ================\n".
 8870: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 8871: # Save the file
 8872:         if (!open(FH,'>'.$source)) {
 8873:             &Apache::lonnet::logthis('Failed to create '.$source);
 8874:             return (&mt('Failed to create file'));
 8875:         }
 8876:         if ($action eq 'upload') {
 8877:             if (!print FH ($env{'form.'.$formname})) {
 8878:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 8879:                 return (&mt('Failed to write file'));
 8880:             }
 8881:         } else {
 8882:             my $original = &Apache::lonnet::filelocation('',$formname);
 8883:             if(!copy($original,$source)) {
 8884:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 8885:                 return (&mt('Failed to write file'));
 8886:             }
 8887:         }
 8888:         close(FH);
 8889:         chmod(0660, $source); # Permissions to rw-rw---.
 8890: 
 8891:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 8892:         my $copyfile=$targetdir.'/'.$file;
 8893: 
 8894:         my @parts=split(/\//,$targetdir);
 8895:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 8896:         for (my $count=5;$count<=$#parts;$count++) {
 8897:             $path.="/$parts[$count]";
 8898:             if (!-e $path) {
 8899:                 print $logfile "\nCreating directory ".$path;
 8900:                 mkdir($path,02770);
 8901:             }
 8902:         }
 8903:         my $versionresult;
 8904:         if (-e $copyfile) {
 8905:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 8906:         } else {
 8907:             $versionresult = 'ok';
 8908:         }
 8909:         if ($versionresult eq 'ok') {
 8910:             if (copy($source,$copyfile)) {
 8911:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 8912:                 $output = 'ok';
 8913:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 8914:                 push(@{$modified_urls},[$copyfile,$source]);
 8915:                 my $metaoutput = 
 8916:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 8917:                 unless ($registered_cleanup) {
 8918:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8919:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8920:                     $registered_cleanup=1;
 8921:                 }
 8922:             } else {
 8923:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 8924:                 $output = &mt('Failed to copy file to RES space').", $!";
 8925:             }
 8926:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 8927:                 my $inputfile = $filepath.'/'.$file;
 8928:                 my $outfile = $filepath.'/'.'tn-'.$file;
 8929:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 8930:                 if ($fullwidth ne '' && $fullheight ne '') { 
 8931:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 8932:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 8933:                         system("convert -sample $thumbsize $inputfile $outfile");
 8934:                         chmod(0660, $filepath.'/tn-'.$file);
 8935:                         if (-e $outfile) {
 8936:                             my $copyfile=$targetdir.'/tn-'.$file;
 8937:                             if (copy($outfile,$copyfile)) {
 8938:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 8939:                                 my $thumb_metaoutput = 
 8940:                                     &write_metadata($dom,$confname,$formname,
 8941:                                                     $targetdir,'tn-'.$file,$logfile);
 8942:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 8943:                                 unless ($registered_cleanup) {
 8944:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8945:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8946:                                     $registered_cleanup=1;
 8947:                                 }
 8948:                                 $madethumb = 1;
 8949:                             } else {
 8950:                                 print $logfile "\nUnable to write ".$copyfile.
 8951:                                                ':'.$!."\n";
 8952:                             }
 8953:                         }
 8954:                     }
 8955:                 }
 8956:             }
 8957:         } else {
 8958:             $output = $versionresult;
 8959:         }
 8960:     }
 8961:     return ($output,$logourl,$madethumb);
 8962: }
 8963: 
 8964: sub logo_versioning {
 8965:     my ($targetdir,$file,$logfile) = @_;
 8966:     my $target = $targetdir.'/'.$file;
 8967:     my ($maxversion,$fn,$extn,$output);
 8968:     $maxversion = 0;
 8969:     if ($file =~ /^(.+)\.(\w+)$/) {
 8970:         $fn=$1;
 8971:         $extn=$2;
 8972:     }
 8973:     opendir(DIR,$targetdir);
 8974:     while (my $filename=readdir(DIR)) {
 8975:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 8976:             $maxversion=($1>$maxversion)?$1:$maxversion;
 8977:         }
 8978:     }
 8979:     $maxversion++;
 8980:     print $logfile "\nCreating old version ".$maxversion."\n";
 8981:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 8982:     if (copy($target,$copyfile)) {
 8983:         print $logfile "Copied old target to ".$copyfile."\n";
 8984:         $copyfile=$copyfile.'.meta';
 8985:         if (copy($target.'.meta',$copyfile)) {
 8986:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 8987:             $output = 'ok';
 8988:         } else {
 8989:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 8990:             $output = &mt('Failed to copy old meta').", $!, ";
 8991:         }
 8992:     } else {
 8993:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 8994:         $output = &mt('Failed to copy old target').", $!, ";
 8995:     }
 8996:     return $output;
 8997: }
 8998: 
 8999: sub write_metadata {
 9000:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 9001:     my (%metadatafields,%metadatakeys,$output);
 9002:     $metadatafields{'title'}=$formname;
 9003:     $metadatafields{'creationdate'}=time;
 9004:     $metadatafields{'lastrevisiondate'}=time;
 9005:     $metadatafields{'copyright'}='public';
 9006:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 9007:                                          $env{'user.domain'};
 9008:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 9009:     $metadatafields{'domain'}=$dom;
 9010:     {
 9011:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 9012:         my $mfh;
 9013:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 9014:             foreach (sort(keys(%metadatafields))) {
 9015:                 unless ($_=~/\./) {
 9016:                     my $unikey=$_;
 9017:                     $unikey=~/^([A-Za-z]+)/;
 9018:                     my $tag=$1;
 9019:                     $tag=~tr/A-Z/a-z/;
 9020:                     print $mfh "\n\<$tag";
 9021:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 9022:                         my $value=$metadatafields{$unikey.'.'.$_};
 9023:                         $value=~s/\"/\'\'/g;
 9024:                         print $mfh ' '.$_.'="'.$value.'"';
 9025:                     }
 9026:                     print $mfh '>'.
 9027:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 9028:                             .'</'.$tag.'>';
 9029:                 }
 9030:             }
 9031:             $output = 'ok';
 9032:             print $logfile "\nWrote metadata";
 9033:             close($mfh);
 9034:         } else {
 9035:             print $logfile "\nFailed to open metadata file";
 9036:             $output = &mt('Could not write metadata');
 9037:         }
 9038:     }
 9039:     return $output;
 9040: }
 9041: 
 9042: sub notifysubscribed {
 9043:     foreach my $targetsource (@{$modified_urls}){
 9044:         next unless (ref($targetsource) eq 'ARRAY');
 9045:         my ($target,$source)=@{$targetsource};
 9046:         if ($source ne '') {
 9047:             if (open(my $logfh,'>>'.$source.'.log')) {
 9048:                 print $logfh "\nCleanup phase: Notifications\n";
 9049:                 my @subscribed=&subscribed_hosts($target);
 9050:                 foreach my $subhost (@subscribed) {
 9051:                     print $logfh "\nNotifying host ".$subhost.':';
 9052:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 9053:                     print $logfh $reply;
 9054:                 }
 9055:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 9056:                 foreach my $subhost (@subscribedmeta) {
 9057:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 9058:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 9059:                                                         $subhost);
 9060:                     print $logfh $reply;
 9061:                 }
 9062:                 print $logfh "\n============ Done ============\n";
 9063:                 close($logfh);
 9064:             }
 9065:         }
 9066:     }
 9067:     return OK;
 9068: }
 9069: 
 9070: sub subscribed_hosts {
 9071:     my ($target) = @_;
 9072:     my @subscribed;
 9073:     if (open(my $fh,"<$target.subscription")) {
 9074:         while (my $subline=<$fh>) {
 9075:             if ($subline =~ /^($match_lonid):/) {
 9076:                 my $host = $1;
 9077:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 9078:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 9079:                         push(@subscribed,$host);
 9080:                     }
 9081:                 }
 9082:             }
 9083:         }
 9084:     }
 9085:     return @subscribed;
 9086: }
 9087: 
 9088: sub check_switchserver {
 9089:     my ($dom,$confname) = @_;
 9090:     my ($allowed,$switchserver);
 9091:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 9092:     if ($home eq 'no_host') {
 9093:         $home = &Apache::lonnet::domain($dom,'primary');
 9094:     }
 9095:     my @ids=&Apache::lonnet::current_machine_ids();
 9096:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 9097:     if (!$allowed) {
 9098: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 9099:     }
 9100:     return $switchserver;
 9101: }
 9102: 
 9103: sub modify_quotas {
 9104:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 9105:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 9106:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 9107:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 9108:         $validationfieldsref);
 9109:     if ($action eq 'quotas') {
 9110:         $context = 'tools'; 
 9111:     } else {
 9112:         $context = $action;
 9113:     }
 9114:     if ($context eq 'requestcourses') {
 9115:         @usertools = ('official','unofficial','community','textbook','placement');
 9116:         @options =('norequest','approval','validate','autolimit');
 9117:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 9118:         %titles = &courserequest_titles();
 9119:         $toolregexp = join('|',@usertools);
 9120:         %conditions = &courserequest_conditions();
 9121:         $confname = $dom.'-domainconfig';
 9122:         my $servadm = $r->dir_config('lonAdmEMail');
 9123:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9124:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 9125:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 9126:     } elsif ($context eq 'requestauthor') {
 9127:         @usertools = ('author');
 9128:         %titles = &authorrequest_titles();
 9129:     } else {
 9130:         @usertools = ('aboutme','blog','webdav','portfolio');
 9131:         %titles = &tool_titles();
 9132:     }
 9133:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9134:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9135:     foreach my $key (keys(%env)) {
 9136:         if ($context eq 'requestcourses') {
 9137:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 9138:                 my $item = $1;
 9139:                 my $type = $2;
 9140:                 if ($type =~ /^limit_(.+)/) {
 9141:                     $limithash{$item}{$1} = $env{$key};
 9142:                 } else {
 9143:                     $confhash{$item}{$type} = $env{$key};
 9144:                 }
 9145:             }
 9146:         } elsif ($context eq 'requestauthor') {
 9147:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 9148:                 $confhash{$1} = $env{$key};
 9149:             }
 9150:         } else {
 9151:             if ($key =~ /^form\.quota_(.+)$/) {
 9152:                 $confhash{'defaultquota'}{$1} = $env{$key};
 9153:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 9154:                 $confhash{'authorquota'}{$1} = $env{$key};
 9155:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 9156:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 9157:             }
 9158:         }
 9159:     }
 9160:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 9161:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 9162:         @approvalnotify = sort(@approvalnotify);
 9163:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 9164:         my @crstypes = ('official','unofficial','community','textbook','placement');
 9165:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 9166:         foreach my $type (@hasuniquecode) {
 9167:             if (grep(/^\Q$type\E$/,@crstypes)) {
 9168:                 $confhash{'uniquecode'}{$type} = 1;
 9169:             }
 9170:         }
 9171:         my (%newbook,%allpos);
 9172:         if ($context eq 'requestcourses') {
 9173:             foreach my $type ('textbooks','templates') {
 9174:                 @{$allpos{$type}} = (); 
 9175:                 my $invalid;
 9176:                 if ($type eq 'textbooks') {
 9177:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 9178:                 } else {
 9179:                     $invalid = &mt('Invalid LON-CAPA course for template');
 9180:                 }
 9181:                 if ($env{'form.'.$type.'_addbook'}) {
 9182:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 9183:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 9184:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 9185:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 9186:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 9187:                         } else {
 9188:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 9189:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 9190:                             $position =~ s/\D+//g;
 9191:                             if ($position ne '') {
 9192:                                 $allpos{$type}[$position] = $newbook{$type};
 9193:                             }
 9194:                         }
 9195:                     } else {
 9196:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 9197:                     }
 9198:                 }
 9199:             } 
 9200:         }
 9201:         if (ref($domconfig{$action}) eq 'HASH') {
 9202:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 9203:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 9204:                     $changes{'notify'}{'approval'} = 1;
 9205:                 }
 9206:             } else {
 9207:                 if ($confhash{'notify'}{'approval'}) {
 9208:                     $changes{'notify'}{'approval'} = 1;
 9209:                 }
 9210:             }
 9211:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 9212:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 9213:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 9214:                         unless ($confhash{'uniquecode'}{$crstype}) {
 9215:                             $changes{'uniquecode'} = 1;
 9216:                         }
 9217:                     }
 9218:                     unless ($changes{'uniquecode'}) {
 9219:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 9220:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 9221:                                 $changes{'uniquecode'} = 1;
 9222:                             }
 9223:                         }
 9224:                     }
 9225:                } else {
 9226:                    $changes{'uniquecode'} = 1;
 9227:                }
 9228:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 9229:                 $changes{'uniquecode'} = 1;
 9230:             }
 9231:             if ($context eq 'requestcourses') {
 9232:                 foreach my $type ('textbooks','templates') {
 9233:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 9234:                         my %deletions;
 9235:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 9236:                         if (@todelete) {
 9237:                             map { $deletions{$_} = 1; } @todelete;
 9238:                         }
 9239:                         my %imgdeletions;
 9240:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 9241:                         if (@todeleteimages) {
 9242:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 9243:                         }
 9244:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 9245:                         for (my $i=0; $i<=$maxnum; $i++) {
 9246:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 9247:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 9248:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 9249:                                 if ($deletions{$key}) {
 9250:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 9251:                                         #FIXME need to obsolete item in RES space
 9252:                                     }
 9253:                                     next;
 9254:                                 } else {
 9255:                                     my $newpos = $env{'form.'.$itemid};
 9256:                                     $newpos =~ s/\D+//g;
 9257:                                     foreach my $item ('subject','title','publisher','author') {
 9258:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9259:                                                  ($type eq 'templates'));
 9260:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 9261:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 9262:                                             $changes{$type}{$key} = 1;
 9263:                                         }
 9264:                                     }
 9265:                                     $allpos{$type}[$newpos] = $key;
 9266:                                 }
 9267:                                 if ($imgdeletions{$key}) {
 9268:                                     $changes{$type}{$key} = 1;
 9269:                                     #FIXME need to obsolete item in RES space
 9270:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 9271:                                     my ($cdom,$cnum) = split(/_/,$key);
 9272:                                     my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 9273:                                                                                   $cdom,$cnum,$type,$configuserok,
 9274:                                                                                   $switchserver,$author_ok);
 9275:                                     if ($imgurl) {
 9276:                                         $confhash{$type}{$key}{'image'} = $imgurl;
 9277:                                         $changes{$type}{$key} = 1; 
 9278:                                     }
 9279:                                     if ($error) {
 9280:                                         &Apache::lonnet::logthis($error);
 9281:                                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9282:                                     } 
 9283:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 9284:                                     $confhash{$type}{$key}{'image'} = 
 9285:                                         $domconfig{$action}{$type}{$key}{'image'};
 9286:                                 }
 9287:                             }
 9288:                         }
 9289:                     }
 9290:                 }
 9291:             }
 9292:         } else {
 9293:             if ($confhash{'notify'}{'approval'}) {
 9294:                 $changes{'notify'}{'approval'} = 1;
 9295:             }
 9296:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 9297:                 $changes{'uniquecode'} = 1;
 9298:             }
 9299:         }
 9300:         if ($context eq 'requestcourses') {
 9301:             foreach my $type ('textbooks','templates') {
 9302:                 if ($newbook{$type}) {
 9303:                     $changes{$type}{$newbook{$type}} = 1;
 9304:                     foreach my $item ('subject','title','publisher','author') {
 9305:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9306:                                  ($type eq 'template'));
 9307:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 9308:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 9309:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 9310:                         }
 9311:                     }
 9312:                     if ($type eq 'textbooks') {
 9313:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 9314:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 9315:                             my ($imageurl,$error) =
 9316:                                 &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 9317:                                                         $configuserok,$switchserver,$author_ok);
 9318:                             if ($imageurl) {
 9319:                                 $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 9320:                             }
 9321:                             if ($error) {
 9322:                                 &Apache::lonnet::logthis($error);
 9323:                                 $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9324:                             }
 9325:                         }
 9326:                     }
 9327:                 }
 9328:                 if (@{$allpos{$type}} > 0) {
 9329:                     my $idx = 0;
 9330:                     foreach my $item (@{$allpos{$type}}) {
 9331:                         if ($item ne '') {
 9332:                             $confhash{$type}{$item}{'order'} = $idx;
 9333:                             if (ref($domconfig{$action}) eq 'HASH') {
 9334:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 9335:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 9336:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 9337:                                             $changes{$type}{$item} = 1;
 9338:                                         }
 9339:                                     }
 9340:                                 }
 9341:                             }
 9342:                             $idx ++;
 9343:                         }
 9344:                     }
 9345:                 }
 9346:             }
 9347:             if (ref($validationitemsref) eq 'ARRAY') {
 9348:                 foreach my $item (@{$validationitemsref}) {
 9349:                     if ($item eq 'fields') {
 9350:                         my @changed;
 9351:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 9352:                         if (@{$confhash{'validation'}{$item}} > 0) {
 9353:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 9354:                         }
 9355:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9356:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9357:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 9358:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 9359:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
 9360:                                 } else {
 9361:                                     @changed = @{$confhash{'validation'}{$item}};
 9362:                                 }
 9363:                             } else {
 9364:                                 @changed = @{$confhash{'validation'}{$item}};
 9365:                             }
 9366:                         } else {
 9367:                             @changed = @{$confhash{'validation'}{$item}};
 9368:                         }
 9369:                         if (@changed) {
 9370:                             if ($confhash{'validation'}{$item}) {
 9371:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 9372:                             } else {
 9373:                                 $changes{'validation'}{$item} = &mt('None');
 9374:                             }
 9375:                         }
 9376:                     } else {
 9377:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 9378:                         if ($item eq 'markup') {
 9379:                             if ($env{'form.requestcourses_validation_'.$item}) {
 9380:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 9381:                             }
 9382:                         }
 9383:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9384:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9385:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 9386:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9387:                                 }
 9388:                             } else {
 9389:                                 if ($confhash{'validation'}{$item} ne '') {
 9390:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9391:                                 }
 9392:                             }
 9393:                         } else {
 9394:                             if ($confhash{'validation'}{$item} ne '') {
 9395:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9396:                             }
 9397:                         }
 9398:                     }
 9399:                 }
 9400:             }
 9401:             if ($env{'form.validationdc'}) {
 9402:                 my $newval = $env{'form.validationdc'};
 9403:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 9404:                 if (exists($domcoords{$newval})) {
 9405:                     $confhash{'validation'}{'dc'} = $newval;
 9406:                 }
 9407:             }
 9408:             if (ref($confhash{'validation'}) eq 'HASH') {
 9409:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9410:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9411:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9412:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9413:                                 if ($confhash{'validation'}{'dc'} eq '') {
 9414:                                     $changes{'validation'}{'dc'} = &mt('None');
 9415:                                 } else {
 9416:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9417:                                 }
 9418:                             }
 9419:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
 9420:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9421:                         }
 9422:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 9423:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9424:                     }
 9425:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 9426:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9427:                 }
 9428:             } else {
 9429:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9430:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9431:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9432:                             $changes{'validation'}{'dc'} = &mt('None');
 9433:                         }
 9434:                     }
 9435:                 }
 9436:             }
 9437:         }
 9438:     } else {
 9439:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 9440:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 9441:     }
 9442:     foreach my $item (@usertools) {
 9443:         foreach my $type (@{$types},'default','_LC_adv') {
 9444:             my $unset; 
 9445:             if ($context eq 'requestcourses') {
 9446:                 $unset = '0';
 9447:                 if ($type eq '_LC_adv') {
 9448:                     $unset = '';
 9449:                 }
 9450:                 if ($confhash{$item}{$type} eq 'autolimit') {
 9451:                     $confhash{$item}{$type} .= '=';
 9452:                     unless ($limithash{$item}{$type} =~ /\D/) {
 9453:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 9454:                     }
 9455:                 }
 9456:             } elsif ($context eq 'requestauthor') {
 9457:                 $unset = '0';
 9458:                 if ($type eq '_LC_adv') {
 9459:                     $unset = '';
 9460:                 }
 9461:             } else {
 9462:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 9463:                     $confhash{$item}{$type} = 1;
 9464:                 } else {
 9465:                     $confhash{$item}{$type} = 0;
 9466:                 }
 9467:             }
 9468:             if (ref($domconfig{$action}) eq 'HASH') {
 9469:                 if ($action eq 'requestauthor') {
 9470:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 9471:                         $changes{$type} = 1;
 9472:                     }
 9473:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 9474:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 9475:                         $changes{$item}{$type} = 1;
 9476:                     }
 9477:                 } else {
 9478:                     if ($context eq 'requestcourses') {
 9479:                         if ($confhash{$item}{$type} ne $unset) {
 9480:                             $changes{$item}{$type} = 1;
 9481:                         }
 9482:                     } else {
 9483:                         if (!$confhash{$item}{$type}) {
 9484:                             $changes{$item}{$type} = 1;
 9485:                         }
 9486:                     }
 9487:                 }
 9488:             } else {
 9489:                 if ($context eq 'requestcourses') {
 9490:                     if ($confhash{$item}{$type} ne $unset) {
 9491:                         $changes{$item}{$type} = 1;
 9492:                     }
 9493:                 } elsif ($context eq 'requestauthor') {
 9494:                     if ($confhash{$type} ne $unset) {
 9495:                         $changes{$type} = 1;
 9496:                     }
 9497:                 } else {
 9498:                     if (!$confhash{$item}{$type}) {
 9499:                         $changes{$item}{$type} = 1;
 9500:                     }
 9501:                 }
 9502:             }
 9503:         }
 9504:     }
 9505:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 9506:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 9507:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 9508:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 9509:                     if (exists($confhash{'defaultquota'}{$key})) {
 9510:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 9511:                             $changes{'defaultquota'}{$key} = 1;
 9512:                         }
 9513:                     } else {
 9514:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 9515:                     }
 9516:                 }
 9517:             } else {
 9518:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 9519:                     if (exists($confhash{'defaultquota'}{$key})) {
 9520:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 9521:                             $changes{'defaultquota'}{$key} = 1;
 9522:                         }
 9523:                     } else {
 9524:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 9525:                     }
 9526:                 }
 9527:             }
 9528:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9529:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 9530:                     if (exists($confhash{'authorquota'}{$key})) {
 9531:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 9532:                             $changes{'authorquota'}{$key} = 1;
 9533:                         }
 9534:                     } else {
 9535:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 9536:                     }
 9537:                 }
 9538:             }
 9539:         }
 9540:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 9541:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 9542:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9543:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 9544:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 9545:                             $changes{'defaultquota'}{$key} = 1;
 9546:                         }
 9547:                     } else {
 9548:                         if (!exists($domconfig{'quotas'}{$key})) {
 9549:                             $changes{'defaultquota'}{$key} = 1;
 9550:                         }
 9551:                     }
 9552:                 } else {
 9553:                     $changes{'defaultquota'}{$key} = 1;
 9554:                 }
 9555:             }
 9556:         }
 9557:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 9558:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 9559:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9560:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9561:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 9562:                             $changes{'authorquota'}{$key} = 1;
 9563:                         }
 9564:                     } else {
 9565:                         $changes{'authorquota'}{$key} = 1;
 9566:                     }
 9567:                 } else {
 9568:                     $changes{'authorquota'}{$key} = 1;
 9569:                 }
 9570:             }
 9571:         }
 9572:     }
 9573: 
 9574:     if ($context eq 'requestauthor') {
 9575:         $domdefaults{'requestauthor'} = \%confhash;
 9576:     } else {
 9577:         foreach my $key (keys(%confhash)) {
 9578:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 9579:                 $domdefaults{$key} = $confhash{$key};
 9580:             }
 9581:         }
 9582:     }
 9583: 
 9584:     my %quotahash = (
 9585:                       $action => { %confhash }
 9586:                     );
 9587:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 9588:                                              $dom);
 9589:     if ($putresult eq 'ok') {
 9590:         if (keys(%changes) > 0) {
 9591:             my $cachetime = 24*60*60;
 9592:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9593:             if (ref($lastactref) eq 'HASH') {
 9594:                 $lastactref->{'domdefaults'} = 1;
 9595:             }
 9596:             $resulttext = &mt('Changes made:').'<ul>';
 9597:             unless (($context eq 'requestcourses') ||
 9598:                     ($context eq 'requestauthor')) {
 9599:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 9600:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 9601:                     foreach my $type (@{$types},'default') {
 9602:                         if (defined($changes{'defaultquota'}{$type})) {
 9603:                             my $typetitle = $usertypes->{$type};
 9604:                             if ($type eq 'default') {
 9605:                                 $typetitle = $othertitle;
 9606:                             }
 9607:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 9608:                         }
 9609:                     }
 9610:                     $resulttext .= '</ul></li>';
 9611:                 }
 9612:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 9613:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 9614:                     foreach my $type (@{$types},'default') {
 9615:                         if (defined($changes{'authorquota'}{$type})) {
 9616:                             my $typetitle = $usertypes->{$type};
 9617:                             if ($type eq 'default') {
 9618:                                 $typetitle = $othertitle;
 9619:                             }
 9620:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 9621:                         }
 9622:                     }
 9623:                     $resulttext .= '</ul></li>';
 9624:                 }
 9625:             }
 9626:             my %newenv;
 9627:             foreach my $item (@usertools) {
 9628:                 my (%haschgs,%inconf);
 9629:                 if ($context eq 'requestauthor') {
 9630:                     %haschgs = %changes;
 9631:                     %inconf = %confhash;
 9632:                 } else {
 9633:                     if (ref($changes{$item}) eq 'HASH') {
 9634:                         %haschgs = %{$changes{$item}};
 9635:                     }
 9636:                     if (ref($confhash{$item}) eq 'HASH') {
 9637:                         %inconf = %{$confhash{$item}};
 9638:                     }
 9639:                 }
 9640:                 if (keys(%haschgs) > 0) {
 9641:                     my $newacc = 
 9642:                         &Apache::lonnet::usertools_access($env{'user.name'},
 9643:                                                           $env{'user.domain'},
 9644:                                                           $item,'reload',$context);
 9645:                     if (($context eq 'requestcourses') ||
 9646:                         ($context eq 'requestauthor')) {
 9647:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 9648:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 9649:                         }
 9650:                     } else {
 9651:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 9652:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 9653:                         }
 9654:                     }
 9655:                     unless ($context eq 'requestauthor') {
 9656:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 9657:                     }
 9658:                     foreach my $type (@{$types},'default','_LC_adv') {
 9659:                         if ($haschgs{$type}) {
 9660:                             my $typetitle = $usertypes->{$type};
 9661:                             if ($type eq 'default') {
 9662:                                 $typetitle = $othertitle;
 9663:                             } elsif ($type eq '_LC_adv') {
 9664:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 9665:                             }
 9666:                             if ($inconf{$type}) {
 9667:                                 if ($context eq 'requestcourses') {
 9668:                                     my $cond;
 9669:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 9670:                                         if ($1 eq '') {
 9671:                                             $cond = &mt('(Automatic processing of any request).');
 9672:                                         } else {
 9673:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 9674:                                         }
 9675:                                     } else { 
 9676:                                         $cond = $conditions{$inconf{$type}};
 9677:                                     }
 9678:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 9679:                                 } elsif ($context eq 'requestauthor') {
 9680:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 9681:                                                              $titles{$inconf{$type}},$typetitle);
 9682: 
 9683:                                 } else {
 9684:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 9685:                                 }
 9686:                             } else {
 9687:                                 if ($type eq '_LC_adv') {
 9688:                                     if ($inconf{$type} eq '0') {
 9689:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9690:                                     } else { 
 9691:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 9692:                                     }
 9693:                                 } else {
 9694:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9695:                                 }
 9696:                             }
 9697:                         }
 9698:                     }
 9699:                     unless ($context eq 'requestauthor') {
 9700:                         $resulttext .= '</ul></li>';
 9701:                     }
 9702:                 }
 9703:             }
 9704:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 9705:                 if (ref($changes{'notify'}) eq 'HASH') {
 9706:                     if ($changes{'notify'}{'approval'}) {
 9707:                         if (ref($confhash{'notify'}) eq 'HASH') {
 9708:                             if ($confhash{'notify'}{'approval'}) {
 9709:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 9710:                             } else {
 9711:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 9712:                             }
 9713:                         }
 9714:                     }
 9715:                 }
 9716:             }
 9717:             if ($action eq 'requestcourses') {
 9718:                 my @offon = ('off','on');
 9719:                 if ($changes{'uniquecode'}) {
 9720:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 9721:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 9722:                         $resulttext .= '<li>'.
 9723:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 9724:                                        '</li>';
 9725:                     } else {
 9726:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 9727:                                        '</li>';
 9728:                     }
 9729:                 }
 9730:                 foreach my $type ('textbooks','templates') {
 9731:                     if (ref($changes{$type}) eq 'HASH') {
 9732:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 9733:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 9734:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 9735:                             my $coursetitle = $coursehash{'description'};
 9736:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 9737:                             $resulttext .= '<li>';
 9738:                             foreach my $item ('subject','title','publisher','author') {
 9739:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9740:                                          ($type eq 'templates'));
 9741:                                 my $name = $item.':';
 9742:                                 $name =~ s/^(\w)/\U$1/;
 9743:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 9744:                             }
 9745:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 9746:                             if ($type eq 'textbooks') {
 9747:                                 if ($confhash{$type}{$key}{'image'}) {
 9748:                                     $resulttext .= ' '.&mt('Image: [_1]',
 9749:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 9750:                                                    ' alt="Textbook cover" />').'<br />';
 9751:                                 }
 9752:                             }
 9753:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 9754:                         }
 9755:                         $resulttext .= '</ul></li>';
 9756:                     }
 9757:                 }
 9758:                 if (ref($changes{'validation'}) eq 'HASH') {
 9759:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 9760:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 9761:                         foreach my $item (@{$validationitemsref}) {
 9762:                             if (exists($changes{'validation'}{$item})) {
 9763:                                 if ($item eq 'markup') {
 9764:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9765:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 9766:                                 } else {
 9767:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9768:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 9769:                                 }
 9770:                             }
 9771:                         }
 9772:                         if (exists($changes{'validation'}{'dc'})) {
 9773:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 9774:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 9775:                         }
 9776:                     }
 9777:                 }
 9778:             }
 9779:             $resulttext .= '</ul>';
 9780:             if (keys(%newenv)) {
 9781:                 &Apache::lonnet::appenv(\%newenv);
 9782:             }
 9783:         } else {
 9784:             if ($context eq 'requestcourses') {
 9785:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 9786:             } elsif ($context eq 'requestauthor') {
 9787:                 $resulttext = &mt('No changes made to rights to request author space.');
 9788:             } else {
 9789:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 9790:             }
 9791:         }
 9792:     } else {
 9793:         $resulttext = '<span class="LC_error">'.
 9794: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9795:     }
 9796:     if ($errors) {
 9797:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 9798:                        '<ul>'.$errors.'</ul></p>';
 9799:     }
 9800:     return $resulttext;
 9801: }
 9802: 
 9803: sub process_textbook_image {
 9804:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 9805:     my $filename = $env{'form.'.$caller.'.filename'};
 9806:     my ($error,$url);
 9807:     my ($width,$height) = (50,50);
 9808:     if ($configuserok eq 'ok') {
 9809:         if ($switchserver) {
 9810:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 9811:                          $switchserver);
 9812:         } elsif ($author_ok eq 'ok') {
 9813:             my ($result,$imageurl) =
 9814:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 9815:                              "$type/$dom/$cnum/cover",$width,$height);
 9816:             if ($result eq 'ok') {
 9817:                 $url = $imageurl;
 9818:             } else {
 9819:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 9820:             }
 9821:         } else {
 9822:             $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);
 9823:         }
 9824:     } else {
 9825:         $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);
 9826:     }
 9827:     return ($url,$error);
 9828: }
 9829: 
 9830: sub modify_ltitools {
 9831:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 9832:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9833:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
 9834:     my $confname = $dom.'-domainconfig';
 9835:     my $servadm = $r->dir_config('lonAdmEMail');
 9836:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9837:     my (%posslti,%possfield);
 9838:     my @courseroles = ('cc','in','ta','ep','st');
 9839:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 9840:     map { $posslti{$_} = 1; } @ltiroles;
 9841:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
 9842:     map { $possfield{$_} = 1; } @allfields;
 9843:     my %lt = &ltitools_names(); 
 9844:     if ($env{'form.ltitools_add'}) {
 9845:         my $title = $env{'form.ltitools_add_title'};
 9846:         $title =~ s/(`)/'/g;
 9847:         ($newid,my $error) = &get_ltitools_id($dom,$title);
 9848:         if ($newid) {
 9849:             my $position = $env{'form.ltitools_add_pos'};
 9850:             $position =~ s/\D+//g;
 9851:             if ($position ne '') {
 9852:                 $allpos[$position] = $newid;
 9853:             }
 9854:             $changes{$newid} = 1;
 9855:             foreach my $item ('title','url','key','secret') {
 9856:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
 9857:                 if ($env{'form.ltitools_add_'.$item}) {
 9858:                     if (($item eq 'key') || ($item eq 'secret')) {
 9859:                         $encconfig{$newid}{$item} = $env{'form.ltitools_add_'.$item};
 9860:                     } else {
 9861:                         $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
 9862:                     }
 9863:                 }
 9864:             }
 9865:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
 9866:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
 9867:             }
 9868:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
 9869:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
 9870:             }
 9871:             foreach my $item ('width','height','linktext','explanation') {
 9872:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
 9873:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
 9874:                 if (($item eq 'width') || ($item eq 'height')) {
 9875:                     if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
 9876:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
 9877:                     }
 9878:                 } else {
 9879:                     if ($env{'form.ltitools_add_'.$item} ne '') {
 9880:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item}; 
 9881:                     }
 9882:                 }
 9883:             }
 9884:             if ($env{'form.ltitools_add_target'} eq 'window') {
 9885:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
 9886:             } elsif ($env{'form.ltitools_add_target'} eq 'tab') {
 9887:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
 9888:             } else {
 9889:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
 9890:             }
 9891:             foreach my $item ('passback','roster') {
 9892:                 if ($env{'form.ltitools_add_'.$item}) {
 9893:                     $confhash{$newid}{$item} = 1;
 9894:                 }
 9895:             }
 9896:             if ($env{'form.ltitools_add_image.filename'} ne '') {
 9897:                 my ($imageurl,$error) =
 9898:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$newid,
 9899:                                             $configuserok,$switchserver,$author_ok);
 9900:                 if ($imageurl) {
 9901:                     $confhash{$newid}{'image'} = $imageurl;
 9902:                 }
 9903:                 if ($error) {
 9904:                     &Apache::lonnet::logthis($error);
 9905:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9906:                 }
 9907:             }
 9908:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
 9909:             foreach my $field (@fields) {
 9910:                 if ($possfield{$field}) {
 9911:                     if ($field eq 'roles') {
 9912:                         foreach my $role (@courseroles) {
 9913:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
 9914:                             if (($choice ne '') && ($posslti{$choice})) {
 9915:                                 $confhash{$newid}{'roles'}{$role} = $choice;
 9916:                                 if ($role eq 'cc') {
 9917:                                     $confhash{$newid}{'roles'}{'co'} = $choice; 
 9918:                                 }
 9919:                             }
 9920:                         }
 9921:                     } else {
 9922:                         $confhash{$newid}{'fields'}{$field} = 1;
 9923:                     }
 9924:                 }
 9925:             }
 9926:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
 9927:             foreach my $item (@courseconfig) {
 9928:                 $confhash{$newid}{'crsconf'}{$item} = 1;
 9929:             }
 9930:             if ($env{'form.ltitools_add_custom'}) {
 9931:                 my $name = $env{'form.ltitools_add_custom_name'};
 9932:                 my $value = $env{'form.ltitools_add_custom_value'};
 9933:                 $value =~ s/(`)/'/g;
 9934:                 $name =~ s/(`)/'/g;
 9935:                 $confhash{$newid}{'custom'}{$name} = $value;
 9936:             }
 9937:         } else {
 9938:             my $error = &mt('Failed to acquire unique ID for new external tool');   
 9939:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9940:         }
 9941:     }
 9942:     if (ref($domconfig{$action}) eq 'HASH') {
 9943:         my %deletions;
 9944:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
 9945:         if (@todelete) {
 9946:             map { $deletions{$_} = 1; } @todelete;
 9947:         }
 9948:         my %customadds;
 9949:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
 9950:         if (@newcustom) {
 9951:             map { $customadds{$_} = 1; } @newcustom;
 9952:         } 
 9953:         my %imgdeletions;
 9954:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
 9955:         if (@todeleteimages) {
 9956:             map { $imgdeletions{$_} = 1; } @todeleteimages;
 9957:         }
 9958:         my $maxnum = $env{'form.ltitools_maxnum'};
 9959:         for (my $i=0; $i<=$maxnum; $i++) {
 9960:             my $itemid = $env{'form.ltitools_id_'.$i};
 9961:             $itemid =~ s/\D+//g;
 9962:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
 9963:                 if ($deletions{$itemid}) {
 9964:                     if ($domconfig{$action}{$itemid}{'image'}) {
 9965:                         #FIXME need to obsolete item in RES space
 9966:                     }
 9967:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
 9968:                     next;
 9969:                 } else {
 9970:                     my $newpos = $env{'form.ltitools_'.$itemid};
 9971:                     $newpos =~ s/\D+//g;
 9972:                     foreach my $item ('title','url') {
 9973:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
 9974:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
 9975:                             $changes{$itemid} = 1;
 9976:                         }
 9977:                     }
 9978:                     foreach my $item ('key','secret') {
 9979:                         $encconfig{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
 9980:                         if ($domconfig{$action}{$itemid}{$item} ne $encconfig{$itemid}{$item}) {
 9981:                             $changes{$itemid} = 1;
 9982:                         }
 9983:                     }
 9984:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
 9985:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
 9986:                     }
 9987:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
 9988:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
 9989:                     }
 9990:                     foreach my $size ('width','height') {
 9991:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
 9992:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
 9993:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
 9994:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
 9995:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9996:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
 9997:                                     $changes{$itemid} = 1;
 9998:                                 }
 9999:                             } else {
10000:                                 $changes{$itemid} = 1;
10001:                             }
10002:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10003:                             if ($domconfig{$action}{$itemid}{'display'}{$size} ne '') {
10004:                                 $changes{$itemid} = 1;
10005:                             }
10006:                         }
10007:                     }
10008:                     foreach my $item ('linktext','explanation') {
10009:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/^\s+//;
10010:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/\s+$//;
10011:                         if ($env{'form.ltitools_'.$item.'_'.$i} ne '') {
10012:                             $confhash{$itemid}{'display'}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
10013:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10014:                                 if ($domconfig{$action}{$itemid}{'display'}{$item} ne $confhash{$itemid}{'display'}{$item}) {
10015:                                     $changes{$itemid} = 1;
10016:                                 }
10017:                             } else {
10018:                                 $changes{$itemid} = 1;
10019:                             }
10020:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10021:                             if ($domconfig{$action}{$itemid}{'display'}{$item} ne '') {
10022:                                 $changes{$itemid} = 1;
10023:                             }
10024:                         }
10025:                     }
10026:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
10027:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
10028:                     } elsif ($env{'form.ltitools_target_'.$i} eq 'tab') {
10029:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
10030:                     } else {
10031:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
10032:                     }
10033:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10034:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
10035:                             $changes{$itemid} = 1;
10036:                         }
10037:                     } else {
10038:                         $changes{$itemid} = 1;
10039:                     }
10040:                     foreach my $extra ('passback','roster') {
10041:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
10042:                             $confhash{$itemid}{$extra} = 1;
10043:                         }
10044:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
10045:                             $changes{$itemid} = 1;
10046:                         }
10047:                     }
10048:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
10049:                     foreach my $item ('label','title','target','linktext','explanation') {
10050:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
10051:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
10052:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
10053:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
10054:                                     $changes{$itemid} = 1;
10055:                                 }
10056:                             } else {
10057:                                 $changes{$itemid} = 1;
10058:                             }
10059:                         }
10060:                     }
10061:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
10062:                     foreach my $field (@fields) {
10063:                         if ($possfield{$field}) {
10064:                             if ($field eq 'roles') {
10065:                                 foreach my $role (@courseroles) {
10066:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
10067:                                     if (($choice ne '') && ($posslti{$choice})) {
10068:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
10069:                                         if ($role eq 'cc') {
10070:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
10071:                                         }
10072:                                     }
10073:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
10074:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
10075:                                             $changes{$itemid} = 1;
10076:                                         }
10077:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
10078:                                         $changes{$itemid} = 1;
10079:                                     }
10080:                                 }
10081:                             } else {
10082:                                 $confhash{$itemid}{'fields'}{$field} = 1;
10083:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
10084:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
10085:                                         $changes{$itemid} = 1;
10086:                                     }
10087:                                 } else {
10088:                                     $changes{$itemid} = 1;
10089:                                 }
10090:                             }
10091:                         }
10092:                     }
10093:                     $allpos[$newpos] = $itemid;
10094:                 }
10095:                 if ($imgdeletions{$itemid}) {
10096:                     $changes{$itemid} = 1;
10097:                     #FIXME need to obsolete item in RES space
10098:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
10099:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
10100:                                                                  $itemid,$configuserok,$switchserver,
10101:                                                                  $author_ok);
10102:                     if ($imgurl) {
10103:                         $confhash{$itemid}{'image'} = $imgurl;
10104:                         $changes{$itemid} = 1;
10105:                     }
10106:                     if ($error) {
10107:                         &Apache::lonnet::logthis($error);
10108:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10109:                     }
10110:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
10111:                     $confhash{$itemid}{'image'} =
10112:                        $domconfig{$action}{$itemid}{'image'};
10113:                 }
10114:                 if ($customadds{$i}) {
10115:                     my $name = $env{'form.ltitools_custom_name_'.$i};
10116:                     $name =~ s/(`)/'/g;
10117:                     $name =~ s/^\s+//;
10118:                     $name =~ s/\s+$//;
10119:                     my $value = $env{'form.ltitools_custom_value_'.$i};
10120:                     $value =~ s/(`)/'/g;
10121:                     $value =~ s/^\s+//;
10122:                     $value =~ s/\s+$//;
10123:                     if ($name ne '') {
10124:                         $confhash{$itemid}{'custom'}{$name} = $value;
10125:                         $changes{$itemid} = 1;
10126:                     }
10127:                 }
10128:                 my %customdels;
10129:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i); 
10130:                 if (@customdeletions) {
10131:                     $changes{$itemid} = 1;
10132:                 }
10133:                 map { $customdels{$_} = 1; } @customdeletions;
10134:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
10135:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
10136:                         unless ($customdels{$key}) {
10137:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
10138:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i}; 
10139:                             }
10140:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
10141:                                 $changes{$itemid} = 1;
10142:                             }
10143:                         }
10144:                     }
10145:                 }
10146:                 unless ($changes{$itemid}) {
10147:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
10148:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
10149:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
10150:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
10151:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
10152:                                         $changes{$itemid} = 1;
10153:                                         last;
10154:                                     }
10155:                                 }
10156:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
10157:                                 $changes{$itemid} = 1;
10158:                             }
10159:                         }
10160:                         last if ($changes{$itemid});
10161:                     }
10162:                 }
10163:             }
10164:         }
10165:     }
10166:     if (@allpos > 0) {
10167:         my $idx = 0;
10168:         foreach my $itemid (@allpos) {
10169:             if ($itemid ne '') {
10170:                 $confhash{$itemid}{'order'} = $idx;
10171:                 if (ref($domconfig{$action}) eq 'HASH') {
10172:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
10173:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
10174:                             $changes{$itemid} = 1;
10175:                         }
10176:                     }
10177:                 }
10178:                 $idx ++;
10179:             }
10180:         }
10181:     }
10182:     my %ltitoolshash = (
10183:                           $action => { %confhash }
10184:                        );
10185:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
10186:                                              $dom);
10187:     if ($putresult eq 'ok') {
10188:         my %ltienchash = (
10189:                              $action => { %encconfig }
10190:                          );
10191:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
10192:         if (keys(%changes) > 0) {
10193:             my $cachetime = 24*60*60;
10194:             my %ltiall = %confhash;
10195:             foreach my $id (keys(%ltiall)) {
10196:                 if (ref($encconfig{$id}) eq 'HASH') {
10197:                     foreach my $item ('key','secret') {
10198:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
10199:                     }
10200:                 }
10201:             }
10202:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%ltiall,$cachetime);
10203:             if (ref($lastactref) eq 'HASH') {
10204:                 $lastactref->{'ltitools'} = 1;
10205:             }
10206:             $resulttext = &mt('Changes made:').'<ul>';
10207:             my %bynum;
10208:             foreach my $itemid (sort(keys(%changes))) {
10209:                 my $position = $confhash{$itemid}{'order'};
10210:                 $bynum{$position} = $itemid;
10211:             }
10212:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
10213:                 my $itemid = $bynum{$pos}; 
10214:                 if (ref($confhash{$itemid}) ne 'HASH') {
10215:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
10216:                 } else {
10217:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
10218:                     if ($confhash{$itemid}{'image'}) {
10219:                         $resulttext .= '&nbsp;'.
10220:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
10221:                                        ' alt="'.&mt('Tool Provider icon').'" />';
10222:                     }
10223:                     $resulttext .= '</li><ul>';
10224:                     my $position = $pos + 1;
10225:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
10226:                     foreach my $item ('version','msgtype','url') {
10227:                         if ($confhash{$itemid}{$item} ne '') {
10228:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
10229:                         }
10230:                     }
10231:                     if ($encconfig{$itemid}{'key'} ne '') {
10232:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
10233:                     }
10234:                     if ($encconfig{$itemid}{'secret'} ne '') {
10235:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
10236:                         my $num = length($encconfig{$itemid}{'secret'});
10237:                         $resulttext .= ('*'x$num).'</li>';
10238:                     }
10239:                     $resulttext .= '<li>'.&mt('Configurable in course:');
10240:                     my @possconfig = ('label','title','target','linktext','explanation');
10241:                     my $numconfig = 0; 
10242:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') { 
10243:                         foreach my $item (@possconfig) {
10244:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
10245:                                 $numconfig ++;
10246:                                 $resulttext .= ' "'.$lt{'crs'.$item}.'"';
10247:                             }
10248:                         }
10249:                     }
10250:                     if (!$numconfig) {
10251:                         $resulttext .= &mt('None');
10252:                     }
10253:                     $resulttext .= '</li>';
10254:                     foreach my $item ('passback','roster') {
10255:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
10256:                         if ($confhash{$itemid}{$item}) {
10257:                             $resulttext .= &mt('Yes');
10258:                         } else {
10259:                             $resulttext .= &mt('No');
10260:                         }
10261:                         $resulttext .= '</li>';
10262:                     }
10263:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
10264:                         my $displaylist;
10265:                         if ($confhash{$itemid}{'display'}{'target'}) {
10266:                             $displaylist = &mt('Display target').':&nbsp;'.
10267:                                            $confhash{$itemid}{'display'}{'target'}.',';
10268:                         }
10269:                         foreach my $size ('width','height') { 
10270:                             if ($confhash{$itemid}{'display'}{$size}) {
10271:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
10272:                                                 $confhash{$itemid}{'display'}{$size}.',';
10273:                             }
10274:                         }
10275:                         if ($displaylist) {
10276:                             $displaylist =~ s/,$//;
10277:                             $resulttext .= '<li>'.$displaylist.'</li>';
10278:                         }
10279:                         foreach my $item ('linktext','explanation') {
10280:                             if ($confhash{$itemid}{'display'}{$item}) {
10281:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{'display'}{$item}.'</li>';
10282:                             }
10283:                         }
10284:                     }
10285:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
10286:                         my $fieldlist;
10287:                         foreach my $field (@allfields) {
10288:                             if ($confhash{$itemid}{'fields'}{$field}) {
10289:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
10290:                             }
10291:                         }
10292:                         if ($fieldlist) {
10293:                             $fieldlist =~ s/,$//;
10294:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
10295:                         }
10296:                     }
10297:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
10298:                         my $rolemaps;
10299:                         foreach my $role (@courseroles) {
10300:                             if ($confhash{$itemid}{'roles'}{$role}) {
10301:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
10302:                                              $confhash{$itemid}{'roles'}{$role}.',';
10303:                             }
10304:                         }
10305:                         if ($rolemaps) {
10306:                             $rolemaps =~ s/,$//; 
10307:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
10308:                         }
10309:                     }
10310:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
10311:                         my $customlist;
10312:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
10313:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
10314:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
10315:                             } 
10316:                         }
10317:                         if ($customlist) {
10318:                             $resulttext .= '<li>'.&mt('Custom items').':'.$customlist.'</li>';
10319:                         }
10320:                     } 
10321:                     $resulttext .= '</ul></li>';
10322:                 }
10323:             }
10324:             $resulttext .= '</ul>';
10325:         } else {
10326:             $resulttext = &mt('No changes made.');
10327:         }
10328:     } else {
10329:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
10330:     }
10331:     if ($errors) {
10332:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
10333:                        $errors.'</ul>';
10334:     }
10335:     return $resulttext;
10336: }
10337: 
10338: sub process_ltitools_image {
10339:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
10340:     my $filename = $env{'form.'.$caller.'.filename'};
10341:     my ($error,$url);
10342:     my ($width,$height) = (21,21);
10343:     if ($configuserok eq 'ok') {
10344:         if ($switchserver) {
10345:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
10346:                          $switchserver);
10347:         } elsif ($author_ok eq 'ok') {
10348:             my ($result,$imageurl,$madethumb) =
10349:                 &publishlogo($r,'upload',$caller,$dom,$confname,
10350:                              "ltitools/$itemid/icon",$width,$height);
10351:             if ($result eq 'ok') {
10352:                 if ($madethumb) {
10353:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
10354:                     my $imagethumb = "$path/tn-".$imagefile;
10355:                     $url = $imagethumb;
10356:                 } else {
10357:                     $url = $imageurl;
10358:                 }
10359:             } else {
10360:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
10361:             }
10362:         } else {
10363:             $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);
10364:         }
10365:     } else {
10366:         $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);
10367:     }
10368:     return ($url,$error);
10369: }
10370: 
10371: sub get_ltitools_id {
10372:     my ($cdom,$title) = @_;
10373:     # get lock on ltitools db
10374:     my $lockhash = {
10375:                       lock => $env{'user.name'}.
10376:                               ':'.$env{'user.domain'},
10377:                    };
10378:     my $tries = 0;
10379:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
10380:     my ($id,$error);
10381:  
10382:     while (($gotlock ne 'ok') && ($tries<10)) {
10383:         $tries ++;
10384:         sleep (0.1);
10385:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
10386:     }
10387:     if ($gotlock eq 'ok') {
10388:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
10389:         if ($currids{'lock'}) {
10390:             delete($currids{'lock'});
10391:             if (keys(%currids)) {
10392:                 my @curr = sort { $a <=> $b } keys(%currids);
10393:                 if ($curr[-1] =~ /^\d+$/) {
10394:                     $id = 1 + $curr[-1];
10395:                 }
10396:             } else {
10397:                 $id = 1;
10398:             }
10399:             if ($id) {
10400:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
10401:                     $error = 'nostore';
10402:                 }
10403:             } else {
10404:                 $error = 'nonumber';
10405:             }
10406:         }
10407:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
10408:     } else {
10409:         $error = 'nolock';
10410:     }
10411:     return ($id,$error);
10412: }
10413: 
10414: sub modify_autoenroll {
10415:     my ($dom,$lastactref,%domconfig) = @_;
10416:     my ($resulttext,%changes);
10417:     my %currautoenroll;
10418:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
10419:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
10420:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
10421:         }
10422:     }
10423:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
10424:     my %title = ( run => 'Auto-enrollment active',
10425:                   sender => 'Sender for notification messages',
10426:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
10427:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
10428:     my @offon = ('off','on');
10429:     my $sender_uname = $env{'form.sender_uname'};
10430:     my $sender_domain = $env{'form.sender_domain'};
10431:     if ($sender_domain eq '') {
10432:         $sender_uname = '';
10433:     } elsif ($sender_uname eq '') {
10434:         $sender_domain = '';
10435:     }
10436:     my $coowners = $env{'form.autoassign_coowners'};
10437:     my $failsafe = $env{'form.autoenroll_failsafe'};
10438:     $failsafe =~ s{^\s+|\s+$}{}g;
10439:     if ($failsafe =~ /\D/) {
10440:         undef($failsafe);
10441:     }
10442:     my %autoenrollhash =  (
10443:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
10444:                                        'sender_uname' => $sender_uname,
10445:                                        'sender_domain' => $sender_domain,
10446:                                        'co-owners' => $coowners,
10447:                                        'autofailsafe' => $failsafe,
10448:                                 }
10449:                      );
10450:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
10451:                                              $dom);
10452:     if ($putresult eq 'ok') {
10453:         if (exists($currautoenroll{'run'})) {
10454:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
10455:                  $changes{'run'} = 1;
10456:              }
10457:         } elsif ($autorun) {
10458:             if ($env{'form.autoenroll_run'} ne '1') {
10459:                  $changes{'run'} = 1;
10460:             }
10461:         }
10462:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
10463:             $changes{'sender'} = 1;
10464:         }
10465:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
10466:             $changes{'sender'} = 1;
10467:         }
10468:         if ($currautoenroll{'co-owners'} ne '') {
10469:             if ($currautoenroll{'co-owners'} ne $coowners) {
10470:                 $changes{'coowners'} = 1;
10471:             }
10472:         } elsif ($coowners) {
10473:             $changes{'coowners'} = 1;
10474:         }
10475:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
10476:             $changes{'autofailsafe'} = 1;
10477:         }
10478:         if (keys(%changes) > 0) {
10479:             $resulttext = &mt('Changes made:').'<ul>';
10480:             if ($changes{'run'}) {
10481:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
10482:             }
10483:             if ($changes{'sender'}) {
10484:                 if ($sender_uname eq '' || $sender_domain eq '') {
10485:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
10486:                 } else {
10487:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
10488:                 }
10489:             }
10490:             if ($changes{'coowners'}) {
10491:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
10492:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
10493:                 if (ref($lastactref) eq 'HASH') {
10494:                     $lastactref->{'domainconfig'} = 1;
10495:                 }
10496:             }
10497:             if ($changes{'autofailsafe'}) {
10498:                 if ($failsafe ne '') {
10499:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
10500:                 } else {
10501:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
10502:                 }
10503:                 &Apache::lonnet::get_domain_defaults($dom,1);
10504:                 if (ref($lastactref) eq 'HASH') {
10505:                     $lastactref->{'domdefaults'} = 1;
10506:                 }
10507:             }
10508:             $resulttext .= '</ul>';
10509:         } else {
10510:             $resulttext = &mt('No changes made to auto-enrollment settings');
10511:         }
10512:     } else {
10513:         $resulttext = '<span class="LC_error">'.
10514: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10515:     }
10516:     return $resulttext;
10517: }
10518: 
10519: sub modify_autoupdate {
10520:     my ($dom,%domconfig) = @_;
10521:     my ($resulttext,%currautoupdate,%fields,%changes);
10522:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
10523:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
10524:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
10525:         }
10526:     }
10527:     my @offon = ('off','on');
10528:     my %title = &Apache::lonlocal::texthash (
10529:                    run => 'Auto-update:',
10530:                    classlists => 'Updates to user information in classlists?'
10531:                 );
10532:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10533:     my %fieldtitles = &Apache::lonlocal::texthash (
10534:                         id => 'Student/Employee ID',
10535:                         permanentemail => 'E-mail address',
10536:                         lastname => 'Last Name',
10537:                         firstname => 'First Name',
10538:                         middlename => 'Middle Name',
10539:                         generation => 'Generation',
10540:                       );
10541:     $othertitle = &mt('All users');
10542:     if (keys(%{$usertypes}) >  0) {
10543:         $othertitle = &mt('Other users');
10544:     }
10545:     foreach my $key (keys(%env)) {
10546:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
10547:             my ($usertype,$item) = ($1,$2);
10548:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
10549:                 if ($usertype eq 'default') {   
10550:                     push(@{$fields{$1}},$2);
10551:                 } elsif (ref($types) eq 'ARRAY') {
10552:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
10553:                         push(@{$fields{$1}},$2);
10554:                     }
10555:                 }
10556:             }
10557:         }
10558:     }
10559:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
10560:     @lockablenames = sort(@lockablenames);
10561:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
10562:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
10563:         if (@changed) {
10564:             $changes{'lockablenames'} = 1;
10565:         }
10566:     } else {
10567:         if (@lockablenames) {
10568:             $changes{'lockablenames'} = 1;
10569:         }
10570:     }
10571:     my %updatehash = (
10572:                       autoupdate => { run => $env{'form.autoupdate_run'},
10573:                                       classlists => $env{'form.classlists'},
10574:                                       fields => {%fields},
10575:                                       lockablenames => \@lockablenames,
10576:                                     }
10577:                      );
10578:     foreach my $key (keys(%currautoupdate)) {
10579:         if (($key eq 'run') || ($key eq 'classlists')) {
10580:             if (exists($updatehash{autoupdate}{$key})) {
10581:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
10582:                     $changes{$key} = 1;
10583:                 }
10584:             }
10585:         } elsif ($key eq 'fields') {
10586:             if (ref($currautoupdate{$key}) eq 'HASH') {
10587:                 foreach my $item (@{$types},'default') {
10588:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
10589:                         my $change = 0;
10590:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
10591:                             if (!exists($fields{$item})) {
10592:                                 $change = 1;
10593:                                 last;
10594:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
10595:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
10596:                                     $change = 1;
10597:                                     last;
10598:                                 }
10599:                             }
10600:                         }
10601:                         if ($change) {
10602:                             push(@{$changes{$key}},$item);
10603:                         }
10604:                     } 
10605:                 }
10606:             }
10607:         } elsif ($key eq 'lockablenames') {
10608:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
10609:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
10610:                 if (@changed) {
10611:                     $changes{'lockablenames'} = 1;
10612:                 }
10613:             } else {
10614:                 if (@lockablenames) {
10615:                     $changes{'lockablenames'} = 1;
10616:                 }
10617:             }
10618:         }
10619:     }
10620:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
10621:         if (@lockablenames) {
10622:             $changes{'lockablenames'} = 1;
10623:         }
10624:     }
10625:     foreach my $item (@{$types},'default') {
10626:         if (defined($fields{$item})) {
10627:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
10628:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
10629:                     my $change = 0;
10630:                     if (ref($fields{$item}) eq 'ARRAY') {
10631:                         foreach my $type (@{$fields{$item}}) {
10632:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
10633:                                 $change = 1;
10634:                                 last;
10635:                             }
10636:                         }
10637:                     }
10638:                     if ($change) {
10639:                         push(@{$changes{'fields'}},$item);
10640:                     }
10641:                 } else {
10642:                     push(@{$changes{'fields'}},$item);
10643:                 }
10644:             } else {
10645:                 push(@{$changes{'fields'}},$item);
10646:             }
10647:         }
10648:     }
10649:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
10650:                                              $dom);
10651:     if ($putresult eq 'ok') {
10652:         if (keys(%changes) > 0) {
10653:             $resulttext = &mt('Changes made:').'<ul>';
10654:             foreach my $key (sort(keys(%changes))) {
10655:                 if ($key eq 'lockablenames') {
10656:                     $resulttext .= '<li>';
10657:                     if (@lockablenames) {
10658:                         $usertypes->{'default'} = $othertitle;
10659:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
10660:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
10661:                     } else {
10662:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
10663:                     }
10664:                     $resulttext .= '</li>';
10665:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
10666:                     foreach my $item (@{$changes{$key}}) {
10667:                         my @newvalues;
10668:                         foreach my $type (@{$fields{$item}}) {
10669:                             push(@newvalues,$fieldtitles{$type});
10670:                         }
10671:                         my $newvaluestr;
10672:                         if (@newvalues > 0) {
10673:                             $newvaluestr = join(', ',@newvalues);
10674:                         } else {
10675:                             $newvaluestr = &mt('none');
10676:                         }
10677:                         if ($item eq 'default') {
10678:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
10679:                         } else {
10680:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
10681:                         }
10682:                     }
10683:                 } else {
10684:                     my $newvalue;
10685:                     if ($key eq 'run') {
10686:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
10687:                     } else {
10688:                         $newvalue = $offon[$env{'form.'.$key}];
10689:                     }
10690:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
10691:                 }
10692:             }
10693:             $resulttext .= '</ul>';
10694:         } else {
10695:             $resulttext = &mt('No changes made to autoupdates');
10696:         }
10697:     } else {
10698:         $resulttext = '<span class="LC_error">'.
10699: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10700:     }
10701:     return $resulttext;
10702: }
10703: 
10704: sub modify_autocreate {
10705:     my ($dom,%domconfig) = @_;
10706:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
10707:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
10708:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
10709:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
10710:         }
10711:     }
10712:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
10713:                  req => 'Auto-creation of validated requests for official courses',
10714:                  xmldc => 'Identity of course creator of courses from XML files',
10715:                );
10716:     my @types = ('xml','req');
10717:     foreach my $item (@types) {
10718:         $newvals{$item} = $env{'form.autocreate_'.$item};
10719:         $newvals{$item} =~ s/\D//g;
10720:         $newvals{$item} = 0 if ($newvals{$item} eq '');
10721:     }
10722:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
10723:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
10724:     unless (exists($domcoords{$newvals{'xmldc'}})) {
10725:         $newvals{'xmldc'} = '';
10726:     } 
10727:     %autocreatehash =  (
10728:                         autocreate => { xml => $newvals{'xml'},
10729:                                         req => $newvals{'req'},
10730:                                       }
10731:                        );
10732:     if ($newvals{'xmldc'} ne '') {
10733:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
10734:     }
10735:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
10736:                                              $dom);
10737:     if ($putresult eq 'ok') {
10738:         my @items = @types;
10739:         if ($newvals{'xml'}) {
10740:             push(@items,'xmldc');
10741:         }
10742:         foreach my $item (@items) {
10743:             if (exists($currautocreate{$item})) {
10744:                 if ($currautocreate{$item} ne $newvals{$item}) {
10745:                     $changes{$item} = 1;
10746:                 }
10747:             } elsif ($newvals{$item}) {
10748:                 $changes{$item} = 1;
10749:             }
10750:         }
10751:         if (keys(%changes) > 0) {
10752:             my @offon = ('off','on'); 
10753:             $resulttext = &mt('Changes made:').'<ul>';
10754:             foreach my $item (@types) {
10755:                 if ($changes{$item}) {
10756:                     my $newtxt = $offon[$newvals{$item}];
10757:                     $resulttext .= '<li>'.
10758:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
10759:                                        '<b>','</b>').
10760:                                    '</li>';
10761:                 }
10762:             }
10763:             if ($changes{'xmldc'}) {
10764:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
10765:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
10766:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
10767:             }
10768:             $resulttext .= '</ul>';
10769:         } else {
10770:             $resulttext = &mt('No changes made to auto-creation settings');
10771:         }
10772:     } else {
10773:         $resulttext = '<span class="LC_error">'.
10774:             &mt('An error occurred: [_1]',$putresult).'</span>';
10775:     }
10776:     return $resulttext;
10777: }
10778: 
10779: sub modify_directorysrch {
10780:     my ($dom,$lastactref,%domconfig) = @_;
10781:     my ($resulttext,%changes);
10782:     my %currdirsrch;
10783:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
10784:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
10785:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
10786:         }
10787:     }
10788:     my %title = ( available => 'Institutional directory search available',
10789:                   localonly => 'Other domains can search institution',
10790:                   lcavailable => 'LON-CAPA directory search available',
10791:                   lclocalonly => 'Other domains can search LON-CAPA domain',
10792:                   searchby => 'Search types',
10793:                   searchtypes => 'Search latitude');
10794:     my @offon = ('off','on');
10795:     my @otherdoms = ('Yes','No');
10796: 
10797:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
10798:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
10799:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
10800: 
10801:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10802:     if (keys(%{$usertypes}) == 0) {
10803:         @cansearch = ('default');
10804:     } else {
10805:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
10806:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
10807:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
10808:                     push(@{$changes{'cansearch'}},$type);
10809:                 }
10810:             }
10811:             foreach my $type (@cansearch) {
10812:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
10813:                     push(@{$changes{'cansearch'}},$type);
10814:                 }
10815:             }
10816:         } else {
10817:             push(@{$changes{'cansearch'}},@cansearch);
10818:         }
10819:     }
10820: 
10821:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
10822:         foreach my $by (@{$currdirsrch{'searchby'}}) {
10823:             if (!grep(/^\Q$by\E$/,@searchby)) {
10824:                 push(@{$changes{'searchby'}},$by);
10825:             }
10826:         }
10827:         foreach my $by (@searchby) {
10828:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
10829:                 push(@{$changes{'searchby'}},$by);
10830:             }
10831:         }
10832:     } else {
10833:         push(@{$changes{'searchby'}},@searchby);
10834:     }
10835: 
10836:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
10837:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
10838:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
10839:                 push(@{$changes{'searchtypes'}},$type);
10840:             }
10841:         }
10842:         foreach my $type (@searchtypes) {
10843:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
10844:                 push(@{$changes{'searchtypes'}},$type);
10845:             }
10846:         }
10847:     } else {
10848:         if (exists($currdirsrch{'searchtypes'})) {
10849:             foreach my $type (@searchtypes) {  
10850:                 if ($type ne $currdirsrch{'searchtypes'}) { 
10851:                     push(@{$changes{'searchtypes'}},$type);
10852:                 }
10853:             }
10854:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
10855:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
10856:             }   
10857:         } else {
10858:             push(@{$changes{'searchtypes'}},@searchtypes); 
10859:         }
10860:     }
10861: 
10862:     my %dirsrch_hash =  (
10863:             directorysrch => { available => $env{'form.dirsrch_available'},
10864:                                cansearch => \@cansearch,
10865:                                localonly => $env{'form.dirsrch_instlocalonly'},
10866:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
10867:                                lcavailable => $env{'form.dirsrch_domavailable'},
10868:                                searchby => \@searchby,
10869:                                searchtypes => \@searchtypes,
10870:                              }
10871:             );
10872:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
10873:                                              $dom);
10874:     if ($putresult eq 'ok') {
10875:         if (exists($currdirsrch{'available'})) {
10876:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
10877:                  $changes{'available'} = 1;
10878:              }
10879:         } else {
10880:             if ($env{'form.dirsrch_available'} eq '1') {
10881:                 $changes{'available'} = 1;
10882:             }
10883:         }
10884:         if (exists($currdirsrch{'lcavailable'})) {
10885:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
10886:                 $changes{'lcavailable'} = 1;
10887:             }
10888:         } else {
10889:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
10890:                 $changes{'lcavailable'} = 1;
10891:             }
10892:         }
10893:         if (exists($currdirsrch{'localonly'})) {
10894:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
10895:                 $changes{'localonly'} = 1;
10896:             }
10897:         } else {
10898:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
10899:                 $changes{'localonly'} = 1;
10900:             }
10901:         }
10902:         if (exists($currdirsrch{'lclocalonly'})) {
10903:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
10904:                 $changes{'lclocalonly'} = 1;
10905:             }
10906:         } else {
10907:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
10908:                 $changes{'lclocalonly'} = 1;
10909:             }
10910:         }
10911:         if (keys(%changes) > 0) {
10912:             $resulttext = &mt('Changes made:').'<ul>';
10913:             if ($changes{'available'}) {
10914:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
10915:             }
10916:             if ($changes{'lcavailable'}) {
10917:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
10918:             }
10919:             if ($changes{'localonly'}) {
10920:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
10921:             }
10922:             if ($changes{'lclocalonly'}) {
10923:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
10924:             }
10925:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
10926:                 my $chgtext;
10927:                 if (ref($usertypes) eq 'HASH') {
10928:                     if (keys(%{$usertypes}) > 0) {
10929:                         foreach my $type (@{$types}) {
10930:                             if (grep(/^\Q$type\E$/,@cansearch)) {
10931:                                 $chgtext .= $usertypes->{$type}.'; ';
10932:                             }
10933:                         }
10934:                         if (grep(/^default$/,@cansearch)) {
10935:                             $chgtext .= $othertitle;
10936:                         } else {
10937:                             $chgtext =~ s/\; $//;
10938:                         }
10939:                         $resulttext .=
10940:                             '<li>'.
10941:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
10942:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
10943:                             '</li>';
10944:                     }
10945:                 }
10946:             }
10947:             if (ref($changes{'searchby'}) eq 'ARRAY') {
10948:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
10949:                 my $chgtext;
10950:                 foreach my $type (@{$titleorder}) {
10951:                     if (grep(/^\Q$type\E$/,@searchby)) {
10952:                         if (defined($searchtitles->{$type})) {
10953:                             $chgtext .= $searchtitles->{$type}.'; ';
10954:                         }
10955:                     }
10956:                 }
10957:                 $chgtext =~ s/\; $//;
10958:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
10959:             }
10960:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
10961:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
10962:                 my $chgtext;
10963:                 foreach my $type (@{$srchtypeorder}) {
10964:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
10965:                         if (defined($srchtypes_desc->{$type})) {
10966:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
10967:                         }
10968:                     }
10969:                 }
10970:                 $chgtext =~ s/\; $//;
10971:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
10972:             }
10973:             $resulttext .= '</ul>';
10974:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
10975:             if (ref($lastactref) eq 'HASH') {
10976:                 $lastactref->{'directorysrch'} = 1;
10977:             }
10978:         } else {
10979:             $resulttext = &mt('No changes made to directory search settings');
10980:         }
10981:     } else {
10982:         $resulttext = '<span class="LC_error">'.
10983:                       &mt('An error occurred: [_1]',$putresult).'</span>';
10984:     }
10985:     return $resulttext;
10986: }
10987: 
10988: sub modify_contacts {
10989:     my ($dom,$lastactref,%domconfig) = @_;
10990:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
10991:     if (ref($domconfig{'contacts'}) eq 'HASH') {
10992:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
10993:             $currsetting{$key} = $domconfig{'contacts'}{$key};
10994:         }
10995:     }
10996:     my (%others,%to,%bcc,%includestr,%includeloc);
10997:     my @contacts = ('supportemail','adminemail');
10998:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
10999:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail');
11000:     my @toggles = ('reporterrors','reportupdates');
11001:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
11002:     foreach my $type (@mailings) {
11003:         @{$newsetting{$type}} = 
11004:             &Apache::loncommon::get_env_multiple('form.'.$type);
11005:         foreach my $item (@contacts) {
11006:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
11007:                 $contacts_hash{contacts}{$type}{$item} = 1;
11008:             } else {
11009:                 $contacts_hash{contacts}{$type}{$item} = 0;
11010:             }
11011:         }
11012:         $others{$type} = $env{'form.'.$type.'_others'};
11013:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
11014:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11015:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
11016:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
11017:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
11018:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
11019:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
11020:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
11021:             }
11022:         }
11023:     }
11024:     foreach my $item (@contacts) {
11025:         $to{$item} = $env{'form.'.$item};
11026:         $contacts_hash{'contacts'}{$item} = $to{$item};
11027:     }
11028:     foreach my $item (@toggles) {
11029:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
11030:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
11031:         }
11032:     }
11033:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
11034:         foreach my $field (@{$fields}) {
11035:             if (ref($possoptions->{$field}) eq 'ARRAY') {
11036:                 my $value = $env{'form.helpform_'.$field};
11037:                 $value =~ s/^\s+|\s+$//g;
11038:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
11039:                     $contacts_hash{contacts}{'helpform'}{$field} = $value;
11040:                     if ($field eq 'screenshot') {
11041:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
11042:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
11043:                             $contacts_hash{contacts}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
11044:                         }
11045:                     }
11046:                 }
11047:             }
11048:         }
11049:     }
11050:     if (keys(%currsetting) > 0) {
11051:         foreach my $item (@contacts) {
11052:             if ($to{$item} ne $currsetting{$item}) {
11053:                 $changes{$item} = 1;
11054:             }
11055:         }
11056:         foreach my $type (@mailings) {
11057:             foreach my $item (@contacts) {
11058:                 if (ref($currsetting{$type}) eq 'HASH') {
11059:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
11060:                         push(@{$changes{$type}},$item);
11061:                     }
11062:                 } else {
11063:                     push(@{$changes{$type}},@{$newsetting{$type}});
11064:                 }
11065:             }
11066:             if ($others{$type} ne $currsetting{$type}{'others'}) {
11067:                 push(@{$changes{$type}},'others');
11068:             }
11069:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11070:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
11071:                     push(@{$changes{$type}},'bcc'); 
11072:                 }
11073:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
11074:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
11075:                     push(@{$changes{$type}},'include');
11076:                 }
11077:             }
11078:         }
11079:         if (ref($fields) eq 'ARRAY') {
11080:             if (ref($currsetting{'helpform'}) eq 'HASH') {
11081:                 foreach my $field (@{$fields}) {
11082:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
11083:                         push(@{$changes{'helpform'}},$field);
11084:                     }
11085:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
11086:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
11087:                             push(@{$changes{'helpform'}},'maxsize');
11088:                         }
11089:                     }
11090:                 }
11091:             } else {
11092:                 foreach my $field (@{$fields}) {
11093:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
11094:                         push(@{$changes{'helpform'}},$field);
11095:                     }
11096:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
11097:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
11098:                             push(@{$changes{'helpform'}},'maxsize');
11099:                         }
11100:                     }
11101:                 }
11102:             }
11103:         }
11104:     } else {
11105:         my %default;
11106:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
11107:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
11108:         $default{'errormail'} = 'adminemail';
11109:         $default{'packagesmail'} = 'adminemail';
11110:         $default{'helpdeskmail'} = 'supportemail';
11111:         $default{'otherdomsmail'} = 'supportemail';
11112:         $default{'lonstatusmail'} = 'adminemail';
11113:         $default{'requestsmail'} = 'adminemail';
11114:         $default{'updatesmail'} = 'adminemail';
11115:         foreach my $item (@contacts) {
11116:            if ($to{$item} ne $default{$item}) {
11117:                $changes{$item} = 1;
11118:            }
11119:         }
11120:         foreach my $type (@mailings) {
11121:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
11122:                 push(@{$changes{$type}},@{$newsetting{$type}});
11123:             }
11124:             if ($others{$type} ne '') {
11125:                 push(@{$changes{$type}},'others');
11126:             }
11127:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11128:                 if ($bcc{$type} ne '') {
11129:                     push(@{$changes{$type}},'bcc');
11130:                 }
11131:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
11132:                     push(@{$changes{$type}},'include');
11133:                 }
11134:             }
11135:         }
11136:         if (ref($fields) eq 'ARRAY') {
11137:             foreach my $field (@{$fields}) {
11138:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
11139:                     push(@{$changes{'helpform'}},$field);
11140:                 }
11141:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
11142:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
11143:                         push(@{$changes{'helpform'}},'maxsize');
11144:                     }
11145:                 }
11146:             }
11147:         }
11148:     }
11149:     foreach my $item (@toggles) {
11150:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
11151:             $changes{$item} = 1;
11152:         } elsif ((!$env{'form.'.$item}) &&
11153:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
11154:             $changes{$item} = 1;
11155:         }
11156:     }
11157:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
11158:                                              $dom);
11159:     if ($putresult eq 'ok') {
11160:         if (keys(%changes) > 0) {
11161:             &Apache::loncommon::devalidate_domconfig_cache($dom);
11162:             if (ref($lastactref) eq 'HASH') {
11163:                 $lastactref->{'domainconfig'} = 1;
11164:             }
11165:             my ($titles,$short_titles)  = &contact_titles();
11166:             $resulttext = &mt('Changes made:').'<ul>';
11167:             foreach my $item (@contacts) {
11168:                 if ($changes{$item}) {
11169:                     $resulttext .= '<li>'.$titles->{$item}.
11170:                                     &mt(' set to: ').
11171:                                     '<span class="LC_cusr_emph">'.
11172:                                     $to{$item}.'</span></li>';
11173:                 }
11174:             }
11175:             foreach my $type (@mailings) {
11176:                 if (ref($changes{$type}) eq 'ARRAY') {
11177:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11178:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
11179:                     } else {
11180:                         $resulttext .= '<li>'.$titles->{$type}.': ';
11181:                     }
11182:                     my @text;
11183:                     foreach my $item (@{$newsetting{$type}}) {
11184:                         push(@text,$short_titles->{$item});
11185:                     }
11186:                     if ($others{$type} ne '') {
11187:                         push(@text,$others{$type});
11188:                     }
11189:                     if (@text) {
11190:                         $resulttext .= '<span class="LC_cusr_emph">'.
11191:                                        join(', ',@text).'</span>';
11192:                     }
11193:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11194:                         if ($bcc{$type} ne '') {
11195:                             my $bcctext;
11196:                             if (@text) {
11197:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
11198:                             } else {
11199:                                 $bcctext = '(Bcc)';
11200:                             }
11201:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
11202:                         } elsif (!@text) {
11203:                             $resulttext .= &mt('No one');
11204:                         }   
11205:                         if ($includestr{$type} ne '') {
11206:                             if ($includeloc{$type} eq 'b') {
11207:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
11208:                             } elsif ($includeloc{$type} eq 's') {
11209:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
11210:                             }
11211:                         }
11212:                     } elsif (!@text) {
11213:                         $resulttext .= &mt('No recipients');
11214:                     }
11215:                     $resulttext .= '</li>';
11216:                 }
11217:             }
11218:             my @offon = ('off','on');
11219:             if ($changes{'reporterrors'}) {
11220:                 $resulttext .= '<li>'.
11221:                                &mt('E-mail error reports to [_1] set to "'.
11222:                                    $offon[$env{'form.reporterrors'}].'".',
11223:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
11224:                                        &mt('LON-CAPA core group - MSU'),600,500)).
11225:                                '</li>';
11226:             }
11227:             if ($changes{'reportupdates'}) {
11228:                 $resulttext .= '<li>'.
11229:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
11230:                                     $offon[$env{'form.reportupdates'}].'".',
11231:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
11232:                                         &mt('LON-CAPA core group - MSU'),600,500)).
11233:                                 '</li>';
11234:             }
11235:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
11236:                 my (@optional,@required,@unused,$maxsizechg);
11237:                 foreach my $field (@{$changes{'helpform'}}) {
11238:                     if ($field eq 'maxsize') {
11239:                         $maxsizechg = 1;
11240:                         next;
11241:                     }
11242:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
11243:                         push(@optional,$field);
11244:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
11245:                         push(@unused,$field);
11246:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
11247:                         push(@required,$field);
11248:                     }
11249:                 }
11250:                 if (@optional) {
11251:                     $resulttext .= '<li>'.
11252:                                    &mt('Help form fields changed to "Optional": [_1].',
11253:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
11254:                                    '</li>';
11255:                 }
11256:                 if (@required) {
11257:                     $resulttext .= '<li>'.
11258:                                    &mt('Help form fields changed to "Required": [_1].',
11259:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
11260:                                    '</li>';
11261:                 }
11262:                 if (@unused) {
11263:                     $resulttext .= '<li>'.
11264:                                    &mt('Help form fields changed to "Not shown": [_1].',
11265:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
11266:                                    '</li>';
11267:                 }
11268:                 if ($maxsizechg) {
11269:                     $resulttext .= '<li>'.
11270:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
11271:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
11272:                                    '</li>';
11273: 
11274:                 }
11275:             }
11276:             $resulttext .= '</ul>';
11277:         } else {
11278:             $resulttext = &mt('No changes made to contacts and form settings');
11279:         }
11280:     } else {
11281:         $resulttext = '<span class="LC_error">'.
11282:             &mt('An error occurred: [_1].',$putresult).'</span>';
11283:     }
11284:     return $resulttext;
11285: }
11286: 
11287: sub modify_usercreation {
11288:     my ($dom,%domconfig) = @_;
11289:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
11290:     my $warningmsg;
11291:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
11292:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
11293:             if ($key eq 'cancreate') {
11294:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
11295:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
11296:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
11297:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11298:                         } else {
11299:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11300:                         }
11301:                     }
11302:                 }
11303:             } elsif ($key eq 'email_rule') {
11304:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
11305:             } else {
11306:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
11307:             }
11308:         }
11309:     }
11310:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
11311:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
11312:     my @contexts = ('author','course','requestcrs');
11313:     foreach my $item(@contexts) {
11314:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
11315:     }
11316:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11317:         foreach my $item (@contexts) {
11318:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
11319:                 push(@{$changes{'cancreate'}},$item);
11320:             }
11321:         }
11322:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
11323:         foreach my $item (@contexts) {
11324:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
11325:                 if ($cancreate{$item} ne 'any') {
11326:                     push(@{$changes{'cancreate'}},$item);
11327:                 }
11328:             } else {
11329:                 if ($cancreate{$item} ne 'none') {
11330:                     push(@{$changes{'cancreate'}},$item);
11331:                 }
11332:             }
11333:         }
11334:     } else {
11335:         foreach my $item (@contexts)  {
11336:             push(@{$changes{'cancreate'}},$item);
11337:         }
11338:     }
11339: 
11340:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
11341:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
11342:             if (!grep(/^\Q$type\E$/,@username_rule)) {
11343:                 push(@{$changes{'username_rule'}},$type);
11344:             }
11345:         }
11346:         foreach my $type (@username_rule) {
11347:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
11348:                 push(@{$changes{'username_rule'}},$type);
11349:             }
11350:         }
11351:     } else {
11352:         push(@{$changes{'username_rule'}},@username_rule);
11353:     }
11354: 
11355:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
11356:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
11357:             if (!grep(/^\Q$type\E$/,@id_rule)) {
11358:                 push(@{$changes{'id_rule'}},$type);
11359:             }
11360:         }
11361:         foreach my $type (@id_rule) {
11362:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
11363:                 push(@{$changes{'id_rule'}},$type);
11364:             }
11365:         }
11366:     } else {
11367:         push(@{$changes{'id_rule'}},@id_rule);
11368:     }
11369: 
11370:     my @authen_contexts = ('author','course','domain');
11371:     my @authtypes = ('int','krb4','krb5','loc');
11372:     my %authhash;
11373:     foreach my $item (@authen_contexts) {
11374:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
11375:         foreach my $auth (@authtypes) {
11376:             if (grep(/^\Q$auth\E$/,@authallowed)) {
11377:                 $authhash{$item}{$auth} = 1;
11378:             } else {
11379:                 $authhash{$item}{$auth} = 0;
11380:             }
11381:         }
11382:     }
11383:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
11384:         foreach my $item (@authen_contexts) {
11385:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
11386:                 foreach my $auth (@authtypes) {
11387:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
11388:                         push(@{$changes{'authtypes'}},$item);
11389:                         last;
11390:                     }
11391:                 }
11392:             }
11393:         }
11394:     } else {
11395:         foreach my $item (@authen_contexts) {
11396:             push(@{$changes{'authtypes'}},$item);
11397:         }
11398:     }
11399: 
11400:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
11401:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
11402:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
11403:     $save_usercreate{'id_rule'} = \@id_rule;
11404:     $save_usercreate{'username_rule'} = \@username_rule,
11405:     $save_usercreate{'authtypes'} = \%authhash;
11406: 
11407:     my %usercreation_hash =  (
11408:         usercreation     => \%save_usercreate,
11409:     );
11410: 
11411:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
11412:                                              $dom);
11413: 
11414:     if ($putresult eq 'ok') {
11415:         if (keys(%changes) > 0) {
11416:             $resulttext = &mt('Changes made:').'<ul>';
11417:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
11418:                 my %lt = &usercreation_types();
11419:                 foreach my $type (@{$changes{'cancreate'}}) {
11420:                     my $chgtext = $lt{$type}.', ';
11421:                     if ($cancreate{$type} eq 'none') {
11422:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
11423:                     } elsif ($cancreate{$type} eq 'any') {
11424:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
11425:                     } elsif ($cancreate{$type} eq 'official') {
11426:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
11427:                     } elsif ($cancreate{$type} eq 'unofficial') {
11428:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
11429:                     }
11430:                     $resulttext .= '<li>'.$chgtext.'</li>';
11431:                 }
11432:             }
11433:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
11434:                 my ($rules,$ruleorder) = 
11435:                     &Apache::lonnet::inst_userrules($dom,'username');
11436:                 my $chgtext = '<ul>';
11437:                 foreach my $type (@username_rule) {
11438:                     if (ref($rules->{$type}) eq 'HASH') {
11439:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
11440:                     }
11441:                 }
11442:                 $chgtext .= '</ul>';
11443:                 if (@username_rule > 0) {
11444:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
11445:                 } else {
11446:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
11447:                 }
11448:             }
11449:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
11450:                 my ($idrules,$idruleorder) = 
11451:                     &Apache::lonnet::inst_userrules($dom,'id');
11452:                 my $chgtext = '<ul>';
11453:                 foreach my $type (@id_rule) {
11454:                     if (ref($idrules->{$type}) eq 'HASH') {
11455:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
11456:                     }
11457:                 }
11458:                 $chgtext .= '</ul>';
11459:                 if (@id_rule > 0) {
11460:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
11461:                 } else {
11462:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
11463:                 }
11464:             }
11465:             my %authname = &authtype_names();
11466:             my %context_title = &context_names();
11467:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
11468:                 my $chgtext = '<ul>';
11469:                 foreach my $type (@{$changes{'authtypes'}}) {
11470:                     my @allowed;
11471:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
11472:                     foreach my $auth (@authtypes) {
11473:                         if ($authhash{$type}{$auth}) {
11474:                             push(@allowed,$authname{$auth});
11475:                         }
11476:                     }
11477:                     if (@allowed > 0) {
11478:                         $chgtext .= join(', ',@allowed).'</li>';
11479:                     } else {
11480:                         $chgtext .= &mt('none').'</li>';
11481:                     }
11482:                 }
11483:                 $chgtext .= '</ul>';
11484:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
11485:                 $resulttext .= '</li>';
11486:             }
11487:             $resulttext .= '</ul>';
11488:         } else {
11489:             $resulttext = &mt('No changes made to user creation settings');
11490:         }
11491:     } else {
11492:         $resulttext = '<span class="LC_error">'.
11493:             &mt('An error occurred: [_1]',$putresult).'</span>';
11494:     }
11495:     if ($warningmsg ne '') {
11496:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
11497:     }
11498:     return $resulttext;
11499: }
11500: 
11501: sub modify_selfcreation {
11502:     my ($dom,$lastactref,%domconfig) = @_;
11503:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
11504:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
11505:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11506:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
11507:     if (ref($typesref) eq 'ARRAY') {
11508:         @types = @{$typesref};
11509:     }
11510:     if (ref($usertypesref) eq 'HASH') {
11511:         %usertypes = %{$usertypesref};
11512:     }
11513:     $usertypes{'default'} = $othertitle;
11514: #
11515: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
11516: #
11517:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
11518:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
11519:             if ($key eq 'cancreate') {
11520:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
11521:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
11522:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
11523:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
11524:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
11525:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
11526:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
11527:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
11528:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11529:                         } else {
11530:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11531:                         }
11532:                     }
11533:                 }
11534:             } elsif ($key eq 'email_rule') {
11535:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
11536:             } else {
11537:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
11538:             }
11539:         }
11540:     }
11541: #
11542: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
11543: #
11544:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
11545:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
11546:             if ($key eq 'selfcreate') {
11547:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
11548:             } else {
11549:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
11550:             }
11551:         }
11552:     }
11553: #
11554: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
11555: #
11556:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
11557:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
11558:             if ($key eq 'inststatusguest') {
11559:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
11560:             } else {
11561:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
11562:             }
11563:         }
11564:     }
11565: 
11566:     my @contexts = ('selfcreate');
11567:     @{$cancreate{'selfcreate'}} = ();
11568:     %{$cancreate{'emailusername'}} = ();
11569:     if (@types) {
11570:         @{$cancreate{'statustocreate'}} = ();
11571:     }
11572:     %{$cancreate{'selfcreateprocessing'}} = ();
11573:     %{$cancreate{'shibenv'}} = ();
11574:     %{$cancreate{'emailverified'}} = ();
11575:     %{$cancreate{'emailoptions'}} = ();
11576:     %{$cancreate{'emaildomain'}} = ();
11577:     my %selfcreatetypes = (
11578:                              sso   => 'users authenticated by institutional single sign on',
11579:                              login => 'users authenticated by institutional log-in',
11580:                              email => 'users verified by e-mail',
11581:                           );
11582: #
11583: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
11584: # is permitted.
11585: #
11586:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
11587: 
11588:     my (@statuses,%email_rule);
11589:     foreach my $item ('login','sso','email') {
11590:         if ($item eq 'email') {
11591:             if ($env{'form.cancreate_email'}) {
11592:                 if (@types) {
11593:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
11594:                     foreach my $status (@poss_statuses) {
11595:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
11596:                             push(@statuses,$status);
11597:                         }
11598:                     }
11599:                     $save_inststatus{'inststatusguest'} = \@statuses;
11600:                 } else {
11601:                     push(@statuses,'default');
11602:                 }
11603:                 if (@statuses) {
11604:                     my %curr_rule;
11605:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
11606:                         foreach my $type (@statuses) {
11607:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
11608:                         }
11609:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
11610:                         foreach my $type (@statuses) {
11611:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
11612:                         }
11613:                     }
11614:                     push(@{$cancreate{'selfcreate'}},'email');
11615:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
11616:                     my %curremaildom;
11617:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
11618:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
11619:                     }
11620:                     foreach my $type (@statuses) {
11621:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
11622:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
11623:                         }
11624:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
11625:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
11626:                         }
11627:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
11628: #
11629: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
11630: #
11631:                             my $chosen = $1;
11632:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
11633:                                 my $emaildom;
11634:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
11635:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type}; 
11636:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
11637:                                     if (ref($curremaildom{$type}) eq 'HASH') {
11638:                                         if (exists($curremaildom{$type}{$chosen})) {
11639:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
11640:                                                 push(@{$changes{'cancreate'}},'emaildomain');
11641:                                             }
11642:                                         } elsif ($emaildom ne '') {
11643:                                             push(@{$changes{'cancreate'}},'emaildomain');
11644:                                         }
11645:                                     } elsif ($emaildom ne '') {
11646:                                         push(@{$changes{'cancreate'}},'emaildomain');
11647:                                     } 
11648:                                 }
11649:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
11650:                             } elsif ($chosen eq 'custom') {
11651:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
11652:                                 $email_rule{$type} = [];
11653:                                 if (ref($emailrules) eq 'HASH') {
11654:                                     foreach my $rule (@possemail_rules) {
11655:                                         if (exists($emailrules->{$rule})) {
11656:                                             push(@{$email_rule{$type}},$rule);
11657:                                         }
11658:                                     }
11659:                                 }
11660:                                 if (@{$email_rule{$type}}) {
11661:                                     $cancreate{'emailoptions'}{$type} = 'custom';
11662:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
11663:                                         if (@{$curr_rule{$type}} > 0) {
11664:                                             foreach my $rule (@{$curr_rule{$type}}) {
11665:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
11666:                                                     push(@{$changes{'email_rule'}},$type);
11667:                                                 }
11668:                                             }
11669:                                         }
11670:                                         foreach my $type (@{$email_rule{$type}}) {
11671:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
11672:                                                 push(@{$changes{'email_rule'}},$type);
11673:                                             }
11674:                                         }
11675:                                     } else {
11676:                                         push(@{$changes{'email_rule'}},$type);
11677:                                     }
11678:                                 }
11679:                             } else {
11680:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
11681:                             }
11682:                         }
11683:                     }
11684:                     if (@types) {
11685:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
11686:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
11687:                             if (@changed) {
11688:                                 push(@{$changes{'inststatus'}},'inststatusguest');
11689:                             }
11690:                         } else {
11691:                             push(@{$changes{'inststatus'}},'inststatusguest');
11692:                         }
11693:                     }
11694:                 } else {
11695:                     delete($env{'form.cancreate_email'});
11696:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
11697:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
11698:                             push(@{$changes{'inststatus'}},'inststatusguest');
11699:                         }
11700:                     }
11701:                 }
11702:             } else {
11703:                 $save_inststatus{'inststatusguest'} = [];
11704:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
11705:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
11706:                         push(@{$changes{'inststatus'}},'inststatusguest');
11707:                     }
11708:                 }
11709:             }
11710:         } else {
11711:             if ($env{'form.cancreate_'.$item}) {
11712:                 push(@{$cancreate{'selfcreate'}},$item);
11713:             }
11714:         }
11715:     }
11716:     my (%userinfo,%savecaptcha);
11717:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
11718: #
11719: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
11720: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
11721: #
11722: 
11723:     if ($env{'form.cancreate_email'}) {
11724:         push(@contexts,'emailusername');
11725:         if (@statuses) {
11726:             foreach my $type (@statuses) {
11727:                 if (ref($infofields) eq 'ARRAY') {
11728:                     foreach my $field (@{$infofields}) {
11729:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
11730:                             $cancreate{'emailusername'}{$type}{$field} = $1;
11731:                         }
11732:                     }
11733:                 }
11734:             }
11735:         }
11736: #
11737: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
11738: # queued requests for self-creation of account verified by e-mail.
11739: #
11740: 
11741:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
11742:         @approvalnotify = sort(@approvalnotify);
11743:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
11744:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11745:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
11746:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
11747:                     push(@{$changes{'cancreate'}},'notify');
11748:                 }
11749:             } else {
11750:                 if ($cancreate{'notify'}{'approval'}) {
11751:                     push(@{$changes{'cancreate'}},'notify');
11752:                 }
11753:             }
11754:         } elsif ($cancreate{'notify'}{'approval'}) {
11755:             push(@{$changes{'cancreate'}},'notify');
11756:         }
11757: 
11758:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
11759:     }
11760: #  
11761: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
11762: # institutional log-in.
11763: #
11764:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
11765:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
11766:                ($domdefaults{'auth_def'} eq 'localauth'))) {
11767:             $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.').' '.
11768:                           &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.');
11769:         }
11770:     }
11771:     my @fields = ('lastname','firstname','middlename','generation',
11772:                   'permanentemail','id');
11773:     my @shibfields = (@fields,'inststatus');
11774:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11775: #
11776: # Where usernames may created for institutional log-in and/or institutional single sign on:
11777: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
11778: # may self-create accounts 
11779: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
11780: # which the user may supply, if institutional data is unavailable.
11781: #
11782:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
11783:         if (@types) {
11784:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
11785:             push(@contexts,'statustocreate');
11786:             foreach my $type (@types) {
11787:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
11788:                 foreach my $field (@fields) {
11789:                     if (grep(/^\Q$field\E$/,@modifiable)) {
11790:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
11791:                     } else {
11792:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
11793:                     }
11794:                 }
11795:             }
11796:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
11797:                 foreach my $type (@types) {
11798:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
11799:                         foreach my $field (@fields) {
11800:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
11801:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
11802:                                 push(@{$changes{'selfcreate'}},$type);
11803:                                 last;
11804:                             }
11805:                         }
11806:                     }
11807:                 }
11808:             } else {
11809:                 foreach my $type (@types) {
11810:                     push(@{$changes{'selfcreate'}},$type);
11811:                 }
11812:             }
11813:         }
11814:         foreach my $field (@shibfields) {
11815:             if ($env{'form.shibenv_'.$field} ne '') {
11816:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
11817:             }
11818:         }
11819:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11820:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
11821:                 foreach my $field (@shibfields) {
11822:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
11823:                         push(@{$changes{'cancreate'}},'shibenv');
11824:                     }
11825:                 }
11826:             } else {
11827:                 foreach my $field (@shibfields) {
11828:                     if ($env{'form.shibenv_'.$field}) {
11829:                         push(@{$changes{'cancreate'}},'shibenv');
11830:                         last;
11831:                     }
11832:                 }
11833:             }
11834:         }
11835:     }
11836:     foreach my $item (@contexts) {
11837:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
11838:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
11839:                 if (ref($cancreate{$item}) eq 'ARRAY') {
11840:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
11841:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11842:                             push(@{$changes{'cancreate'}},$item);
11843:                         }
11844:                     }
11845:                 }
11846:             }
11847:             if (ref($cancreate{$item}) eq 'ARRAY') {
11848:                 foreach my $type (@{$cancreate{$item}}) {
11849:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
11850:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11851:                             push(@{$changes{'cancreate'}},$item);
11852:                         }
11853:                     }
11854:                 }
11855:             }
11856:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
11857:             if (ref($cancreate{$item}) eq 'HASH') {
11858:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
11859:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
11860:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
11861:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
11862:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11863:                                     push(@{$changes{'cancreate'}},$item);
11864:                                 }
11865:                             }
11866:                         }
11867:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
11868:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
11869:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11870:                                 push(@{$changes{'cancreate'}},$item);
11871:                             }
11872:                         }
11873:                     }
11874:                 }
11875:                 foreach my $type (keys(%{$cancreate{$item}})) {
11876:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
11877:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
11878:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
11879:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
11880:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11881:                                         push(@{$changes{'cancreate'}},$item);
11882:                                     }
11883:                                 }
11884:                             } else {
11885:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11886:                                     push(@{$changes{'cancreate'}},$item);
11887:                                 }
11888:                             }
11889:                         }
11890:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
11891:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
11892:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11893:                                 push(@{$changes{'cancreate'}},$item);
11894:                             }
11895:                         }
11896:                     }
11897:                 }
11898:             }
11899:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
11900:             if (ref($cancreate{$item}) eq 'ARRAY') {
11901:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
11902:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11903:                         push(@{$changes{'cancreate'}},$item);
11904:                     }
11905:                 }
11906:             }
11907:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
11908:             if (ref($cancreate{$item}) eq 'HASH') {
11909:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11910:                     push(@{$changes{'cancreate'}},$item);
11911:                 }
11912:             }
11913:         } elsif ($item eq 'emailusername') {
11914:             if (ref($cancreate{$item}) eq 'HASH') {
11915:                 foreach my $type (keys(%{$cancreate{$item}})) {
11916:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
11917:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
11918:                             if ($cancreate{$item}{$type}{$field}) {
11919:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11920:                                     push(@{$changes{'cancreate'}},$item);
11921:                                 }
11922:                                 last;
11923:                             }
11924:                         }
11925:                     }
11926:                 }
11927:             }
11928:         }
11929:     }
11930: #
11931: # Populate %save_usercreate hash with updates to self-creation configuration.
11932: #
11933:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
11934:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
11935:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
11936:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
11937:     if (ref($cancreate{'notify'}) eq 'HASH') {
11938:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
11939:     }
11940:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
11941:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
11942:     }
11943:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
11944:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
11945:     }
11946:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
11947:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
11948:     }
11949:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
11950:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
11951:     }
11952:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
11953:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
11954:     }
11955:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
11956:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
11957:     }
11958:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
11959:     $save_usercreate{'email_rule'} = \%email_rule;
11960: 
11961:     my %userconfig_hash = (
11962:             usercreation     => \%save_usercreate,
11963:             usermodification => \%save_usermodify,
11964:             inststatus       => \%save_inststatus,
11965:     );
11966: 
11967:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
11968:                                              $dom);
11969: #
11970: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
11971: #
11972:     if ($putresult eq 'ok') {
11973:         if (keys(%changes) > 0) {
11974:             $resulttext = &mt('Changes made:').'<ul>';
11975:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
11976:                 my %lt = &selfcreation_types();
11977:                 foreach my $type (@{$changes{'cancreate'}}) {
11978:                     my $chgtext = '';
11979:                     if ($type eq 'selfcreate') {
11980:                         if (@{$cancreate{$type}} == 0) {
11981:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
11982:                         } else {
11983:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
11984:                                         '<ul>';
11985:                             foreach my $case (@{$cancreate{$type}}) {
11986:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
11987:                             }
11988:                             $chgtext .= '</ul>';
11989:                             if (ref($cancreate{$type}) eq 'ARRAY') {
11990:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
11991:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
11992:                                         if (@{$cancreate{'statustocreate'}} == 0) {
11993:                                             $chgtext .= '<span class="LC_warning">'.
11994:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
11995:                                                         '</span><br />';
11996:                                         }
11997:                                     }
11998:                                 }
11999:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
12000:                                     if (!@statuses) {
12001:                                         $chgtext .= '<span class="LC_warning">'.
12002:                                                     &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.").
12003:                                                     '</span><br />';
12004: 
12005:                                     }
12006:                                 }
12007:                             }
12008:                         }
12009:                     } elsif ($type eq 'shibenv') {
12010:                         if (keys(%{$cancreate{$type}}) == 0) {
12011:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
12012:                         } else {
12013:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
12014:                                         '<ul>';
12015:                             foreach my $field (@shibfields) {
12016:                                 next if ($cancreate{$type}{$field} eq '');
12017:                                 if ($field eq 'inststatus') {
12018:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
12019:                                 } else {
12020:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
12021:                                 }
12022:                             }
12023:                             $chgtext .= '</ul>';
12024:                         }
12025:                     } elsif ($type eq 'statustocreate') {
12026:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
12027:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
12028:                             if (@{$cancreate{'selfcreate'}} > 0) {
12029:                                 if (@{$cancreate{'statustocreate'}} == 0) {
12030:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
12031:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
12032:                                         $chgtext .= '<br />'.
12033:                                                     '<span class="LC_warning">'.
12034:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
12035:                                                     '</span>';
12036:                                     }
12037:                                 } elsif (keys(%usertypes) > 0) {
12038:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
12039:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
12040:                                     } else {
12041:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
12042:                                     }
12043:                                     $chgtext .= '<ul>';
12044:                                     foreach my $case (@{$cancreate{$type}}) {
12045:                                         if ($case eq 'default') {
12046:                                             $chgtext .= '<li>'.$othertitle.'</li>';
12047:                                         } else {
12048:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
12049:                                         }
12050:                                     }
12051:                                     $chgtext .= '</ul>';
12052:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
12053:                                         $chgtext .= '<span class="LC_warning">'.
12054:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
12055:                                                     '</span>';
12056:                                     }
12057:                                 }
12058:                             } else {
12059:                                 if (@{$cancreate{$type}} == 0) {
12060:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
12061:                                 } else {
12062:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
12063:                                 }
12064:                             }
12065:                             $chgtext .= '<br />';
12066:                         }
12067:                     } elsif ($type eq 'selfcreateprocessing') {
12068:                         my %choices = &Apache::lonlocal::texthash (
12069:                                                                     automatic => 'Automatic approval',
12070:                                                                     approval  => 'Queued for approval',
12071:                                                                   );
12072:                         if (@types) {
12073:                             if (@statuses) {
12074:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:'). 
12075:                                             '<ul>';
12076:                                 foreach my $status (@statuses) {
12077:                                     if ($status eq 'default') {
12078:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
12079:                                     } else {
12080:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
12081:                                     }
12082:                                 }
12083:                                 $chgtext .= '</ul>';
12084:                             }
12085:                         } else {
12086:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
12087:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
12088:                         }
12089:                     } elsif ($type eq 'emailverified') {
12090:                         my %options = &Apache::lonlocal::texthash (
12091:                                                                     all   => 'Same as e-mail',
12092:                                                                     first => 'Omit @domain',
12093:                                                                     free  => 'Free to choose',
12094:                                                                   );
12095:                         if (@types) {
12096:                             if (@statuses) {
12097:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
12098:                                             '<ul>';
12099:                                 foreach my $status (@statuses) {
12100:                                     if ($type eq 'default') {
12101:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
12102:                                     } else {
12103:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
12104:                                     }
12105:                                 }
12106:                                 $chgtext .= '</ul>';
12107:                             }
12108:                         } else {
12109:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
12110:                                             $options{$cancreate{'emailverified'}{'default'}});
12111:                         }
12112:                     } elsif ($type eq 'emailoptions') {
12113:                         my %options = &Apache::lonlocal::texthash (
12114:                                                                     any     => 'Any e-mail',
12115:                                                                     inst    => 'Institutional only',
12116:                                                                     noninst => 'Non-institutional only',
12117:                                                                     custom  => 'Custom restrictions',
12118:                                                                   );
12119:                         if (@types) {
12120:                             if (@statuses) {
12121:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
12122:                                             '<ul>';
12123:                                 foreach my $status (@statuses) {
12124:                                     if ($type eq 'default') {
12125:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
12126:                                     } else {
12127:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
12128:                                     }
12129:                                 }
12130:                                 $chgtext .= '</ul>';
12131:                             }
12132:                         } else {
12133:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
12134:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
12135:                             } else {
12136:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
12137:                                                 $options{$cancreate{'emailoptions'}{'default'}});
12138:                             }
12139:                         }
12140:                     } elsif ($type eq 'emaildomain') {
12141:                         my $output;
12142:                         if (@statuses) {
12143:                             foreach my $type (@statuses) {
12144:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
12145:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
12146:                                         if ($type eq 'default') {
12147:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
12148:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
12149:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
12150:                                             } else {
12151:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
12152:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
12153:                                             }
12154:                                         } else {
12155:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
12156:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
12157:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
12158:                                             } else {
12159:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
12160:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>'; 
12161:                                             }
12162:                                         }
12163:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
12164:                                         if ($type eq 'default') {
12165:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
12166:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
12167:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
12168:                                             } else {
12169:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
12170:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
12171:                                             }
12172:                                         } else {
12173:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
12174:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
12175:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
12176:                                             } else {
12177:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
12178:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';   
12179:                                             }
12180:                                         }
12181:                                     }
12182:                                 }
12183:                             }
12184:                         }
12185:                         if ($output ne '') {
12186:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
12187:                                         '<ul>'.$output.'</ul>';
12188:                         }
12189:                     } elsif ($type eq 'captcha') {
12190:                         if ($savecaptcha{$type} eq 'notused') {
12191:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
12192:                         } else {
12193:                             my %captchas = &captcha_phrases();
12194:                             if ($captchas{$savecaptcha{$type}}) {
12195:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
12196:                             } else {
12197:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
12198:                             }
12199:                         }
12200:                     } elsif ($type eq 'recaptchakeys') {
12201:                         my ($privkey,$pubkey);
12202:                         if (ref($savecaptcha{$type}) eq 'HASH') {
12203:                             $pubkey = $savecaptcha{$type}{'public'};
12204:                             $privkey = $savecaptcha{$type}{'private'};
12205:                         }
12206:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
12207:                         if (!$pubkey) {
12208:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
12209:                         } else {
12210:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
12211:                         }
12212:                         if (!$privkey) {
12213:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
12214:                         } else {
12215:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
12216:                         }
12217:                         $chgtext .= '</ul>';
12218:                     } elsif ($type eq 'recaptchaversion') {
12219:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
12220:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
12221:                         }
12222:                     } elsif ($type eq 'emailusername') {
12223:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
12224:                             if (@statuses) {
12225:                                 foreach my $type (@statuses) {
12226:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
12227:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
12228:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
12229:                                                     '<ul>';
12230:                                             foreach my $field (@{$infofields}) {
12231:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
12232:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
12233:                                                 }
12234:                                             }
12235:                                             $chgtext .= '</ul>';
12236:                                         } else {
12237:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
12238:                                         }
12239:                                     } else {
12240:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
12241:                                     }
12242:                                 }
12243:                             }
12244:                         }
12245:                     } elsif ($type eq 'notify') {
12246:                         my $numapprove = 0;
12247:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
12248:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
12249:                                 if ($cancreate{'notify'}{'approval'}) {
12250:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
12251:                                     $numapprove ++;
12252:                                 }
12253:                             }
12254:                         }
12255:                         unless ($numapprove) {
12256:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
12257:                         }
12258:                     }
12259:                     if ($chgtext) {
12260:                         $resulttext .= '<li>'.$chgtext.'</li>';
12261:                     }
12262:                 }
12263:             }
12264:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
12265:                 my ($emailrules,$emailruleorder) =
12266:                     &Apache::lonnet::inst_userrules($dom,'email');
12267:                 foreach my $type (@{$changes{'email_rule'}}) {
12268:                     if (ref($email_rule{$type}) eq 'ARRAY') {
12269:                         my $chgtext = '<ul>';
12270:                         foreach my $rule (@{$email_rule{$type}}) {
12271:                             if (ref($emailrules->{$rule}) eq 'HASH') {
12272:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
12273:                             }
12274:                         }
12275:                         $chgtext .= '</ul>';
12276:                         my $typename;
12277:                         if (@types) {
12278:                             if ($type eq 'default') {
12279:                                 $typename = $othertitle;
12280:                             } else {
12281:                                 $typename = $usertypes{$type};
12282:                             } 
12283:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
12284:                         }
12285:                         if (@{$email_rule{$type}} > 0) {
12286:                             $resulttext .= '<li>'.
12287:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
12288:                                                $usertypes{$type}).
12289:                                            $chgtext.
12290:                                            '</li>';
12291:                         } else {
12292:                             $resulttext .= '<li>'.
12293:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
12294:                                            '</li>'.
12295:                                            &mt('(Affiliation: [_1])',$typename);
12296:                         }
12297:                     }
12298:                 }
12299:             }
12300:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
12301:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
12302:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
12303:                         my $chgtext = '<ul>';
12304:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
12305:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
12306:                         }
12307:                         $chgtext .= '</ul>';
12308:                         $resulttext .= '<li>'.
12309:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
12310:                                           $chgtext.
12311:                                        '</li>';
12312:                     } else {
12313:                         $resulttext .= '<li>'.
12314:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
12315:                                        '</li>';
12316:                     }
12317:                 }
12318:             }
12319:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
12320:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
12321:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
12322:                 foreach my $type (@{$changes{'selfcreate'}}) {
12323:                     my $typename = $type;
12324:                     if (keys(%usertypes) > 0) {
12325:                         if ($usertypes{$type} ne '') {
12326:                             $typename = $usertypes{$type};
12327:                         }
12328:                     }
12329:                     my @modifiable;
12330:                     $resulttext .= '<li>'.
12331:                                     &mt('Self-creation of account by users with status: [_1]',
12332:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
12333:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
12334:                     foreach my $field (@fields) {
12335:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
12336:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
12337:                         }
12338:                     }
12339:                     if (@modifiable > 0) {
12340:                         $resulttext .= join(', ',@modifiable);
12341:                     } else {
12342:                         $resulttext .= &mt('none');
12343:                     }
12344:                     $resulttext .= '</li>';
12345:                 }
12346:                 $resulttext .= '</ul></li>';
12347:             }
12348:             $resulttext .= '</ul>';
12349:             my $cachetime = 24*60*60;
12350:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
12351:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12352:             if (ref($lastactref) eq 'HASH') {
12353:                 $lastactref->{'domdefaults'} = 1;
12354:             }
12355:         } else {
12356:             $resulttext = &mt('No changes made to self-creation settings');
12357:         }
12358:     } else {
12359:         $resulttext = '<span class="LC_error">'.
12360:             &mt('An error occurred: [_1]',$putresult).'</span>';
12361:     }
12362:     if ($warningmsg ne '') {
12363:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
12364:     }
12365:     return $resulttext;
12366: }
12367: 
12368: sub process_captcha {
12369:     my ($container,$changes,$newsettings,$current) = @_;
12370:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
12371:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
12372:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
12373:         $newsettings->{'captcha'} = 'original';
12374:     }
12375:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
12376:         if ($container eq 'cancreate') {
12377:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
12378:                 push(@{$changes->{'cancreate'}},'captcha');
12379:             } elsif (!defined($changes->{'cancreate'})) {
12380:                 $changes->{'cancreate'} = ['captcha'];
12381:             }
12382:         } else {
12383:             $changes->{'captcha'} = 1;
12384:         }
12385:     }
12386:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
12387:     if ($newsettings->{'captcha'} eq 'recaptcha') {
12388:         $newpub = $env{'form.'.$container.'_recaptchapub'};
12389:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
12390:         $newpub =~ s/[^\w\-]//g;
12391:         $newpriv =~ s/[^\w\-]//g;
12392:         $newsettings->{'recaptchakeys'} = {
12393:                                              public  => $newpub,
12394:                                              private => $newpriv,
12395:                                           };
12396:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
12397:         $newversion =~ s/\D//g;
12398:         if ($newversion ne '2') {
12399:             $newversion = 1;
12400:         }
12401:         $newsettings->{'recaptchaversion'} = $newversion;
12402:     }
12403:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
12404:         $currpub = $current->{'recaptchakeys'}{'public'};
12405:         $currpriv = $current->{'recaptchakeys'}{'private'};
12406:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
12407:             $newsettings->{'recaptchakeys'} = {
12408:                                                  public  => '',
12409:                                                  private => '',
12410:                                               }
12411:         }
12412:     }
12413:     if ($current->{'captcha'} eq 'recaptcha') {
12414:         $currversion = $current->{'recaptchaversion'};
12415:         if ($currversion ne '2') {
12416:             $currversion = 1;
12417:         }
12418:     }
12419:     if ($currversion ne $newversion) {
12420:         if ($container eq 'cancreate') {
12421:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
12422:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
12423:             } elsif (!defined($changes->{'cancreate'})) {
12424:                 $changes->{'cancreate'} = ['recaptchaversion'];
12425:             }
12426:         } else {
12427:             $changes->{'recaptchaversion'} = 1;
12428:         }
12429:     }
12430:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
12431:         if ($container eq 'cancreate') {
12432:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
12433:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
12434:             } elsif (!defined($changes->{'cancreate'})) {
12435:                 $changes->{'cancreate'} = ['recaptchakeys'];
12436:             }
12437:         } else {
12438:             $changes->{'recaptchakeys'} = 1;
12439:         }
12440:     }
12441:     return;
12442: }
12443: 
12444: sub modify_usermodification {
12445:     my ($dom,%domconfig) = @_;
12446:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
12447:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
12448:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
12449:             if ($key eq 'selfcreate') {
12450:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
12451:             } else {  
12452:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
12453:             }
12454:         }
12455:     }
12456:     my @contexts = ('author','course');
12457:     my %context_title = (
12458:                            author => 'In author context',
12459:                            course => 'In course context',
12460:                         );
12461:     my @fields = ('lastname','firstname','middlename','generation',
12462:                   'permanentemail','id');
12463:     my %roles = (
12464:                   author => ['ca','aa'],
12465:                   course => ['st','ep','ta','in','cr'],
12466:                 );
12467:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
12468:     foreach my $context (@contexts) {
12469:         foreach my $role (@{$roles{$context}}) {
12470:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
12471:             foreach my $item (@fields) {
12472:                 if (grep(/^\Q$item\E$/,@modifiable)) {
12473:                     $modifyhash{$context}{$role}{$item} = 1;
12474:                 } else {
12475:                     $modifyhash{$context}{$role}{$item} = 0;
12476:                 }
12477:             }
12478:         }
12479:         if (ref($curr_usermodification{$context}) eq 'HASH') {
12480:             foreach my $role (@{$roles{$context}}) {
12481:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
12482:                     foreach my $field (@fields) {
12483:                         if ($modifyhash{$context}{$role}{$field} ne 
12484:                                 $curr_usermodification{$context}{$role}{$field}) {
12485:                             push(@{$changes{$context}},$role);
12486:                             last;
12487:                         }
12488:                     }
12489:                 }
12490:             }
12491:         } else {
12492:             foreach my $context (@contexts) {
12493:                 foreach my $role (@{$roles{$context}}) {
12494:                     push(@{$changes{$context}},$role);
12495:                 }
12496:             }
12497:         }
12498:     }
12499:     my %usermodification_hash =  (
12500:                                    usermodification => \%modifyhash,
12501:                                  );
12502:     my $putresult = &Apache::lonnet::put_dom('configuration',
12503:                                              \%usermodification_hash,$dom);
12504:     if ($putresult eq 'ok') {
12505:         if (keys(%changes) > 0) {
12506:             $resulttext = &mt('Changes made: ').'<ul>';
12507:             foreach my $context (@contexts) {
12508:                 if (ref($changes{$context}) eq 'ARRAY') {
12509:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
12510:                     if (ref($changes{$context}) eq 'ARRAY') {
12511:                         foreach my $role (@{$changes{$context}}) {
12512:                             my $rolename;
12513:                             if ($role eq 'cr') {
12514:                                 $rolename = &mt('Custom');
12515:                             } else {
12516:                                 $rolename = &Apache::lonnet::plaintext($role);
12517:                             }
12518:                             my @modifiable;
12519:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
12520:                             foreach my $field (@fields) {
12521:                                 if ($modifyhash{$context}{$role}{$field}) {
12522:                                     push(@modifiable,$fieldtitles{$field});
12523:                                 }
12524:                             }
12525:                             if (@modifiable > 0) {
12526:                                 $resulttext .= join(', ',@modifiable);
12527:                             } else {
12528:                                 $resulttext .= &mt('none'); 
12529:                             }
12530:                             $resulttext .= '</li>';
12531:                         }
12532:                         $resulttext .= '</ul></li>';
12533:                     }
12534:                 }
12535:             }
12536:             $resulttext .= '</ul>';
12537:         } else {
12538:             $resulttext = &mt('No changes made to user modification settings');
12539:         }
12540:     } else {
12541:         $resulttext = '<span class="LC_error">'.
12542:             &mt('An error occurred: [_1]',$putresult).'</span>';
12543:     }
12544:     return $resulttext;
12545: }
12546: 
12547: sub modify_defaults {
12548:     my ($dom,$lastactref,%domconfig) = @_;
12549:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
12550:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12551:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
12552:                  'portal_def','intauth_cost','intauth_check','intauth_switch');
12553:     my @authtypes = ('internal','krb4','krb5','localauth');
12554:     foreach my $item (@items) {
12555:         $newvalues{$item} = $env{'form.'.$item};
12556:         if ($item eq 'auth_def') {
12557:             if ($newvalues{$item} ne '') {
12558:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
12559:                     push(@errors,$item);
12560:                 }
12561:             }
12562:         } elsif ($item eq 'lang_def') {
12563:             if ($newvalues{$item} ne '') {
12564:                 if ($newvalues{$item} =~ /^(\w+)/) {
12565:                     my $langcode = $1;
12566:                     if ($langcode ne 'x_chef') {
12567:                         if (code2language($langcode) eq '') {
12568:                             push(@errors,$item);
12569:                         }
12570:                     }
12571:                 } else {
12572:                     push(@errors,$item);
12573:                 }
12574:             }
12575:         } elsif ($item eq 'timezone_def') {
12576:             if ($newvalues{$item} ne '') {
12577:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
12578:                     push(@errors,$item);   
12579:                 }
12580:             }
12581:         } elsif ($item eq 'datelocale_def') {
12582:             if ($newvalues{$item} ne '') {
12583:                 my @datelocale_ids = DateTime::Locale->ids();
12584:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
12585:                     push(@errors,$item);
12586:                 }
12587:             }
12588:         } elsif ($item eq 'portal_def') {
12589:             if ($newvalues{$item} ne '') {
12590:                 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])\/?$/) {
12591:                     push(@errors,$item);
12592:                 }
12593:             }
12594:         } elsif ($item eq 'intauth_cost') {
12595:             if ($newvalues{$item} ne '') {
12596:                 if ($newvalues{$item} =~ /\D/) {
12597:                     push(@errors,$item);
12598:                 }
12599:             }
12600:         } elsif ($item eq 'intauth_check') {
12601:             if ($newvalues{$item} ne '') {
12602:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
12603:                     push(@errors,$item);
12604:                 }
12605:             }
12606:         } elsif ($item eq 'intauth_switch') {
12607:             if ($newvalues{$item} ne '') {
12608:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
12609:                     push(@errors,$item);
12610:                 }
12611:             }
12612:         }
12613:         if (grep(/^\Q$item\E$/,@errors)) {
12614:             $newvalues{$item} = $domdefaults{$item};
12615:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
12616:             $changes{$item} = 1;
12617:         }
12618:         $domdefaults{$item} = $newvalues{$item};
12619:     }
12620:     my %defaults_hash = (
12621:                          defaults => \%newvalues,
12622:                         );
12623:     my $title = &defaults_titles();
12624: 
12625:     my $currinststatus;
12626:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
12627:         $currinststatus = $domconfig{'inststatus'};
12628:     } else {
12629:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12630:         $currinststatus = {
12631:                              inststatustypes => $usertypes,
12632:                              inststatusorder => $types,
12633:                              inststatusguest => [],
12634:                           };
12635:     }
12636:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
12637:     my @allpos;
12638:     my %alltypes;
12639:     my @inststatusguest;
12640:     if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
12641:         foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
12642:             unless (grep(/^\Q$type\E$/,@todelete)) {
12643:                 push(@inststatusguest,$type);
12644:             }
12645:         }
12646:     }
12647:     my ($currtitles,$currorder);
12648:     if (ref($currinststatus) eq 'HASH') {
12649:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
12650:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
12651:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
12652:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
12653:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
12654:                     }
12655:                 }
12656:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
12657:                     my $position = $env{'form.inststatus_pos_'.$type};
12658:                     $position =~ s/\D+//g;
12659:                     $allpos[$position] = $type;
12660:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
12661:                     $alltypes{$type} =~ s/`//g;
12662:                 }
12663:             }
12664:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
12665:             $currtitles =~ s/,$//;
12666:         }
12667:     }
12668:     if ($env{'form.addinststatus'}) {
12669:         my $newtype = $env{'form.addinststatus'};
12670:         $newtype =~ s/\W//g;
12671:         unless (exists($alltypes{$newtype})) {
12672:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
12673:             $alltypes{$newtype} =~ s/`//g; 
12674:             my $position = $env{'form.addinststatus_pos'};
12675:             $position =~ s/\D+//g;
12676:             if ($position ne '') {
12677:                 $allpos[$position] = $newtype;
12678:             }
12679:         }
12680:     }
12681:     my @orderedstatus;
12682:     foreach my $type (@allpos) {
12683:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
12684:             push(@orderedstatus,$type);
12685:         }
12686:     }
12687:     foreach my $type (keys(%alltypes)) {
12688:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
12689:             delete($alltypes{$type});
12690:         }
12691:     }
12692:     $defaults_hash{'inststatus'} = {
12693:                                      inststatustypes => \%alltypes,
12694:                                      inststatusorder => \@orderedstatus,
12695:                                      inststatusguest => \@inststatusguest,
12696:                                    };
12697:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
12698:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
12699:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
12700:         }
12701:     }
12702:     if ($currorder ne join(',',@orderedstatus)) {
12703:         $changes{'inststatus'}{'inststatusorder'} = 1;
12704:     }
12705:     my $newtitles;
12706:     foreach my $item (@orderedstatus) {
12707:         $newtitles .= $alltypes{$item}.',';
12708:     }
12709:     $newtitles =~ s/,$//;
12710:     if ($currtitles ne $newtitles) {
12711:         $changes{'inststatus'}{'inststatustypes'} = 1;
12712:     }
12713:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
12714:                                              $dom);
12715:     if ($putresult eq 'ok') {
12716:         if (keys(%changes) > 0) {
12717:             $resulttext = &mt('Changes made:').'<ul>';
12718:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
12719:             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";
12720:             foreach my $item (sort(keys(%changes))) {
12721:                 if ($item eq 'inststatus') {
12722:                     if (ref($changes{'inststatus'}) eq 'HASH') {
12723:                         if (@orderedstatus) {
12724:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
12725:                             foreach my $type (@orderedstatus) { 
12726:                                 $resulttext .= $alltypes{$type}.', ';
12727:                             }
12728:                             $resulttext =~ s/, $//;
12729:                             $resulttext .= '</li>';
12730:                         } else {
12731:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>'; 
12732:                         }
12733:                     }
12734:                 } else {
12735:                     my $value = $env{'form.'.$item};
12736:                     if ($value eq '') {
12737:                         $value = &mt('none');
12738:                     } elsif ($item eq 'auth_def') {
12739:                         my %authnames = &authtype_names();
12740:                         my %shortauth = (
12741:                                           internal   => 'int',
12742:                                           krb4       => 'krb4',
12743:                                           krb5       => 'krb5',
12744:                                           localauth  => 'loc',
12745:                         );
12746:                         $value = $authnames{$shortauth{$value}};
12747:                     } elsif ($item eq 'intauth_switch') {
12748:                         my %optiondesc = &Apache::lonlocal::texthash (
12749:                                             0 => 'No',
12750:                                             1 => 'Yes',
12751:                                             2 => 'Yes, and copy existing passwd file to passwd.bak file',
12752:                                          );
12753:                         if ($value =~ /^(0|1|2)$/) {
12754:                             $value = $optiondesc{$value};
12755:                         } else {
12756:                             $value = &mt('none -- defaults to No');
12757:                         }
12758:                     } elsif ($item eq 'intauth_check') {
12759:                         my %optiondesc = &Apache::lonlocal::texthash (
12760:                                              0 => 'No',
12761:                                              1 => 'Yes, allow login then update passwd file using default cost (if higher)',
12762:                                              2 => 'Yes, disallow login if stored cost is less than domain default',
12763:                                          );
12764:                         if ($value =~ /^(0|1|2)$/) {
12765:                             $value = $optiondesc{$value};
12766:                         } else {
12767:                             $value = &mt('none -- defaults to No');
12768:                         }
12769:                     }
12770:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
12771:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
12772:                 }
12773:             }
12774:             $resulttext .= '</ul>';
12775:             $mailmsgtext .= "\n";
12776:             my $cachetime = 24*60*60;
12777:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12778:             if (ref($lastactref) eq 'HASH') {
12779:                 $lastactref->{'domdefaults'} = 1;
12780:             }
12781:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
12782:                 my $notify = 1;
12783:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
12784:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
12785:                         $notify = 0;
12786:                     }
12787:                 }
12788:                 if ($notify) {
12789:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
12790:                                                "LON-CAPA Domain Settings Change - $dom",
12791:                                                $mailmsgtext);
12792:                 }
12793:             }
12794:         } else {
12795:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
12796:         }
12797:     } else {
12798:         $resulttext = '<span class="LC_error">'.
12799:             &mt('An error occurred: [_1]',$putresult).'</span>';
12800:     }
12801:     if (@errors > 0) {
12802:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
12803:         foreach my $item (@errors) {
12804:             $resulttext .= ' "'.$title->{$item}.'",';
12805:         }
12806:         $resulttext =~ s/,$//;
12807:     }
12808:     return $resulttext;
12809: }
12810: 
12811: sub modify_scantron {
12812:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12813:     my ($resulttext,%confhash,%changes,$errors);
12814:     my $custom = 'custom.tab';
12815:     my $default = 'default.tab';
12816:     my $servadm = $r->dir_config('lonAdmEMail');
12817:     my ($configuserok,$author_ok,$switchserver) = 
12818:         &config_check($dom,$confname,$servadm);
12819:     if ($env{'form.scantronformat.filename'} ne '') {
12820:         my $error;
12821:         if ($configuserok eq 'ok') {
12822:             if ($switchserver) {
12823:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
12824:             } else {
12825:                 if ($author_ok eq 'ok') {
12826:                     my ($result,$scantronurl) =
12827:                         &publishlogo($r,'upload','scantronformat',$dom,
12828:                                      $confname,'scantron','','',$custom);
12829:                     if ($result eq 'ok') {
12830:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
12831:                         $changes{'scantronformat'} = 1;
12832:                     } else {
12833:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
12834:                     }
12835:                 } else {
12836:                     $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);
12837:                 }
12838:             }
12839:         } else {
12840:             $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);
12841:         }
12842:         if ($error) {
12843:             &Apache::lonnet::logthis($error);
12844:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12845:         }
12846:     }
12847:     if (ref($domconfig{'scantron'}) eq 'HASH') {
12848:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
12849:             if ($env{'form.scantronformat_del'}) {
12850:                 $confhash{'scantron'}{'scantronformat'} = '';
12851:                 $changes{'scantronformat'} = 1;
12852:             }
12853:         }
12854:     }
12855:     if (keys(%confhash) > 0) {
12856:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
12857:                                                  $dom);
12858:         if ($putresult eq 'ok') {
12859:             if (keys(%changes) > 0) {
12860:                 if (ref($confhash{'scantron'}) eq 'HASH') {
12861:                     $resulttext = &mt('Changes made:').'<ul>';
12862:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
12863:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
12864:                     } else {
12865:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
12866:                     }
12867:                     $resulttext .= '</ul>';
12868:                 } else {
12869:                     $resulttext = &mt('Changes made to bubblesheet format file.');
12870:                 }
12871:                 $resulttext .= '</ul>';
12872:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
12873:                 if (ref($lastactref) eq 'HASH') {
12874:                     $lastactref->{'domainconfig'} = 1;
12875:                 }
12876:             } else {
12877:                 $resulttext = &mt('No changes made to bubblesheet format file');
12878:             }
12879:         } else {
12880:             $resulttext = '<span class="LC_error">'.
12881:                 &mt('An error occurred: [_1]',$putresult).'</span>';
12882:         }
12883:     } else {
12884:         $resulttext = &mt('No changes made to bubblesheet format file'); 
12885:     }
12886:     if ($errors) {
12887:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
12888:                        $errors.'</ul>';
12889:     }
12890:     return $resulttext;
12891: }
12892: 
12893: sub modify_coursecategories {
12894:     my ($dom,$lastactref,%domconfig) = @_;
12895:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
12896:         $cathash);
12897:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
12898:     my @catitems = ('unauth','auth');
12899:     my @cattypes = ('std','domonly','codesrch','none');
12900:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
12901:         $cathash = $domconfig{'coursecategories'}{'cats'};
12902:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
12903:             $changes{'togglecats'} = 1;
12904:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
12905:         }
12906:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
12907:             $changes{'categorize'} = 1;
12908:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
12909:         }
12910:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
12911:             $changes{'togglecatscomm'} = 1;
12912:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
12913:         }
12914:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
12915:             $changes{'categorizecomm'} = 1;
12916:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
12917: 
12918:         }
12919:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
12920:             $changes{'togglecatsplace'} = 1;
12921:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
12922:         }
12923:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
12924:             $changes{'categorizeplace'} = 1;
12925:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
12926:         }
12927:         foreach my $item (@catitems) {
12928:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
12929:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
12930:                     $changes{$item} = 1;
12931:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
12932:                 }
12933:             }
12934:         }
12935:     } else {
12936:         $changes{'togglecats'} = 1;
12937:         $changes{'categorize'} = 1;
12938:         $changes{'togglecatscomm'} = 1;
12939:         $changes{'categorizecomm'} = 1;
12940:         $changes{'togglecatsplace'} = 1;
12941:         $changes{'categorizeplace'} = 1;
12942:         $domconfig{'coursecategories'} = {
12943:                                              togglecats => $env{'form.togglecats'},
12944:                                              categorize => $env{'form.categorize'},
12945:                                              togglecatscomm => $env{'form.togglecatscomm'},
12946:                                              categorizecomm => $env{'form.categorizecomm'},
12947:                                              togglecatsplace => $env{'form.togglecatsplace'},
12948:                                              categorizeplace => $env{'form.categorizeplace'},
12949:                                          };
12950:         foreach my $item (@catitems) {
12951:             if ($env{'form.coursecat_'.$item} ne 'std') {
12952:                 $changes{$item} = 1;
12953:             }
12954:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
12955:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
12956:             }
12957:         }
12958:     }
12959:     if (ref($cathash) eq 'HASH') {
12960:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
12961:             push (@deletecategory,'instcode::0');
12962:         }
12963:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
12964:             push(@deletecategory,'communities::0');
12965:         }
12966:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
12967:             push(@deletecategory,'placement::0');
12968:         }
12969:     }
12970:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
12971:     if (ref($cathash) eq 'HASH') {
12972:         if (@deletecategory > 0) {
12973:             #FIXME Need to remove category from all courses using a deleted category 
12974:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
12975:             foreach my $item (@deletecategory) {
12976:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
12977:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
12978:                     $deletions{$item} = 1;
12979:                     &recurse_cat_deletes($item,$cathash,\%deletions);
12980:                 }
12981:             }
12982:         }
12983:         foreach my $item (keys(%{$cathash})) {
12984:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
12985:             if ($cathash->{$item} ne $env{'form.'.$item}) {
12986:                 $reorderings{$item} = 1;
12987:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
12988:             }
12989:             if ($env{'form.addcategory_name_'.$item} ne '') {
12990:                 my $newcat = $env{'form.addcategory_name_'.$item};
12991:                 my $newdepth = $depth+1;
12992:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
12993:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
12994:                 $adds{$newitem} = 1; 
12995:             }
12996:             if ($env{'form.subcat_'.$item} ne '') {
12997:                 my $newcat = $env{'form.subcat_'.$item};
12998:                 my $newdepth = $depth+1;
12999:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
13000:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
13001:                 $adds{$newitem} = 1;
13002:             }
13003:         }
13004:     }
13005:     if ($env{'form.instcode'} eq '1') {
13006:         if (ref($cathash) eq 'HASH') {
13007:             my $newitem = 'instcode::0';
13008:             if ($cathash->{$newitem} eq '') {  
13009:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
13010:                 $adds{$newitem} = 1;
13011:             }
13012:         } else {
13013:             my $newitem = 'instcode::0';
13014:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
13015:             $adds{$newitem} = 1;
13016:         }
13017:     }
13018:     if ($env{'form.communities'} eq '1') {
13019:         if (ref($cathash) eq 'HASH') {
13020:             my $newitem = 'communities::0';
13021:             if ($cathash->{$newitem} eq '') {
13022:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
13023:                 $adds{$newitem} = 1;
13024:             }
13025:         } else {
13026:             my $newitem = 'communities::0';
13027:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
13028:             $adds{$newitem} = 1;
13029:         }
13030:     }
13031:     if ($env{'form.placement'} eq '1') {
13032:         if (ref($cathash) eq 'HASH') {
13033:             my $newitem = 'placement::0';
13034:             if ($cathash->{$newitem} eq '') {
13035:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
13036:                 $adds{$newitem} = 1;
13037:             }
13038:         } else {
13039:             my $newitem = 'placement::0';
13040:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
13041:             $adds{$newitem} = 1;
13042:         }
13043:     }
13044:     if ($env{'form.addcategory_name'} ne '') {
13045:         if (($env{'form.addcategory_name'} ne 'instcode') &&
13046:             ($env{'form.addcategory_name'} ne 'communities') &&
13047:             ($env{'form.addcategory_name'} ne 'placement')) {
13048:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
13049:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
13050:             $adds{$newitem} = 1;
13051:         }
13052:     }
13053:     my $putresult;
13054:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
13055:         if (keys(%deletions) > 0) {
13056:             foreach my $key (keys(%deletions)) {
13057:                 if ($predelallitems{$key} ne '') {
13058:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
13059:                 }
13060:             }
13061:         }
13062:         my (@chkcats,@chktrails,%chkallitems);
13063:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
13064:         if (ref($chkcats[0]) eq 'ARRAY') {
13065:             my $depth = 0;
13066:             my $chg = 0;
13067:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
13068:                 my $name = $chkcats[0][$i];
13069:                 my $item;
13070:                 if ($name eq '') {
13071:                     $chg ++;
13072:                 } else {
13073:                     $item = &escape($name).'::0';
13074:                     if ($chg) {
13075:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
13076:                     }
13077:                     $depth ++; 
13078:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
13079:                     $depth --;
13080:                 }
13081:             }
13082:         }
13083:     }
13084:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
13085:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
13086:         if ($putresult eq 'ok') {
13087:             my %title = (
13088:                          togglecats     => 'Show/Hide a course in catalog',
13089:                          categorize     => 'Assign a category to a course',
13090:                          togglecatscomm => 'Show/Hide a community in catalog',
13091:                          categorizecomm => 'Assign a category to a community',
13092:                         );
13093:             my %level = (
13094:                          dom  => 'set in Domain ("Modify Course/Community")',
13095:                          crs  => 'set in Course ("Course Configuration")',
13096:                          comm => 'set in Community ("Community Configuration")',
13097:                          none     => 'No catalog',
13098:                          std      => 'Standard catalog',
13099:                          domonly  => 'Domain-only catalog',
13100:                          codesrch => 'Code search form',
13101:                         );
13102:             $resulttext = &mt('Changes made:').'<ul>';
13103:             if ($changes{'togglecats'}) {
13104:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
13105:             }
13106:             if ($changes{'categorize'}) {
13107:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
13108:             }
13109:             if ($changes{'togglecatscomm'}) {
13110:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
13111:             }
13112:             if ($changes{'categorizecomm'}) {
13113:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
13114:             }
13115:             if ($changes{'unauth'}) {
13116:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
13117:             }
13118:             if ($changes{'auth'}) {
13119:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
13120:             }
13121:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
13122:                 my $cathash;
13123:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
13124:                     $cathash = $domconfig{'coursecategories'}{'cats'};
13125:                 } else {
13126:                     $cathash = {};
13127:                 } 
13128:                 my (@cats,@trails,%allitems);
13129:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
13130:                 if (keys(%deletions) > 0) {
13131:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
13132:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
13133:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
13134:                     }
13135:                     $resulttext .= '</ul></li>';
13136:                 }
13137:                 if (keys(%reorderings) > 0) {
13138:                     my %sort_by_trail;
13139:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
13140:                     foreach my $key (keys(%reorderings)) {
13141:                         if ($allitems{$key} ne '') {
13142:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
13143:                         }
13144:                     }
13145:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
13146:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
13147:                     }
13148:                     $resulttext .= '</ul></li>';
13149:                 }
13150:                 if (keys(%adds) > 0) {
13151:                     my %sort_by_trail;
13152:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
13153:                     foreach my $key (keys(%adds)) {
13154:                         if ($allitems{$key} ne '') {
13155:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
13156:                         }
13157:                     }
13158:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
13159:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
13160:                     }
13161:                     $resulttext .= '</ul></li>';
13162:                 }
13163:             }
13164:             $resulttext .= '</ul>';
13165:             if ($changes{'unauth'} || $changes{'auth'}) {
13166:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
13167:                 if ($changes{'auth'}) {
13168:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
13169:                 }
13170:                 if ($changes{'unauth'}) {
13171:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
13172:                 }
13173:                 my $cachetime = 24*60*60;
13174:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13175:                 if (ref($lastactref) eq 'HASH') {
13176:                     $lastactref->{'domdefaults'} = 1;
13177:                 }
13178:             }
13179:         } else {
13180:             $resulttext = '<span class="LC_error">'.
13181:                           &mt('An error occurred: [_1]',$putresult).'</span>';
13182:         }
13183:     } else {
13184:         $resulttext = &mt('No changes made to course and community categories');
13185:     }
13186:     return $resulttext;
13187: }
13188: 
13189: sub modify_serverstatuses {
13190:     my ($dom,%domconfig) = @_;
13191:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
13192:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
13193:         %currserverstatus = %{$domconfig{'serverstatuses'}};
13194:     }
13195:     my @pages = &serverstatus_pages();
13196:     foreach my $type (@pages) {
13197:         $newserverstatus{$type}{'namedusers'} = '';
13198:         $newserverstatus{$type}{'machines'} = '';
13199:         if (defined($env{'form.'.$type.'_namedusers'})) {
13200:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
13201:             my @okusers;
13202:             foreach my $user (@users) {
13203:                 my ($uname,$udom) = split(/:/,$user);
13204:                 if (($udom =~ /^$match_domain$/) &&   
13205:                     (&Apache::lonnet::domain($udom)) &&
13206:                     ($uname =~ /^$match_username$/)) {
13207:                     if (!grep(/^\Q$user\E/,@okusers)) {
13208:                         push(@okusers,$user);
13209:                     }
13210:                 }
13211:             }
13212:             if (@okusers > 0) {
13213:                  @okusers = sort(@okusers);
13214:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
13215:             }
13216:         }
13217:         if (defined($env{'form.'.$type.'_machines'})) {
13218:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
13219:             my @okmachines;
13220:             foreach my $ip (@machines) {
13221:                 my @parts = split(/\./,$ip);
13222:                 next if (@parts < 4);
13223:                 my $badip = 0;
13224:                 for (my $i=0; $i<4; $i++) {
13225:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
13226:                         $badip = 1;
13227:                         last;
13228:                     }
13229:                 }
13230:                 if (!$badip) {
13231:                     push(@okmachines,$ip);     
13232:                 }
13233:             }
13234:             @okmachines = sort(@okmachines);
13235:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
13236:         }
13237:     }
13238:     my %serverstatushash =  (
13239:                                 serverstatuses => \%newserverstatus,
13240:                             );
13241:     foreach my $type (@pages) {
13242:         foreach my $setting ('namedusers','machines') {
13243:             my (@current,@new);
13244:             if (ref($currserverstatus{$type}) eq 'HASH') {
13245:                 if ($currserverstatus{$type}{$setting} ne '') { 
13246:                     @current = split(/,/,$currserverstatus{$type}{$setting});
13247:                 }
13248:             }
13249:             if ($newserverstatus{$type}{$setting} ne '') {
13250:                 @new = split(/,/,$newserverstatus{$type}{$setting});
13251:             }
13252:             if (@current > 0) {
13253:                 if (@new > 0) {
13254:                     foreach my $item (@current) {
13255:                         if (!grep(/^\Q$item\E$/,@new)) {
13256:                             $changes{$type}{$setting} = 1;
13257:                             last;
13258:                         }
13259:                     }
13260:                     foreach my $item (@new) {
13261:                         if (!grep(/^\Q$item\E$/,@current)) {
13262:                             $changes{$type}{$setting} = 1;
13263:                             last;
13264:                         }
13265:                     }
13266:                 } else {
13267:                     $changes{$type}{$setting} = 1;
13268:                 }
13269:             } elsif (@new > 0) {
13270:                 $changes{$type}{$setting} = 1;
13271:             }
13272:         }
13273:     }
13274:     if (keys(%changes) > 0) {
13275:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
13276:         my $putresult = &Apache::lonnet::put_dom('configuration',
13277:                                                  \%serverstatushash,$dom);
13278:         if ($putresult eq 'ok') {
13279:             $resulttext .= &mt('Changes made:').'<ul>';
13280:             foreach my $type (@pages) {
13281:                 if (ref($changes{$type}) eq 'HASH') {
13282:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
13283:                     if ($changes{$type}{'namedusers'}) {
13284:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
13285:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
13286:                         } else {
13287:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
13288:                         }
13289:                     }
13290:                     if ($changes{$type}{'machines'}) {
13291:                         if ($newserverstatus{$type}{'machines'} eq '') {
13292:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
13293:                         } else {
13294:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
13295:                         }
13296: 
13297:                     }
13298:                     $resulttext .= '</ul></li>';
13299:                 }
13300:             }
13301:             $resulttext .= '</ul>';
13302:         } else {
13303:             $resulttext = '<span class="LC_error">'.
13304:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
13305: 
13306:         }
13307:     } else {
13308:         $resulttext = &mt('No changes made to access to server status pages');
13309:     }
13310:     return $resulttext;
13311: }
13312: 
13313: sub modify_helpsettings {
13314:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
13315:     my ($resulttext,$errors,%changes,%helphash);
13316:     my %defaultchecked = ('submitbugs' => 'on');
13317:     my @offon = ('off','on');
13318:     my @toggles = ('submitbugs');
13319:     my %current = ('submitbugs' => '',
13320:                    'adhoc'      => {},
13321:                   );
13322:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
13323:         %current = %{$domconfig{'helpsettings'}};
13324:     }
13325:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13326:     foreach my $item (@toggles) {
13327:         if ($defaultchecked{$item} eq 'on') { 
13328:             if ($current{$item} eq '') {
13329:                 if ($env{'form.'.$item} eq '0') {
13330:                     $changes{$item} = 1;
13331:                 }
13332:             } elsif ($current{$item} ne $env{'form.'.$item}) {
13333:                 $changes{$item} = 1;
13334:             }
13335:         } elsif ($defaultchecked{$item} eq 'off') {
13336:             if ($current{$item} eq '') {
13337:                 if ($env{'form.'.$item} eq '1') {
13338:                     $changes{$item} = 1;
13339:                 }
13340:             } elsif ($current{$item} ne $env{'form.'.$item}) {
13341:                 $changes{$item} = 1;
13342:             }
13343:         }
13344:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
13345:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
13346:         }
13347:     }
13348:     my $maxnum = $env{'form.helproles_maxnum'};
13349:     my $confname = $dom.'-domainconfig';
13350:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
13351:     my (@allpos,%newsettings,%changedprivs,$newrole);
13352:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
13353:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
13354:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
13355:     my %lt = &Apache::lonlocal::texthash(
13356:                     s      => 'system',
13357:                     d      => 'domain',
13358:                     order  => 'Display order',
13359:                     access => 'Role usage',
13360:                     all    => 'All with domain helpdesk or helpdesk assistant role',
13361:                     dh     => 'All with domain helpdesk role',
13362:                     da     => 'All with domain helpdesk assistant role',
13363:                     none   => 'None',
13364:                     status => 'Determined based on institutional status',
13365:                     inc    => 'Include all, but exclude specific personnel',
13366:                     exc    => 'Exclude all, but include specific personnel',
13367:     );
13368:     for (my $num=0; $num<=$maxnum; $num++) {
13369:         my ($prefix,$identifier,$rolename,%curr);
13370:         if ($num == $maxnum) {
13371:             next unless ($env{'form.newcusthelp'} == $maxnum);
13372:             $identifier = 'custhelp'.$num;
13373:             $prefix = 'helproles_'.$num;
13374:             $rolename = $env{'form.custhelpname'.$num};
13375:             $rolename=~s/[^A-Za-z0-9]//gs;
13376:             next if ($rolename eq '');
13377:             next if (exists($existing{'rolesdef_'.$rolename}));
13378:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
13379:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
13380:                                                      $newprivs{'c'},$confname,$dom);
13381:             if ($result ne 'ok') {
13382:                 $errors .= '<li><span class="LC_error">'.
13383:                            &mt('An error occurred storing the new custom role: [_1]',
13384:                            $result).'</span></li>';
13385:                 next;
13386:             } else {
13387:                 $changedprivs{$rolename} = \%newprivs;
13388:                 $newrole = $rolename;
13389:             }
13390:         } else {
13391:             $prefix = 'helproles_'.$num;
13392:             $rolename = $env{'form.'.$prefix};
13393:             next if ($rolename eq '');
13394:             next unless (exists($existing{'rolesdef_'.$rolename}));
13395:             $identifier = 'custhelp'.$num;
13396:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
13397:             my %currprivs;
13398:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
13399:                 split(/\_/,$existing{'rolesdef_'.$rolename});
13400:             foreach my $level ('c','d','s') {
13401:                 if ($newprivs{$level} ne $currprivs{$level}) {
13402:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
13403:                                                              $newprivs{'c'},$confname,$dom);
13404:                     if ($result ne 'ok') {
13405:                         $errors .= '<li><span class="LC_error">'.
13406:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
13407:                                        $rolename,$result).'</span></li>';
13408:                     } else {
13409:                         $changedprivs{$rolename} = \%newprivs;
13410:                     }
13411:                     last;
13412:                 }
13413:             }
13414:             if (ref($current{'adhoc'}) eq 'HASH') {
13415:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
13416:                     %curr = %{$current{'adhoc'}{$rolename}};
13417:                 }
13418:             }
13419:         }
13420:         my $newpos = $env{'form.'.$prefix.'_pos'};
13421:         $newpos =~ s/\D+//g;
13422:         $allpos[$newpos] = $rolename;
13423:         my $newdesc = $env{'form.'.$prefix.'_desc'};
13424:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
13425:         if ($curr{'desc'}) {
13426:             if ($curr{'desc'} ne $newdesc) {
13427:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
13428:                 $newsettings{$rolename}{'desc'} = $newdesc;
13429:             }
13430:         } elsif ($newdesc ne '') {
13431:             $changes{'customrole'}{$rolename}{'desc'} = 1;
13432:             $newsettings{$rolename}{'desc'} = $newdesc;
13433:         }
13434:         my $access = $env{'form.'.$prefix.'_access'};
13435:         if (grep(/^\Q$access\E$/,@accesstypes)) {
13436:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
13437:             if ($access eq 'status') {
13438:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
13439:                 if (scalar(@statuses) == 0) {
13440:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
13441:                 } else {
13442:                     my (@shownstatus,$numtypes);
13443:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
13444:                     if (ref($types) eq 'ARRAY') {
13445:                         $numtypes = scalar(@{$types});
13446:                         foreach my $type (sort(@statuses)) {
13447:                             if ($type eq 'default') {
13448:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
13449:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
13450:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
13451:                                 push(@shownstatus,$usertypes->{$type});
13452:                             }
13453:                         }
13454:                     }
13455:                     if (grep(/^default$/,@statuses)) {
13456:                         push(@shownstatus,$othertitle);
13457:                     }
13458:                     if (scalar(@shownstatus) == 1+$numtypes) {
13459:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
13460:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
13461:                     } else {
13462:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
13463:                         if (ref($curr{'status'}) eq 'ARRAY') {
13464:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
13465:                             if (@diffs) {
13466:                                 $changes{'customrole'}{$rolename}{$access} = 1;
13467:                             }
13468:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
13469:                             $changes{'customrole'}{$rolename}{$access} = 1;
13470:                         }
13471:                     }
13472:                 }
13473:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
13474:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
13475:                 my @newspecstaff;
13476:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
13477:                 foreach my $person (sort(@personnel)) {
13478:                     if ($domhelpdesk{$person}) {
13479:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
13480:                     }
13481:                 }
13482:                 if (ref($curr{$access}) eq 'ARRAY') {
13483:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
13484:                     if (@diffs) {
13485:                         $changes{'customrole'}{$rolename}{$access} = 1;
13486:                     }
13487:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
13488:                     $changes{'customrole'}{$rolename}{$access} = 1;
13489:                 }
13490:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
13491:                     my ($uname,$udom) = split(/:/,$person);
13492:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
13493:                 }
13494:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
13495:             }
13496:         } else {
13497:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
13498:         }
13499:         unless ($curr{'access'} eq $access) {
13500:             $changes{'customrole'}{$rolename}{'access'} = 1;
13501:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
13502:         }
13503:     }
13504:     if (@allpos > 0) {
13505:         my $idx = 0;
13506:         foreach my $rolename (@allpos) {
13507:             if ($rolename ne '') {
13508:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
13509:                 if (ref($current{'adhoc'}) eq 'HASH') {
13510:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
13511:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
13512:                             $changes{'customrole'}{$rolename}{'order'} = 1;
13513:                             $newsettings{$rolename}{'order'} = $idx+1;
13514:                         }
13515:                     }
13516:                 }
13517:                 $idx ++;
13518:             }
13519:         }
13520:     }
13521:     my $putresult;
13522:     if (keys(%changes) > 0) {
13523:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
13524:         if ($putresult eq 'ok') {
13525:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
13526:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
13527:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
13528:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
13529:                 }
13530:             }
13531:             my $cachetime = 24*60*60;
13532:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13533:             if (ref($lastactref) eq 'HASH') {
13534:                 $lastactref->{'domdefaults'} = 1;
13535:             }
13536:         } else {
13537:             $errors .= '<li><span class="LC_error">'.
13538:                        &mt('An error occurred storing the settings: [_1]',
13539:                            $putresult).'</span></li>';
13540:         }
13541:     }
13542:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
13543:         $resulttext = &mt('Changes made:').'<ul>';
13544:         my (%shownprivs,@levelorder);
13545:         @levelorder = ('c','d','s');
13546:         if ((keys(%changes)) && ($putresult eq 'ok')) {
13547:             foreach my $item (sort(keys(%changes))) {
13548:                 if ($item eq 'submitbugs') {
13549:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
13550:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
13551:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
13552:                 } elsif ($item eq 'customrole') {
13553:                     if (ref($changes{'customrole'}) eq 'HASH') {
13554:                         my @keyorder = ('order','desc','access','status','exc','inc');
13555:                         my %keytext = &Apache::lonlocal::texthash(
13556:                                                                    order  => 'Order',
13557:                                                                    desc   => 'Role description',
13558:                                                                    access => 'Role usage',
13559:                                                                    status => 'Allowed institutional types',
13560:                                                                    exc    => 'Allowed personnel',
13561:                                                                    inc    => 'Disallowed personnel',
13562:                         );
13563:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
13564:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
13565:                                 if ($role eq $newrole) {
13566:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
13567:                                                               $role).'<ul>';
13568:                                 } else {
13569:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
13570:                                                               $role).'<ul>';
13571:                                 }
13572:                                 foreach my $key (@keyorder) {
13573:                                     if ($changes{'customrole'}{$role}{$key}) {
13574:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
13575:                                                                   $keytext{$key},$newsettings{$role}{$key}).
13576:                                                        '</li>';
13577:                                     }
13578:                                 }
13579:                                 if (ref($changedprivs{$role}) eq 'HASH') {
13580:                                     $shownprivs{$role} = 1;
13581:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
13582:                                     foreach my $level (@levelorder) {
13583:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
13584:                                             next if ($item eq '');
13585:                                             my ($priv) = split(/\&/,$item,2);
13586:                                             if (&Apache::lonnet::plaintext($priv)) {
13587:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
13588:                                                 unless ($level eq 'c') {
13589:                                                     $resulttext .= ' ('.$lt{$level}.')';
13590:                                                 }
13591:                                                 $resulttext .= '</li>';
13592:                                             }
13593:                                         }
13594:                                     }
13595:                                     $resulttext .= '</ul>';
13596:                                 }
13597:                                 $resulttext .= '</ul></li>';
13598:                             }
13599:                         }
13600:                     }
13601:                 }
13602:             }
13603:         }
13604:         if (keys(%changedprivs)) {
13605:             foreach my $role (sort(keys(%changedprivs))) {
13606:                 unless ($shownprivs{$role}) {
13607:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
13608:                                               $role).'<ul>'.
13609:                                    '<li>'.&mt('Privileges set to :').'<ul>';
13610:                     foreach my $level (@levelorder) {
13611:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
13612:                             next if ($item eq '');
13613:                             my ($priv) = split(/\&/,$item,2);
13614:                             if (&Apache::lonnet::plaintext($priv)) {
13615:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
13616:                                 unless ($level eq 'c') {
13617:                                     $resulttext .= ' ('.$lt{$level}.')';
13618:                                 }
13619:                                 $resulttext .= '</li>';
13620:                             }
13621:                         }
13622:                     }
13623:                     $resulttext .= '</ul></li></ul></li>';
13624:                 }
13625:             }
13626:         }
13627:         $resulttext .= '</ul>';
13628:     } else {
13629:         $resulttext = &mt('No changes made to help settings');
13630:     }
13631:     if ($errors) {
13632:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
13633:                                     $errors.'</ul>';
13634:     }
13635:     return $resulttext;
13636: }
13637: 
13638: sub modify_coursedefaults {
13639:     my ($dom,$lastactref,%domconfig) = @_;
13640:     my ($resulttext,$errors,%changes,%defaultshash);
13641:     my %defaultchecked = (
13642:                            'canuse_pdfforms' => 'off',
13643:                            'uselcmath'       => 'on',
13644:                            'usejsme'         => 'on'
13645:                          );
13646:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme');
13647:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
13648:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
13649:                    'mysqltables_official','mysqltables_unofficial','mysqltables_community',
13650:                    'mysqltables_textbook','mysqltables_placement');
13651:     my @types = ('official','unofficial','community','textbook','placement');
13652:     my %staticdefaults = (
13653:                            anonsurvey_threshold => 10,
13654:                            uploadquota          => 500,
13655:                            postsubmit           => 60,
13656:                            mysqltables          => 172800,
13657:                          );
13658: 
13659:     $defaultshash{'coursedefaults'} = {};
13660: 
13661:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
13662:         if ($domconfig{'coursedefaults'} eq '') {
13663:             $domconfig{'coursedefaults'} = {};
13664:         }
13665:     }
13666: 
13667:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
13668:         foreach my $item (@toggles) {
13669:             if ($defaultchecked{$item} eq 'on') {
13670:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
13671:                     ($env{'form.'.$item} eq '0')) {
13672:                     $changes{$item} = 1;
13673:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
13674:                     $changes{$item} = 1;
13675:                 }
13676:             } elsif ($defaultchecked{$item} eq 'off') {
13677:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
13678:                     ($env{'form.'.$item} eq '1')) {
13679:                     $changes{$item} = 1;
13680:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
13681:                     $changes{$item} = 1;
13682:                 }
13683:             }
13684:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
13685:         }
13686:         foreach my $item (@numbers) {
13687:             my ($currdef,$newdef);
13688:             $newdef = $env{'form.'.$item};
13689:             if ($item eq 'anonsurvey_threshold') {
13690:                 $currdef = $domconfig{'coursedefaults'}{$item};
13691:                 $newdef =~ s/\D//g;
13692:                 if ($newdef eq '' || $newdef < 1) {
13693:                     $newdef = 1;
13694:                 }
13695:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
13696:             } else {
13697:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
13698:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
13699:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
13700:                 }
13701:                 $newdef =~ s/[^\w.\-]//g;
13702:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
13703:             }
13704:             if ($currdef ne $newdef) {
13705:                 my $staticdef;
13706:                 if ($item eq 'anonsurvey_threshold') {
13707:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
13708:                         $changes{$item} = 1;
13709:                     }
13710:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
13711:                     my $setting = $1;
13712:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
13713:                         $changes{$setting} = 1;
13714:                     }
13715:                 }
13716:             }
13717:         }
13718:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
13719:         my @currclonecode;
13720:         if (ref($currclone) eq 'HASH') {
13721:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
13722:                 @currclonecode = @{$currclone->{'instcode'}};
13723:             }
13724:         }
13725:         my $newclone;
13726:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
13727:             $newclone = $env{'form.canclone'};
13728:         }
13729:         if ($newclone eq 'instcode') {
13730:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
13731:             my (%codedefaults,@code_order,@clonecode);
13732:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
13733:                                                     \@code_order);
13734:             foreach my $item (@code_order) {
13735:                 if (grep(/^\Q$item\E$/,@newcodes)) {
13736:                     push(@clonecode,$item);
13737:                 }
13738:             }
13739:             if (@clonecode) {
13740:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
13741:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
13742:                 if (@diffs) {
13743:                     $changes{'canclone'} = 1;
13744:                 }
13745:             } else {
13746:                 $newclone eq '';
13747:             }
13748:         } elsif ($newclone ne '') {
13749:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
13750:         }
13751:         if ($newclone ne $currclone) {
13752:             $changes{'canclone'} = 1;
13753:         }
13754:         my %credits;
13755:         foreach my $type (@types) {
13756:             unless ($type eq 'community') {
13757:                 $credits{$type} = $env{'form.'.$type.'_credits'};
13758:                 $credits{$type} =~ s/[^\d.]+//g;
13759:             }
13760:         }
13761:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
13762:             ($env{'form.coursecredits'} eq '1')) {
13763:             $changes{'coursecredits'} = 1;
13764:             foreach my $type (keys(%credits)) {
13765:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
13766:             }
13767:         } else {
13768:             if ($env{'form.coursecredits'} eq '1') {
13769:                 foreach my $type (@types) {
13770:                     unless ($type eq 'community') {
13771:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
13772:                             $changes{'coursecredits'} = 1;
13773:                         }
13774:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
13775:                     }
13776:                 }
13777:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13778:                 foreach my $type (@types) {
13779:                     unless ($type eq 'community') {
13780:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
13781:                             $changes{'coursecredits'} = 1;
13782:                             last;
13783:                         }
13784:                     }
13785:                 }
13786:             }
13787:         }
13788:         if ($env{'form.postsubmit'} eq '1') {
13789:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
13790:             my %currtimeout;
13791:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13792:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
13793:                     $changes{'postsubmit'} = 1;
13794:                 }
13795:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13796:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
13797:                 }
13798:             } else {
13799:                 $changes{'postsubmit'} = 1;
13800:             }
13801:             foreach my $type (@types) {
13802:                 my $timeout = $env{'form.'.$type.'_timeout'};
13803:                 $timeout =~ s/\D//g;
13804:                 if ($timeout == $staticdefaults{'postsubmit'}) {
13805:                     $timeout = '';
13806:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
13807:                     $timeout = '0';
13808:                 }
13809:                 unless ($timeout eq '') {
13810:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
13811:                 }
13812:                 if (exists($currtimeout{$type})) {
13813:                     if ($timeout ne $currtimeout{$type}) {
13814:                         $changes{'postsubmit'} = 1;
13815:                     }
13816:                 } elsif ($timeout ne '') {
13817:                     $changes{'postsubmit'} = 1;
13818:                 }
13819:             }
13820:         } else {
13821:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
13822:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13823:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
13824:                     $changes{'postsubmit'} = 1;
13825:                 }
13826:             } else {
13827:                 $changes{'postsubmit'} = 1;
13828:             }
13829:         }
13830:     }
13831:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13832:                                              $dom);
13833:     if ($putresult eq 'ok') {
13834:         if (keys(%changes) > 0) {
13835:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13836:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
13837:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
13838:                 ($changes{'canclone'}) || ($changes{'mysqltables'})) {
13839:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme') { 
13840:                     if ($changes{$item}) {
13841:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
13842:                     }
13843:                 }
13844:                 if ($changes{'coursecredits'}) {
13845:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13846:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
13847:                             $domdefaults{$type.'credits'} =
13848:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
13849:                         }
13850:                     }
13851:                 }
13852:                 if ($changes{'postsubmit'}) {
13853:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13854:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
13855:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13856:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
13857:                                 $domdefaults{$type.'postsubtimeout'} =
13858:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
13859:                             }
13860:                         }
13861:                     }
13862:                 }
13863:                 if ($changes{'uploadquota'}) {
13864:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
13865:                         foreach my $type (@types) {
13866:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
13867:                         }
13868:                     }
13869:                 }
13870:                 if ($changes{'canclone'}) {
13871:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
13872:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
13873:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
13874:                             if (@clonecodes) {
13875:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
13876:                             }
13877:                         }
13878:                     } else {
13879:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
13880:                     }
13881:                 }
13882:                 my $cachetime = 24*60*60;
13883:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13884:                 if (ref($lastactref) eq 'HASH') {
13885:                     $lastactref->{'domdefaults'} = 1;
13886:                 }
13887:             }
13888:             $resulttext = &mt('Changes made:').'<ul>';
13889:             foreach my $item (sort(keys(%changes))) {
13890:                 if ($item eq 'canuse_pdfforms') {
13891:                     if ($env{'form.'.$item} eq '1') {
13892:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
13893:                     } else {
13894:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
13895:                     }
13896:                 } elsif ($item eq 'uselcmath') {
13897:                     if ($env{'form.'.$item} eq '1') {
13898:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
13899:                     } else {
13900:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
13901:                     }
13902:                 } elsif ($item eq 'usejsme') {
13903:                     if ($env{'form.'.$item} eq '1') {
13904:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
13905:                     } else {
13906:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
13907:                     }
13908:                 } elsif ($item eq 'anonsurvey_threshold') {
13909:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
13910:                 } elsif ($item eq 'uploadquota') {
13911:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
13912:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
13913:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
13914:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
13915:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
13916:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
13917:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
13918:                                        '</ul>'.
13919:                                        '</li>';
13920:                     } else {
13921:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
13922:                     }
13923:                 } elsif ($item eq 'mysqltables') {
13924:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
13925:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
13926:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
13927:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
13928:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
13929:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
13930:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
13931:                                        '</ul>'.
13932:                                        '</li>';
13933:                     } else {
13934:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
13935:                     }
13936:                 } elsif ($item eq 'postsubmit') {
13937:                     if ($domdefaults{'postsubmit'} eq 'off') {
13938:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
13939:                     } else {
13940:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
13941:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13942:                             $resulttext .= &mt('durations:').'<ul>';
13943:                             foreach my $type (@types) {
13944:                                 $resulttext .= '<li>';
13945:                                 my $timeout;
13946:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13947:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
13948:                                 }
13949:                                 my $display;
13950:                                 if ($timeout eq '0') {
13951:                                     $display = &mt('unlimited');
13952:                                 } elsif ($timeout eq '') {
13953:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
13954:                                 } else {
13955:                                     $display = &mt('[quant,_1,second]',$timeout);
13956:                                 }
13957:                                 if ($type eq 'community') {
13958:                                     $resulttext .= &mt('Communities');
13959:                                 } elsif ($type eq 'official') {
13960:                                     $resulttext .= &mt('Official courses');
13961:                                 } elsif ($type eq 'unofficial') {
13962:                                     $resulttext .= &mt('Unofficial courses');
13963:                                 } elsif ($type eq 'textbook') {
13964:                                     $resulttext .= &mt('Textbook courses');
13965:                                 } elsif ($type eq 'placement') {
13966:                                     $resulttext .= &mt('Placement tests');
13967:                                 }
13968:                                 $resulttext .= ' -- '.$display.'</li>';
13969:                             }
13970:                             $resulttext .= '</ul>';
13971:                         }
13972:                         $resulttext .= '</li>';
13973:                     }
13974:                 } elsif ($item eq 'coursecredits') {
13975:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13976:                         if (($domdefaults{'officialcredits'} eq '') &&
13977:                             ($domdefaults{'unofficialcredits'} eq '') &&
13978:                             ($domdefaults{'textbookcredits'} eq '')) {
13979:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
13980:                         } else {
13981:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
13982:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
13983:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
13984:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
13985:                                            '</ul>'.
13986:                                            '</li>';
13987:                         }
13988:                     } else {
13989:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
13990:                     }
13991:                 } elsif ($item eq 'canclone') {
13992:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
13993:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
13994:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
13995:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
13996:                         }
13997:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
13998:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
13999:                     } else {
14000:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
14001:                     }
14002:                 }
14003:             }
14004:             $resulttext .= '</ul>';
14005:         } else {
14006:             $resulttext = &mt('No changes made to course defaults');
14007:         }
14008:     } else {
14009:         $resulttext = '<span class="LC_error">'.
14010:             &mt('An error occurred: [_1]',$putresult).'</span>';
14011:     }
14012:     return $resulttext;
14013: }
14014: 
14015: sub modify_selfenrollment {
14016:     my ($dom,$lastactref,%domconfig) = @_;
14017:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
14018:     my @types = ('official','unofficial','community','textbook','placement');
14019:     my %titles = &tool_titles();
14020:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
14021:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
14022:     $ordered{'default'} = ['types','registered','approval','limit'];
14023: 
14024:     my (%roles,%shown,%toplevel);
14025:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
14026: 
14027:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
14028:         if ($domconfig{'selfenrollment'} eq '') {
14029:             $domconfig{'selfenrollment'} = {};
14030:         }
14031:     }
14032:     %toplevel = (
14033:                   admin      => 'Configuration Rights',
14034:                   default    => 'Default settings',
14035:                   validation => 'Validation of self-enrollment requests',
14036:                 );
14037:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
14038: 
14039:     if (ref($ordered{'admin'}) eq 'ARRAY') {
14040:         foreach my $item (@{$ordered{'admin'}}) {
14041:             foreach my $type (@types) {
14042:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
14043:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
14044:                 } else {
14045:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
14046:                 }
14047:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
14048:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
14049:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
14050:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
14051:                             push(@{$changes{'admin'}{$type}},$item);
14052:                         }
14053:                     } else {
14054:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
14055:                             push(@{$changes{'admin'}{$type}},$item);
14056:                         }
14057:                     }
14058:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
14059:                     push(@{$changes{'admin'}{$type}},$item);
14060:                 }
14061:             }
14062:         }
14063:     }
14064: 
14065:     foreach my $item (@{$ordered{'default'}}) {
14066:         foreach my $type (@types) {
14067:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
14068:             if ($item eq 'types') {
14069:                 unless (($value eq 'all') || ($value eq 'dom')) {
14070:                     $value = '';
14071:                 }
14072:             } elsif ($item eq 'registered') {
14073:                 unless ($value eq '1') {
14074:                     $value = 0;
14075:                 }
14076:             } elsif ($item eq 'approval') {
14077:                 unless ($value =~ /^[012]$/) {
14078:                     $value = 0;
14079:                 }
14080:             } else {
14081:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
14082:                     $value = 'none';
14083:                 }
14084:             }
14085:             $selfenrollhash{'default'}{$type}{$item} = $value;
14086:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
14087:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
14088:                     if ($selfenrollhash{'default'}{$type}{$item} ne
14089:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
14090:                          push(@{$changes{'default'}{$type}},$item);
14091:                     }
14092:                 } else {
14093:                     push(@{$changes{'default'}{$type}},$item);
14094:                 }
14095:             } else {
14096:                 push(@{$changes{'default'}{$type}},$item);
14097:             }
14098:             if ($item eq 'limit') {
14099:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
14100:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
14101:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
14102:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
14103:                     }
14104:                 } else {
14105:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
14106:                 }
14107:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
14108:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
14109:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
14110:                          push(@{$changes{'default'}{$type}},'cap');
14111:                     }
14112:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
14113:                     push(@{$changes{'default'}{$type}},'cap');
14114:                 }
14115:             }
14116:         }
14117:     }
14118: 
14119:     foreach my $item (@{$itemsref}) {
14120:         if ($item eq 'fields') {
14121:             my @changed;
14122:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
14123:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
14124:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
14125:             }
14126:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
14127:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
14128:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
14129:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
14130:                 } else {
14131:                     @changed = @{$selfenrollhash{'validation'}{$item}};
14132:                 }
14133:             } else {
14134:                 @changed = @{$selfenrollhash{'validation'}{$item}};
14135:             }
14136:             if (@changed) {
14137:                 if ($selfenrollhash{'validation'}{$item}) { 
14138:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
14139:                 } else {
14140:                     $changes{'validation'}{$item} = &mt('None');
14141:                 }
14142:             }
14143:         } else {
14144:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
14145:             if ($item eq 'markup') {
14146:                if ($env{'form.selfenroll_validation_'.$item}) {
14147:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
14148:                }
14149:             }
14150:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
14151:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
14152:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
14153:                 }
14154:             }
14155:         }
14156:     }
14157: 
14158:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
14159:                                              $dom);
14160:     if ($putresult eq 'ok') {
14161:         if (keys(%changes) > 0) {
14162:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14163:             $resulttext = &mt('Changes made:').'<ul>';
14164:             foreach my $key ('admin','default','validation') {
14165:                 if (ref($changes{$key}) eq 'HASH') {
14166:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
14167:                     if ($key eq 'validation') {
14168:                         foreach my $item (@{$itemsref}) {
14169:                             if (exists($changes{$key}{$item})) {
14170:                                 if ($item eq 'markup') {
14171:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
14172:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
14173:                                 } else {  
14174:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
14175:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
14176:                                 }
14177:                             }
14178:                         }
14179:                     } else {
14180:                         foreach my $type (@types) {
14181:                             if ($type eq 'community') {
14182:                                 $roles{'1'} = &mt('Community personnel');
14183:                             } else {
14184:                                 $roles{'1'} = &mt('Course personnel');
14185:                             }
14186:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
14187:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
14188:                                     if ($key eq 'admin') {
14189:                                         my @mgrdc = ();
14190:                                         if (ref($ordered{$key}) eq 'ARRAY') {
14191:                                             foreach my $item (@{$ordered{'admin'}}) {
14192:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
14193:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
14194:                                                         push(@mgrdc,$item);
14195:                                                     }
14196:                                                 }
14197:                                             }
14198:                                             if (@mgrdc) {
14199:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
14200:                                             } else {
14201:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
14202:                                             }
14203:                                         }
14204:                                     } else {
14205:                                         if (ref($ordered{$key}) eq 'ARRAY') {
14206:                                             foreach my $item (@{$ordered{$key}}) {
14207:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
14208:                                                     $domdefaults{$type.'selfenroll'.$item} =
14209:                                                         $selfenrollhash{$key}{$type}{$item};
14210:                                                 }
14211:                                             }
14212:                                         }
14213:                                     }
14214:                                 }
14215:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
14216:                                 foreach my $item (@{$ordered{$key}}) {
14217:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
14218:                                         $resulttext .= '<li>';
14219:                                         if ($key eq 'admin') {
14220:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
14221:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
14222:                                         } else {
14223:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
14224:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
14225:                                         }
14226:                                         $resulttext .= '</li>';
14227:                                     }
14228:                                 }
14229:                                 $resulttext .= '</ul></li>';
14230:                             }
14231:                         }
14232:                         $resulttext .= '</ul></li>'; 
14233:                     }
14234:                 }
14235:             }
14236:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
14237:                 my $cachetime = 24*60*60;
14238:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14239:                 if (ref($lastactref) eq 'HASH') {
14240:                     $lastactref->{'domdefaults'} = 1;
14241:                 }
14242:             }
14243:             $resulttext .= '</ul>';
14244:         } else {
14245:             $resulttext = &mt('No changes made to self-enrollment settings');
14246:         }
14247:     } else {
14248:         $resulttext = '<span class="LC_error">'.
14249:             &mt('An error occurred: [_1]',$putresult).'</span>';
14250:     }
14251:     return $resulttext;
14252: }
14253: 
14254: sub modify_usersessions {
14255:     my ($dom,$lastactref,%domconfig) = @_;
14256:     my @hostingtypes = ('version','excludedomain','includedomain');
14257:     my @offloadtypes = ('primary','default');
14258:     my %types = (
14259:                   remote => \@hostingtypes,
14260:                   hosted => \@hostingtypes,
14261:                   spares => \@offloadtypes,
14262:                 );
14263:     my @prefixes = ('remote','hosted','spares');
14264:     my @lcversions = &Apache::lonnet::all_loncaparevs();
14265:     my (%by_ip,%by_location,@intdoms,@instdoms);
14266:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
14267:     my @locations = sort(keys(%by_location));
14268:     my (%defaultshash,%changes);
14269:     foreach my $prefix (@prefixes) {
14270:         $defaultshash{'usersessions'}{$prefix} = {};
14271:     }
14272:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14273:     my $resulttext;
14274:     my %iphost = &Apache::lonnet::get_iphost();
14275:     foreach my $prefix (@prefixes) {
14276:         next if ($prefix eq 'spares');
14277:         foreach my $type (@{$types{$prefix}}) {
14278:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
14279:             if ($type eq 'version') {
14280:                 my $value = $env{'form.'.$prefix.'_'.$type};
14281:                 my $okvalue;
14282:                 if ($value ne '') {
14283:                     if (grep(/^\Q$value\E$/,@lcversions)) {
14284:                         $okvalue = $value;
14285:                     }
14286:                 }
14287:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
14288:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
14289:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
14290:                             if ($inuse == 0) {
14291:                                 $changes{$prefix}{$type} = 1;
14292:                             } else {
14293:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
14294:                                     $changes{$prefix}{$type} = 1;
14295:                                 }
14296:                                 if ($okvalue ne '') {
14297:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
14298:                                 } 
14299:                             }
14300:                         } else {
14301:                             if (($inuse == 1) && ($okvalue ne '')) {
14302:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
14303:                                 $changes{$prefix}{$type} = 1;
14304:                             }
14305:                         }
14306:                     } else {
14307:                         if (($inuse == 1) && ($okvalue ne '')) {
14308:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
14309:                             $changes{$prefix}{$type} = 1;
14310:                         }
14311:                     }
14312:                 } else {
14313:                     if (($inuse == 1) && ($okvalue ne '')) {
14314:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
14315:                         $changes{$prefix}{$type} = 1;
14316:                     }
14317:                 }
14318:             } else {
14319:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
14320:                 my @okvals;
14321:                 foreach my $val (@vals) {
14322:                     if ($val =~ /:/) {
14323:                         my @items = split(/:/,$val);
14324:                         foreach my $item (@items) {
14325:                             if (ref($by_location{$item}) eq 'ARRAY') {
14326:                                 push(@okvals,$item);
14327:                             }
14328:                         }
14329:                     } else {
14330:                         if (ref($by_location{$val}) eq 'ARRAY') {
14331:                             push(@okvals,$val);
14332:                         }
14333:                     }
14334:                 }
14335:                 @okvals = sort(@okvals);
14336:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
14337:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
14338:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
14339:                             if ($inuse == 0) {
14340:                                 $changes{$prefix}{$type} = 1; 
14341:                             } else {
14342:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
14343:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
14344:                                 if (@changed > 0) {
14345:                                     $changes{$prefix}{$type} = 1;
14346:                                 }
14347:                             }
14348:                         } else {
14349:                             if ($inuse == 1) {
14350:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
14351:                                 $changes{$prefix}{$type} = 1;
14352:                             }
14353:                         } 
14354:                     } else {
14355:                         if ($inuse == 1) {
14356:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
14357:                             $changes{$prefix}{$type} = 1;
14358:                         }
14359:                     }
14360:                 } else {
14361:                     if ($inuse == 1) {
14362:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
14363:                         $changes{$prefix}{$type} = 1;
14364:                     }
14365:                 }
14366:             }
14367:         }
14368:     }
14369: 
14370:     my @alldoms = &Apache::lonnet::all_domains();
14371:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14372:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
14373:     my $savespares;
14374: 
14375:     foreach my $lonhost (sort(keys(%servers))) {
14376:         my $serverhomeID =
14377:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
14378:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
14379:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
14380:         my %spareschg;
14381:         foreach my $type (@{$types{'spares'}}) {
14382:             my @okspares;
14383:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
14384:             foreach my $server (@checked) {
14385:                 if (&Apache::lonnet::hostname($server) ne '') {
14386:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
14387:                         unless (grep(/^\Q$server\E$/,@okspares)) {
14388:                             push(@okspares,$server);
14389:                         }
14390:                     }
14391:                 }
14392:             }
14393:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
14394:             my $newspare;
14395:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
14396:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
14397:                     $newspare = $new;
14398:                 }
14399:             }
14400:             my @spares;
14401:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
14402:                 @spares = sort(@okspares,$newspare);
14403:             } else {
14404:                 @spares = sort(@okspares);
14405:             }
14406:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
14407:             if (ref($spareid{$lonhost}) eq 'HASH') {
14408:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
14409:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
14410:                     if (@diffs > 0) {
14411:                         $spareschg{$type} = 1;
14412:                     }
14413:                 }
14414:             }
14415:         }
14416:         if (keys(%spareschg) > 0) {
14417:             $changes{'spares'}{$lonhost} = \%spareschg;
14418:         }
14419:     }
14420:     $defaultshash{'usersessions'}{'offloadnow'} = {};
14421:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
14422:     my @okoffload;
14423:     if (@offloadnow) {
14424:         foreach my $server (@offloadnow) {
14425:             if (&Apache::lonnet::hostname($server) ne '') {
14426:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
14427:                     push(@okoffload,$server);
14428:                 }
14429:             }
14430:         }
14431:         if (@okoffload) {
14432:             foreach my $lonhost (@okoffload) {
14433:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
14434:             }
14435:         }
14436:     }
14437:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
14438:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
14439:             if (ref($changes{'spares'}) eq 'HASH') {
14440:                 if (keys(%{$changes{'spares'}}) > 0) {
14441:                     $savespares = 1;
14442:                 }
14443:             }
14444:         } else {
14445:             $savespares = 1;
14446:         }
14447:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
14448:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
14449:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
14450:                     $changes{'offloadnow'} = 1;
14451:                     last;
14452:                 }
14453:             }
14454:             unless ($changes{'offloadnow'}) {
14455:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
14456:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
14457:                         $changes{'offloadnow'} = 1;
14458:                         last;
14459:                     }
14460:                 }
14461:             }
14462:         } elsif (@okoffload) {
14463:             $changes{'offloadnow'} = 1;
14464:         }
14465:     } elsif (@okoffload) {
14466:         $changes{'offloadnow'} = 1;
14467:     }
14468:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
14469:     if ((keys(%changes) > 0) || ($savespares)) {
14470:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
14471:                                                  $dom);
14472:         if ($putresult eq 'ok') {
14473:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
14474:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
14475:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
14476:                 }
14477:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
14478:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
14479:                 }
14480:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
14481:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
14482:                 }
14483:             }
14484:             my $cachetime = 24*60*60;
14485:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14486:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
14487:             if (ref($lastactref) eq 'HASH') {
14488:                 $lastactref->{'domdefaults'} = 1;
14489:                 $lastactref->{'usersessions'} = 1;
14490:             }
14491:             if (keys(%changes) > 0) {
14492:                 my %lt = &usersession_titles();
14493:                 $resulttext = &mt('Changes made:').'<ul>';
14494:                 foreach my $prefix (@prefixes) {
14495:                     if (ref($changes{$prefix}) eq 'HASH') {
14496:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
14497:                         if ($prefix eq 'spares') {
14498:                             if (ref($changes{$prefix}) eq 'HASH') {
14499:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
14500:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
14501:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
14502:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
14503:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
14504:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
14505:                                         foreach my $type (@{$types{$prefix}}) {
14506:                                             if ($changes{$prefix}{$lonhost}{$type}) {
14507:                                                 my $offloadto = &mt('None');
14508:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
14509:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
14510:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
14511:                                                     }
14512:                                                 }
14513:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
14514:                                             }
14515:                                         }
14516:                                     }
14517:                                     $resulttext .= '</li>';
14518:                                 }
14519:                             }
14520:                         } else {
14521:                             foreach my $type (@{$types{$prefix}}) {
14522:                                 if (defined($changes{$prefix}{$type})) {
14523:                                     my $newvalue;
14524:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
14525:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
14526:                                             if ($type eq 'version') {
14527:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
14528:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
14529:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
14530:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
14531:                                                 }
14532:                                             }
14533:                                         }
14534:                                     }
14535:                                     if ($newvalue eq '') {
14536:                                         if ($type eq 'version') {
14537:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
14538:                                         } else {
14539:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
14540:                                         }
14541:                                     } else {
14542:                                         if ($type eq 'version') {
14543:                                             $newvalue .= ' '.&mt('(or later)'); 
14544:                                         }
14545:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
14546:                                     }
14547:                                 }
14548:                             }
14549:                         }
14550:                         $resulttext .= '</ul>';
14551:                     }
14552:                 }
14553:                 if ($changes{'offloadnow'}) {
14554:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
14555:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
14556:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
14557:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
14558:                                 $resulttext .= '<li>'.$lonhost.'</li>';
14559:                             }
14560:                             $resulttext .= '</ul>';
14561:                         } else {
14562:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
14563:                         }
14564:                     } else {
14565:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
14566:                     }
14567:                 }
14568:                 $resulttext .= '</ul>';
14569:             } else {
14570:                 $resulttext = $nochgmsg;
14571:             }
14572:         } else {
14573:             $resulttext = '<span class="LC_error">'.
14574:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14575:         }
14576:     } else {
14577:         $resulttext = $nochgmsg;
14578:     }
14579:     return $resulttext;
14580: }
14581: 
14582: sub modify_ssl {
14583:     my ($dom,$lastactref,%domconfig) = @_;
14584:     my (%by_ip,%by_location,@intdoms,@instdoms);
14585:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
14586:     my @locations = sort(keys(%by_location));
14587:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14588:     my (%defaultshash,%changes);
14589:     my $action = 'ssl';
14590:     my @prefixes = ('connto','connfrom','replication');
14591:     foreach my $prefix (@prefixes) {
14592:         $defaultshash{$action}{$prefix} = {};
14593:     }
14594:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14595:     my $resulttext;
14596:     my %iphost = &Apache::lonnet::get_iphost();
14597:     my @reptypes = ('certreq','nocertreq');
14598:     my @connecttypes = ('dom','intdom','other');
14599:     my %types = (
14600:                   connto      => \@connecttypes,
14601:                   connfrom    => \@connecttypes,
14602:                   replication => \@reptypes,
14603:                 );
14604:     foreach my $prefix (sort(keys(%types))) {
14605:         foreach my $type (@{$types{$prefix}}) {
14606:             if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
14607:                 my $value = 'yes';
14608:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
14609:                     $value = $env{'form.'.$prefix.'_'.$type};
14610:                 }
14611:                 if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
14612:                     if ($domconfig{$action}{$prefix}{$type} ne '') {
14613:                         if ($value ne $domconfig{$action}{$prefix}{$type}) {
14614:                             $changes{$prefix}{$type} = 1;
14615:                         }
14616:                         $defaultshash{$action}{$prefix}{$type} = $value;
14617:                     } else {
14618:                         $defaultshash{$action}{$prefix}{$type} = $value;
14619:                         $changes{$prefix}{$type} = 1;
14620:                     }
14621:                 } else {
14622:                     $defaultshash{$action}{$prefix}{$type} = $value;
14623:                     $changes{$prefix}{$type} = 1;
14624:                 }
14625:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
14626:                     delete($changes{$prefix}{$type});
14627:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
14628:                     delete($changes{$prefix}{$type});
14629:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
14630:                     delete($changes{$prefix}{$type});
14631:                 }
14632:             } elsif ($prefix eq 'replication') {
14633:                 if (@locations > 0) {
14634:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
14635:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
14636:                     my @okvals;
14637:                     foreach my $val (@vals) {
14638:                         if ($val =~ /:/) {
14639:                             my @items = split(/:/,$val);
14640:                             foreach my $item (@items) {
14641:                                 if (ref($by_location{$item}) eq 'ARRAY') {
14642:                                     push(@okvals,$item);
14643:                                 }
14644:                             }
14645:                         } else {
14646:                             if (ref($by_location{$val}) eq 'ARRAY') {
14647:                                 push(@okvals,$val);
14648:                             }
14649:                         }
14650:                     }
14651:                     @okvals = sort(@okvals);
14652:                     if (ref($domconfig{$action}) eq 'HASH') {
14653:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
14654:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
14655:                                 if ($inuse == 0) {
14656:                                     $changes{$prefix}{$type} = 1;
14657:                                 } else {
14658:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
14659:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
14660:                                     if (@changed > 0) {
14661:                                         $changes{$prefix}{$type} = 1;
14662:                                     }
14663:                                 }
14664:                             } else {
14665:                                 if ($inuse == 1) {
14666:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
14667:                                     $changes{$prefix}{$type} = 1;
14668:                                 }
14669:                             }
14670:                         } else {
14671:                             if ($inuse == 1) {
14672:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
14673:                                 $changes{$prefix}{$type} = 1;
14674:                             }
14675:                         }
14676:                     } else {
14677:                         if ($inuse == 1) {
14678:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
14679:                             $changes{$prefix}{$type} = 1;
14680:                         }
14681:                     }
14682:                 }
14683:             }
14684:         }
14685:     }
14686:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
14687:     if (keys(%changes) > 0) {
14688:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
14689:                                                  $dom);
14690:         if ($putresult eq 'ok') {
14691:             if (ref($defaultshash{$action}) eq 'HASH') {
14692:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
14693:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
14694:                 }
14695:                 if (ref($defaultshash{$action}{'connto'}) eq 'HASH') {
14696:                     $domdefaults{'connto'} = $domconfig{$action}{'connto'};
14697:                 }
14698:                 if (ref($defaultshash{$action}{'connfrom'}) eq 'HASH') {
14699:                     $domdefaults{'connfrom'} = $domconfig{$action}{'connfrom'};
14700:                 }
14701:             }
14702:             my $cachetime = 24*60*60;
14703:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14704:             if (ref($lastactref) eq 'HASH') {
14705:                 $lastactref->{'domdefaults'} = 1;
14706:             }
14707:             if (keys(%changes) > 0) {
14708:                 my %titles = &ssl_titles();
14709:                 $resulttext = &mt('Changes made:').'<ul>';
14710:                 foreach my $prefix (@prefixes) {
14711:                     if (ref($changes{$prefix}) eq 'HASH') {
14712:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
14713:                         foreach my $type (@{$types{$prefix}}) {
14714:                             if (defined($changes{$prefix}{$type})) {
14715:                                 my $newvalue;
14716:                                 if (ref($defaultshash{$action}) eq 'HASH') {
14717:                                     if (ref($defaultshash{$action}{$prefix})) {
14718:                                         if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
14719:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
14720:                                         } elsif (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
14721:                                             if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
14722:                                                 $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
14723:                                             }
14724:                                         }
14725:                                     }
14726:                                     if ($newvalue eq '') {
14727:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
14728:                                     } else {
14729:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
14730:                                     }
14731:                                 }
14732:                             }
14733:                         }
14734:                         $resulttext .= '</ul>';
14735:                     }
14736:                 }
14737:             } else {
14738:                 $resulttext = $nochgmsg;
14739:             }
14740:         } else {
14741:             $resulttext = '<span class="LC_error">'.
14742:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14743:         }
14744:     } else {
14745:         $resulttext = $nochgmsg;
14746:     }
14747:     return $resulttext;
14748: }
14749: 
14750: sub modify_trust {
14751:     my ($dom,$lastactref,%domconfig) = @_;
14752:     my (%by_ip,%by_location,@intdoms,@instdoms);
14753:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
14754:     my @locations = sort(keys(%by_location));
14755:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
14756:     my @types = ('exc','inc');
14757:     my (%defaultshash,%changes);
14758:     foreach my $prefix (@prefixes) {
14759:         $defaultshash{'trust'}{$prefix} = {};
14760:     }
14761:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14762:     my $resulttext;
14763:     foreach my $prefix (@prefixes) {
14764:         foreach my $type (@types) {
14765:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
14766:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
14767:             my @okvals;
14768:             foreach my $val (@vals) {
14769:                 if ($val =~ /:/) {
14770:                     my @items = split(/:/,$val);
14771:                     foreach my $item (@items) {
14772:                         if (ref($by_location{$item}) eq 'ARRAY') {
14773:                             push(@okvals,$item);
14774:                         }
14775:                     }
14776:                 } else {
14777:                     if (ref($by_location{$val}) eq 'ARRAY') {
14778:                         push(@okvals,$val);
14779:                     }
14780:                 }
14781:             }
14782:             @okvals = sort(@okvals);
14783:             if (ref($domconfig{'trust'}) eq 'HASH') {
14784:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
14785:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
14786:                         if ($inuse == 0) {
14787:                             $changes{$prefix}{$type} = 1;
14788:                         } else {
14789:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
14790:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
14791:                             if (@changed > 0) {
14792:                                 $changes{$prefix}{$type} = 1;
14793:                             }
14794:                         }
14795:                     } else {
14796:                         if ($inuse == 1) {
14797:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
14798:                             $changes{$prefix}{$type} = 1;
14799:                         }
14800:                     }
14801:                 } else {
14802:                     if ($inuse == 1) {
14803:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
14804:                         $changes{$prefix}{$type} = 1;
14805:                     }
14806:                 }
14807:             } else {
14808:                 if ($inuse == 1) {
14809:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
14810:                     $changes{$prefix}{$type} = 1;
14811:                 }
14812:             }
14813:         }
14814:     }
14815:     my $nochgmsg = &mt('No changes made to trust settings.');
14816:     if (keys(%changes) > 0) {
14817:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
14818:                                                  $dom);
14819:         if ($putresult eq 'ok') {
14820:             if (ref($defaultshash{'trust'}) eq 'HASH') {
14821:                 foreach my $prefix (@prefixes) {
14822:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
14823:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
14824:                     }
14825:                 }
14826:             }
14827:             my $cachetime = 24*60*60;
14828:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14829:             if (ref($lastactref) eq 'HASH') {
14830:                 $lastactref->{'domdefaults'} = 1;
14831:             }
14832:             if (keys(%changes) > 0) {
14833:                 my %lt = &trust_titles();
14834:                 $resulttext = &mt('Changes made:').'<ul>';
14835:                 foreach my $prefix (@prefixes) {
14836:                     if (ref($changes{$prefix}) eq 'HASH') {
14837:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
14838:                         foreach my $type (@types) {
14839:                             if (defined($changes{$prefix}{$type})) {
14840:                                 my $newvalue;
14841:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
14842:                                     if (ref($defaultshash{'trust'}{$prefix})) {
14843:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
14844:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
14845:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
14846:                                             }
14847:                                         }
14848:                                     }
14849:                                 }
14850:                                 if ($newvalue eq '') {
14851:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
14852:                                 } else {
14853:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
14854:                                 }
14855:                             }
14856:                         }
14857:                         $resulttext .= '</ul>';
14858:                     }
14859:                 }
14860:                 $resulttext .= '</ul>';
14861:             } else {
14862:                 $resulttext = $nochgmsg;
14863:             }
14864:         } else {
14865:             $resulttext = '<span class="LC_error">'.
14866:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14867:         }
14868:     } else {
14869:         $resulttext = $nochgmsg;
14870:     }
14871:     return $resulttext;
14872: }
14873: 
14874: sub modify_loadbalancing {
14875:     my ($dom,%domconfig) = @_;
14876:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
14877:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
14878:     my ($othertitle,$usertypes,$types) =
14879:         &Apache::loncommon::sorted_inst_types($dom);
14880:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14881:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
14882:     my @sparestypes = ('primary','default');
14883:     my %typetitles = &sparestype_titles();
14884:     my $resulttext;
14885:     my (%currbalancer,%currtargets,%currrules,%existing);
14886:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
14887:         %existing = %{$domconfig{'loadbalancing'}};
14888:     }
14889:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
14890:                               \%currtargets,\%currrules);
14891:     my ($saveloadbalancing,%defaultshash,%changes);
14892:     my ($alltypes,$othertypes,$titles) =
14893:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
14894:     my %ruletitles = &offloadtype_text();
14895:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
14896:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
14897:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
14898:         if ($balancer eq '') {
14899:             next;
14900:         }
14901:         if (!exists($servers{$balancer})) {
14902:             if (exists($currbalancer{$balancer})) {
14903:                 push(@{$changes{'delete'}},$balancer);
14904:             }
14905:             next;
14906:         }
14907:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
14908:             push(@{$changes{'delete'}},$balancer);
14909:             next;
14910:         }
14911:         if (!exists($currbalancer{$balancer})) {
14912:             push(@{$changes{'add'}},$balancer);
14913:         }
14914:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
14915:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
14916:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
14917:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
14918:             $saveloadbalancing = 1;
14919:         }
14920:         foreach my $sparetype (@sparestypes) {
14921:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
14922:             my @offloadto;
14923:             foreach my $target (@targets) {
14924:                 if (($servers{$target}) && ($target ne $balancer)) {
14925:                     if ($sparetype eq 'default') {
14926:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
14927:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
14928:                         }
14929:                     }
14930:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
14931:                         push(@offloadto,$target);
14932:                     }
14933:                 }
14934:             }
14935:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
14936:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
14937:                     push(@offloadto,$balancer);
14938:                 }
14939:             }
14940:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
14941:         }
14942:         if (ref($currtargets{$balancer}) eq 'HASH') {
14943:             foreach my $sparetype (@sparestypes) {
14944:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
14945:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
14946:                     if (@targetdiffs > 0) {
14947:                         $changes{'curr'}{$balancer}{'targets'} = 1;
14948:                     }
14949:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14950:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14951:                         $changes{'curr'}{$balancer}{'targets'} = 1;
14952:                     }
14953:                 }
14954:             }
14955:         } else {
14956:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
14957:                 foreach my $sparetype (@sparestypes) {
14958:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14959:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14960:                             $changes{'curr'}{$balancer}{'targets'} = 1;
14961:                         }
14962:                     }
14963:                 }
14964:             }
14965:         }
14966:         my $ishomedom;
14967:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
14968:             $ishomedom = 1;
14969:         }
14970:         if (ref($alltypes) eq 'ARRAY') {
14971:             foreach my $type (@{$alltypes}) {
14972:                 my $rule;
14973:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
14974:                          (!$ishomedom)) {
14975:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
14976:                 }
14977:                 if ($rule eq 'specific') {
14978:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
14979:                     if (exists($servers{$specifiedhost})) {
14980:                         $rule = $specifiedhost;
14981:                     }
14982:                 }
14983:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
14984:                 if (ref($currrules{$balancer}) eq 'HASH') {
14985:                     if ($rule ne $currrules{$balancer}{$type}) {
14986:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
14987:                     }
14988:                 } elsif ($rule ne '') {
14989:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
14990:                 }
14991:             }
14992:         }
14993:     }
14994:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
14995:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
14996:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
14997:             $defaultshash{'loadbalancing'} = {};
14998:         }
14999:         my $putresult = &Apache::lonnet::put_dom('configuration',
15000:                                                  \%defaultshash,$dom);
15001:         if ($putresult eq 'ok') {
15002:             if (keys(%changes) > 0) {
15003:                 my %toupdate;
15004:                 if (ref($changes{'delete'}) eq 'ARRAY') {
15005:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
15006:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
15007:                         $toupdate{$balancer} = 1;
15008:                     }
15009:                 }
15010:                 if (ref($changes{'add'}) eq 'ARRAY') {
15011:                     foreach my $balancer (sort(@{$changes{'add'}})) {
15012:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
15013:                         $toupdate{$balancer} = 1;
15014:                     }
15015:                 }
15016:                 if (ref($changes{'curr'}) eq 'HASH') {
15017:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
15018:                         $toupdate{$balancer} = 1;
15019:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
15020:                             if ($changes{'curr'}{$balancer}{'targets'}) {
15021:                                 my %offloadstr;
15022:                                 foreach my $sparetype (@sparestypes) {
15023:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
15024:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
15025:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
15026:                                         }
15027:                                     }
15028:                                 }
15029:                                 if (keys(%offloadstr) == 0) {
15030:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
15031:                                 } else {
15032:                                     my $showoffload;
15033:                                     foreach my $sparetype (@sparestypes) {
15034:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
15035:                                         if (defined($offloadstr{$sparetype})) {
15036:                                             $showoffload .= $offloadstr{$sparetype};
15037:                                         } else {
15038:                                             $showoffload .= &mt('None');
15039:                                         }
15040:                                         $showoffload .= ('&nbsp;'x3);
15041:                                     }
15042:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
15043:                                 }
15044:                             }
15045:                         }
15046:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
15047:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
15048:                                 foreach my $type (@{$alltypes}) {
15049:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
15050:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
15051:                                         my $balancetext;
15052:                                         if ($rule eq '') {
15053:                                             $balancetext =  $ruletitles{'default'};
15054:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
15055:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
15056:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
15057:                                                 foreach my $sparetype (@sparestypes) {
15058:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
15059:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
15060:                                                     }
15061:                                                 }
15062:                                                 foreach my $item (@{$alltypes}) {
15063:                                                     next if ($item =~  /^_LC_ipchange/);
15064:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
15065:                                                     if ($hasrule eq 'homeserver') {
15066:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
15067:                                                     } else {
15068:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
15069:                                                             if ($servers{$hasrule}) {
15070:                                                                 $toupdate{$hasrule} = 1;
15071:                                                             }
15072:                                                         }
15073:                                                     }
15074:                                                 }
15075:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
15076:                                                     $balancetext =  $ruletitles{$rule};
15077:                                                 } else {
15078:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
15079:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
15080:                                                     if ($receiver) {
15081:                                                         $toupdate{$receiver};
15082:                                                     }
15083:                                                 }
15084:                                             } else {
15085:                                                 $balancetext =  $ruletitles{$rule};
15086:                                             }
15087:                                         } else {
15088:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
15089:                                         }
15090:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
15091:                                     }
15092:                                 }
15093:                             }
15094:                         }
15095:                         if (keys(%toupdate)) {
15096:                             my %thismachine;
15097:                             my $updatedhere;
15098:                             my $cachetime = 60*60*24;
15099:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
15100:                             foreach my $lonhost (keys(%toupdate)) {
15101:                                 if ($thismachine{$lonhost}) {
15102:                                     unless ($updatedhere) {
15103:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
15104:                                                                       $defaultshash{'loadbalancing'},
15105:                                                                       $cachetime);
15106:                                         $updatedhere = 1;
15107:                                     }
15108:                                 } else {
15109:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
15110:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
15111:                                 }
15112:                             }
15113:                         }
15114:                     }
15115:                 }
15116:                 if ($resulttext ne '') {
15117:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
15118:                 } else {
15119:                     $resulttext = $nochgmsg;
15120:                 }
15121:             } else {
15122:                 $resulttext = $nochgmsg;
15123:             }
15124:         } else {
15125:             $resulttext = '<span class="LC_error">'.
15126:                           &mt('An error occurred: [_1]',$putresult).'</span>';
15127:         }
15128:     } else {
15129:         $resulttext = $nochgmsg;
15130:     }
15131:     return $resulttext;
15132: }
15133: 
15134: sub recurse_check {
15135:     my ($chkcats,$categories,$depth,$name) = @_;
15136:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
15137:         my $chg = 0;
15138:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
15139:             my $category = $chkcats->[$depth]{$name}[$j];
15140:             my $item;
15141:             if ($category eq '') {
15142:                 $chg ++;
15143:             } else {
15144:                 my $deeper = $depth + 1;
15145:                 $item = &escape($category).':'.&escape($name).':'.$depth;
15146:                 if ($chg) {
15147:                     $categories->{$item} -= $chg;
15148:                 }
15149:                 &recurse_check($chkcats,$categories,$deeper,$category);
15150:                 $deeper --;
15151:             }
15152:         }
15153:     }
15154:     return;
15155: }
15156: 
15157: sub recurse_cat_deletes {
15158:     my ($item,$coursecategories,$deletions) = @_;
15159:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
15160:     my $subdepth = $depth + 1;
15161:     if (ref($coursecategories) eq 'HASH') {
15162:         foreach my $subitem (keys(%{$coursecategories})) {
15163:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
15164:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
15165:                 delete($coursecategories->{$subitem});
15166:                 $deletions->{$subitem} = 1;
15167:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
15168:             }
15169:         }
15170:     }
15171:     return;
15172: }
15173: 
15174: sub active_dc_picker {
15175:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
15176:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
15177:     my @domcoord = keys(%domcoords);
15178:     if (keys(%currhash)) {
15179:         foreach my $dc (keys(%currhash)) {
15180:             unless (exists($domcoords{$dc})) {
15181:                 push(@domcoord,$dc);
15182:             }
15183:         }
15184:     }
15185:     @domcoord = sort(@domcoord);
15186:     my $numdcs = scalar(@domcoord);
15187:     my $rows = 0;
15188:     my $table;
15189:     if ($numdcs > 1) {
15190:         $table = '<table>';
15191:         for (my $i=0; $i<@domcoord; $i++) {
15192:             my $rem = $i%($numinrow);
15193:             if ($rem == 0) {
15194:                 if ($i > 0) {
15195:                     $table .= '</tr>';
15196:                 }
15197:                 $table .= '<tr>';
15198:                 $rows ++;
15199:             }
15200:             my $check = '';
15201:             if ($inputtype eq 'radio') {
15202:                 if (keys(%currhash) == 0) {
15203:                     if (!$i) {
15204:                         $check = ' checked="checked"';
15205:                     }
15206:                 } elsif (exists($currhash{$domcoord[$i]})) {
15207:                     $check = ' checked="checked"';
15208:                 }
15209:             } else {
15210:                 if (exists($currhash{$domcoord[$i]})) {
15211:                     $check = ' checked="checked"';
15212:                 }
15213:             }
15214:             if ($i == @domcoord - 1) {
15215:                 my $colsleft = $numinrow - $rem;
15216:                 if ($colsleft > 1) {
15217:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
15218:                 } else {
15219:                     $table .= '<td class="LC_left_item">';
15220:                 }
15221:             } else {
15222:                 $table .= '<td class="LC_left_item">';
15223:             }
15224:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
15225:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
15226:             $table .= '<span class="LC_nobreak"><label>'.
15227:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
15228:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
15229:             if ($user ne $dcname.':'.$dcdom) {
15230:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
15231:             }
15232:             $table .= '</label></span></td>';
15233:         }
15234:         $table .= '</tr></table>';
15235:     } elsif ($numdcs == 1) {
15236:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
15237:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
15238:         if ($inputtype eq 'radio') {
15239:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
15240:             if ($user ne $dcname.':'.$dcdom) {
15241:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
15242:             }
15243:         } else {
15244:             my $check;
15245:             if (exists($currhash{$domcoord[0]})) {
15246:                 $check = ' checked="checked"';
15247:             }
15248:             $table = '<span class="LC_nobreak"><label>'.
15249:                      '<input type="checkbox" name="'.$name.'" '.
15250:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
15251:             if ($user ne $dcname.':'.$dcdom) {
15252:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
15253:             }
15254:             $table .= '</label></span>';
15255:             $rows ++;
15256:         }
15257:     }
15258:     return ($numdcs,$table,$rows);
15259: }
15260: 
15261: sub usersession_titles {
15262:     return &Apache::lonlocal::texthash(
15263:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
15264:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
15265:                spares => 'Servers offloaded to, when busy',
15266:                version => 'LON-CAPA version requirement',
15267:                excludedomain => 'Allow all, but exclude specific domains',
15268:                includedomain => 'Deny all, but include specific domains',
15269:                primary => 'Primary (checked first)',
15270:                default => 'Default',
15271:            );
15272: }
15273: 
15274: sub id_for_thisdom {
15275:     my (%servers) = @_;
15276:     my %altids;
15277:     foreach my $server (keys(%servers)) {
15278:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
15279:         if ($serverhome ne $server) {
15280:             $altids{$serverhome} = $server;
15281:         }
15282:     }
15283:     return %altids;
15284: }
15285: 
15286: sub count_servers {
15287:     my ($currbalancer,%servers) = @_;
15288:     my (@spares,$numspares);
15289:     foreach my $lonhost (sort(keys(%servers))) {
15290:         next if ($currbalancer eq $lonhost);
15291:         push(@spares,$lonhost);
15292:     }
15293:     if ($currbalancer) {
15294:         $numspares = scalar(@spares);
15295:     } else {
15296:         $numspares = scalar(@spares) - 1;
15297:     }
15298:     return ($numspares,@spares);
15299: }
15300: 
15301: sub lonbalance_targets_js {
15302:     my ($dom,$types,$servers,$settings) = @_;
15303:     my $select = &mt('Select');
15304:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
15305:     if (ref($servers) eq 'HASH') {
15306:         $alltargets = join("','",sort(keys(%{$servers})));
15307:         my @homedoms;
15308:         foreach my $server (sort(keys(%{$servers}))) {
15309:             if (&Apache::lonnet::host_domain($server) eq $dom) {
15310:                 push(@homedoms,'1');
15311:             } else {
15312:                 push(@homedoms,'0');
15313:             }
15314:         }
15315:         $allishome = join("','",@homedoms);
15316:     }
15317:     if (ref($types) eq 'ARRAY') {
15318:         if (@{$types} > 0) {
15319:             @alltypes = @{$types};
15320:         }
15321:     }
15322:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
15323:     $allinsttypes = join("','",@alltypes);
15324:     my (%currbalancer,%currtargets,%currrules,%existing);
15325:     if (ref($settings) eq 'HASH') {
15326:         %existing = %{$settings};
15327:     }
15328:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
15329:                               \%currtargets,\%currrules);
15330:     my $balancers = join("','",sort(keys(%currbalancer)));
15331:     return <<"END";
15332: 
15333: <script type="text/javascript">
15334: // <![CDATA[
15335: 
15336: currBalancers = new Array('$balancers');
15337: 
15338: function toggleTargets(balnum) {
15339:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
15340:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
15341:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
15342:     var prevbalancer = prevhostitem.value;
15343:     var baltotal = document.getElementById('loadbalancing_total').value;
15344:     prevhostitem.value = balancer;
15345:     if (prevbalancer != '') {
15346:         var prevIdx = currBalancers.indexOf(prevbalancer);
15347:         if (prevIdx != -1) {
15348:             currBalancers.splice(prevIdx,1);
15349:         }
15350:     }
15351:     if (balancer == '') {
15352:         hideSpares(balnum);
15353:     } else {
15354:         var currIdx = currBalancers.indexOf(balancer);
15355:         if (currIdx == -1) {
15356:             currBalancers.push(balancer);
15357:         }
15358:         var homedoms = new Array('$allishome');
15359:         var ishomedom = homedoms[lonhostitem.selectedIndex];
15360:         showSpares(balancer,ishomedom,balnum);
15361:     }
15362:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
15363:     return;
15364: }
15365: 
15366: function showSpares(balancer,ishomedom,balnum) {
15367:     var alltargets = new Array('$alltargets');
15368:     var insttypes = new Array('$allinsttypes');
15369:     var offloadtypes = new Array('primary','default');
15370: 
15371:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
15372:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
15373:  
15374:     for (var i=0; i<offloadtypes.length; i++) {
15375:         var count = 0;
15376:         for (var j=0; j<alltargets.length; j++) {
15377:             if (alltargets[j] != balancer) {
15378:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
15379:                 item.value = alltargets[j];
15380:                 item.style.textAlign='left';
15381:                 item.style.textFace='normal';
15382:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
15383:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
15384:                     item.disabled = '';
15385:                 } else {
15386:                     item.disabled = 'disabled';
15387:                     item.checked = false;
15388:                 }
15389:                 count ++;
15390:             }
15391:         }
15392:     }
15393:     for (var k=0; k<insttypes.length; k++) {
15394:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
15395:             if (ishomedom == 1) {
15396:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
15397:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
15398:             } else {
15399:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
15400:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
15401:             }
15402:         } else {
15403:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
15404:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
15405:         }
15406:         if ((insttypes[k] != '_LC_external') && 
15407:             ((insttypes[k] != '_LC_internetdom') ||
15408:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
15409:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
15410:             item.options.length = 0;
15411:             item.options[0] = new Option("","",true,true);
15412:             var idx = 0;
15413:             for (var m=0; m<alltargets.length; m++) {
15414:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
15415:                     idx ++;
15416:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
15417:                 }
15418:             }
15419:         }
15420:     }
15421:     return;
15422: }
15423: 
15424: function hideSpares(balnum) {
15425:     var alltargets = new Array('$alltargets');
15426:     var insttypes = new Array('$allinsttypes');
15427:     var offloadtypes = new Array('primary','default');
15428: 
15429:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
15430:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
15431: 
15432:     var total = alltargets.length - 1;
15433:     for (var i=0; i<offloadtypes; i++) {
15434:         for (var j=0; j<total; j++) {
15435:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
15436:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
15437:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
15438:         }
15439:     }
15440:     for (var k=0; k<insttypes.length; k++) {
15441:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
15442:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
15443:         if (insttypes[k] != '_LC_external') {
15444:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
15445:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
15446:         }
15447:     }
15448:     return;
15449: }
15450: 
15451: function checkOffloads(item,balnum,type) {
15452:     var alltargets = new Array('$alltargets');
15453:     var offloadtypes = new Array('primary','default');
15454:     if (item.checked) {
15455:         var total = alltargets.length - 1;
15456:         var other;
15457:         if (type == offloadtypes[0]) {
15458:             other = offloadtypes[1];
15459:         } else {
15460:             other = offloadtypes[0];
15461:         }
15462:         for (var i=0; i<total; i++) {
15463:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
15464:             if (server == item.value) {
15465:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
15466:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
15467:                 }
15468:             }
15469:         }
15470:     }
15471:     return;
15472: }
15473: 
15474: function singleServerToggle(balnum,type) {
15475:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
15476:     if (offloadtoSelIdx == 0) {
15477:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
15478:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
15479: 
15480:     } else {
15481:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
15482:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
15483:     }
15484:     return;
15485: }
15486: 
15487: function balanceruleChange(formname,balnum,type) {
15488:     if (type == '_LC_external') {
15489:         return;
15490:     }
15491:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
15492:     for (var i=0; i<typesRules.length; i++) {
15493:         if (formname.elements[typesRules[i]].checked) {
15494:             if (formname.elements[typesRules[i]].value != 'specific') {
15495:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
15496:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
15497:             } else {
15498:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
15499:             }
15500:         }
15501:     }
15502:     return;
15503: }
15504: 
15505: function balancerDeleteChange(balnum) {
15506:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
15507:     var baltotal = document.getElementById('loadbalancing_total').value;
15508:     var addtarget;
15509:     var removetarget;
15510:     var action = 'delete';
15511:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
15512:         var lonhost = hostitem.value;
15513:         var currIdx = currBalancers.indexOf(lonhost);
15514:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
15515:             if (currIdx != -1) {
15516:                 currBalancers.splice(currIdx,1);
15517:             }
15518:             addtarget = lonhost;
15519:         } else {
15520:             if (currIdx == -1) {
15521:                 currBalancers.push(lonhost);
15522:             }
15523:             removetarget = lonhost;
15524:             action = 'undelete';
15525:         }
15526:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
15527:     }
15528:     return;
15529: }
15530: 
15531: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
15532:     if (baltotal > 1) {
15533:         var offloadtypes = new Array('primary','default');
15534:         var alltargets = new Array('$alltargets');
15535:         var insttypes = new Array('$allinsttypes');
15536:         for (var i=0; i<baltotal; i++) {
15537:             if (i != balnum) {
15538:                 for (var j=0; j<offloadtypes.length; j++) {
15539:                     var total = alltargets.length - 1;
15540:                     for (var k=0; k<total; k++) {
15541:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
15542:                         var server = serveritem.value;
15543:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
15544:                             if (server == addtarget) {
15545:                                 serveritem.disabled = '';
15546:                             }
15547:                         }
15548:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
15549:                             if (server == removetarget) {
15550:                                 serveritem.disabled = 'disabled';
15551:                                 serveritem.checked = false;
15552:                             }
15553:                         }
15554:                     }
15555:                 }
15556:                 for (var j=0; j<insttypes.length; j++) {
15557:                     if (insttypes[j] != '_LC_external') {
15558:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
15559:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
15560:                             var currSel = singleserver.selectedIndex;
15561:                             var currVal = singleserver.options[currSel].value;
15562:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
15563:                                 var numoptions = singleserver.options.length;
15564:                                 var needsnew = 1;
15565:                                 for (var k=0; k<numoptions; k++) {
15566:                                     if (singleserver.options[k] == addtarget) {
15567:                                         needsnew = 0;
15568:                                         break;
15569:                                     }
15570:                                 }
15571:                                 if (needsnew == 1) {
15572:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
15573:                                 }
15574:                             }
15575:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
15576:                                 singleserver.options.length = 0;
15577:                                 if ((currVal) && (currVal != removetarget)) {
15578:                                     singleserver.options[0] = new Option("","",false,false);
15579:                                 } else {
15580:                                     singleserver.options[0] = new Option("","",true,true);
15581:                                 }
15582:                                 var idx = 0;
15583:                                 for (var m=0; m<alltargets.length; m++) {
15584:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
15585:                                         idx ++;
15586:                                         if (currVal == alltargets[m]) {
15587:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
15588:                                         } else {
15589:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
15590:                                         }
15591:                                     }
15592:                                 }
15593:                             }
15594:                         }
15595:                     }
15596:                 }
15597:             }
15598:         }
15599:     }
15600:     return;
15601: }
15602: 
15603: // ]]>
15604: </script>
15605: 
15606: END
15607: }
15608: 
15609: sub new_spares_js {
15610:     my @sparestypes = ('primary','default');
15611:     my $types = join("','",@sparestypes);
15612:     my $select = &mt('Select');
15613:     return <<"END";
15614: 
15615: <script type="text/javascript">
15616: // <![CDATA[
15617: 
15618: function updateNewSpares(formname,lonhost) {
15619:     var types = new Array('$types');
15620:     var include = new Array();
15621:     var exclude = new Array();
15622:     for (var i=0; i<types.length; i++) {
15623:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
15624:         for (var j=0; j<spareboxes.length; j++) {
15625:             if (formname.elements[spareboxes[j]].checked) {
15626:                 exclude.push(formname.elements[spareboxes[j]].value);
15627:             } else {
15628:                 include.push(formname.elements[spareboxes[j]].value);
15629:             }
15630:         }
15631:     }
15632:     for (var i=0; i<types.length; i++) {
15633:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
15634:         var selIdx = newSpare.selectedIndex;
15635:         var currnew = newSpare.options[selIdx].value;
15636:         var okSpares = new Array();
15637:         for (var j=0; j<newSpare.options.length; j++) {
15638:             var possible = newSpare.options[j].value;
15639:             if (possible != '') {
15640:                 if (exclude.indexOf(possible) == -1) {
15641:                     okSpares.push(possible);
15642:                 } else {
15643:                     if (currnew == possible) {
15644:                         selIdx = 0;
15645:                     }
15646:                 }
15647:             }
15648:         }
15649:         for (var k=0; k<include.length; k++) {
15650:             if (okSpares.indexOf(include[k]) == -1) {
15651:                 okSpares.push(include[k]);
15652:             }
15653:         }
15654:         okSpares.sort();
15655:         newSpare.options.length = 0;
15656:         if (selIdx == 0) {
15657:             newSpare.options[0] = new Option("$select","",true,true);
15658:         } else {
15659:             newSpare.options[0] = new Option("$select","",false,false);
15660:         }
15661:         for (var m=0; m<okSpares.length; m++) {
15662:             var idx = m+1;
15663:             var selThis = 0;
15664:             if (selIdx != 0) {
15665:                 if (okSpares[m] == currnew) {
15666:                     selThis = 1;
15667:                 }
15668:             }
15669:             if (selThis == 1) {
15670:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
15671:             } else {
15672:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
15673:             }
15674:         }
15675:     }
15676:     return;
15677: }
15678: 
15679: function checkNewSpares(lonhost,type) {
15680:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
15681:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
15682:     if (chosen != '') { 
15683:         var othertype;
15684:         var othernewSpare;
15685:         if (type == 'primary') {
15686:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
15687:         }
15688:         if (type == 'default') {
15689:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
15690:         }
15691:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
15692:             othernewSpare.selectedIndex = 0;
15693:         }
15694:     }
15695:     return;
15696: }
15697: 
15698: // ]]>
15699: </script>
15700: 
15701: END
15702: 
15703: }
15704: 
15705: sub common_domprefs_js {
15706:     return <<"END";
15707: 
15708: <script type="text/javascript">
15709: // <![CDATA[
15710: 
15711: function getIndicesByName(formname,item) {
15712:     var group = new Array();
15713:     for (var i=0;i<formname.elements.length;i++) {
15714:         if (formname.elements[i].name == item) {
15715:             group.push(formname.elements[i].id);
15716:         }
15717:     }
15718:     return group;
15719: }
15720: 
15721: // ]]>
15722: </script>
15723: 
15724: END
15725: 
15726: }
15727: 
15728: sub recaptcha_js {
15729:     my %lt = &captcha_phrases();
15730:     return <<"END";
15731: 
15732: <script type="text/javascript">
15733: // <![CDATA[
15734: 
15735: function updateCaptcha(caller,context) {
15736:     var privitem;
15737:     var pubitem;
15738:     var privtext;
15739:     var pubtext;
15740:     var versionitem;
15741:     var versiontext;
15742:     if (document.getElementById(context+'_recaptchapub')) {
15743:         pubitem = document.getElementById(context+'_recaptchapub');
15744:     } else {
15745:         return;
15746:     }
15747:     if (document.getElementById(context+'_recaptchapriv')) {
15748:         privitem = document.getElementById(context+'_recaptchapriv');
15749:     } else {
15750:         return;
15751:     }
15752:     if (document.getElementById(context+'_recaptchapubtxt')) {
15753:         pubtext = document.getElementById(context+'_recaptchapubtxt');
15754:     } else {
15755:         return;
15756:     }
15757:     if (document.getElementById(context+'_recaptchaprivtxt')) {
15758:         privtext = document.getElementById(context+'_recaptchaprivtxt');
15759:     } else {
15760:         return;
15761:     }
15762:     if (document.getElementById(context+'_recaptchaversion')) {
15763:         versionitem = document.getElementById(context+'_recaptchaversion');
15764:     } else {
15765:         return;
15766:     }
15767:     if (document.getElementById(context+'_recaptchavertxt')) {
15768:         versiontext = document.getElementById(context+'_recaptchavertxt');
15769:     } else {
15770:         return;
15771:     }
15772:     if (caller.checked) {
15773:         if (caller.value == 'recaptcha') {
15774:             pubitem.type = 'text';
15775:             privitem.type = 'text';
15776:             pubitem.size = '40';
15777:             privitem.size = '40';
15778:             pubtext.innerHTML = "$lt{'pub'}";
15779:             privtext.innerHTML = "$lt{'priv'}";
15780:             versionitem.type = 'text';
15781:             versionitem.size = '3';
15782:             versiontext.innerHTML = "$lt{'ver'}";
15783:         } else {
15784:             pubitem.type = 'hidden';
15785:             privitem.type = 'hidden';
15786:             versionitem.type = 'hidden';
15787:             pubtext.innerHTML = '';
15788:             privtext.innerHTML = '';
15789:             versiontext.innerHTML = '';
15790:         }
15791:     }
15792:     return;
15793: }
15794: 
15795: // ]]>
15796: </script>
15797: 
15798: END
15799: 
15800: }
15801: 
15802: sub toggle_display_js {
15803:     return <<"END";
15804: 
15805: <script type="text/javascript">
15806: // <![CDATA[
15807: 
15808: function toggleDisplay(domForm,caller) {
15809:     if (document.getElementById(caller)) {
15810:         var divitem = document.getElementById(caller);
15811:         var optionsElement = domForm.coursecredits;
15812:         var checkval = 1;
15813:         var dispval = 'block';
15814:         var selfcreateRegExp = /^cancreate_emailverified/;
15815:         if (caller == 'emailoptions') {
15816:             optionsElement = domForm.cancreate_email; 
15817:         }
15818:         if (caller == 'studentsubmission') {
15819:             optionsElement = domForm.postsubmit;
15820:         }
15821:         if (caller == 'cloneinstcode') {
15822:             optionsElement = domForm.canclone;
15823:             checkval = 'instcode';
15824:         }
15825:         if (selfcreateRegExp.test(caller)) {
15826:             optionsElement = domForm.elements[caller];
15827:             checkval = 'other';
15828:             dispval = 'inline'
15829:         }
15830:         if (optionsElement.length) {
15831:             var currval;
15832:             for (var i=0; i<optionsElement.length; i++) {
15833:                 if (optionsElement[i].checked) {
15834:                    currval = optionsElement[i].value;
15835:                 }
15836:             }
15837:             if (currval == checkval) {
15838:                 divitem.style.display = dispval;
15839:             } else {
15840:                 divitem.style.display = 'none';
15841:             }
15842:         }
15843:     }
15844:     return;
15845: }
15846: 
15847: // ]]>
15848: </script>
15849: 
15850: END
15851: 
15852: }
15853: 
15854: sub captcha_phrases {
15855:     return &Apache::lonlocal::texthash (
15856:                  priv => 'Private key',
15857:                  pub  => 'Public key',
15858:                  original  => 'original (CAPTCHA)',
15859:                  recaptcha => 'successor (ReCAPTCHA)',
15860:                  notused   => 'unused',
15861:                  ver => 'ReCAPTCHA version (1 or 2)',
15862:     );
15863: }
15864: 
15865: sub devalidate_remote_domconfs {
15866:     my ($dom,$cachekeys) = @_;
15867:     return unless (ref($cachekeys) eq 'HASH');
15868:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
15869:     my %thismachine;
15870:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
15871:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions','directorysrch');
15872:     if (keys(%servers)) {
15873:         foreach my $server (keys(%servers)) {
15874:             next if ($thismachine{$server});
15875:             my @cached;
15876:             foreach my $name (@posscached) {
15877:                 if ($cachekeys->{$name}) {
15878:                     push(@cached,&escape($name).':'.&escape($dom));
15879:                 }
15880:             }
15881:             if (@cached) {
15882:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
15883:             }
15884:         }
15885:     }
15886:     return;
15887: }
15888: 
15889: 1;

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