File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.318: download - view: text, annotated - select for diffs
Thu Nov 30 01:52:14 2017 UTC (6 years, 6 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- LON-CAPA as LTI Consumer.
  - Value in custom field passed to Tool Provider on launch can be the value
    of an item in %env. (Include LONCAPA::env{<var name>} when adding the
    custom field in the domain configuration, where <var name> is the
    env var for which the value is to be sent).
  - Course Coordinator can set a path to append to the standard URL to
    which the signed POSTed data are sent. (Path can be different for
    each tool instance).

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.318 2017/11/30 01:52:14 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:                                 my @args = ('convert','-sample',$size,$input,$output);
 1660:                                 system({$args[0]} @args);
 1661:                                 $showfile = "/$imgdir/tn-".$filename;
 1662:                             }
 1663:                         }
 1664:                     }
 1665:                 }
 1666:             }
 1667:             if ($showfile) {
 1668:                 if ($showfile =~ m{^/(adm|res)/}) {
 1669:                     if ($showfile =~ m{^/res/}) {
 1670:                         my $local_showfile =
 1671:                             &Apache::lonnet::filelocation('',$showfile);
 1672:                         &Apache::lonnet::repcopy($local_showfile);
 1673:                     }
 1674:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1675:                 }
 1676:                 if ($imgfile) {
 1677:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1678:                         if ($imgfile =~ m{^/res/}) {
 1679:                             my $local_imgfile =
 1680:                                 &Apache::lonnet::filelocation('',$imgfile);
 1681:                             &Apache::lonnet::repcopy($local_imgfile);
 1682:                         }
 1683:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1684:                     } else {
 1685:                         $fullsize = $imgfile;
 1686:                     }
 1687:                 }
 1688:                 $datatable .= '<td>';
 1689:                 if ($img eq 'login') {
 1690:                     $datatable .= $login_hdr_pick;
 1691:                 } 
 1692:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1693:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1694:             } else {
 1695:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1696:                               &mt('Upload:').'<br />';
 1697:             }
 1698:         } else {
 1699:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1700:                           &mt('Upload:').'<br />';
 1701:         }
 1702:         if ($switchserver) {
 1703:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1704:         } else {
 1705:             if ($img ne 'login') { # suppress file selection for Log-in header
 1706:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1707:             }
 1708:         }
 1709:         $datatable .= '</td></tr>';
 1710:     }
 1711:     $itemcount ++;
 1712:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1713:     $datatable .= '<tr'.$css_class.'>'.
 1714:                   '<td>'.$choices->{'bgs'}.'</td>';
 1715:     my $bgs_def;
 1716:     foreach my $item (@{$bgs}) {
 1717:         if (!$is_custom->{$item}) {
 1718:             $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>';
 1719:         }
 1720:     }
 1721:     if ($bgs_def) {
 1722:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1723:     } else {
 1724:         $datatable .= '<td>&nbsp;</td>';
 1725:     }
 1726:     $datatable .= '<td class="LC_right_item">'.
 1727:                   '<table border="0"><tr>';
 1728: 
 1729:     foreach my $item (@{$bgs}) {
 1730:         $datatable .= '<td style="text-align: center">'.$choices->{$item};
 1731: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1732:         if ($designs->{'bgs'}{$item}) {
 1733:             $datatable .= '&nbsp;';
 1734:         }
 1735:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1736:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1737:     }
 1738:     $datatable .= '</tr></table></td></tr>';
 1739:     $itemcount ++;
 1740:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1741:     $datatable .= '<tr'.$css_class.'>'.
 1742:                   '<td>'.$choices->{'links'}.'</td>';
 1743:     my $links_def;
 1744:     foreach my $item (@{$links}) {
 1745:         if (!$is_custom->{$item}) {
 1746:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1747:         }
 1748:     }
 1749:     if ($links_def) {
 1750:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1751:     } else {
 1752:         $datatable .= '<td>&nbsp;</td>';
 1753:     }
 1754:     $datatable .= '<td class="LC_right_item">'.
 1755:                   '<table border="0"><tr>';
 1756:     foreach my $item (@{$links}) {
 1757: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1758:         $datatable .= '<td style="text-align: center">'.$choices->{$item}."\n";
 1759:         if ($designs->{'links'}{$item}) {
 1760:             $datatable.='&nbsp;';
 1761:         }
 1762:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1763:                       '" /></td>';
 1764:     }
 1765:     $$rowtotal += $itemcount;
 1766:     return $datatable;
 1767: }
 1768: 
 1769: sub logo_display_options {
 1770:     my ($img,$defaults,$designs) = @_;
 1771:     my $checkedon;
 1772:     if (ref($defaults) eq 'HASH') {
 1773:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1774:             if ($defaults->{'showlogo'}{$img}) {
 1775:                 $checkedon = 'checked="checked" ';     
 1776:             }
 1777:         } 
 1778:     }
 1779:     if (ref($designs) eq 'HASH') {
 1780:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1781:             if (defined($designs->{'showlogo'}{$img})) {
 1782:                 if ($designs->{'showlogo'}{$img} == 0) {
 1783:                     $checkedon = '';
 1784:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1785:                     $checkedon = 'checked="checked" ';
 1786:                 }
 1787:             }
 1788:         }
 1789:     }
 1790:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1791:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1792:            &mt('show').'</label>'."\n";
 1793: }
 1794: 
 1795: sub login_header_options  {
 1796:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1797:     my $output = '';
 1798:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1799:         $output .= &mt('Text default(s):').'<br />';
 1800:         if (!$is_custom->{'textcol'}) {
 1801:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1802:                        '&nbsp;&nbsp;&nbsp;';
 1803:         }
 1804:         if (!$is_custom->{'bgcol'}) {
 1805:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1806:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1807:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1808:         }
 1809:         $output .= '<br />';
 1810:     }
 1811:     $output .='<br />';
 1812:     return $output;
 1813: }
 1814: 
 1815: sub login_text_colors {
 1816:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1817:     my $color_menu = '<table border="0"><tr>';
 1818:     foreach my $item (@{$logintext}) {
 1819:         $color_menu .= '<td style="text-align: center">'.$choices->{$item};
 1820:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1821:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1822:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1823:     }
 1824:     $color_menu .= '</tr></table><br />';
 1825:     return $color_menu;
 1826: }
 1827: 
 1828: sub image_changes {
 1829:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1830:     my $output;
 1831:     if ($img eq 'login') {
 1832:             # suppress image for Log-in header
 1833:     } elsif (!$is_custom) {
 1834:         if ($img ne 'domlogo') {
 1835:             $output .= &mt('Default image:').'<br />';
 1836:         } else {
 1837:             $output .= &mt('Default in use:').'<br />';
 1838:         }
 1839:     }
 1840:     if ($img eq 'login') { # suppress image for Log-in header
 1841:         $output .= '<td>'.$logincolors;
 1842:     } else {
 1843:         if ($img_import) {
 1844:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1845:         }
 1846:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1847:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1848:         if ($is_custom) {
 1849:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1850:                        '<input type="checkbox" name="'.
 1851:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1852:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1853:         } else {
 1854:             $output .= '<td class="LC_middle">'.$logincolors.&mt('Upload:').'<br />';
 1855:         }
 1856:     }
 1857:     return $output;
 1858: }
 1859: 
 1860: sub print_quotas {
 1861:     my ($dom,$settings,$rowtotal,$action) = @_;
 1862:     my $context;
 1863:     if ($action eq 'quotas') {
 1864:         $context = 'tools';
 1865:     } else {
 1866:         $context = $action;
 1867:     }
 1868:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1869:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1870:     my $typecount = 0;
 1871:     my ($css_class,%titles);
 1872:     if ($context eq 'requestcourses') {
 1873:         @usertools = ('official','unofficial','community','textbook','placement');
 1874:         @options =('norequest','approval','validate','autolimit');
 1875:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1876:         %titles = &courserequest_titles();
 1877:     } elsif ($context eq 'requestauthor') {
 1878:         @usertools = ('author');
 1879:         @options = ('norequest','approval','automatic');
 1880:         %titles = &authorrequest_titles();
 1881:     } else {
 1882:         @usertools = ('aboutme','blog','webdav','portfolio');
 1883:         %titles = &tool_titles();
 1884:     }
 1885:     if (ref($types) eq 'ARRAY') {
 1886:         foreach my $type (@{$types}) {
 1887:             my ($currdefquota,$currauthorquota);
 1888:             unless (($context eq 'requestcourses') ||
 1889:                     ($context eq 'requestauthor')) {
 1890:                 if (ref($settings) eq 'HASH') {
 1891:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1892:                         $currdefquota = $settings->{defaultquota}->{$type};
 1893:                     } else {
 1894:                         $currdefquota = $settings->{$type};
 1895:                     }
 1896:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1897:                         $currauthorquota = $settings->{authorquota}->{$type};
 1898:                     }
 1899:                 }
 1900:             }
 1901:             if (defined($usertypes->{$type})) {
 1902:                 $typecount ++;
 1903:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1904:                 $datatable .= '<tr'.$css_class.'>'.
 1905:                               '<td>'.$usertypes->{$type}.'</td>'.
 1906:                               '<td class="LC_left_item">';
 1907:                 if ($context eq 'requestcourses') {
 1908:                     $datatable .= '<table><tr>';
 1909:                 }
 1910:                 my %cell;  
 1911:                 foreach my $item (@usertools) {
 1912:                     if ($context eq 'requestcourses') {
 1913:                         my ($curroption,$currlimit);
 1914:                         if (ref($settings) eq 'HASH') {
 1915:                             if (ref($settings->{$item}) eq 'HASH') {
 1916:                                 $curroption = $settings->{$item}->{$type};
 1917:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1918:                                     $currlimit = $1; 
 1919:                                 }
 1920:                             }
 1921:                         }
 1922:                         if (!$curroption) {
 1923:                             $curroption = 'norequest';
 1924:                         }
 1925:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1926:                         foreach my $option (@options) {
 1927:                             my $val = $option;
 1928:                             if ($option eq 'norequest') {
 1929:                                 $val = 0;  
 1930:                             }
 1931:                             if ($option eq 'validate') {
 1932:                                 my $canvalidate = 0;
 1933:                                 if (ref($validations{$item}) eq 'HASH') { 
 1934:                                     if ($validations{$item}{$type}) {
 1935:                                         $canvalidate = 1;
 1936:                                     }
 1937:                                 }
 1938:                                 next if (!$canvalidate);
 1939:                             }
 1940:                             my $checked = '';
 1941:                             if ($option eq $curroption) {
 1942:                                 $checked = ' checked="checked"';
 1943:                             } elsif ($option eq 'autolimit') {
 1944:                                 if ($curroption =~ /^autolimit/) {
 1945:                                     $checked = ' checked="checked"';
 1946:                                 }                       
 1947:                             } 
 1948:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1949:                                   '<input type="radio" name="crsreq_'.$item.
 1950:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1951:                                   $titles{$option}.'</label>';
 1952:                             if ($option eq 'autolimit') {
 1953:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1954:                                                 $item.'_limit_'.$type.'" size="1" '.
 1955:                                                 'value="'.$currlimit.'" />';
 1956:                             }
 1957:                             $cell{$item} .= '</span> ';
 1958:                             if ($option eq 'autolimit') {
 1959:                                 $cell{$item} .= $titles{'unlimited'};
 1960:                             }
 1961:                         }
 1962:                     } elsif ($context eq 'requestauthor') {
 1963:                         my $curroption;
 1964:                         if (ref($settings) eq 'HASH') {
 1965:                             $curroption = $settings->{$type};
 1966:                         }
 1967:                         if (!$curroption) {
 1968:                             $curroption = 'norequest';
 1969:                         }
 1970:                         foreach my $option (@options) {
 1971:                             my $val = $option;
 1972:                             if ($option eq 'norequest') {
 1973:                                 $val = 0;
 1974:                             }
 1975:                             my $checked = '';
 1976:                             if ($option eq $curroption) {
 1977:                                 $checked = ' checked="checked"';
 1978:                             }
 1979:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1980:                                   '<input type="radio" name="authorreq_'.$type.
 1981:                                   '" value="'.$val.'"'.$checked.' />'.
 1982:                                   $titles{$option}.'</label></span>&nbsp; ';
 1983:                         }
 1984:                     } else {
 1985:                         my $checked = 'checked="checked" ';
 1986:                         if (ref($settings) eq 'HASH') {
 1987:                             if (ref($settings->{$item}) eq 'HASH') {
 1988:                                 if ($settings->{$item}->{$type} == 0) {
 1989:                                     $checked = '';
 1990:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1991:                                     $checked =  'checked="checked" ';
 1992:                                 }
 1993:                             }
 1994:                         }
 1995:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1996:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1997:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1998:                                       '</label></span>&nbsp; ';
 1999:                     }
 2000:                 }
 2001:                 if ($context eq 'requestcourses') {
 2002:                     $datatable .= '</tr><tr>';
 2003:                     foreach my $item (@usertools) {
 2004:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 2005:                     }
 2006:                     $datatable .= '</tr></table>';
 2007:                 }
 2008:                 $datatable .= '</td>';
 2009:                 unless (($context eq 'requestcourses') ||
 2010:                         ($context eq 'requestauthor')) {
 2011:                     $datatable .= 
 2012:                               '<td class="LC_right_item">'.
 2013:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2014:                               '<input type="text" name="quota_'.$type.
 2015:                               '" value="'.$currdefquota.
 2016:                               '" size="5" /></span>'.('&nbsp;' x 2).
 2017:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2018:                               '<input type="text" name="authorquota_'.$type.
 2019:                               '" value="'.$currauthorquota.
 2020:                               '" size="5" /></span></td>';
 2021:                 }
 2022:                 $datatable .= '</tr>';
 2023:             }
 2024:         }
 2025:     }
 2026:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2027:         $defaultquota = '20';
 2028:         $authorquota = '500';
 2029:         if (ref($settings) eq 'HASH') {
 2030:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2031:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2032:             } elsif (defined($settings->{'default'})) {
 2033:                 $defaultquota = $settings->{'default'};
 2034:             }
 2035:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 2036:                 $authorquota = $settings->{'authorquota'}->{'default'};
 2037:             }
 2038:         }
 2039:     }
 2040:     $typecount ++;
 2041:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2042:     $datatable .= '<tr'.$css_class.'>'.
 2043:                   '<td>'.$othertitle.'</td>'.
 2044:                   '<td class="LC_left_item">';
 2045:     if ($context eq 'requestcourses') {
 2046:         $datatable .= '<table><tr>';
 2047:     }
 2048:     my %defcell;
 2049:     foreach my $item (@usertools) {
 2050:         if ($context eq 'requestcourses') {
 2051:             my ($curroption,$currlimit);
 2052:             if (ref($settings) eq 'HASH') {
 2053:                 if (ref($settings->{$item}) eq 'HASH') {
 2054:                     $curroption = $settings->{$item}->{'default'};
 2055:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2056:                         $currlimit = $1;
 2057:                     }
 2058:                 }
 2059:             }
 2060:             if (!$curroption) {
 2061:                 $curroption = 'norequest';
 2062:             }
 2063:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2064:             foreach my $option (@options) {
 2065:                 my $val = $option;
 2066:                 if ($option eq 'norequest') {
 2067:                     $val = 0;
 2068:                 }
 2069:                 if ($option eq 'validate') {
 2070:                     my $canvalidate = 0;
 2071:                     if (ref($validations{$item}) eq 'HASH') {
 2072:                         if ($validations{$item}{'default'}) {
 2073:                             $canvalidate = 1;
 2074:                         }
 2075:                     }
 2076:                     next if (!$canvalidate);
 2077:                 }
 2078:                 my $checked = '';
 2079:                 if ($option eq $curroption) {
 2080:                     $checked = ' checked="checked"';
 2081:                 } elsif ($option eq 'autolimit') {
 2082:                     if ($curroption =~ /^autolimit/) {
 2083:                         $checked = ' checked="checked"';
 2084:                     }
 2085:                 }
 2086:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2087:                                   '<input type="radio" name="crsreq_'.$item.
 2088:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2089:                                   $titles{$option}.'</label>';
 2090:                 if ($option eq 'autolimit') {
 2091:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2092:                                        $item.'_limit_default" size="1" '.
 2093:                                        'value="'.$currlimit.'" />';
 2094:                 }
 2095:                 $defcell{$item} .= '</span> ';
 2096:                 if ($option eq 'autolimit') {
 2097:                     $defcell{$item} .= $titles{'unlimited'};
 2098:                 }
 2099:             }
 2100:         } elsif ($context eq 'requestauthor') {
 2101:             my $curroption;
 2102:             if (ref($settings) eq 'HASH') {
 2103:                 $curroption = $settings->{'default'};
 2104:             }
 2105:             if (!$curroption) {
 2106:                 $curroption = 'norequest';
 2107:             }
 2108:             foreach my $option (@options) {
 2109:                 my $val = $option;
 2110:                 if ($option eq 'norequest') {
 2111:                     $val = 0;
 2112:                 }
 2113:                 my $checked = '';
 2114:                 if ($option eq $curroption) {
 2115:                     $checked = ' checked="checked"';
 2116:                 }
 2117:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2118:                               '<input type="radio" name="authorreq_default"'.
 2119:                               ' value="'.$val.'"'.$checked.' />'.
 2120:                               $titles{$option}.'</label></span>&nbsp; ';
 2121:             }
 2122:         } else {
 2123:             my $checked = 'checked="checked" ';
 2124:             if (ref($settings) eq 'HASH') {
 2125:                 if (ref($settings->{$item}) eq 'HASH') {
 2126:                     if ($settings->{$item}->{'default'} == 0) {
 2127:                         $checked = '';
 2128:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2129:                         $checked = 'checked="checked" ';
 2130:                     }
 2131:                 }
 2132:             }
 2133:             $datatable .= '<span class="LC_nobreak"><label>'.
 2134:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2135:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2136:                           '</label></span>&nbsp; ';
 2137:         }
 2138:     }
 2139:     if ($context eq 'requestcourses') {
 2140:         $datatable .= '</tr><tr>';
 2141:         foreach my $item (@usertools) {
 2142:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2143:         }
 2144:         $datatable .= '</tr></table>';
 2145:     }
 2146:     $datatable .= '</td>';
 2147:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2148:         $datatable .= '<td class="LC_right_item">'.
 2149:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2150:                       '<input type="text" name="defaultquota" value="'.
 2151:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2152:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2153:                       '<input type="text" name="authorquota" value="'.
 2154:                       $authorquota.'" size="5" /></span></td>';
 2155:     }
 2156:     $datatable .= '</tr>';
 2157:     $typecount ++;
 2158:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2159:     $datatable .= '<tr'.$css_class.'>'.
 2160:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2161:     if ($context eq 'requestcourses') {
 2162:         $datatable .= &mt('(overrides affiliation, if set)').
 2163:                       '</td>'.
 2164:                       '<td class="LC_left_item">'.
 2165:                       '<table><tr>';
 2166:     } else {
 2167:         $datatable .= &mt('(overrides affiliation, if checked)').
 2168:                       '</td>'.
 2169:                       '<td class="LC_left_item" colspan="2">'.
 2170:                       '<br />';
 2171:     }
 2172:     my %advcell;
 2173:     foreach my $item (@usertools) {
 2174:         if ($context eq 'requestcourses') {
 2175:             my ($curroption,$currlimit);
 2176:             if (ref($settings) eq 'HASH') {
 2177:                 if (ref($settings->{$item}) eq 'HASH') {
 2178:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2179:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2180:                         $currlimit = $1;
 2181:                     }
 2182:                 }
 2183:             }
 2184:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2185:             my $checked = '';
 2186:             if ($curroption eq '') {
 2187:                 $checked = ' checked="checked"';
 2188:             }
 2189:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2190:                                '<input type="radio" name="crsreq_'.$item.
 2191:                                '__LC_adv" value=""'.$checked.' />'.
 2192:                                &mt('No override set').'</label></span>&nbsp; ';
 2193:             foreach my $option (@options) {
 2194:                 my $val = $option;
 2195:                 if ($option eq 'norequest') {
 2196:                     $val = 0;
 2197:                 }
 2198:                 if ($option eq 'validate') {
 2199:                     my $canvalidate = 0;
 2200:                     if (ref($validations{$item}) eq 'HASH') {
 2201:                         if ($validations{$item}{'_LC_adv'}) {
 2202:                             $canvalidate = 1;
 2203:                         }
 2204:                     }
 2205:                     next if (!$canvalidate);
 2206:                 }
 2207:                 my $checked = '';
 2208:                 if ($val eq $curroption) {
 2209:                     $checked = ' checked="checked"';
 2210:                 } elsif ($option eq 'autolimit') {
 2211:                     if ($curroption =~ /^autolimit/) {
 2212:                         $checked = ' checked="checked"';
 2213:                     }
 2214:                 }
 2215:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2216:                                   '<input type="radio" name="crsreq_'.$item.
 2217:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2218:                                   $titles{$option}.'</label>';
 2219:                 if ($option eq 'autolimit') {
 2220:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2221:                                        $item.'_limit__LC_adv" size="1" '.
 2222:                                        'value="'.$currlimit.'" />';
 2223:                 }
 2224:                 $advcell{$item} .= '</span> ';
 2225:                 if ($option eq 'autolimit') {
 2226:                     $advcell{$item} .= $titles{'unlimited'};
 2227:                 }
 2228:             }
 2229:         } elsif ($context eq 'requestauthor') {
 2230:             my $curroption;
 2231:             if (ref($settings) eq 'HASH') {
 2232:                 $curroption = $settings->{'_LC_adv'};
 2233:             }
 2234:             my $checked = '';
 2235:             if ($curroption eq '') {
 2236:                 $checked = ' checked="checked"';
 2237:             }
 2238:             $datatable .= '<span class="LC_nobreak"><label>'.
 2239:                           '<input type="radio" name="authorreq__LC_adv"'.
 2240:                           ' value=""'.$checked.' />'.
 2241:                           &mt('No override set').'</label></span>&nbsp; ';
 2242:             foreach my $option (@options) {
 2243:                 my $val = $option;
 2244:                 if ($option eq 'norequest') {
 2245:                     $val = 0;
 2246:                 }
 2247:                 my $checked = '';
 2248:                 if ($val eq $curroption) {
 2249:                     $checked = ' checked="checked"';
 2250:                 }
 2251:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2252:                               '<input type="radio" name="authorreq__LC_adv"'.
 2253:                               ' value="'.$val.'"'.$checked.' />'.
 2254:                               $titles{$option}.'</label></span>&nbsp; ';
 2255:             }
 2256:         } else {
 2257:             my $checked = 'checked="checked" ';
 2258:             if (ref($settings) eq 'HASH') {
 2259:                 if (ref($settings->{$item}) eq 'HASH') {
 2260:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2261:                         $checked = '';
 2262:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2263:                         $checked = 'checked="checked" ';
 2264:                     }
 2265:                 }
 2266:             }
 2267:             $datatable .= '<span class="LC_nobreak"><label>'.
 2268:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2269:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2270:                           '</label></span>&nbsp; ';
 2271:         }
 2272:     }
 2273:     if ($context eq 'requestcourses') {
 2274:         $datatable .= '</tr><tr>';
 2275:         foreach my $item (@usertools) {
 2276:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2277:         }
 2278:         $datatable .= '</tr></table>';
 2279:     }
 2280:     $datatable .= '</td></tr>';
 2281:     $$rowtotal += $typecount;
 2282:     return $datatable;
 2283: }
 2284: 
 2285: sub print_requestmail {
 2286:     my ($dom,$action,$settings,$rowtotal,$customcss,$rowstyle) = @_;
 2287:     my ($now,$datatable,%currapp);
 2288:     $now = time;
 2289:     if (ref($settings) eq 'HASH') {
 2290:         if (ref($settings->{'notify'}) eq 'HASH') {
 2291:             if ($settings->{'notify'}{'approval'} ne '') {
 2292:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2293:             }
 2294:         }
 2295:     }
 2296:     my $numinrow = 2;
 2297:     my $css_class;
 2298:     if ($$rowtotal%2) {
 2299:         $css_class = 'LC_odd_row';
 2300:     }
 2301:     if ($customcss) {
 2302:         $css_class .= " $customcss";
 2303:     }
 2304:     $css_class =~ s/^\s+//;
 2305:     if ($css_class) {
 2306:         $css_class = ' class="'.$css_class.'"';
 2307:     }
 2308:     if ($rowstyle) {
 2309:         $css_class .= ' style="'.$rowstyle.'"';
 2310:     }
 2311:     my $text;
 2312:     if ($action eq 'requestcourses') {
 2313:         $text = &mt('Receive notification of course requests requiring approval');
 2314:     } elsif ($action eq 'requestauthor') {
 2315:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2316:     } else {
 2317:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2318:     }
 2319:     $datatable = '<tr'.$css_class.'>'.
 2320:                  ' <td>'.$text.'</td>'.
 2321:                  ' <td class="LC_left_item">';
 2322:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2323:                                                  $action.'notifyapproval',%currapp);
 2324:     if ($numdc > 0) {
 2325:         $datatable .= $table;
 2326:     } else {
 2327:         $datatable .= &mt('There are no active Domain Coordinators');
 2328:     }
 2329:     $datatable .='</td></tr>';
 2330:     return $datatable;
 2331: }
 2332: 
 2333: sub print_studentcode {
 2334:     my ($settings,$rowtotal) = @_;
 2335:     my $rownum = 0; 
 2336:     my ($output,%current);
 2337:     my @crstypes = ('official','unofficial','community','textbook','placement');
 2338:     if (ref($settings) eq 'HASH') {
 2339:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2340:             foreach my $type (@crstypes) {
 2341:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2342:             }
 2343:         }
 2344:     }
 2345:     $output .= '<tr>'.
 2346:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2347:                '<td class="LC_left_item">';
 2348:     foreach my $type (@crstypes) {
 2349:         my $check = ' ';
 2350:         if ($current{$type}) {
 2351:             $check = ' checked="checked" ';
 2352:         }
 2353:         $output .= '<span class="LC_nobreak"><label>'.
 2354:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2355:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2356:     }
 2357:     $output .= '</td></tr>';
 2358:     $$rowtotal ++;
 2359:     return $output;
 2360: }
 2361: 
 2362: sub print_textbookcourses {
 2363:     my ($dom,$type,$settings,$rowtotal) = @_;
 2364:     my $rownum = 0;
 2365:     my $css_class;
 2366:     my $itemcount = 1;
 2367:     my $maxnum = 0;
 2368:     my $bookshash;
 2369:     if (ref($settings) eq 'HASH') {
 2370:         $bookshash = $settings->{$type};
 2371:     }
 2372:     my %ordered;
 2373:     if (ref($bookshash) eq 'HASH') {
 2374:         foreach my $item (keys(%{$bookshash})) {
 2375:             if (ref($bookshash->{$item}) eq 'HASH') {
 2376:                 my $num = $bookshash->{$item}{'order'};
 2377:                 $ordered{$num} = $item;
 2378:             }
 2379:         }
 2380:     }
 2381:     my $confname = $dom.'-domainconfig';
 2382:     my $switchserver = &check_switchserver($dom,$confname);
 2383:     my $maxnum = scalar(keys(%ordered));
 2384:     my $datatable;
 2385:     if (keys(%ordered)) {
 2386:         my @items = sort { $a <=> $b } keys(%ordered);
 2387:         for (my $i=0; $i<@items; $i++) {
 2388:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2389:             my $key = $ordered{$items[$i]};
 2390:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2391:             my $coursetitle = $coursehash{'description'};
 2392:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2393:             if (ref($bookshash->{$key}) eq 'HASH') {
 2394:                 $subject = $bookshash->{$key}->{'subject'};
 2395:                 $title = $bookshash->{$key}->{'title'};
 2396:                 if ($type eq 'textbooks') {
 2397:                     $publisher = $bookshash->{$key}->{'publisher'};
 2398:                     $author = $bookshash->{$key}->{'author'};
 2399:                     $image = $bookshash->{$key}->{'image'};
 2400:                     if ($image ne '') {
 2401:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2402:                         my $imagethumb = "$path/tn-".$imagefile;
 2403:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2404:                     }
 2405:                 }
 2406:             }
 2407:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2408:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2409:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2410:             for (my $k=0; $k<=$maxnum; $k++) {
 2411:                 my $vpos = $k+1;
 2412:                 my $selstr;
 2413:                 if ($k == $i) {
 2414:                     $selstr = ' selected="selected" ';
 2415:                 }
 2416:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2417:             }
 2418:             $datatable .= '</select>'.('&nbsp;'x2).
 2419:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2420:                 &mt('Delete?').'</label></span></td>'.
 2421:                 '<td colspan="2">'.
 2422:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2423:                 ('&nbsp;'x2).
 2424:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2425:             if ($type eq 'textbooks') {
 2426:                 $datatable .= ('&nbsp;'x2).
 2427:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2428:                               ('&nbsp;'x2).
 2429:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2430:                               ('&nbsp;'x2).
 2431:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2432:                 if ($image) {
 2433:                     $datatable .= $imgsrc.
 2434:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2435:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2436:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2437:                 }
 2438:                 if ($switchserver) {
 2439:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2440:                 } else {
 2441:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2442:                 }
 2443:             }
 2444:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2445:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2446:                           $coursetitle.'</span></td></tr>'."\n";
 2447:             $itemcount ++;
 2448:         }
 2449:     }
 2450:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2451:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2452:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2453:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2454:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2455:     for (my $k=0; $k<$maxnum+1; $k++) {
 2456:         my $vpos = $k+1;
 2457:         my $selstr;
 2458:         if ($k == $maxnum) {
 2459:             $selstr = ' selected="selected" ';
 2460:         }
 2461:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2462:     }
 2463:     $datatable .= '</select>&nbsp;'."\n".
 2464:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2465:                   '<td colspan="2">'.
 2466:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2467:                   ('&nbsp;'x2).
 2468:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2469:                   ('&nbsp;'x2);
 2470:     if ($type eq 'textbooks') {
 2471:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2472:                       ('&nbsp;'x2).
 2473:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2474:                       ('&nbsp;'x2).
 2475:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2476:         if ($switchserver) {
 2477:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2478:         } else {
 2479:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2480:         }
 2481:     }
 2482:     $datatable .= '</span>'."\n".
 2483:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2484:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2485:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2486:                   &Apache::loncommon::selectcourse_link
 2487:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course');
 2488:                   '</span></td>'."\n".
 2489:                   '</tr>'."\n";
 2490:     $itemcount ++;
 2491:     return $datatable;
 2492: }
 2493: 
 2494: sub textbookcourses_javascript {
 2495:     my ($settings) = @_;
 2496:     return unless(ref($settings) eq 'HASH');
 2497:     my (%ordered,%total,%jstext);
 2498:     foreach my $type ('textbooks','templates') {
 2499:         $total{$type} = 0;
 2500:         if (ref($settings->{$type}) eq 'HASH') {
 2501:             foreach my $item (keys(%{$settings->{$type}})) {
 2502:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2503:                     my $num = $settings->{$type}->{$item}{'order'};
 2504:                     $ordered{$type}{$num} = $item;
 2505:                 }
 2506:             }
 2507:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2508:         }
 2509:         my @jsarray = ();
 2510:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2511:             push(@jsarray,$ordered{$type}{$item});
 2512:         }
 2513:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2514:     }
 2515:     return <<"ENDSCRIPT";
 2516: <script type="text/javascript">
 2517: // <![CDATA[
 2518: function reorderBooks(form,item,caller) {
 2519:     var changedVal;
 2520: $jstext{'textbooks'};
 2521: $jstext{'templates'};
 2522:     var newpos;
 2523:     var maxh;
 2524:     if (caller == 'textbooks') {  
 2525:         newpos = 'textbooks_addbook_pos';
 2526:         maxh = 1 + $total{'textbooks'};
 2527:     } else {
 2528:         newpos = 'templates_addbook_pos';
 2529:         maxh = 1 + $total{'templates'};
 2530:     }
 2531:     var current = new Array;
 2532:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2533:     if (item == newpos) {
 2534:         changedVal = newitemVal;
 2535:     } else {
 2536:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2537:         current[newitemVal] = newpos;
 2538:     }
 2539:     if (caller == 'textbooks') {
 2540:         for (var i=0; i<textbooks.length; i++) {
 2541:             var elementName = 'textbooks_'+textbooks[i];
 2542:             if (elementName != item) {
 2543:                 if (form.elements[elementName]) {
 2544:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2545:                     current[currVal] = elementName;
 2546:                 }
 2547:             }
 2548:         }
 2549:     }
 2550:     if (caller == 'templates') {
 2551:         for (var i=0; i<templates.length; i++) {
 2552:             var elementName = 'templates_'+templates[i];
 2553:             if (elementName != item) {
 2554:                 if (form.elements[elementName]) {
 2555:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2556:                     current[currVal] = elementName;
 2557:                 }
 2558:             }
 2559:         }
 2560:     }
 2561:     var oldVal;
 2562:     for (var j=0; j<maxh; j++) {
 2563:         if (current[j] == undefined) {
 2564:             oldVal = j;
 2565:         }
 2566:     }
 2567:     if (oldVal < changedVal) {
 2568:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2569:            var elementName = current[k];
 2570:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2571:         }
 2572:     } else {
 2573:         for (var k=changedVal; k<oldVal; k++) {
 2574:             var elementName = current[k];
 2575:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2576:         }
 2577:     }
 2578:     return;
 2579: }
 2580: 
 2581: // ]]>
 2582: </script>
 2583: 
 2584: ENDSCRIPT
 2585: }
 2586: 
 2587: sub ltitools_javascript {
 2588:     my ($settings) = @_;
 2589:     return unless(ref($settings) eq 'HASH');
 2590:     my (%ordered,$total,%jstext);
 2591:     $total = 0;
 2592:     foreach my $item (keys(%{$settings})) {
 2593:         if (ref($settings->{$item}) eq 'HASH') {
 2594:             my $num = $settings->{$item}{'order'};
 2595:             $ordered{$num} = $item;
 2596:         }
 2597:     }
 2598:     $total = scalar(keys(%{$settings}));
 2599:     my @jsarray = ();
 2600:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2601:         push(@jsarray,$ordered{$item});
 2602:     }
 2603:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 2604:     return <<"ENDSCRIPT";
 2605: <script type="text/javascript">
 2606: // <![CDATA[
 2607: function reorderLTI(form,item) {
 2608:     var changedVal;
 2609: $jstext
 2610:     var newpos = 'ltitools_add_pos';
 2611:     var maxh = 1 + $total;
 2612:     var current = new Array;
 2613:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2614:     if (item == newpos) {
 2615:         changedVal = newitemVal;
 2616:     } else {
 2617:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2618:         current[newitemVal] = newpos;
 2619:     }
 2620:     for (var i=0; i<ltitools.length; i++) {
 2621:         var elementName = 'ltitools_'+ltitools[i];
 2622:         if (elementName != item) {
 2623:             if (form.elements[elementName]) {
 2624:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2625:                 current[currVal] = elementName;
 2626:             }
 2627:         }
 2628:     }
 2629:     var oldVal;
 2630:     for (var j=0; j<maxh; j++) {
 2631:         if (current[j] == undefined) {
 2632:             oldVal = j;
 2633:         }
 2634:     }
 2635:     if (oldVal < changedVal) {
 2636:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2637:            var elementName = current[k];
 2638:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2639:         }
 2640:     } else {
 2641:         for (var k=changedVal; k<oldVal; k++) {
 2642:             var elementName = current[k];
 2643:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2644:         }
 2645:     }
 2646:     return;
 2647: }
 2648: 
 2649: // ]]>
 2650: </script>
 2651: 
 2652: ENDSCRIPT
 2653: }
 2654: 
 2655: sub print_autoenroll {
 2656:     my ($dom,$settings,$rowtotal) = @_;
 2657:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2658:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 2659:     if (ref($settings) eq 'HASH') {
 2660:         if (exists($settings->{'run'})) {
 2661:             if ($settings->{'run'} eq '0') {
 2662:                 $runoff = ' checked="checked" ';
 2663:                 $runon = ' ';
 2664:             } else {
 2665:                 $runon = ' checked="checked" ';
 2666:                 $runoff = ' ';
 2667:             }
 2668:         } else {
 2669:             if ($autorun) {
 2670:                 $runon = ' checked="checked" ';
 2671:                 $runoff = ' ';
 2672:             } else {
 2673:                 $runoff = ' checked="checked" ';
 2674:                 $runon = ' ';
 2675:             }
 2676:         }
 2677:         if (exists($settings->{'co-owners'})) {
 2678:             if ($settings->{'co-owners'} eq '0') {
 2679:                 $coownersoff = ' checked="checked" ';
 2680:                 $coownerson = ' ';
 2681:             } else {
 2682:                 $coownerson = ' checked="checked" ';
 2683:                 $coownersoff = ' ';
 2684:             }
 2685:         } else {
 2686:             $coownersoff = ' checked="checked" ';
 2687:             $coownerson = ' ';
 2688:         }
 2689:         if (exists($settings->{'sender_domain'})) {
 2690:             $defdom = $settings->{'sender_domain'};
 2691:         }
 2692:         if (exists($settings->{'autofailsafe'})) {
 2693:             $failsafe = $settings->{'autofailsafe'};
 2694:         }
 2695:     } else {
 2696:         if ($autorun) {
 2697:             $runon = ' checked="checked" ';
 2698:             $runoff = ' ';
 2699:         } else {
 2700:             $runoff = ' checked="checked" ';
 2701:             $runon = ' ';
 2702:         }
 2703:     }
 2704:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2705:     my $notif_sender;
 2706:     if (ref($settings) eq 'HASH') {
 2707:         $notif_sender = $settings->{'sender_uname'};
 2708:     }
 2709:     my $datatable='<tr class="LC_odd_row">'.
 2710:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2711:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2712:                   '<input type="radio" name="autoenroll_run"'.
 2713:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2714:                   '<label><input type="radio" name="autoenroll_run"'.
 2715:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2716:                   '</tr><tr>'.
 2717:                   '<td>'.&mt('Notification messages - sender').
 2718:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2719:                   &mt('username').':&nbsp;'.
 2720:                   '<input type="text" name="sender_uname" value="'.
 2721:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2722:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2723:                   '<tr class="LC_odd_row">'.
 2724:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2725:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2726:                   '<input type="radio" name="autoassign_coowners"'.
 2727:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2728:                   '<label><input type="radio" name="autoassign_coowners"'.
 2729:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2730:                   '</tr><tr>'.
 2731:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 2732:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2733:                   '<input type="text" name="autoenroll_failsafe"'.
 2734:                   ' value="'.$failsafe.'" size="4" /></td></tr>';
 2735:     $$rowtotal += 4;
 2736:     return $datatable;
 2737: }
 2738: 
 2739: sub print_autoupdate {
 2740:     my ($position,$dom,$settings,$rowtotal) = @_;
 2741:     my $datatable;
 2742:     if ($position eq 'top') {
 2743:         my $updateon = ' ';
 2744:         my $updateoff = ' checked="checked" ';
 2745:         my $classlistson = ' ';
 2746:         my $classlistsoff = ' checked="checked" ';
 2747:         if (ref($settings) eq 'HASH') {
 2748:             if ($settings->{'run'} eq '1') {
 2749:                 $updateon = $updateoff;
 2750:                 $updateoff = ' ';
 2751:             }
 2752:             if ($settings->{'classlists'} eq '1') {
 2753:                 $classlistson = $classlistsoff;
 2754:                 $classlistsoff = ' ';
 2755:             }
 2756:         }
 2757:         my %title = (
 2758:                    run => 'Auto-update active?',
 2759:                    classlists => 'Update information in classlists?',
 2760:                     );
 2761:         $datatable = '<tr class="LC_odd_row">'. 
 2762:                   '<td>'.&mt($title{'run'}).'</td>'.
 2763:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2764:                   '<input type="radio" name="autoupdate_run"'.
 2765:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2766:                   '<label><input type="radio" name="autoupdate_run"'.
 2767:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2768:                   '</tr><tr>'.
 2769:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2770:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2771:                   '<label><input type="radio" name="classlists"'.
 2772:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2773:                   '<label><input type="radio" name="classlists"'.
 2774:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2775:                   '</tr>';
 2776:         $$rowtotal += 2;
 2777:     } elsif ($position eq 'middle') {
 2778:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2779:         my $numinrow = 3;
 2780:         my $locknamesettings;
 2781:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2782:                                      $dom,$numinrow,$othertitle,
 2783:                                     'lockablenames',$rowtotal);
 2784:         $$rowtotal ++;
 2785:     } else {
 2786:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2787:         my @fields = ('lastname','firstname','middlename','generation',
 2788:                       'permanentemail','id');
 2789:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2790:         my $numrows = 0;
 2791:         if (ref($types) eq 'ARRAY') {
 2792:             if (@{$types} > 0) {
 2793:                 $datatable = 
 2794:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2795:                                          \@fields,$types,\$numrows);
 2796:                     $$rowtotal += @{$types}; 
 2797:             }
 2798:         }
 2799:         $datatable .= 
 2800:             &usertype_update_row($settings,{'default' => $othertitle},
 2801:                                  \%fieldtitles,\@fields,['default'],
 2802:                                  \$numrows);
 2803:         $$rowtotal ++;     
 2804:     }
 2805:     return $datatable;
 2806: }
 2807: 
 2808: sub print_autocreate {
 2809:     my ($dom,$settings,$rowtotal) = @_;
 2810:     my (%createon,%createoff,%currhash);
 2811:     my @types = ('xml','req');
 2812:     if (ref($settings) eq 'HASH') {
 2813:         foreach my $item (@types) {
 2814:             $createoff{$item} = ' checked="checked" ';
 2815:             $createon{$item} = ' ';
 2816:             if (exists($settings->{$item})) {
 2817:                 if ($settings->{$item}) {
 2818:                     $createon{$item} = ' checked="checked" ';
 2819:                     $createoff{$item} = ' ';
 2820:                 }
 2821:             }
 2822:         }
 2823:         if ($settings->{'xmldc'} ne '') {
 2824:             $currhash{$settings->{'xmldc'}} = 1;
 2825:         }
 2826:     } else {
 2827:         foreach my $item (@types) {
 2828:             $createoff{$item} = ' checked="checked" ';
 2829:             $createon{$item} = ' ';
 2830:         }
 2831:     }
 2832:     $$rowtotal += 2;
 2833:     my $numinrow = 2;
 2834:     my $datatable='<tr class="LC_odd_row">'.
 2835:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2836:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2837:                   '<input type="radio" name="autocreate_xml"'.
 2838:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2839:                   '<label><input type="radio" name="autocreate_xml"'.
 2840:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2841:                   '</td></tr><tr>'.
 2842:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2843:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2844:                   '<input type="radio" name="autocreate_req"'.
 2845:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2846:                   '<label><input type="radio" name="autocreate_req"'.
 2847:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2848:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2849:                                                    'autocreate_xmldc',%currhash);
 2850:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2851:     if ($numdc > 1) {
 2852:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2853:                       '</td><td class="LC_left_item">';
 2854:     } else {
 2855:         $datatable .= &mt('Course creation processed as:').
 2856:                       '</td><td class="LC_right_item">';
 2857:     }
 2858:     $datatable .= $dctable.'</td></tr>';
 2859:     $$rowtotal += $rows;
 2860:     return $datatable;
 2861: }
 2862: 
 2863: sub print_directorysrch {
 2864:     my ($position,$dom,$settings,$rowtotal) = @_;
 2865:     my $datatable;
 2866:     if ($position eq 'top') {
 2867:         my $instsrchon = ' ';
 2868:         my $instsrchoff = ' checked="checked" ';
 2869:         my ($exacton,$containson,$beginson);
 2870:         my $instlocalon = ' ';
 2871:         my $instlocaloff = ' checked="checked" ';
 2872:         if (ref($settings) eq 'HASH') {
 2873:             if ($settings->{'available'} eq '1') {
 2874:                 $instsrchon = $instsrchoff;
 2875:                 $instsrchoff = ' ';
 2876:             }
 2877:             if ($settings->{'localonly'} eq '1') {
 2878:                 $instlocalon = $instlocaloff;
 2879:                 $instlocaloff = ' ';
 2880:             }
 2881:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2882:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 2883:                     if ($type eq 'exact') {
 2884:                         $exacton = ' checked="checked" ';
 2885:                     } elsif ($type eq 'contains') {
 2886:                         $containson = ' checked="checked" ';
 2887:                     } elsif ($type eq 'begins') {
 2888:                         $beginson = ' checked="checked" ';
 2889:                     }
 2890:                 }
 2891:             } else {
 2892:                 if ($settings->{'searchtypes'} eq 'exact') {
 2893:                     $exacton = ' checked="checked" ';
 2894:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 2895:                     $containson = ' checked="checked" ';
 2896:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 2897:                     $exacton = ' checked="checked" ';
 2898:                     $containson = ' checked="checked" ';
 2899:                 }
 2900:             }
 2901:         }
 2902:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2903:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2904: 
 2905:         my $numinrow = 4;
 2906:         my $cansrchrow = 0;
 2907:         $datatable='<tr class="LC_odd_row">'.
 2908:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 2909:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2910:                    '<input type="radio" name="dirsrch_available"'.
 2911:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2912:                    '<label><input type="radio" name="dirsrch_available"'.
 2913:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2914:                    '</tr><tr>'.
 2915:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 2916:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2917:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 2918:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2919:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 2920:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2921:                    '</tr>';
 2922:         $$rowtotal += 2;
 2923:         if (ref($usertypes) eq 'HASH') {
 2924:             if (keys(%{$usertypes}) > 0) {
 2925:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2926:                                              $numinrow,$othertitle,'cansearch',
 2927:                                              $rowtotal);
 2928:                 $cansrchrow = 1;
 2929:             }
 2930:         }
 2931:         if ($cansrchrow) {
 2932:             $$rowtotal ++;
 2933:             $datatable .= '<tr>';
 2934:         } else {
 2935:             $datatable .= '<tr class="LC_odd_row">';
 2936:         }
 2937:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2938:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2939:         foreach my $title (@{$titleorder}) {
 2940:             if (defined($searchtitles->{$title})) {
 2941:                 my $check = ' ';
 2942:                 if (ref($settings) eq 'HASH') {
 2943:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2944:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2945:                             $check = ' checked="checked" ';
 2946:                         }
 2947:                     }
 2948:                 }
 2949:                 $datatable .= '<td class="LC_left_item">'.
 2950:                               '<span class="LC_nobreak"><label>'.
 2951:                               '<input type="checkbox" name="searchby" '.
 2952:                               'value="'.$title.'"'.$check.'/>'.
 2953:                               $searchtitles->{$title}.'</label></span></td>';
 2954:             }
 2955:         }
 2956:         $datatable .= '</tr></table></td></tr>';
 2957:         $$rowtotal ++;
 2958:         if ($cansrchrow) {
 2959:             $datatable .= '<tr class="LC_odd_row">';
 2960:         } else {
 2961:             $datatable .= '<tr>';
 2962:         }
 2963:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2964:                       '<td class="LC_left_item" colspan="2">'.
 2965:                       '<span class="LC_nobreak"><label>'.
 2966:                       '<input type="checkbox" name="searchtypes" '.
 2967:                       $exacton.' value="exact" />'.&mt('Exact match').
 2968:                       '</label>&nbsp;'.
 2969:                       '<label><input type="checkbox" name="searchtypes" '.
 2970:                       $beginson.' value="begins" />'.&mt('Begins with').
 2971:                       '</label>&nbsp;'.
 2972:                       '<label><input type="checkbox" name="searchtypes" '.
 2973:                       $containson.' value="contains" />'.&mt('Contains').
 2974:                       '</label></span></td></tr>';
 2975:         $$rowtotal ++;
 2976:     } else {
 2977:         my $domsrchon = ' checked="checked" ';
 2978:         my $domsrchoff = ' ';
 2979:         my $domlocalon = ' ';
 2980:         my $domlocaloff = ' checked="checked" ';
 2981:         if (ref($settings) eq 'HASH') {
 2982:             if ($settings->{'lclocalonly'} eq '1') {
 2983:                 $domlocalon = $domlocaloff;
 2984:                 $domlocaloff = ' ';
 2985:             }
 2986:             if ($settings->{'lcavailable'} eq '0') {
 2987:                 $domsrchoff = $domsrchon;
 2988:                 $domsrchon = ' ';
 2989:             }
 2990:         }
 2991:         $datatable='<tr class="LC_odd_row">'.
 2992:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 2993:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2994:                       '<input type="radio" name="dirsrch_domavailable"'.
 2995:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2996:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 2997:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2998:                       '</tr><tr>'.
 2999:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 3000:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 3001:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 3002:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 3003:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 3004:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 3005:                       '</tr>';
 3006:         $$rowtotal += 2;
 3007:     }
 3008:     return $datatable;
 3009: }
 3010: 
 3011: sub print_contacts {
 3012:     my ($position,$dom,$settings,$rowtotal) = @_;
 3013:     my $datatable;
 3014:     my @contacts = ('adminemail','supportemail');
 3015:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 3016:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings);
 3017:     if ($position eq 'top') {
 3018:         if (ref($settings) eq 'HASH') {
 3019:             foreach my $item (@contacts) {
 3020:                 if (exists($settings->{$item})) {
 3021:                     $to{$item} = $settings->{$item};
 3022:                 }
 3023:             }
 3024:         }
 3025:     } elsif ($position eq 'middle') {
 3026:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 3027:                      'updatesmail','idconflictsmail');
 3028:         foreach my $type (@mailings) {
 3029:             $otheremails{$type} = '';
 3030:         }
 3031:     } else {
 3032:         @mailings = ('helpdeskmail','otherdomsmail');
 3033:         foreach my $type (@mailings) {
 3034:             $otheremails{$type} = '';
 3035:         }
 3036:         $bccemails{'helpdeskmail'} = '';
 3037:         $bccemails{'otherdomsmail'} = '';
 3038:         $includestr{'helpdeskmail'} = '';
 3039:         $includestr{'otherdomsmail'} = '';
 3040:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 3041:     }
 3042:     if (ref($settings) eq 'HASH') {
 3043:         unless ($position eq 'top') {
 3044:             foreach my $type (@mailings) {
 3045:                 if (exists($settings->{$type})) {
 3046:                     if (ref($settings->{$type}) eq 'HASH') {
 3047:                         foreach my $item (@contacts) {
 3048:                             if ($settings->{$type}{$item}) {
 3049:                                 $checked{$type}{$item} = ' checked="checked" ';
 3050:                             }
 3051:                         }
 3052:                         $otheremails{$type} = $settings->{$type}{'others'};
 3053:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3054:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 3055:                             if ($settings->{$type}{'include'} ne '') {
 3056:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3057:                                 $includestr{$type} = &unescape($includestr{$type});
 3058:                             }
 3059:                         }
 3060:                     }
 3061:                 } elsif ($type eq 'lonstatusmail') {
 3062:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3063:                 }
 3064:             }
 3065:         }
 3066:         if ($position eq 'bottom') {
 3067:             foreach my $type (@mailings) {
 3068:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 3069:                 if ($settings->{$type}{'include'} ne '') {
 3070:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3071:                     $includestr{$type} = &unescape($includestr{$type});
 3072:                 }
 3073:             }
 3074:             if (ref($settings->{'helpform'}) eq 'HASH') {
 3075:                 if (ref($fields) eq 'ARRAY') {
 3076:                     foreach my $field (@{$fields}) {
 3077:                         $currfield{$field} = $settings->{'helpform'}{$field};
 3078:                     }
 3079:                 }
 3080:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 3081:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 3082:                 } else {
 3083:                     $maxsize = '1.0';
 3084:                 }
 3085:             } else {
 3086:                 if (ref($fields) eq 'ARRAY') {
 3087:                     foreach my $field (@{$fields}) {
 3088:                         $currfield{$field} = 'yes';
 3089:                     }
 3090:                 }
 3091:                 $maxsize = '1.0';
 3092:             }
 3093:         }
 3094:     } else {
 3095:         if ($position eq 'top') {
 3096:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 3097:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 3098:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 3099:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 3100:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3101:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 3102:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 3103:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 3104:         } elsif ($position eq 'bottom') {
 3105:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 3106:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 3107:             if (ref($fields) eq 'ARRAY') {
 3108:                 foreach my $field (@{$fields}) {
 3109:                     $currfield{$field} = 'yes';
 3110:                 }
 3111:             }
 3112:             $maxsize = '1.0';
 3113:         }
 3114:     }
 3115:     my ($titles,$short_titles) = &contact_titles();
 3116:     my $rownum = 0;
 3117:     my $css_class;
 3118:     if ($position eq 'top') {
 3119:         foreach my $item (@contacts) {
 3120:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3121:             $datatable .= '<tr'.$css_class.'>'. 
 3122:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 3123:                           '</span></td><td class="LC_right_item">'.
 3124:                           '<input type="text" name="'.$item.'" value="'.
 3125:                           $to{$item}.'" /></td></tr>';
 3126:             $rownum ++;
 3127:         }
 3128:     } elsif ($position eq 'bottom') {
 3129:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3130:         $datatable .= '<tr'.$css_class.'>'.
 3131:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 3132:                       &mt('(e-mail, subject, and description always shown)').
 3133:                       '</td><td class="LC_left_item">';
 3134:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 3135:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 3136:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 3137:             foreach my $field (@{$fields}) {
 3138:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 3139:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 3140:                     $datatable .= ' '.&mt('(logged-in users)');
 3141:                 }
 3142:                 $datatable .='</td><td>';
 3143:                 my $clickaction;
 3144:                 if ($field eq 'screenshot') {
 3145:                     $clickaction = ' onclick="screenshotSize(this);"';
 3146:                 }
 3147:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 3148:                     foreach my $option (@{$possoptions->{$field}}) {
 3149:                         my $checked;
 3150:                         if ($currfield{$field} eq $option) {
 3151:                             $checked = ' checked="checked"';
 3152:                         }
 3153:                         $datatable .= '<span class="LC_nobreak"><label>'.
 3154:                                       '<input type="radio" name="helpform_'.$field.'" '.
 3155:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 3156:                                       '</label></span>'.('&nbsp;'x2);
 3157:                     }
 3158:                 }
 3159:                 if ($field eq 'screenshot') {
 3160:                     my $display;
 3161:                     if ($currfield{$field} eq 'no') {
 3162:                         $display = ' style="display:none"';
 3163:                     }
 3164:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.' />'.
 3165:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3166:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3167:                 }
 3168:                 $datatable .= '</td></tr>';
 3169:             }
 3170:             $datatable .= '</table>';
 3171:         }
 3172:         $datatable .= '</td></tr>'."\n";
 3173:         $rownum ++;
 3174:     }
 3175:     unless ($position eq 'top') {
 3176:         foreach my $type (@mailings) {
 3177:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3178:             $datatable .= '<tr'.$css_class.'>'.
 3179:                           '<td><span class="LC_nobreak">'.
 3180:                           $titles->{$type}.': </span></td>'.
 3181:                           '<td class="LC_left_item">';
 3182:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3183:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 3184:             }
 3185:             $datatable .= '<span class="LC_nobreak">';
 3186:             foreach my $item (@contacts) {
 3187:                 $datatable .= '<label>'.
 3188:                               '<input type="checkbox" name="'.$type.'"'.
 3189:                               $checked{$type}{$item}.
 3190:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 3191:                               '</label>&nbsp;';
 3192:             }
 3193:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3194:                           '<input type="text" name="'.$type.'_others" '.
 3195:                           'value="'.$otheremails{$type}.'"  />';
 3196:             my %locchecked;
 3197:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3198:                 foreach my $loc ('s','b') {
 3199:                     if ($includeloc{$type} eq $loc) {
 3200:                         $locchecked{$loc} = ' checked="checked"';
 3201:                         last;
 3202:                     }
 3203:                 }
 3204:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3205:                               '<input type="text" name="'.$type.'_bcc" '.
 3206:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 3207:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3208:                               &mt('Text automatically added to e-mail:').' '.
 3209:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br >'.
 3210:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3211:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3212:                               ('&nbsp;'x2).
 3213:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3214:                               '</span></fieldset>';
 3215:             }
 3216:             $datatable .= '</td></tr>'."\n";
 3217:             $rownum ++;
 3218:         }
 3219:     }
 3220:     if ($position eq 'middle') {
 3221:         my %choices;
 3222:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 3223:                                        &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3224:                                        &mt('LON-CAPA core group - MSU'),600,500));
 3225:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3226:                                         &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3227:                                         &mt('LON-CAPA core group - MSU'),600,500));
 3228:         my @toggles = ('reporterrors','reportupdates');
 3229:         my %defaultchecked = ('reporterrors'  => 'on',
 3230:                               'reportupdates' => 'on');
 3231:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3232:                                                    \%choices,$rownum);
 3233:         $datatable .= $reports;
 3234:     } elsif ($position eq 'bottom') {
 3235:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3236:         my (@posstypes,%usertypeshash);
 3237:         if (ref($types) eq 'ARRAY') {
 3238:             @posstypes = @{$types};
 3239:         }
 3240:         if (@posstypes) {
 3241:             if (ref($usertypes) eq 'HASH') {
 3242:                 %usertypeshash = %{$usertypes};
 3243:             }
 3244:             my @overridden;
 3245:             my $numinrow = 4;
 3246:             if (ref($settings) eq 'HASH') {
 3247:                 if (ref($settings->{'overrides'}) eq 'HASH') {
 3248:                     foreach my $key (sort(keys(%{$settings->{'overrides'}}))) {
 3249:                         if (ref($settings->{'overrides'}{$key}) eq 'HASH') {
 3250:                             push(@overridden,$key);
 3251:                             foreach my $item (@contacts) {
 3252:                                 if ($settings->{'overrides'}{$key}{$item}) {
 3253:                                     $checked{'override_'.$key}{$item} = ' checked="checked" ';
 3254:                                 }
 3255:                             }
 3256:                             $otheremails{'override_'.$key} = $settings->{'overrides'}{$key}{'others'};
 3257:                             $bccemails{'override_'.$key} = $settings->{'overrides'}{$key}{'bcc'};
 3258:                             $includeloc{'override_'.$key} = '';
 3259:                             $includestr{'override_'.$key} = '';
 3260:                             if ($settings->{'overrides'}{$key}{'include'} ne '') {
 3261:                                 ($includeloc{'override_'.$key},$includestr{'override_'.$key}) = 
 3262:                                     split(/:/,$settings->{'overrides'}{$key}{'include'},2);
 3263:                                 $includestr{'override_'.$key} = &unescape($includestr{'override_'.$key});
 3264:                             }
 3265:                         }
 3266:                     }
 3267:                 }
 3268:             }
 3269:             my $customclass = 'LC_helpdesk_override';
 3270:             my $optionsprefix = 'LC_options_helpdesk_';
 3271: 
 3272:             my $onclicktypes = "toggleHelpdeskRow(this.form,'overrides','$customclass','$optionsprefix');";
 3273:  
 3274:             $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 3275:                                          $numinrow,$othertitle,'overrides',
 3276:                                          \$rownum,$onclicktypes,$customclass);
 3277:             $rownum ++;
 3278:             $usertypeshash{'default'} = $othertitle;
 3279:             foreach my $status (@posstypes) {
 3280:                 my $css_class;
 3281:                 if ($rownum%2) {
 3282:                     $css_class = 'LC_odd_row ';
 3283:                 }
 3284:                 $css_class .= $customclass;
 3285:                 my $rowid = $optionsprefix.$status;
 3286:                 my $hidden = 1;
 3287:                 my $currstyle = 'display:none';
 3288:                 if (grep(/^\Q$status\E$/,@overridden)) {
 3289:                     $currstyle = 'display:table-row';
 3290:                     $hidden = 0;
 3291:                 }
 3292:                 my $key = 'override_'.$status;
 3293:                 $datatable .= &overridden_helpdesk($checked{$key},$otheremails{$key},$bccemails{$key},
 3294:                                                   $includeloc{$key},$includestr{$key},$status,$rowid,
 3295:                                                   $usertypeshash{$status},$css_class,$currstyle,
 3296:                                                   \@contacts,$short_titles);
 3297:                 unless ($hidden) {
 3298:                     $rownum ++;
 3299:                 }
 3300:             }
 3301:         }
 3302:     }
 3303:     $$rowtotal += $rownum;
 3304:     return $datatable;
 3305: }
 3306: 
 3307: sub overridden_helpdesk {
 3308:     my ($checked,$otheremails,$bccemails,$includeloc,$includestr,$type,$rowid,
 3309:         $typetitle,$css_class,$rowstyle,$contacts,$short_titles) = @_;
 3310:     my $class = 'LC_left_item';
 3311:     if ($css_class) {
 3312:         $css_class = ' class="'.$css_class.'"';
 3313:     }
 3314:     if ($rowid) {
 3315:         $rowid = ' id="'.$rowid.'"';
 3316:     }
 3317:     if ($rowstyle) {
 3318:         $rowstyle = ' style="'.$rowstyle.'"';
 3319:     }
 3320:     my ($output,$description);
 3321:     $description = &mt('Helpdesk requests from: [_1] in this domain (overrides default)',"<b>$typetitle</b>");
 3322:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 3323:               "<td>$description</td>\n".
 3324:               '<td class="'.$class.'" colspan="2">'.
 3325:               '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>'.
 3326:               '<span class="LC_nobreak">';
 3327:     if (ref($contacts) eq 'ARRAY') {
 3328:         foreach my $item (@{$contacts}) {
 3329:             my $check;
 3330:             if (ref($checked) eq 'HASH') {
 3331:                $check = $checked->{$item};
 3332:             }
 3333:             my $title;
 3334:             if (ref($short_titles) eq 'HASH') {
 3335:                 $title = $short_titles->{$item}; 
 3336:             }
 3337:             $output .= '<label>'.
 3338:                        '<input type="checkbox" name="override_'.$type.'"'.$check.
 3339:                        ' value="'.$item.'" />'.$title.'</label>&nbsp;';
 3340:         }
 3341:     }
 3342:     $output .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3343:                '<input type="text" name="override_'.$type.'_others" '.
 3344:                'value="'.$otheremails.'"  />';
 3345:     my %locchecked;
 3346:     foreach my $loc ('s','b') {
 3347:         if ($includeloc eq $loc) {
 3348:             $locchecked{$loc} = ' checked="checked"';
 3349:             last;
 3350:         }
 3351:     }
 3352:     $output .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3353:                '<input type="text" name="override_'.$type.'_bcc" '.
 3354:                'value="'.$bccemails.'"  /></fieldset>'.
 3355:                '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3356:                &mt('Text automatically added to e-mail:').' '.
 3357:                '<input type="text" name="override_'.$type.'_includestr" value="'.$includestr.'" /><br >'.
 3358:                '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3359:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3360:                ('&nbsp;'x2).
 3361:                '<label><input type="radio" name="override_'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3362:                '</span></fieldset>'.
 3363:                '</td></tr>'."\n";
 3364:     return $output;
 3365: }
 3366: 
 3367: sub contacts_javascript {
 3368:     return <<"ENDSCRIPT";
 3369: 
 3370: <script type="text/javascript">
 3371: // <![CDATA[
 3372: 
 3373: function screenshotSize(field) {
 3374:     if (document.getElementById('help_screenshotsize')) {
 3375:         if (field.value == 'no') {
 3376:             document.getElementById('help_screenshotsize').style.display="none";
 3377:         } else {
 3378:             document.getElementById('help_screenshotsize').style.display="";
 3379:         }
 3380:     }
 3381:     return;
 3382: }
 3383: 
 3384: function toggleHelpdeskRow(form,checkbox,target,prefix,docount) {
 3385:     if (form.elements[checkbox].length != undefined) {
 3386:         var count = 0;
 3387:         if (docount) {
 3388:             for (var i=0; i<form.elements[checkbox].length; i++) {
 3389:                 if (form.elements[checkbox][i].checked) {
 3390:                     count ++;
 3391:                 }
 3392:             }
 3393:         }
 3394:         for (var i=0; i<form.elements[checkbox].length; i++) {
 3395:             var type = form.elements[checkbox][i].value;
 3396:             if (document.getElementById(prefix+type)) {
 3397:                 if (form.elements[checkbox][i].checked) {
 3398:                     document.getElementById(prefix+type).style.display = 'table-row';
 3399:                     if (count % 2 == 1) {
 3400:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 3401:                     } else {
 3402:                         document.getElementById(prefix+type).className = target;
 3403:                     }
 3404:                     count ++;
 3405:                 } else {
 3406:                     document.getElementById(prefix+type).style.display = 'none';
 3407:                 }
 3408:             }
 3409:         }
 3410:     }
 3411:     return;
 3412: }
 3413: 
 3414: 
 3415: // ]]>
 3416: </script>
 3417: 
 3418: ENDSCRIPT
 3419: }
 3420: 
 3421: sub print_helpsettings {
 3422:     my ($position,$dom,$settings,$rowtotal) = @_;
 3423:     my $confname = $dom.'-domainconfig';
 3424:     my $formname = 'display';
 3425:     my ($datatable,$itemcount);
 3426:     if ($position eq 'top') {
 3427:         $itemcount = 1;
 3428:         my (%choices,%defaultchecked,@toggles);
 3429:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 3430:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 3431:                                      &mt('LON-CAPA bug tracker'),600,500));
 3432:         %defaultchecked = ('submitbugs' => 'on');
 3433:         @toggles = ('submitbugs');
 3434:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3435:                                                      \%choices,$itemcount);
 3436:         $$rowtotal ++;
 3437:     } else {
 3438:         my $css_class;
 3439:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 3440:         my (%customroles,%ordered,%current);
 3441:         if (ref($settings) eq 'HASH') {
 3442:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 3443:                 %current = %{$settings->{'adhoc'}};
 3444:             }
 3445:         }
 3446:         my $count = 0;
 3447:         foreach my $key (sort(keys(%existing))) {
 3448:             if ($key=~/^rolesdef\_(\w+)$/) {
 3449:                 my $rolename = $1;
 3450:                 my (%privs,$order);
 3451:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3452:                 $customroles{$rolename} = \%privs;
 3453:                 if (ref($current{$rolename}) eq 'HASH') {
 3454:                     $order = $current{$rolename}{'order'};
 3455:                 }
 3456:                 if ($order eq '') {
 3457:                     $order = $count;
 3458:                 }
 3459:                 $ordered{$order} = $rolename;
 3460:                 $count++;
 3461:             }
 3462:         }
 3463:         my $maxnum = scalar(keys(%ordered));
 3464:         my @roles_by_num = ();
 3465:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3466:             push(@roles_by_num,$item);
 3467:         }
 3468:         my $context = 'domprefs';
 3469:         my $crstype = 'Course';
 3470:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3471:         my @accesstypes = ('all','dh','da','none');
 3472:         my ($numstatustypes,@jsarray);
 3473:         if (ref($types) eq 'ARRAY') {
 3474:             if (@{$types} > 0) {
 3475:                 $numstatustypes = scalar(@{$types});
 3476:                 push(@accesstypes,'status');
 3477:                 @jsarray = ('bystatus');
 3478:             }
 3479:         }
 3480:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 3481:         if (keys(%domhelpdesk)) {
 3482:             push(@accesstypes,('inc','exc'));
 3483:             push(@jsarray,('notinc','notexc'));
 3484:         }
 3485:         my $hiddenstr = join("','",@jsarray);
 3486:         $datatable .= &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname);
 3487:         my $context = 'domprefs';
 3488:         my $crstype = 'Course';
 3489:         my $prefix = 'helproles_';
 3490:         my $add_class = 'LC_hidden';
 3491:         foreach my $num (@roles_by_num) {
 3492:             my $role = $ordered{$num};
 3493:             my ($desc,$access,@statuses);
 3494:             if (ref($current{$role}) eq 'HASH') {
 3495:                 $desc = $current{$role}{'desc'};
 3496:                 $access = $current{$role}{'access'};
 3497:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 3498:                     @statuses = @{$current{$role}{'insttypes'}};
 3499:                 }
 3500:             }
 3501:             if ($desc eq '') {
 3502:                 $desc = $role;
 3503:             }
 3504:             my $identifier = 'custhelp'.$num;
 3505:             my %full=();
 3506:             my %levels= (
 3507:                          course => {},
 3508:                          domain => {},
 3509:                          system => {},
 3510:                         );
 3511:             my %levelscurrent=(
 3512:                                course => {},
 3513:                                domain => {},
 3514:                                system => {},
 3515:                               );
 3516:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 3517:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3518:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3519:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 3520:             $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><b>'.$role.'</b><br />'.
 3521:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 3522:             for (my $k=0; $k<=$maxnum; $k++) {
 3523:                 my $vpos = $k+1;
 3524:                 my $selstr;
 3525:                 if ($k == $num) {
 3526:                     $selstr = ' selected="selected" ';
 3527:                 }
 3528:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3529:             }
 3530:             $datatable .= '</select>'.('&nbsp;'x2).
 3531:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 3532:                           '</td>'.
 3533:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3534:                           &mt('Name shown to users:').
 3535:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 3536:                           '</fieldset>'.
 3537:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 3538:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 3539:                           '<fieldset>'.
 3540:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 3541:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 3542:                                                                    \%levelscurrent,$identifier,
 3543:                                                                    'LC_hidden',$prefix.$num.'_privs').
 3544:                           '</fieldset></td>';
 3545:             $itemcount ++;
 3546:         }
 3547:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3548:         my $newcust = 'custhelp'.$count;
 3549:         my (%privs,%levelscurrent);
 3550:         my %full=();
 3551:         my %levels= (
 3552:                      course => {},
 3553:                      domain => {},
 3554:                      system => {},
 3555:                     );
 3556:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 3557:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3558:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 3559:         $datatable .= '<tr '.$css_class.'><td style="vertical-align: top"><span class="LC_nobreak"><label>'.
 3560:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 3561:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 3562:         for (my $k=0; $k<$maxnum+1; $k++) {
 3563:             my $vpos = $k+1;
 3564:             my $selstr;
 3565:             if ($k == $maxnum) {
 3566:                 $selstr = ' selected="selected" ';
 3567:             }
 3568:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3569:         }
 3570:         $datatable .= '</select>&nbsp;'."\n".
 3571:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 3572:                       '</label></span></td>'.
 3573:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3574:                       '<span class="LC_nobreak">'.
 3575:                       &mt('Internal name:').
 3576:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 3577:                       '</span>'.('&nbsp;'x4).
 3578:                       '<span class="LC_nobreak">'.
 3579:                       &mt('Name shown to users:').
 3580:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 3581:                       '</span></fieldset>'.
 3582:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 3583:                                              $usertypes,$types,\%domhelpdesk).
 3584:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 3585:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 3586:                                                                 \@templateroles,$newcust).
 3587:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 3588:                                                                \%levelscurrent,$newcust).
 3589:                       '</fieldset></td></tr>';
 3590:         $count ++;
 3591:         $$rowtotal += $count;
 3592:     }
 3593:     return $datatable;
 3594: }
 3595: 
 3596: sub adhocbutton {
 3597:     my ($prefix,$num,$field,$visibility) = @_;
 3598:     my %lt = &Apache::lonlocal::texthash(
 3599:                                           show => 'Show details',
 3600:                                           hide => 'Hide details',
 3601:                                         );
 3602:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 3603:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 3604:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 3605:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 3606: }
 3607: 
 3608: sub helpsettings_javascript {
 3609:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 3610:     return unless(ref($roles_by_num) eq 'ARRAY');
 3611:     my %html_js_lt = &Apache::lonlocal::texthash(
 3612:                                           show => 'Show details',
 3613:                                           hide => 'Hide details',
 3614:                                         );
 3615:     &html_escape(\%html_js_lt);
 3616:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 3617:     return <<"ENDSCRIPT";
 3618: <script type="text/javascript">
 3619: // <![CDATA[
 3620: 
 3621: function reorderHelpRoles(form,item) {
 3622:     var changedVal;
 3623: $jstext
 3624:     var newpos = 'helproles_${total}_pos';
 3625:     var maxh = 1 + $total;
 3626:     var current = new Array();
 3627:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3628:     if (item == newpos) {
 3629:         changedVal = newitemVal;
 3630:     } else {
 3631:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3632:         current[newitemVal] = newpos;
 3633:     }
 3634:     for (var i=0; i<helproles.length; i++) {
 3635:         var elementName = 'helproles_'+helproles[i]+'_pos';
 3636:         if (elementName != item) {
 3637:             if (form.elements[elementName]) {
 3638:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3639:                 current[currVal] = elementName;
 3640:             }
 3641:         }
 3642:     }
 3643:     var oldVal;
 3644:     for (var j=0; j<maxh; j++) {
 3645:         if (current[j] == undefined) {
 3646:             oldVal = j;
 3647:         }
 3648:     }
 3649:     if (oldVal < changedVal) {
 3650:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3651:            var elementName = current[k];
 3652:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3653:         }
 3654:     } else {
 3655:         for (var k=changedVal; k<oldVal; k++) {
 3656:             var elementName = current[k];
 3657:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3658:         }
 3659:     }
 3660:     return;
 3661: }
 3662: 
 3663: function helpdeskAccess(num) {
 3664:     var curraccess = null;
 3665:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 3666:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 3667:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 3668:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 3669:             }
 3670:         }
 3671:     }
 3672:     var shown = Array();
 3673:     var hidden = Array();
 3674:     if (curraccess == 'none') {
 3675:         hidden = Array('$hiddenstr');
 3676:     } else {
 3677:         if (curraccess == 'status') {
 3678:             shown = Array('bystatus');
 3679:             hidden = Array('notinc','notexc');
 3680:         } else {
 3681:             if (curraccess == 'exc') {
 3682:                 shown = Array('notexc');
 3683:                 hidden = Array('notinc','bystatus');
 3684:             }
 3685:             if (curraccess == 'inc') {
 3686:                 shown = Array('notinc');
 3687:                 hidden = Array('notexc','bystatus');
 3688:             }
 3689:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 3690:                 hidden = Array('notinc','notexc','bystatus');
 3691:             }
 3692:         }
 3693:     }
 3694:     if (hidden.length > 0) {
 3695:         for (var i=0; i<hidden.length; i++) {
 3696:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 3697:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 3698:             }
 3699:         }
 3700:     }
 3701:     if (shown.length > 0) {
 3702:         for (var i=0; i<shown.length; i++) {
 3703:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 3704:                 if (shown[i] == 'privs') {
 3705:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 3706:                 } else {
 3707:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 3708:                 }
 3709:             }
 3710:         }
 3711:     }
 3712:     return;
 3713: }
 3714: 
 3715: function toggleHelpdeskItem(num,field) {
 3716:     if (document.getElementById('helproles_'+num+'_'+field)) {
 3717:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 3718:             document.getElementById('helproles_'+num+'_'+field).className =
 3719:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 3720:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3721:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 3722:             }
 3723:         } else {
 3724:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 3725:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3726:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 3727:             }
 3728:         }
 3729:     }
 3730:     return;
 3731: }
 3732: 
 3733: // ]]>
 3734: </script>
 3735: 
 3736: ENDSCRIPT
 3737: }
 3738: 
 3739: sub helpdeskroles_access {
 3740:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 3741:         $usertypes,$types,$domhelpdesk) = @_;
 3742:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 3743:     my %lt = &Apache::lonlocal::texthash(
 3744:                     'rou'    => 'Role usage',
 3745:                     'whi'    => 'Which helpdesk personnel may use this role?',
 3746:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 3747:                     'dh'     => 'All with domain helpdesk role',
 3748:                     'da'     => 'All with domain helpdesk assistant role',
 3749:                     'none'   => 'None',
 3750:                     'status' => 'Determined based on institutional status',
 3751:                     'inc'    => 'Include all, but exclude specific personnel',
 3752:                     'exc'    => 'Exclude all, but include specific personnel',
 3753:                   );
 3754:     my %usecheck = (
 3755:                      all => ' checked="checked"',
 3756:                    );
 3757:     my %displaydiv = (
 3758:                       status => 'none',
 3759:                       inc    => 'none',
 3760:                       exc    => 'none',
 3761:                       priv   => 'block',
 3762:                      );
 3763:     my $output;
 3764:     if (ref($current) eq 'HASH') {
 3765:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 3766:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 3767:                 $usecheck{$current->{access}} = $usecheck{'all'};
 3768:                 delete($usecheck{'all'});
 3769:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 3770:                     my $access = $1;
 3771:                     $displaydiv{$access} = 'inline';
 3772:                 } elsif ($current->{access} eq 'none') {
 3773:                     $displaydiv{'priv'} = 'none';
 3774:                 }
 3775:             }
 3776:         }
 3777:     }
 3778:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 3779:               '<p>'.$lt{'whi'}.'</p>';
 3780:     foreach my $access (@{$accesstypes}) {
 3781:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 3782:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 3783:                    $lt{$access}.'</label>';
 3784:         if ($access eq 'status') {
 3785:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 3786:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 3787:                                                                  $othertitle,$usertypes,$types).
 3788:                        '</div>';
 3789:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 3790:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 3791:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3792:                        '</div>';
 3793:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 3794:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 3795:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3796:                        '</div>';
 3797:         }
 3798:         $output .= '</p>';
 3799:     }
 3800:     $output .= '</fieldset>';
 3801:     return $output;
 3802: }
 3803: 
 3804: sub radiobutton_prefs {
 3805:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 3806:         $additional,$align) = @_;
 3807:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 3808:                    (ref($choices) eq 'HASH'));
 3809: 
 3810:     my (%checkedon,%checkedoff,$datatable,$css_class);
 3811: 
 3812:     foreach my $item (@{$toggles}) {
 3813:         if ($defaultchecked->{$item} eq 'on') {
 3814:             $checkedon{$item} = ' checked="checked" ';
 3815:             $checkedoff{$item} = ' ';
 3816:         } elsif ($defaultchecked->{$item} eq 'off') {
 3817:             $checkedoff{$item} = ' checked="checked" ';
 3818:             $checkedon{$item} = ' ';
 3819:         }
 3820:     }
 3821:     if (ref($settings) eq 'HASH') {
 3822:         foreach my $item (@{$toggles}) {
 3823:             if ($settings->{$item} eq '1') {
 3824:                 $checkedon{$item} =  ' checked="checked" ';
 3825:                 $checkedoff{$item} = ' ';
 3826:             } elsif ($settings->{$item} eq '0') {
 3827:                 $checkedoff{$item} =  ' checked="checked" ';
 3828:                 $checkedon{$item} = ' ';
 3829:             }
 3830:         }
 3831:     }
 3832:     if ($onclick) {
 3833:         $onclick = ' onclick="'.$onclick.'"';
 3834:     }
 3835:     foreach my $item (@{$toggles}) {
 3836:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3837:         $datatable .=
 3838:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 3839:             '<span class="LC_nobreak">'.$choices->{$item}.
 3840:             '</span></td>';
 3841:         if ($align eq 'left') {
 3842:             $datatable .= '<td class="LC_left_item">';
 3843:         } else {
 3844:             $datatable .= '<td class="LC_right_item">';
 3845:         }
 3846:         $datatable .=
 3847:             '<span class="LC_nobreak">'.
 3848:             '<label><input type="radio" name="'.
 3849:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 3850:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 3851:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3852:             '</span>'.$additional.
 3853:             '</td>'.
 3854:             '</tr>';
 3855:         $itemcount ++;
 3856:     }
 3857:     return ($datatable,$itemcount);
 3858: }
 3859: 
 3860: sub print_ltitools {
 3861:     my ($dom,$settings,$rowtotal) = @_;
 3862:     my $rownum = 0;
 3863:     my $css_class;
 3864:     my $itemcount = 1;
 3865:     my $maxnum = 0;
 3866:     my %ordered;
 3867:     if (ref($settings) eq 'HASH') {
 3868:         foreach my $item (keys(%{$settings})) {
 3869:             if (ref($settings->{$item}) eq 'HASH') {
 3870:                 my $num = $settings->{$item}{'order'};
 3871:                 $ordered{$num} = $item;
 3872:             }
 3873:         }
 3874:     }
 3875:     my $confname = $dom.'-domainconfig';
 3876:     my $switchserver = &check_switchserver($dom,$confname);
 3877:     my $maxnum = scalar(keys(%ordered));
 3878:     my $datatable = &ltitools_javascript($settings);
 3879:     my %lt = &ltitools_names();
 3880:     my @courseroles = ('cc','in','ta','ep','st');
 3881:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 3882:     my @fields = ('fullname','firstname','lastname','email','user','roles');
 3883:     if (keys(%ordered)) {
 3884:         my @items = sort { $a <=> $b } keys(%ordered);
 3885:         for (my $i=0; $i<@items; $i++) {
 3886:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3887:             my $item = $ordered{$items[$i]};
 3888:             my ($title,$key,$secret,$url,$imgsrc);
 3889:             if (ref($settings->{$item}) eq 'HASH') {
 3890:                 $title = $settings->{$item}->{'title'};
 3891:                 $url = $settings->{$item}->{'url'};
 3892:                 $key = $settings->{$item}->{'key'};
 3893:                 $secret = $settings->{$item}->{'secret'};
 3894:                 my $image = $settings->{$item}->{'image'};
 3895:                 if ($image ne '') {
 3896:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 3897:                 }
 3898:             }
 3899:             my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_".$item."'".');"';
 3900:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3901:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 3902:             for (my $k=0; $k<=$maxnum; $k++) {
 3903:                 my $vpos = $k+1;
 3904:                 my $selstr;
 3905:                 if ($k == $i) {
 3906:                     $selstr = ' selected="selected" ';
 3907:                 }
 3908:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3909:             }
 3910:             $datatable .= '</select>'.('&nbsp;'x2).
 3911:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 3912:                 &mt('Delete?').'</label></span></td>'.
 3913:                 '<td colspan="2">'.
 3914:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 3915:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 3916:                 ('&nbsp;'x2).
 3917:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 3918:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 3919:                 ('&nbsp;'x2).
 3920:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 3921:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 3922:                 '<br /><br />'.
 3923:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_url_'.$i.'"'.
 3924:                 ' value="'.$url.'" /></span>'.
 3925:                 ('&nbsp;'x2).
 3926:                 '<span class="LC_nobreak">'.$lt{'key'}.
 3927:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 3928:                 ('&nbsp;'x2).
 3929:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 3930:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 3931:                 '<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>'.
 3932:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 3933:                 '</fieldset>'.
 3934:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 3935:                 '<span class="LC_nobreak">'.&mt('Display target:');
 3936:             my %currdisp;
 3937:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 3938:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 3939:                     $currdisp{'window'} = ' checked="checked"';
 3940:                 } elsif ($settings->{$item}->{'display'}->{'target'} eq 'tab') {
 3941:                     $currdisp{'tab'} = ' checked="checked"';
 3942:                 } else {
 3943:                     $currdisp{'iframe'} = ' checked="checked"';
 3944:                 }
 3945:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 3946:                     $currdisp{'width'} = $1;
 3947:                 }
 3948:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 3949:                      $currdisp{'height'} = $1;
 3950:                 }
 3951:                 $currdisp{'linktext'} = $settings->{$item}->{'display'}->{'linktext'};
 3952:                 $currdisp{'explanation'} = $settings->{$item}->{'display'}->{'explanation'};
 3953:             } else {
 3954:                 $currdisp{'iframe'} = ' checked="checked"';
 3955:             }
 3956:             foreach my $disp ('iframe','tab','window') {
 3957:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 3958:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 3959:             }
 3960:             $datatable .= ('&nbsp;'x4);
 3961:             foreach my $dimen ('width','height') {
 3962:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 3963:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 3964:                               ('&nbsp;'x2);
 3965:             }
 3966:             $datatable .= '<br />'.
 3967:                           '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 3968:                           '<input type="text" name="ltitools_linktext_'.$i.'" size="25" value="'.$currdisp{'linktext'}.'" /></label></div>'.
 3969:                           '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 3970:                           '<textarea name="ltitools_explanation_'.$i.'" rows="5" cols="40">'.$currdisp{'explanation'}.
 3971:                           '</textarea></div><div style=""></div><br />';
 3972:             $datatable .= '<br />';
 3973:             foreach my $extra ('passback','roster') {
 3974:                 my $checkedon = '';
 3975:                 my $checkedoff = ' checked="checked"';
 3976:                 if ($settings->{$item}->{$extra}) {
 3977:                     $checkedon = $checkedoff;
 3978:                     $checkedoff = '';
 3979:                 }
 3980:                 $datatable .= $lt{$extra}.'&nbsp;'.
 3981:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.' />'.
 3982:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 3983:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.' />'.
 3984:                               &mt('No').'</label>'.('&nbsp;'x4);
 3985:             }
 3986:             $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 3987:             if ($imgsrc) {
 3988:                 $datatable .= $imgsrc.
 3989:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 3990:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 3991:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 3992:             } else {
 3993:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 3994:             }
 3995:             if ($switchserver) {
 3996:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3997:             } else {
 3998:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 3999:             }
 4000:             $datatable .= '</span></fieldset>';
 4001:             my (%checkedfields,%rolemaps);
 4002:             if (ref($settings->{$item}) eq 'HASH') {
 4003:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 4004:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 4005:                 }
 4006:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 4007:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 4008:                     $checkedfields{'roles'} = 1;
 4009:                 }
 4010:             }
 4011:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 4012:                           '<span class="LC_nobreak">';
 4013:             foreach my $field (@fields) {
 4014:                 my $checked;
 4015:                 if ($checkedfields{$field}) {
 4016:                     $checked = ' checked="checked"';
 4017:                 }
 4018:                 $datatable .= '<label>'.
 4019:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$checked.' />'.
 4020:                               $lt{$field}.'</label>'.('&nbsp;' x2);
 4021:             }
 4022:             $datatable .= '</span></fieldset>'.
 4023:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4024:             foreach my $role (@courseroles) {
 4025:                 my ($selected,$selectnone);
 4026:                 if (!$rolemaps{$role}) {
 4027:                     $selectnone = ' selected="selected"';
 4028:                 }
 4029:                 $datatable .= '<td style="text-align: center">'. 
 4030:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4031:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 4032:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 4033:                 foreach my $ltirole (@ltiroles) {
 4034:                     unless ($selectnone) {
 4035:                         if ($rolemaps{$role} eq $ltirole) {
 4036:                             $selected = ' selected="selected"';
 4037:                         } else {
 4038:                             $selected = '';
 4039:                         }
 4040:                     }
 4041:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 4042:                 }
 4043:                 $datatable .= '</select></td>';
 4044:             }
 4045:             $datatable .= '</tr></table></fieldset>';
 4046:             my %courseconfig;
 4047:             if (ref($settings->{$item}) eq 'HASH') {
 4048:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 4049:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 4050:                 }
 4051:             }
 4052:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4053:             foreach my $item ('label','title','target','linktext','explanation','append') {
 4054:                 my $checked;
 4055:                 if ($courseconfig{$item}) {
 4056:                     $checked = ' checked="checked"';
 4057:                 }
 4058:                 $datatable .= '<label>'.
 4059:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 4060:                        $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4061:             }
 4062:             $datatable .= '</span></fieldset>'.
 4063:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4064:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 4065:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 4066:                 my %custom = %{$settings->{$item}->{'custom'}};
 4067:                 if (keys(%custom) > 0) {
 4068:                     foreach my $key (sort(keys(%custom))) {
 4069:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 4070:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 4071:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 4072:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 4073:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 4074:                     }
 4075:                 }
 4076:             }
 4077:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 4078:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 4079:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 4080:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 4081:             $datatable .= '</table></fieldset></td></tr>'."\n";
 4082:             $itemcount ++;
 4083:         }
 4084:     }
 4085:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4086:     my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_add_pos'".');"';
 4087:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 4088:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 4089:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 4090:     for (my $k=0; $k<$maxnum+1; $k++) {
 4091:         my $vpos = $k+1;
 4092:         my $selstr;
 4093:         if ($k == $maxnum) {
 4094:             $selstr = ' selected="selected" ';
 4095:         }
 4096:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 4097:     }
 4098:     $datatable .= '</select>&nbsp;'."\n".
 4099:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</td>'."\n".
 4100:                   '<td colspan="2">'.
 4101:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 4102:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_add_title" value="" /></span> '."\n".
 4103:                   ('&nbsp;'x2).
 4104:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 4105:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 4106:                   ('&nbsp;'x2).
 4107:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 4108:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 4109:                   '<br />'.
 4110:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_add_url" value="" /></span> '."\n".
 4111:                   ('&nbsp;'x2).
 4112:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 4113:                   ('&nbsp;'x2).
 4114:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 4115:                   '<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".
 4116:                   '</fieldset>'.
 4117:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 4118:                   '<span class="LC_nobreak">'.&mt('Display target:');
 4119:     my %defaultdisp;
 4120:     $defaultdisp{'iframe'} = ' checked="checked"';
 4121:     foreach my $disp ('iframe','tab','window') {
 4122:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 4123:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 4124:     }
 4125:     $datatable .= ('&nbsp;'x4);
 4126:     foreach my $dimen ('width','height') {
 4127:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 4128:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 4129:                       ('&nbsp;'x2);
 4130:     }
 4131:     $datatable .= '<br />'.
 4132:                   '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 4133:                   '<input type="text" name="ltitools_add_linktext" size="5" /></label></div>'.
 4134:                   '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 4135:                   '<textarea name=ltitools_add_explanation" rows="5" cols="40"></textarea>'.
 4136:                   '</div><div style=""></div><br />';
 4137:     foreach my $extra ('passback','roster') {
 4138:         $datatable .= $lt{$extra}.'&nbsp;'.
 4139:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="1" />'.
 4140:                       &mt('Yes').'</label>'.('&nbsp;'x2).
 4141:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="0" checked="checked" />'.
 4142:                       &mt('No').'</label>'.('&nbsp;'x4);
 4143:     }
 4144:     $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 4145:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 4146:     if ($switchserver) {
 4147:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 4148:     } else {
 4149:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 4150:     }
 4151:     $datatable .= '</span></fieldset>'.
 4152:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 4153:                   '<span class="LC_nobreak">';
 4154:     foreach my $field (@fields) {
 4155:         $datatable .= '<label>'.
 4156:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'" />'.
 4157:                       $lt{$field}.'</label>'.('&nbsp;' x2);
 4158:     }
 4159:     $datatable .= '</span></fieldset>'.
 4160:                   '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 4161:     foreach my $role (@courseroles) {
 4162:         my ($checked,$checkednone);
 4163:         $datatable .= '<td style="text-align: center">'.
 4164:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 4165:                       '<select name="ltitools_add_roles_'.$role.'">'.
 4166:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 4167:         foreach my $ltirole (@ltiroles) {
 4168:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 4169:         }
 4170:         $datatable .= '</select></td>';
 4171:     }
 4172:     $datatable .= '</tr></table></fieldset>'.
 4173:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 4174:     foreach my $item ('label','title','target','linktext','explanation','append') {
 4175:         $datatable .= '<label>'.
 4176:                       '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 4177:                       $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4178:     }
 4179:     $datatable .= '</span></fieldset>'.
 4180:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4181:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 4182:                   '<tr><td><span class="LC_nobreak">'.
 4183:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 4184:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 4185:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 4186:                   '</table></fieldset></td></tr>'."\n".
 4187:                   '</td>'."\n".
 4188:                   '</tr>'."\n";
 4189:     $itemcount ++;
 4190:     return $datatable;
 4191: }
 4192: 
 4193: sub ltitools_names {
 4194:     my %lt = &Apache::lonlocal::texthash(
 4195:                                           'title'          => 'Title',
 4196:                                           'version'        => 'Version',
 4197:                                           'msgtype'        => 'Message Type',
 4198:                                           'url'            => 'URL',
 4199:                                           'key'            => 'Key',
 4200:                                           'secret'         => 'Secret',
 4201:                                           'icon'           => 'Icon',   
 4202:                                           'user'           => 'Username:domain',
 4203:                                           'fullname'       => 'Full Name',
 4204:                                           'firstname'      => 'First Name',
 4205:                                           'lastname'       => 'Last Name',
 4206:                                           'email'          => 'E-mail',
 4207:                                           'roles'          => 'Role',
 4208:                                           'window'         => 'Window',
 4209:                                           'tab'            => 'Tab',
 4210:                                           'iframe'         => 'iFrame',
 4211:                                           'height'         => 'Height',
 4212:                                           'width'          => 'Width',
 4213:                                           'linktext'       => 'Default Link Text',
 4214:                                           'explanation'    => 'Default Explanation',
 4215:                                           'passback'       => 'Tool can return grades:',
 4216:                                           'roster'         => 'Tool can retrieve roster:',
 4217:                                           'crstarget'      => 'Display target',
 4218:                                           'crslabel'       => 'Course label',
 4219:                                           'crstitle'       => 'Course title', 
 4220:                                           'crslinktext'    => 'Link Text',
 4221:                                           'crsexplanation' => 'Explanation',
 4222:                                           'crsappend'      => 'Provider URL',
 4223:                                         );
 4224:     return %lt;
 4225: }
 4226: 
 4227: sub print_coursedefaults {
 4228:     my ($position,$dom,$settings,$rowtotal) = @_;
 4229:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 4230:     my $itemcount = 1;
 4231:     my %choices =  &Apache::lonlocal::texthash (
 4232:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 4233:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 4234:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 4235:         coursecredits        => 'Credits can be specified for courses',
 4236:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 4237:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 4238:         texengine            => 'Default method to display mathematics',
 4239:         postsubmit           => 'Disable submit button/keypress following student submission',
 4240:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 4241:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 4242:     );
 4243:     my %staticdefaults = (
 4244:                            texengine            => 'MathJax',
 4245:                            anonsurvey_threshold => 10,
 4246:                            uploadquota          => 500,
 4247:                            postsubmit           => 60,
 4248:                            mysqltables          => 172800,
 4249:                          );
 4250:     if ($position eq 'top') {
 4251:         %defaultchecked = (
 4252:                             'canuse_pdfforms' => 'off',
 4253:                             'uselcmath'       => 'on',
 4254:                             'usejsme'         => 'on',
 4255:                             'canclone'        => 'none',
 4256:                           );
 4257:         @toggles = ('canuse_pdfforms','uselcmath','usejsme');
 4258:         my $deftex = $staticdefaults{'texengine'};
 4259:         if (ref($settings) eq 'HASH') {
 4260:             if ($settings->{'texengine'}) {
 4261:                 if ($settings->{'texengine'} =~ /^(MathJax|mimetex|tth)$/) {
 4262:                     $deftex = $settings->{'texengine'};
 4263:                 }
 4264:             }
 4265:         }
 4266:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4267:         my $mathdisp = '<tr'.$css_class.'><td style="vertical-align: top">'.
 4268:                        '<span class="LC_nobreak">'.$choices{'texengine'}.
 4269:                        '</span></td><td class="LC_right_item">'.
 4270:                        '<select name="texengine">'."\n";
 4271:         my %texoptions = (
 4272:                             MathJax  => 'MathJax',
 4273:                             mimetex  => &mt('Convert to Images'),
 4274:                             tth      => &mt('TeX to HTML'),
 4275:                          );
 4276:         foreach my $renderer ('MathJax','mimetex','tth') {
 4277:             my $selected = '';
 4278:             if ($renderer eq $deftex) {
 4279:                 $selected = ' selected="selected"';
 4280:             }
 4281:             $mathdisp .= '<option value="'.$renderer.'"'.$selected.'>'.$texoptions{$renderer}.'</option>'."\n";
 4282:         }
 4283:         $mathdisp .= '</select></td></tr>'."\n";
 4284:         $itemcount ++;
 4285:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4286:                                                      \%choices,$itemcount);
 4287:         $datatable = $mathdisp.$datatable;
 4288:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4289:         $datatable .=
 4290:             '<tr'.$css_class.'><td style="vertical-align: top">'.
 4291:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 4292:             '</span></td><td class="LC_left_item">';
 4293:         my $currcanclone = 'none';
 4294:         my $onclick;
 4295:         my @cloneoptions = ('none','domain');
 4296:         my %clonetitles = (
 4297:                              none     => 'No additional course requesters',
 4298:                              domain   => "Any course requester in course's domain",
 4299:                              instcode => 'Course requests for official courses ...',
 4300:                           );
 4301:         my (%codedefaults,@code_order,@posscodes);
 4302:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 4303:                                                     \@code_order) eq 'ok') {
 4304:             if (@code_order > 0) {
 4305:                 push(@cloneoptions,'instcode');
 4306:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 4307:             }
 4308:         }
 4309:         if (ref($settings) eq 'HASH') {
 4310:             if ($settings->{'canclone'}) {
 4311:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 4312:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 4313:                         if (@code_order > 0) {
 4314:                             $currcanclone = 'instcode';
 4315:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 4316:                         }
 4317:                     }
 4318:                 } elsif ($settings->{'canclone'} eq 'domain') {
 4319:                     $currcanclone = $settings->{'canclone'};
 4320:                 }
 4321:             }
 4322:         }
 4323:         foreach my $option (@cloneoptions) {
 4324:             my ($checked,$additional);
 4325:             if ($currcanclone eq $option) {
 4326:                 $checked = ' checked="checked"';
 4327:             }
 4328:             if ($option eq 'instcode') {
 4329:                 if (@code_order) {
 4330:                     my $show = 'none';
 4331:                     if ($checked) {
 4332:                         $show = 'block';
 4333:                     }
 4334:                     $additional = '<div id="cloneinstcode" style="display:'.$show.';" />'.
 4335:                                   &mt('Institutional codes for new and cloned course have identical:').
 4336:                                   '<br />';
 4337:                     foreach my $item (@code_order) {
 4338:                         my $codechk;
 4339:                         if ($checked) {
 4340:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 4341:                                 $codechk = ' checked="checked"';
 4342:                             }
 4343:                         }
 4344:                         $additional .= '<label>'.
 4345:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 4346:                                        $item.'</label>';
 4347:                     }
 4348:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 4349:                 }
 4350:             }
 4351:             $datatable .=
 4352:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 4353:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 4354:                 '</label>&nbsp;'.$additional.'</span><br />';
 4355:         }
 4356:         $datatable .= '</td>'.
 4357:                       '</tr>';
 4358:         $itemcount ++;
 4359:     } else {
 4360:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4361:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 4362:         my $currusecredits = 0;
 4363:         my $postsubmitclient = 1;
 4364:         my @types = ('official','unofficial','community','textbook','placement');
 4365:         if (ref($settings) eq 'HASH') {
 4366:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 4367:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 4368:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 4369:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 4370:                 }
 4371:             }
 4372:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 4373:                 foreach my $type (@types) {
 4374:                     next if ($type eq 'community');
 4375:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 4376:                     if ($defcredits{$type} ne '') {
 4377:                         $currusecredits = 1;
 4378:                     }
 4379:                 }
 4380:             }
 4381:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 4382:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 4383:                     $postsubmitclient = 0;
 4384:                     foreach my $type (@types) {
 4385:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4386:                     }
 4387:                 } else {
 4388:                     foreach my $type (@types) {
 4389:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 4390:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 4391:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 4392:                             } else {
 4393:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4394:                             }
 4395:                         } else {
 4396:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4397:                         }
 4398:                     }
 4399:                 }
 4400:             } else {
 4401:                 foreach my $type (@types) {
 4402:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4403:                 }
 4404:             }
 4405:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 4406:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 4407:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 4408:                 }
 4409:             } else {
 4410:                 foreach my $type (@types) {
 4411:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 4412:                 }
 4413:             }
 4414:         } else {
 4415:             foreach my $type (@types) {
 4416:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4417:             }
 4418:         }
 4419:         if (!$currdefresponder) {
 4420:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 4421:         } elsif ($currdefresponder < 1) {
 4422:             $currdefresponder = 1;
 4423:         }
 4424:         foreach my $type (@types) {
 4425:             if ($curruploadquota{$type} eq '') {
 4426:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 4427:             }
 4428:         }
 4429:         $datatable .=
 4430:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4431:                 $choices{'anonsurvey_threshold'}.
 4432:                 '</span></td>'.
 4433:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 4434:                 '<input type="text" name="anonsurvey_threshold"'.
 4435:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 4436:                 '</td></tr>'."\n";
 4437:         $itemcount ++;
 4438:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4439:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4440:                       $choices{'uploadquota'}.
 4441:                       '</span></td>'.
 4442:                       '<td style="text-align: right" class="LC_right_item">'.
 4443:                       '<table><tr>';
 4444:         foreach my $type (@types) {
 4445:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 4446:                            '<input type="text" name="uploadquota_'.$type.'"'.
 4447:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 4448:         }
 4449:         $datatable .= '</tr></table></td></tr>'."\n";
 4450:         $itemcount ++;
 4451:         my $onclick = "toggleDisplay(this.form,'credits');";
 4452:         my $display = 'none';
 4453:         if ($currusecredits) {
 4454:             $display = 'block';
 4455:         }
 4456:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 4457:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 4458:         foreach my $type (@types) {
 4459:             next if ($type eq 'community');
 4460:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 4461:                            '<input type="text" name="'.$type.'_credits"'.
 4462:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 4463:         }
 4464:         $additional .= '</tr></table></div>'."\n";
 4465:         %defaultchecked = ('coursecredits' => 'off');
 4466:         @toggles = ('coursecredits');
 4467:         my $current = {
 4468:                         'coursecredits' => $currusecredits,
 4469:                       };
 4470:         (my $table,$itemcount) =
 4471:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4472:                                \%choices,$itemcount,$onclick,$additional,'left');
 4473:         $datatable .= $table;
 4474:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 4475:         my $display = 'none';
 4476:         if ($postsubmitclient) {
 4477:             $display = 'block';
 4478:         }
 4479:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 4480:                       &mt('Number of seconds submit is disabled').'<br />'.
 4481:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 4482:                       '<table><tr>';
 4483:         foreach my $type (@types) {
 4484:             $additional .= '<td style="text-align: center">'.&mt($type).'<br />'.
 4485:                            '<input type="text" name="'.$type.'_timeout" value="'.
 4486:                            $deftimeout{$type}.'" size="5" /></td>';
 4487:         }
 4488:         $additional .= '</tr></table></div>'."\n";
 4489:         %defaultchecked = ('postsubmit' => 'on');
 4490:         @toggles = ('postsubmit');
 4491:         $current = {
 4492:                        'postsubmit' => $postsubmitclient,
 4493:                    };
 4494:         ($table,$itemcount) =
 4495:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4496:                                \%choices,$itemcount,$onclick,$additional,'left');
 4497:         $datatable .= $table;
 4498:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4499:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4500:                       $choices{'mysqltables'}.
 4501:                       '</span></td>'.
 4502:                       '<td style="text-align: right" class="LC_right_item">'.
 4503:                       '<table><tr>';
 4504:         foreach my $type (@types) {
 4505:             $datatable .= '<td style="text-align: center">'.&mt($type).'<br />'.
 4506:                            '<input type="text" name="mysqltables_'.$type.'"'.
 4507:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 4508:         }
 4509:         $datatable .= '</tr></table></td></tr>'."\n";
 4510:         $itemcount ++;
 4511: 
 4512:     }
 4513:     $$rowtotal += $itemcount;
 4514:     return $datatable;
 4515: }
 4516: 
 4517: sub print_selfenrollment {
 4518:     my ($position,$dom,$settings,$rowtotal) = @_;
 4519:     my ($css_class,$datatable);
 4520:     my $itemcount = 1;
 4521:     my @types = ('official','unofficial','community','textbook','placement');
 4522:     if (($position eq 'top') || ($position eq 'middle')) {
 4523:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 4524:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 4525:         my @rows;
 4526:         my $key;
 4527:         if ($position eq 'top') {
 4528:             $key = 'admin'; 
 4529:             if (ref($rowsref) eq 'ARRAY') {
 4530:                 @rows = @{$rowsref};
 4531:             }
 4532:         } elsif ($position eq 'middle') {
 4533:             $key = 'default';
 4534:             @rows = ('types','registered','approval','limit');
 4535:         }
 4536:         foreach my $row (@rows) {
 4537:             if (defined($titlesref->{$row})) {
 4538:                 $itemcount ++;
 4539:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4540:                 $datatable .= '<tr'.$css_class.'>'.
 4541:                               '<td>'.$titlesref->{$row}.'</td>'.
 4542:                               '<td class="LC_left_item">'.
 4543:                               '<table><tr>';
 4544:                 my (%current,%currentcap);
 4545:                 if (ref($settings) eq 'HASH') {
 4546:                     if (ref($settings->{$key}) eq 'HASH') {
 4547:                         foreach my $type (@types) {
 4548:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4549:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 4550:                             }
 4551:                             if (($row eq 'limit') && ($key eq 'default')) {
 4552:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4553:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 4554:                                 }
 4555:                             }
 4556:                         }
 4557:                     }
 4558:                 }
 4559:                 my %roles = (
 4560:                              '0' => &Apache::lonnet::plaintext('dc'),
 4561:                             ); 
 4562:             
 4563:                 foreach my $type (@types) {
 4564:                     unless (($row eq 'registered') && ($key eq 'default')) {
 4565:                         $datatable .= '<th>'.&mt($type).'</th>';
 4566:                     }
 4567:                 }
 4568:                 unless (($row eq 'registered') && ($key eq 'default')) {
 4569:                     $datatable .= '</tr><tr>';
 4570:                 }
 4571:                 foreach my $type (@types) {
 4572:                     if ($type eq 'community') {
 4573:                         $roles{'1'} = &mt('Community personnel');
 4574:                     } else {
 4575:                         $roles{'1'} = &mt('Course personnel');
 4576:                     }
 4577:                     $datatable .= '<td style="vertical-align: top">';
 4578:                     if ($position eq 'top') {
 4579:                         my %checked;
 4580:                         if ($current{$type} eq '0') {
 4581:                             $checked{'0'} = ' checked="checked"';
 4582:                         } else {
 4583:                             $checked{'1'} = ' checked="checked"';
 4584:                         }
 4585:                         foreach my $role ('1','0') {
 4586:                             $datatable .= '<span class="LC_nobreak"><label>'.
 4587:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 4588:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 4589:                                           $roles{$role}.'</label></span> ';
 4590:                         }
 4591:                     } else {
 4592:                         if ($row eq 'types') {
 4593:                             my %checked;
 4594:                             if ($current{$type} =~ /^(all|dom)$/) {
 4595:                                 $checked{$1} = ' checked="checked"';
 4596:                             } else {
 4597:                                 $checked{''} = ' checked="checked"';
 4598:                             }
 4599:                             foreach my $val ('','dom','all') {
 4600:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4601:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4602:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4603:                             }
 4604:                         } elsif ($row eq 'registered') {
 4605:                             my %checked;
 4606:                             if ($current{$type} eq '1') {
 4607:                                 $checked{'1'} = ' checked="checked"';
 4608:                             } else {
 4609:                                 $checked{'0'} = ' checked="checked"';
 4610:                             }
 4611:                             foreach my $val ('0','1') {
 4612:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4613:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4614:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4615:                             }
 4616:                         } elsif ($row eq 'approval') {
 4617:                             my %checked;
 4618:                             if ($current{$type} =~ /^([12])$/) {
 4619:                                 $checked{$1} = ' checked="checked"';
 4620:                             } else {
 4621:                                 $checked{'0'} = ' checked="checked"';
 4622:                             }
 4623:                             for my $val (0..2) {
 4624:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4625:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4626:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4627:                             }
 4628:                         } elsif ($row eq 'limit') {
 4629:                             my %checked;
 4630:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 4631:                                 $checked{$1} = ' checked="checked"';
 4632:                             } else {
 4633:                                 $checked{'none'} = ' checked="checked"';
 4634:                             }
 4635:                             my $cap;
 4636:                             if ($currentcap{$type} =~ /^\d+$/) {
 4637:                                 $cap = $currentcap{$type};
 4638:                             }
 4639:                             foreach my $val ('none','allstudents','selfenrolled') {
 4640:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4641:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4642:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4643:                             }
 4644:                             $datatable .= '<br />'.
 4645:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 4646:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 4647:                                           '</span>'; 
 4648:                         }
 4649:                     }
 4650:                     $datatable .= '</td>';
 4651:                 }
 4652:                 $datatable .= '</tr>';
 4653:             }
 4654:             $datatable .= '</table></td></tr>';
 4655:         }
 4656:     } elsif ($position eq 'bottom') {
 4657:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 4658:     }
 4659:     $$rowtotal += $itemcount;
 4660:     return $datatable;
 4661: }
 4662: 
 4663: sub print_validation_rows {
 4664:     my ($caller,$dom,$settings,$rowtotal) = @_;
 4665:     my ($itemsref,$namesref,$fieldsref);
 4666:     if ($caller eq 'selfenroll') { 
 4667:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 4668:     } elsif ($caller eq 'requestcourses') {
 4669:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 4670:     }
 4671:     my %currvalidation;
 4672:     if (ref($settings) eq 'HASH') {
 4673:         if (ref($settings->{'validation'}) eq 'HASH') {
 4674:             %currvalidation = %{$settings->{'validation'}};
 4675:         }
 4676:     }
 4677:     my $datatable;
 4678:     my $itemcount = 0;
 4679:     foreach my $item (@{$itemsref}) {
 4680:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4681:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4682:                       $namesref->{$item}.
 4683:                       '</span></td>'.
 4684:                       '<td class="LC_left_item">';
 4685:         if (($item eq 'url') || ($item eq 'button')) {
 4686:             $datatable .= '<span class="LC_nobreak">'.
 4687:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 4688:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 4689:         } elsif ($item eq 'fields') {
 4690:             my @currfields;
 4691:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 4692:                 @currfields = @{$currvalidation{$item}};
 4693:             }
 4694:             foreach my $field (@{$fieldsref}) {
 4695:                 my $check = '';
 4696:                 if (grep(/^\Q$field\E$/,@currfields)) {
 4697:                     $check = ' checked="checked"';
 4698:                 }
 4699:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4700:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 4701:                               ' value="'.$field.'"'.$check.' />'.$field.
 4702:                               '</label></span> ';
 4703:             }
 4704:         } elsif ($item eq 'markup') {
 4705:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 4706:                            $currvalidation{$item}.
 4707:                               '</textarea>';
 4708:         }
 4709:         $datatable .= '</td></tr>'."\n";
 4710:         if (ref($rowtotal)) {
 4711:             $itemcount ++;
 4712:         }
 4713:     }
 4714:     if ($caller eq 'requestcourses') {
 4715:         my %currhash;
 4716:         if (ref($settings) eq 'HASH') {
 4717:             if (ref($settings->{'validation'}) eq 'HASH') {
 4718:                 if ($settings->{'validation'}{'dc'} ne '') {
 4719:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 4720:                 }
 4721:             }
 4722:         }
 4723:         my $numinrow = 2;
 4724:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4725:                                                        'validationdc',%currhash);
 4726:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4727:         $datatable .= '</td></tr><tr'.$css_class.'><td>';
 4728:         if ($numdc > 1) {
 4729:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 4730:         } else {
 4731:             $datatable .=  &mt('Course creation processed as: ');
 4732:         }
 4733:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 4734:         $itemcount ++;
 4735:     }
 4736:     if (ref($rowtotal)) {
 4737:         $$rowtotal += $itemcount;
 4738:     }
 4739:     return $datatable;
 4740: }
 4741: 
 4742: sub print_usersessions {
 4743:     my ($position,$dom,$settings,$rowtotal) = @_;
 4744:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 4745:     my (%by_ip,%by_location,@intdoms,@instdoms);
 4746:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 4747: 
 4748:     my @alldoms = &Apache::lonnet::all_domains();
 4749:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 4750:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4751:     my %altids = &id_for_thisdom(%servers);
 4752:     if ($position eq 'top') {
 4753:         if (keys(%serverhomes) > 1) {
 4754:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 4755:             my $curroffloadnow;
 4756:             if (ref($settings) eq 'HASH') {
 4757:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 4758:                     $curroffloadnow = $settings->{'offloadnow'};
 4759:                 }
 4760:             }
 4761:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 4762:         } else {
 4763:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 4764:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 4765:                           '</td></tr>';
 4766:         }
 4767:     } else {
 4768:         my %titles = &usersession_titles();
 4769:         my ($prefix,@types);
 4770:         if ($position eq 'bottom') {
 4771:             $prefix = 'remote';
 4772:             @types = ('version','excludedomain','includedomain');
 4773:         } else {
 4774:             $prefix = 'hosted';
 4775:             @types = ('excludedomain','includedomain');
 4776:         }
 4777:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 4778:     }
 4779:     $$rowtotal += $itemcount;
 4780:     return $datatable;
 4781: }
 4782: 
 4783: sub rules_by_location {
 4784:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 4785:     my ($datatable,$itemcount,$css_class);
 4786:     if (keys(%{$by_location}) == 0) {
 4787:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4788:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 4789:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 4790:                      '</td></tr>';
 4791:         $itemcount = 1;
 4792:     } else {
 4793:         $itemcount = 0;
 4794:         my $numinrow = 5;
 4795:         my (%current,%checkedon,%checkedoff);
 4796:         my @locations = sort(keys(%{$by_location}));
 4797:         foreach my $type (@{$types}) {
 4798:             $checkedon{$type} = '';
 4799:             $checkedoff{$type} = ' checked="checked"';
 4800:         }
 4801:         if (ref($settings) eq 'HASH') {
 4802:             if (ref($settings->{$prefix}) eq 'HASH') {
 4803:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 4804:                     $current{$key} = $settings->{$prefix}{$key};
 4805:                     if ($key eq 'version') {
 4806:                         if ($current{$key} ne '') {
 4807:                             $checkedon{$key} = ' checked="checked"';
 4808:                             $checkedoff{$key} = '';
 4809:                         }
 4810:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 4811:                         $checkedon{$key} = ' checked="checked"';
 4812:                         $checkedoff{$key} = '';
 4813:                     }
 4814:                 }
 4815:             }
 4816:         }
 4817:         foreach my $type (@{$types}) {
 4818:             next if ($type ne 'version' && !@locations);
 4819:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4820:             $datatable .= '<tr'.$css_class.'>
 4821:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 4822:                            <span class="LC_nobreak">&nbsp;
 4823:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 4824:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 4825:             if ($type eq 'version') {
 4826:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 4827:                 my $selector = '<select name="'.$prefix.'_version">';
 4828:                 foreach my $version (@lcversions) {
 4829:                     my $selected = '';
 4830:                     if ($current{'version'} eq $version) {
 4831:                         $selected = ' selected="selected"';
 4832:                     }
 4833:                     $selector .= ' <option value="'.$version.'"'.
 4834:                                  $selected.'>'.$version.'</option>';
 4835:                 }
 4836:                 $selector .= '</select> ';
 4837:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 4838:             } else {
 4839:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 4840:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 4841:                              ' />'.('&nbsp;'x2).
 4842:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 4843:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 4844:                              "\n".
 4845:                              '</div><div><table>';
 4846:                 my $rem;
 4847:                 for (my $i=0; $i<@locations; $i++) {
 4848:                     my ($showloc,$value,$checkedtype);
 4849:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 4850:                         my $ip = $by_location->{$locations[$i]}->[0];
 4851:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4852:                             $value = join(':',@{$by_ip->{$ip}});
 4853:                             $showloc = join(', ',@{$by_ip->{$ip}});
 4854:                             if (ref($current{$type}) eq 'ARRAY') {
 4855:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 4856:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 4857:                                         $checkedtype = ' checked="checked"';
 4858:                                         last;
 4859:                                     }
 4860:                                 }
 4861:                             }
 4862:                         }
 4863:                     }
 4864:                     $rem = $i%($numinrow);
 4865:                     if ($rem == 0) {
 4866:                         if ($i > 0) {
 4867:                             $datatable .= '</tr>';
 4868:                         }
 4869:                         $datatable .= '<tr>';
 4870:                     }
 4871:                     $datatable .= '<td class="LC_left_item">'.
 4872:                                   '<span class="LC_nobreak"><label>'.
 4873:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 4874:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 4875:                                   '</label></span></td>';
 4876:                 }
 4877:                 $rem = @locations%($numinrow);
 4878:                 my $colsleft = $numinrow - $rem;
 4879:                 if ($colsleft > 1 ) {
 4880:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4881:                                   '&nbsp;</td>';
 4882:                 } elsif ($colsleft == 1) {
 4883:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4884:                 }
 4885:                 $datatable .= '</tr></table>';
 4886:             }
 4887:             $datatable .= '</td></tr>';
 4888:             $itemcount ++;
 4889:         }
 4890:     }
 4891:     return ($datatable,$itemcount);
 4892: }
 4893: 
 4894: sub print_ssl {
 4895:     my ($position,$dom,$settings,$rowtotal) = @_;
 4896:     my ($css_class,$datatable);
 4897:     my $itemcount = 1;
 4898:     if ($position eq 'top') {
 4899:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 4900:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 4901:         my $same_institution;
 4902:         if ($intdom ne '') {
 4903:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 4904:             if (ref($internet_names) eq 'ARRAY') {
 4905:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 4906:                     $same_institution = 1;
 4907:                 }
 4908:             }
 4909:         }
 4910:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4911:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 4912:         if ($same_institution) {
 4913:             my %domservers = &Apache::lonnet::get_servers($dom);
 4914:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 4915:         } else {
 4916:             $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.");
 4917:         }
 4918:         $datatable .= '</td></tr>';
 4919:         $itemcount ++;
 4920:     } else {
 4921:         my %titles = &ssl_titles();
 4922:         my (%by_ip,%by_location,@intdoms,@instdoms);
 4923:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 4924:         my @alldoms = &Apache::lonnet::all_domains();
 4925:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 4926:         my @domservers = &Apache::lonnet::get_servers($dom);
 4927:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4928:         my %altids = &id_for_thisdom(%servers);
 4929:         if (($position eq 'connto') || ($position eq 'connfrom')) {
 4930:             my $legacy;
 4931:             unless (ref($settings) eq 'HASH') {
 4932:                 my $name;
 4933:                 if ($position eq 'connto') {
 4934:                     $name = 'loncAllowInsecure';
 4935:                 } else {
 4936:                     $name = 'londAllowInsecure';
 4937:                 }
 4938:                 my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
 4939:                 my @ids=&Apache::lonnet::current_machine_ids();
 4940:                 if (($primarylibserv ne '') && (!grep(/^\Q$primarylibserv\E$/,@ids))) {
 4941:                     my %what = (
 4942:                                    $name => 1,
 4943:                                );
 4944:                     my ($result,$returnhash) =
 4945:                         &Apache::lonnet::get_remote_globals($primarylibserv,\%what);
 4946:                     if ($result eq 'ok') {
 4947:                         if (ref($returnhash) eq 'HASH') {
 4948:                             $legacy = $returnhash->{$name};
 4949:                         }
 4950:                     }
 4951:                 } else {
 4952:                     $legacy = $Apache::lonnet::perlvar{$name};
 4953:                 }
 4954:             }
 4955:             foreach my $type ('dom','intdom','other') {
 4956:                 my %checked;
 4957:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4958:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 4959:                               '<td class="LC_right_item">';
 4960:                 my $skip; 
 4961:                 if ($type eq 'dom') {
 4962:                     unless (keys(%servers) > 1) {
 4963:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 4964:                         $skip = 1;
 4965:                     }
 4966:                 }
 4967:                 if ($type eq 'intdom') {
 4968:                     unless (@instdoms > 1) {
 4969:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 4970:                         $skip = 1;
 4971:                     } 
 4972:                 } elsif ($type eq 'other') {
 4973:                     if (keys(%by_location) == 0) {
 4974:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 4975:                         $skip = 1;
 4976:                     }
 4977:                 }
 4978:                 unless ($skip) {
 4979:                     $checked{'yes'} = ' checked="checked"'; 
 4980:                     if (ref($settings) eq 'HASH') {
 4981:                         if (ref($settings->{$position}) eq 'HASH') {
 4982:                             if ($settings->{$position}->{$type} =~ /^(no|req)$/) {
 4983:                                 $checked{$1} = $checked{'yes'};
 4984:                                 delete($checked{'yes'}); 
 4985:                             }
 4986:                         }
 4987:                     } else {
 4988:                         if ($legacy == 0) {
 4989:                             $checked{'req'} = $checked{'yes'};
 4990:                             delete($checked{'yes'});    
 4991:                         }
 4992:                     }
 4993:                     foreach my $option ('no','yes','req') {
 4994:                         $datatable .= '<span class="LC_nobreak"><label>'.
 4995:                                       '<input type="radio" name="'.$position.'_'.$type.'" '.
 4996:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 4997:                                       '</label></span>'.('&nbsp;'x2);
 4998:                     }
 4999:                 }
 5000:                 $datatable .= '</td></tr>';
 5001:                 $itemcount ++; 
 5002:             }
 5003:         } else {
 5004:             my $prefix = 'replication';
 5005:             my @types = ('certreq','nocertreq');
 5006:             if (keys(%by_location) == 0) {
 5007:                 $datatable .= '<tr'.$css_class.'><td>'.
 5008:                               &mt('Nothing to set here, as there are no other institutions').
 5009:                               '</td></tr>';
 5010:                 $itemcount ++;
 5011:             } else {
 5012:                 ($datatable,$itemcount) = 
 5013:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 5014:             }
 5015:         }
 5016:     }
 5017:     $$rowtotal += $itemcount;
 5018:     return $datatable;
 5019: }
 5020: 
 5021: sub ssl_titles {
 5022:     return &Apache::lonlocal::texthash (
 5023:                dom           => 'LON-CAPA servers/VMs from same domain',
 5024:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 5025:                other         => 'External LON-CAPA servers/VMs',
 5026:                connto        => 'Connections to other servers',
 5027:                connfrom      => 'Connections from other servers',
 5028:                replication   => 'Replicating content to other institutions',
 5029:                certreq       => 'Client certificate required, but specific domains exempt',
 5030:                nocertreq     => 'No client certificate required, except for specific domains',
 5031:                no            => 'SSL not used',
 5032:                yes           => 'SSL Optional (used if available)',
 5033:                req           => 'SSL Required',
 5034:     );
 5035: }
 5036: 
 5037: sub print_trust {
 5038:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 5039:     my ($css_class,$datatable,%checked,%choices);
 5040:     my (%by_ip,%by_location,@intdoms,@instdoms);
 5041:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 5042:     my $itemcount = 1;
 5043:     my %titles = &trust_titles();
 5044:     my @types = ('exc','inc');
 5045:     if ($prefix eq 'top') {
 5046:         $prefix = 'content';
 5047:     } elsif ($prefix eq 'bottom') {
 5048:         $prefix = 'msg';
 5049:     }
 5050:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 5051:     $$rowtotal += $itemcount;
 5052:     return $datatable;
 5053: }
 5054: 
 5055: sub trust_titles {
 5056:     return &Apache::lonlocal::texthash(
 5057:                content  => "Access to this domain's content by others",
 5058:                shared   => "Access to other domain's content by this domain",
 5059:                enroll   => "Enrollment in this domain's courses by others", 
 5060:                othcoau  => "Co-author roles in this domain for others",
 5061:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 5062:                domroles => "Domain roles in this domain assignable to others",
 5063:                catalog  => "Course Catalog for this domain displayed elsewhere",
 5064:                reqcrs   => "Requests for creation of courses in this domain by others",
 5065:                msg      => "Users in other domains can send messages to this domain",
 5066:                exc      => "Allow all, but exclude specific domains",
 5067:                inc      => "Deny all, but include specific domains",
 5068:            );
 5069: } 
 5070: 
 5071: sub build_location_hashes {
 5072:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 5073:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 5074:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 5075:     my %iphost = &Apache::lonnet::get_iphost();
 5076:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 5077:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 5078:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 5079:         foreach my $id (@{$iphost{$primary_ip}}) {
 5080:             my $intdom = &Apache::lonnet::internet_dom($id);
 5081:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 5082:                 push(@{$intdoms},$intdom);
 5083:             }
 5084:         }
 5085:     }
 5086:     foreach my $ip (keys(%iphost)) {
 5087:         if (ref($iphost{$ip}) eq 'ARRAY') {
 5088:             foreach my $id (@{$iphost{$ip}}) {
 5089:                 my $location = &Apache::lonnet::internet_dom($id);
 5090:                 if ($location) {
 5091:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 5092:                         my $dom = &Apache::lonnet::host_domain($id);
 5093:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 5094:                             push(@{$instdoms},$dom);
 5095:                         }
 5096:                         next;
 5097:                     }
 5098:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 5099:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 5100:                             push(@{$by_ip->{$ip}},$location);
 5101:                         }
 5102:                     } else {
 5103:                         $by_ip->{$ip} = [$location];
 5104:                     }
 5105:                 }
 5106:             }
 5107:         }
 5108:     }
 5109:     foreach my $ip (sort(keys(%{$by_ip}))) {
 5110:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 5111:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 5112:             my $first = $by_ip->{$ip}->[0];
 5113:             if (ref($by_location->{$first}) eq 'ARRAY') {
 5114:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 5115:                     push(@{$by_location->{$first}},$ip);
 5116:                 }
 5117:             } else {
 5118:                 $by_location->{$first} = [$ip];
 5119:             }
 5120:         }
 5121:     }
 5122:     return;
 5123: }
 5124: 
 5125: sub current_offloads_to {
 5126:     my ($dom,$settings,$servers) = @_;
 5127:     my (%spareid,%otherdomconfigs);
 5128:     if (ref($servers) eq 'HASH') {
 5129:         foreach my $lonhost (sort(keys(%{$servers}))) {
 5130:             my $gotspares;
 5131:             if (ref($settings) eq 'HASH') {
 5132:                 if (ref($settings->{'spares'}) eq 'HASH') {
 5133:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 5134:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 5135:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 5136:                         $gotspares = 1;
 5137:                     }
 5138:                 }
 5139:             }
 5140:             unless ($gotspares) {
 5141:                 my $gotspares;
 5142:                 my $serverhomeID =
 5143:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 5144:                 my $serverhomedom =
 5145:                     &Apache::lonnet::host_domain($serverhomeID);
 5146:                 if ($serverhomedom ne $dom) {
 5147:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 5148:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 5149:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 5150:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 5151:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 5152:                                 $gotspares = 1;
 5153:                             }
 5154:                         }
 5155:                     } else {
 5156:                         $otherdomconfigs{$serverhomedom} =
 5157:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 5158:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 5159:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 5160:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 5161:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 5162:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 5163:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 5164:                                         $gotspares = 1;
 5165:                                     }
 5166:                                 }
 5167:                             }
 5168:                         }
 5169:                     }
 5170:                 }
 5171:             }
 5172:             unless ($gotspares) {
 5173:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 5174:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 5175:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 5176:                } else {
 5177:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 5178:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 5179:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 5180:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 5181:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 5182:                     } else {
 5183:                         my %what = (
 5184:                              spareid => 1,
 5185:                         );
 5186:                         my ($result,$returnhash) = 
 5187:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 5188:                         if ($result eq 'ok') { 
 5189:                             if (ref($returnhash) eq 'HASH') {
 5190:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 5191:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 5192:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 5193:                                 }
 5194:                             }
 5195:                         }
 5196:                     }
 5197:                 }
 5198:             }
 5199:         }
 5200:     }
 5201:     return %spareid;
 5202: }
 5203: 
 5204: sub spares_row {
 5205:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 5206:     my $css_class;
 5207:     my $numinrow = 4;
 5208:     my $itemcount = 1;
 5209:     my $datatable;
 5210:     my %typetitles = &sparestype_titles();
 5211:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 5212:         foreach my $server (sort(keys(%{$servers}))) {
 5213:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 5214:             my ($othercontrol,$serverdom);
 5215:             if ($serverhome ne $server) {
 5216:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 5217:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 5218:             } else {
 5219:                 $serverdom = &Apache::lonnet::host_domain($server);
 5220:                 if ($serverdom ne $dom) {
 5221:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 5222:                 }
 5223:             }
 5224:             next unless (ref($spareid->{$server}) eq 'HASH');
 5225:             my $checkednow;
 5226:             if (ref($curroffloadnow) eq 'HASH') {
 5227:                 if ($curroffloadnow->{$server}) {
 5228:                     $checkednow = ' checked="checked"';
 5229:                 }
 5230:             }
 5231:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5232:             $datatable .= '<tr'.$css_class.'>
 5233:                            <td rowspan="2">
 5234:                             <span class="LC_nobreak">'.
 5235:                           &mt('[_1] when busy, offloads to:'
 5236:                               ,'<b>'.$server.'</b>').'</span><br />'.
 5237:                           '<span class="LC_nobreak">'."\n".
 5238:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 5239:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 5240:                           "\n";
 5241:             my (%current,%canselect);
 5242:             my @choices = 
 5243:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 5244:             foreach my $type ('primary','default') {
 5245:                 if (ref($spareid->{$server}) eq 'HASH') {
 5246:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 5247:                         my @spares = @{$spareid->{$server}{$type}};
 5248:                         if (@spares > 0) {
 5249:                             if ($othercontrol) {
 5250:                                 $current{$type} = join(', ',@spares);
 5251:                             } else {
 5252:                                 $current{$type} .= '<table>';
 5253:                                 my $numspares = scalar(@spares);
 5254:                                 for (my $i=0;  $i<@spares; $i++) {
 5255:                                     my $rem = $i%($numinrow);
 5256:                                     if ($rem == 0) {
 5257:                                         if ($i > 0) {
 5258:                                             $current{$type} .= '</tr>';
 5259:                                         }
 5260:                                         $current{$type} .= '<tr>';
 5261:                                     }
 5262:                                     $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;'.
 5263:                                                        $spareid->{$server}{$type}[$i].
 5264:                                                        '</label></td>'."\n";
 5265:                                 }
 5266:                                 my $rem = @spares%($numinrow);
 5267:                                 my $colsleft = $numinrow - $rem;
 5268:                                 if ($colsleft > 1 ) {
 5269:                                     $current{$type} .= '<td colspan="'.$colsleft.
 5270:                                                        '" class="LC_left_item">'.
 5271:                                                        '&nbsp;</td>';
 5272:                                 } elsif ($colsleft == 1) {
 5273:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 5274:                                 }
 5275:                                 $current{$type} .= '</tr></table>';
 5276:                             }
 5277:                         }
 5278:                     }
 5279:                     if ($current{$type} eq '') {
 5280:                         $current{$type} = &mt('None specified');
 5281:                     }
 5282:                     if ($othercontrol) {
 5283:                         if ($type eq 'primary') {
 5284:                             $canselect{$type} = $othercontrol;
 5285:                         }
 5286:                     } else {
 5287:                         $canselect{$type} = 
 5288:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 5289:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 5290:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 5291:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 5292:                         if (@choices > 0) {
 5293:                             foreach my $lonhost (@choices) {
 5294:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 5295:                             }
 5296:                         }
 5297:                         $canselect{$type} .= '</select>'."\n";
 5298:                     }
 5299:                 } else {
 5300:                     $current{$type} = &mt('Could not be determined');
 5301:                     if ($type eq 'primary') {
 5302:                         $canselect{$type} =  $othercontrol;
 5303:                     }
 5304:                 }
 5305:                 if ($type eq 'default') {
 5306:                     $datatable .= '<tr'.$css_class.'>';
 5307:                 }
 5308:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 5309:                               '<td>'.$current{$type}.'</td>'."\n".
 5310:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 5311:             }
 5312:             $itemcount ++;
 5313:         }
 5314:     }
 5315:     $$rowtotal += $itemcount;
 5316:     return $datatable;
 5317: }
 5318: 
 5319: sub possible_newspares {
 5320:     my ($server,$currspares,$serverhomes,$altids) = @_;
 5321:     my $serverhostname = &Apache::lonnet::hostname($server);
 5322:     my %excluded;
 5323:     if ($serverhostname ne '') {
 5324:         %excluded = (
 5325:                        $serverhostname => 1,
 5326:                     );
 5327:     }
 5328:     if (ref($currspares) eq 'HASH') {
 5329:         foreach my $type (keys(%{$currspares})) {
 5330:             if (ref($currspares->{$type}) eq 'ARRAY') {
 5331:                 if (@{$currspares->{$type}} > 0) {
 5332:                     foreach my $curr (@{$currspares->{$type}}) {
 5333:                         my $hostname = &Apache::lonnet::hostname($curr);
 5334:                         $excluded{$hostname} = 1;
 5335:                     }
 5336:                 }
 5337:             }
 5338:         }
 5339:     }
 5340:     my @choices;
 5341:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 5342:         if (keys(%{$serverhomes}) > 1) {
 5343:             foreach my $name (sort(keys(%{$serverhomes}))) {
 5344:                 unless ($excluded{$name}) {
 5345:                     if (exists($altids->{$serverhomes->{$name}})) {
 5346:                         push(@choices,$altids->{$serverhomes->{$name}});
 5347:                     } else {
 5348:                         push(@choices,$serverhomes->{$name});
 5349:                     }
 5350:                 }
 5351:             }
 5352:         }
 5353:     }
 5354:     return sort(@choices);
 5355: }
 5356: 
 5357: sub print_loadbalancing {
 5358:     my ($dom,$settings,$rowtotal) = @_;
 5359:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 5360:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 5361:     my $numinrow = 1;
 5362:     my $datatable;
 5363:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5364:     my (%currbalancer,%currtargets,%currrules,%existing);
 5365:     if (ref($settings) eq 'HASH') {
 5366:         %existing = %{$settings};
 5367:     }
 5368:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 5369:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 5370:                                   \%currtargets,\%currrules);
 5371:     } else {
 5372:         return;
 5373:     }
 5374:     my ($othertitle,$usertypes,$types) =
 5375:         &Apache::loncommon::sorted_inst_types($dom);
 5376:     my $rownum = 8;
 5377:     if (ref($types) eq 'ARRAY') {
 5378:         $rownum += scalar(@{$types});
 5379:     }
 5380:     my @css_class = ('LC_odd_row','LC_even_row');
 5381:     my $balnum = 0;
 5382:     my $islast;
 5383:     my (@toshow,$disabledtext);
 5384:     if (keys(%currbalancer) > 0) {
 5385:         @toshow = sort(keys(%currbalancer));
 5386:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 5387:             push(@toshow,'');
 5388:         }
 5389:     } else {
 5390:         @toshow = ('');
 5391:         $disabledtext = &mt('No existing load balancer');
 5392:     }
 5393:     foreach my $lonhost (@toshow) {
 5394:         if ($balnum == scalar(@toshow)-1) {
 5395:             $islast = 1;
 5396:         } else {
 5397:             $islast = 0;
 5398:         }
 5399:         my $cssidx = $balnum%2;
 5400:         my $targets_div_style = 'display: none';
 5401:         my $disabled_div_style = 'display: block';
 5402:         my $homedom_div_style = 'display: none';
 5403:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 5404:                       '<td rowspan="'.$rownum.'" style="vertical-align: top">'.
 5405:                       '<p>';
 5406:         if ($lonhost eq '') {
 5407:             $datatable .= '<span class="LC_nobreak">';
 5408:             if (keys(%currbalancer) > 0) {
 5409:                 $datatable .= &mt('Add balancer:');
 5410:             } else {
 5411:                 $datatable .= &mt('Enable balancer:');
 5412:             }
 5413:             $datatable .= '&nbsp;'.
 5414:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 5415:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 5416:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 5417:                           '<option value="" selected="selected">'.&mt('None').
 5418:                           '</option>'."\n";
 5419:             foreach my $server (sort(keys(%servers))) {
 5420:                 next if ($currbalancer{$server});
 5421:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 5422:             }
 5423:             $datatable .=
 5424:                 '</select>'."\n".
 5425:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 5426:         } else {
 5427:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 5428:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 5429:                            &mt('Stop balancing').'</label>'.
 5430:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 5431:             $targets_div_style = 'display: block';
 5432:             $disabled_div_style = 'display: none';
 5433:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 5434:                 $homedom_div_style = 'display: block';
 5435:             }
 5436:         }
 5437:         $datatable .= '</p></td><td rowspan="'.$rownum.'" style="vertical-align: top">'.
 5438:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 5439:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 5440:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 5441:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 5442:         my @sparestypes = ('primary','default');
 5443:         my %typetitles = &sparestype_titles();
 5444:         my %hostherechecked = (
 5445:                                   no => ' checked="checked"',
 5446:                               );
 5447:         foreach my $sparetype (@sparestypes) {
 5448:             my $targettable;
 5449:             for (my $i=0; $i<$numspares; $i++) {
 5450:                 my $checked;
 5451:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 5452:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5453:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5454:                             $checked = ' checked="checked"';
 5455:                         }
 5456:                     }
 5457:                 }
 5458:                 my ($chkboxval,$disabled);
 5459:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 5460:                     $chkboxval = $spares[$i];
 5461:                 }
 5462:                 if (exists($currbalancer{$spares[$i]})) {
 5463:                     $disabled = ' disabled="disabled"';
 5464:                 }
 5465:                 $targettable .=
 5466:                     '<td><span class="LC_nobreak"><label>'.
 5467:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 5468:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 5469:                     '</span></label></span></td>';
 5470:                 my $rem = $i%($numinrow);
 5471:                 if ($rem == 0) {
 5472:                     if (($i > 0) && ($i < $numspares-1)) {
 5473:                         $targettable .= '</tr>';
 5474:                     }
 5475:                     if ($i < $numspares-1) {
 5476:                         $targettable .= '<tr>';
 5477:                     }
 5478:                 }
 5479:             }
 5480:             if ($targettable ne '') {
 5481:                 my $rem = $numspares%($numinrow);
 5482:                 my $colsleft = $numinrow - $rem;
 5483:                 if ($colsleft > 1 ) {
 5484:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5485:                                     '&nbsp;</td>';
 5486:                 } elsif ($colsleft == 1) {
 5487:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 5488:                 }
 5489:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 5490:                                '<table><tr>'.$targettable.'</tr></table><br />';
 5491:             }
 5492:             $hostherechecked{$sparetype} = '';
 5493:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 5494:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5495:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5496:                         $hostherechecked{$sparetype} = ' checked="checked"';
 5497:                         $hostherechecked{'no'} = '';
 5498:                     }
 5499:                 }
 5500:             }
 5501:         }
 5502:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 5503:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 5504:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 5505:         foreach my $sparetype (@sparestypes) {
 5506:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 5507:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 5508:                           '</i></label><br />';
 5509:         }
 5510:         $datatable .= '</div></td></tr>'.
 5511:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 5512:                                            $othertitle,$usertypes,$types,\%servers,
 5513:                                            \%currbalancer,$lonhost,
 5514:                                            $targets_div_style,$homedom_div_style,
 5515:                                            $css_class[$cssidx],$balnum,$islast);
 5516:         $$rowtotal += $rownum;
 5517:         $balnum ++;
 5518:     }
 5519:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 5520:     return $datatable;
 5521: }
 5522: 
 5523: sub get_loadbalancers_config {
 5524:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 5525:     return unless ((ref($servers) eq 'HASH') &&
 5526:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 5527:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 5528:     if (keys(%{$existing}) > 0) {
 5529:         my $oldlonhost;
 5530:         foreach my $key (sort(keys(%{$existing}))) {
 5531:             if ($key eq 'lonhost') {
 5532:                 $oldlonhost = $existing->{'lonhost'};
 5533:                 $currbalancer->{$oldlonhost} = 1;
 5534:             } elsif ($key eq 'targets') {
 5535:                 if ($oldlonhost) {
 5536:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 5537:                 }
 5538:             } elsif ($key eq 'rules') {
 5539:                 if ($oldlonhost) {
 5540:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 5541:                 }
 5542:             } elsif (ref($existing->{$key}) eq 'HASH') {
 5543:                 $currbalancer->{$key} = 1;
 5544:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 5545:                 $currrules->{$key} = $existing->{$key}{'rules'};
 5546:             }
 5547:         }
 5548:     } else {
 5549:         my ($balancerref,$targetsref) =
 5550:                 &Apache::lonnet::get_lonbalancer_config($servers);
 5551:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 5552:             foreach my $server (sort(keys(%{$balancerref}))) {
 5553:                 $currbalancer->{$server} = 1;
 5554:                 $currtargets->{$server} = $targetsref->{$server};
 5555:             }
 5556:         }
 5557:     }
 5558:     return;
 5559: }
 5560: 
 5561: sub loadbalancing_rules {
 5562:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 5563:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 5564:         $css_class,$balnum,$islast) = @_;
 5565:     my $output;
 5566:     my $num = 0;
 5567:     my ($alltypes,$othertypes,$titles) =
 5568:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 5569:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 5570:         foreach my $type (@{$alltypes}) {
 5571:             $num ++;
 5572:             my $current;
 5573:             if (ref($currrules) eq 'HASH') {
 5574:                 $current = $currrules->{$type};
 5575:             }
 5576:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5577:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 5578:                     $current = '';
 5579:                 }
 5580:             }
 5581:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 5582:                                              $servers,$currbalancer,$lonhost,$dom,
 5583:                                              $targets_div_style,$homedom_div_style,
 5584:                                              $css_class,$balnum,$num,$islast);
 5585:         }
 5586:     }
 5587:     return $output;
 5588: }
 5589: 
 5590: sub loadbalancing_titles {
 5591:     my ($dom,$intdom,$usertypes,$types) = @_;
 5592:     my %othertypes = (
 5593:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 5594:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 5595:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 5596:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 5597:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 5598:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 5599:                      );
 5600:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 5601:     my @available;
 5602:     if (ref($types) eq 'ARRAY') {
 5603:         @available = @{$types};
 5604:     }
 5605:     unless (grep(/^default$/,@available)) {
 5606:         push(@available,'default');
 5607:     }
 5608:     unshift(@alltypes,@available);
 5609:     my %titles;
 5610:     foreach my $type (@alltypes) {
 5611:         if ($type =~ /^_LC_/) {
 5612:             $titles{$type} = $othertypes{$type};
 5613:         } elsif ($type eq 'default') {
 5614:             $titles{$type} = &mt('All users from [_1]',$dom);
 5615:             if (ref($types) eq 'ARRAY') {
 5616:                 if (@{$types} > 0) {
 5617:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 5618:                 }
 5619:             }
 5620:         } elsif (ref($usertypes) eq 'HASH') {
 5621:             $titles{$type} = $usertypes->{$type};
 5622:         }
 5623:     }
 5624:     return (\@alltypes,\%othertypes,\%titles);
 5625: }
 5626: 
 5627: sub loadbalance_rule_row {
 5628:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 5629:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 5630:     my @rulenames;
 5631:     my %ruletitles = &offloadtype_text();
 5632:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 5633:         @rulenames = ('balancer','offloadedto','specific');
 5634:     } else {
 5635:         @rulenames = ('default','homeserver');
 5636:         if ($type eq '_LC_external') {
 5637:             push(@rulenames,'externalbalancer');
 5638:         } else {
 5639:             push(@rulenames,'specific');
 5640:         }
 5641:         push(@rulenames,'none');
 5642:     }
 5643:     my $style = $targets_div_style;
 5644:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5645:         $style = $homedom_div_style;
 5646:     }
 5647:     my $space;
 5648:     if ($islast && $num == 1) {
 5649:         $space = '<div style="display:inline-block;">&nbsp;</div>';
 5650:     }
 5651:     my $output =
 5652:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td style="vertical-align: top">'.$space.
 5653:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 5654:         '<td valaign="top">'.$space.
 5655:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 5656:     for (my $i=0; $i<@rulenames; $i++) {
 5657:         my $rule = $rulenames[$i];
 5658:         my ($checked,$extra);
 5659:         if ($rulenames[$i] eq 'default') {
 5660:             $rule = '';
 5661:         }
 5662:         if ($rulenames[$i] eq 'specific') {
 5663:             if (ref($servers) eq 'HASH') {
 5664:                 my $default;
 5665:                 if (($current ne '') && (exists($servers->{$current}))) {
 5666:                     $checked = ' checked="checked"';
 5667:                 }
 5668:                 unless ($checked) {
 5669:                     $default = ' selected="selected"';
 5670:                 }
 5671:                 $extra =
 5672:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5673:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5674:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 5675:                     '<option value=""'.$default.'></option>'."\n";
 5676:                 foreach my $server (sort(keys(%{$servers}))) {
 5677:                     if (ref($currbalancer) eq 'HASH') {
 5678:                         next if (exists($currbalancer->{$server}));
 5679:                     }
 5680:                     my $selected;
 5681:                     if ($server eq $current) {
 5682:                         $selected = ' selected="selected"';
 5683:                     }
 5684:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 5685:                 }
 5686:                 $extra .= '</select>';
 5687:             }
 5688:         } elsif ($rule eq $current) {
 5689:             $checked = ' checked="checked"';
 5690:         }
 5691:         $output .= '<span class="LC_nobreak"><label>'.
 5692:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 5693:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 5694:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 5695:                    ')"'.$checked.' />&nbsp;';
 5696:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 5697:             $output .= $ruletitles{'particular'};
 5698:         } else {
 5699:             $output .= $ruletitles{$rulenames[$i]};
 5700:         }
 5701:         $output .= '</label>'.$extra.'</span><br />'."\n";
 5702:     }
 5703:     $output .= '</div></td></tr>'."\n";
 5704:     return $output;
 5705: }
 5706: 
 5707: sub offloadtype_text {
 5708:     my %ruletitles = &Apache::lonlocal::texthash (
 5709:            'default'          => 'Offloads to default destinations',
 5710:            'homeserver'       => "Offloads to user's home server",
 5711:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 5712:            'specific'         => 'Offloads to specific server',
 5713:            'none'             => 'No offload',
 5714:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 5715:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 5716:            'particular'       => 'Session hosted (after re-auth) on server:',
 5717:     );
 5718:     return %ruletitles;
 5719: }
 5720: 
 5721: sub sparestype_titles {
 5722:     my %typestitles = &Apache::lonlocal::texthash (
 5723:                           'primary' => 'primary',
 5724:                           'default' => 'default',
 5725:                       );
 5726:     return %typestitles;
 5727: }
 5728: 
 5729: sub contact_titles {
 5730:     my %titles = &Apache::lonlocal::texthash (
 5731:                    'supportemail'    => 'Support E-mail address',
 5732:                    'adminemail'      => 'Default Server Admin E-mail address',
 5733:                    'errormail'       => 'Error reports to be e-mailed to',
 5734:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 5735:                    'helpdeskmail'    => "Helpdesk requests from all users in this domain",
 5736:                    'otherdomsmail'   => 'Helpdesk requests from users in other (unconfigured) domains',
 5737:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 5738:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 5739:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 5740:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 5741:                  );
 5742:     my %short_titles = &Apache::lonlocal::texthash (
 5743:                            adminemail   => 'Admin E-mail address',
 5744:                            supportemail => 'Support E-mail',
 5745:                        );   
 5746:     return (\%titles,\%short_titles);
 5747: }
 5748: 
 5749: sub helpform_fields {
 5750:     my %titles =  &Apache::lonlocal::texthash (
 5751:                        'username'   => 'Name',
 5752:                        'user'       => 'Username/domain',
 5753:                        'phone'      => 'Phone',
 5754:                        'cc'         => 'Cc e-mail',
 5755:                        'course'     => 'Course Details',
 5756:                        'section'    => 'Sections',
 5757:                        'screenshot' => 'File upload',
 5758:     );
 5759:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 5760:     my %possoptions = (
 5761:                         username     => ['yes','no','req'],
 5762:                         phone        => ['yes','no','req'],
 5763:                         user         => ['yes','no'],
 5764:                         cc           => ['yes','no'],
 5765:                         course       => ['yes','no'],
 5766:                         section      => ['yes','no'],
 5767:                         screenshot   => ['yes','no'],
 5768:                       );
 5769:     my %fieldoptions = &Apache::lonlocal::texthash (
 5770:                          'yes'  => 'Optional',
 5771:                          'req'  => 'Required',
 5772:                          'no'   => "Not shown",
 5773:     );
 5774:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 5775: }
 5776: 
 5777: sub tool_titles {
 5778:     my %titles = &Apache::lonlocal::texthash (
 5779:                      aboutme    => 'Personal web page',
 5780:                      blog       => 'Blog',
 5781:                      webdav     => 'WebDAV',
 5782:                      portfolio  => 'Portfolio',
 5783:                      official   => 'Official courses (with institutional codes)',
 5784:                      unofficial => 'Unofficial courses',
 5785:                      community  => 'Communities',
 5786:                      textbook   => 'Textbook courses',
 5787:                      placement  => 'Placement tests',
 5788:                  );
 5789:     return %titles;
 5790: }
 5791: 
 5792: sub courserequest_titles {
 5793:     my %titles = &Apache::lonlocal::texthash (
 5794:                                    official   => 'Official',
 5795:                                    unofficial => 'Unofficial',
 5796:                                    community  => 'Communities',
 5797:                                    textbook   => 'Textbook',
 5798:                                    placement  => 'Placement tests',
 5799:                                    norequest  => 'Not allowed',
 5800:                                    approval   => 'Approval by Dom. Coord.',
 5801:                                    validate   => 'With validation',
 5802:                                    autolimit  => 'Numerical limit',
 5803:                                    unlimited  => '(blank for unlimited)',
 5804:                  );
 5805:     return %titles;
 5806: }
 5807: 
 5808: sub authorrequest_titles {
 5809:     my %titles = &Apache::lonlocal::texthash (
 5810:                                    norequest  => 'Not allowed',
 5811:                                    approval   => 'Approval by Dom. Coord.',
 5812:                                    automatic  => 'Automatic approval',
 5813:                  );
 5814:     return %titles;
 5815: }
 5816: 
 5817: sub courserequest_conditions {
 5818:     my %conditions = &Apache::lonlocal::texthash (
 5819:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 5820:        validate   => '(Processing of request subject to institutional validation).',
 5821:                  );
 5822:     return %conditions;
 5823: }
 5824: 
 5825: 
 5826: sub print_usercreation {
 5827:     my ($position,$dom,$settings,$rowtotal) = @_;
 5828:     my $numinrow = 4;
 5829:     my $datatable;
 5830:     if ($position eq 'top') {
 5831:         $$rowtotal ++;
 5832:         my $rowcount = 0;
 5833:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 5834:         if (ref($rules) eq 'HASH') {
 5835:             if (keys(%{$rules}) > 0) {
 5836:                 $datatable .= &user_formats_row('username',$settings,$rules,
 5837:                                                 $ruleorder,$numinrow,$rowcount);
 5838:                 $$rowtotal ++;
 5839:                 $rowcount ++;
 5840:             }
 5841:         }
 5842:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 5843:         if (ref($idrules) eq 'HASH') {
 5844:             if (keys(%{$idrules}) > 0) {
 5845:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 5846:                                                 $idruleorder,$numinrow,$rowcount);
 5847:                 $$rowtotal ++;
 5848:                 $rowcount ++;
 5849:             }
 5850:         }
 5851:         if ($rowcount == 0) {
 5852:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 5853:             $$rowtotal ++;
 5854:             $rowcount ++;
 5855:         }
 5856:     } elsif ($position eq 'middle') {
 5857:         my @creators = ('author','course','requestcrs');
 5858:         my ($rules,$ruleorder) =
 5859:             &Apache::lonnet::inst_userrules($dom,'username');
 5860:         my %lt = &usercreation_types();
 5861:         my %checked;
 5862:         if (ref($settings) eq 'HASH') {
 5863:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 5864:                 foreach my $item (@creators) {
 5865:                     $checked{$item} = $settings->{'cancreate'}{$item};
 5866:                 }
 5867:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 5868:                 foreach my $item (@creators) {
 5869:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 5870:                         $checked{$item} = 'none';
 5871:                     }
 5872:                 }
 5873:             }
 5874:         }
 5875:         my $rownum = 0;
 5876:         foreach my $item (@creators) {
 5877:             $rownum ++;
 5878:             if ($checked{$item} eq '') {
 5879:                 $checked{$item} = 'any';
 5880:             }
 5881:             my $css_class;
 5882:             if ($rownum%2) {
 5883:                 $css_class = '';
 5884:             } else {
 5885:                 $css_class = ' class="LC_odd_row" ';
 5886:             }
 5887:             $datatable .= '<tr'.$css_class.'>'.
 5888:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 5889:                          '</span></td><td style="text-align: right">';
 5890:             my @options = ('any');
 5891:             if (ref($rules) eq 'HASH') {
 5892:                 if (keys(%{$rules}) > 0) {
 5893:                     push(@options,('official','unofficial'));
 5894:                 }
 5895:             }
 5896:             push(@options,'none');
 5897:             foreach my $option (@options) {
 5898:                 my $type = 'radio';
 5899:                 my $check = ' ';
 5900:                 if ($checked{$item} eq $option) {
 5901:                     $check = ' checked="checked" ';
 5902:                 } 
 5903:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5904:                               '<input type="'.$type.'" name="can_createuser_'.
 5905:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 5906:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 5907:             }
 5908:             $datatable .= '</td></tr>';
 5909:         }
 5910:     } else {
 5911:         my @contexts = ('author','course','domain');
 5912:         my @authtypes = ('int','krb4','krb5','loc');
 5913:         my %checked;
 5914:         if (ref($settings) eq 'HASH') {
 5915:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 5916:                 foreach my $item (@contexts) {
 5917:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 5918:                         foreach my $auth (@authtypes) {
 5919:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 5920:                                 $checked{$item}{$auth} = ' checked="checked" ';
 5921:                             }
 5922:                         }
 5923:                     }
 5924:                 }
 5925:             }
 5926:         } else {
 5927:             foreach my $item (@contexts) {
 5928:                 foreach my $auth (@authtypes) {
 5929:                     $checked{$item}{$auth} = ' checked="checked" ';
 5930:                 }
 5931:             }
 5932:         }
 5933:         my %title = &context_names();
 5934:         my %authname = &authtype_names();
 5935:         my $rownum = 0;
 5936:         my $css_class; 
 5937:         foreach my $item (@contexts) {
 5938:             if ($rownum%2) {
 5939:                 $css_class = '';
 5940:             } else {
 5941:                 $css_class = ' class="LC_odd_row" ';
 5942:             }
 5943:             $datatable .=   '<tr'.$css_class.'>'.
 5944:                             '<td>'.$title{$item}.
 5945:                             '</td><td class="LC_left_item">'.
 5946:                             '<span class="LC_nobreak">';
 5947:             foreach my $auth (@authtypes) {
 5948:                 $datatable .= '<label>'. 
 5949:                               '<input type="checkbox" name="'.$item.'_auth" '.
 5950:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 5951:                               $authname{$auth}.'</label>&nbsp;';
 5952:             }
 5953:             $datatable .= '</span></td></tr>';
 5954:             $rownum ++;
 5955:         }
 5956:         $$rowtotal += $rownum;
 5957:     }
 5958:     return $datatable;
 5959: }
 5960: 
 5961: sub print_selfcreation {
 5962:     my ($position,$dom,$settings,$rowtotal) = @_;
 5963:     my (@selfcreate,$createsettings,$processing,$emailoptions,$emailverified,
 5964:         $emaildomain,$datatable);
 5965:     if (ref($settings) eq 'HASH') {
 5966:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 5967:             $createsettings = $settings->{'cancreate'};
 5968:             if (ref($createsettings) eq 'HASH') {
 5969:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 5970:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 5971:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 5972:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 5973:                         @selfcreate = ('email','login','sso');
 5974:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 5975:                         @selfcreate = ($createsettings->{'selfcreate'});
 5976:                     }
 5977:                 }
 5978:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 5979:                     $processing = $createsettings->{'selfcreateprocessing'};
 5980:                 }
 5981:                 if (ref($createsettings->{'emailoptions'}) eq 'HASH') {
 5982:                     $emailoptions = $createsettings->{'emailoptions'};
 5983:                 }
 5984:                 if (ref($createsettings->{'emailverified'}) eq 'HASH') {
 5985:                     $emailverified = $createsettings->{'emailverified'};
 5986:                 }
 5987:                 if (ref($createsettings->{'emaildomain'}) eq 'HASH') {
 5988:                     $emaildomain = $createsettings->{'emaildomain'};
 5989:                 }
 5990:             }
 5991:         }
 5992:     }
 5993:     my %radiohash;
 5994:     my $numinrow = 4;
 5995:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 5996:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5997:     if ($position eq 'top') {
 5998:         my %choices = &Apache::lonlocal::texthash (
 5999:                                                       cancreate_login      => 'Institutional Login',
 6000:                                                       cancreate_sso        => 'Institutional Single Sign On',
 6001:                                                   );
 6002:         my @toggles = sort(keys(%choices));
 6003:         my %defaultchecked = (
 6004:                                'cancreate_login' => 'off',
 6005:                                'cancreate_sso'   => 'off',
 6006:                              );
 6007:         my ($onclick,$itemcount);
 6008:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 6009:                                                      \%choices,$itemcount,$onclick);
 6010:         $$rowtotal += $itemcount;
 6011:         
 6012:         if (ref($usertypes) eq 'HASH') {
 6013:             if (keys(%{$usertypes}) > 0) {
 6014:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 6015:                                              $dom,$numinrow,$othertitle,
 6016:                                              'statustocreate',$rowtotal);
 6017:                 $$rowtotal ++;
 6018:             }
 6019:         }
 6020:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 6021:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 6022:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 6023:         my $rem;
 6024:         my $numperrow = 2;
 6025:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 6026:         $datatable .= '<tr'.$css_class.'>'.
 6027:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 6028:                      '<td class="LC_left_item">'."\n".
 6029:                      '<table><tr><td>'."\n";
 6030:         for (my $i=0; $i<@fields; $i++) {
 6031:             $rem = $i%($numperrow);
 6032:             if ($rem == 0) {
 6033:                 if ($i > 0) {
 6034:                     $datatable .= '</tr>';
 6035:                 }
 6036:                 $datatable .= '<tr>';
 6037:             }
 6038:             my $currval;
 6039:             if (ref($createsettings) eq 'HASH') {
 6040:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 6041:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 6042:                 }
 6043:             }
 6044:             $datatable .= '<td class="LC_left_item">'.
 6045:                           '<span class="LC_nobreak">'.
 6046:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 6047:                           'value="'.$currval.'" size="10" />&nbsp;'.
 6048:                           $fieldtitles{$fields[$i]}.'</span></td>';
 6049:         }
 6050:         my $colsleft = $numperrow - $rem;
 6051:         if ($colsleft > 1 ) {
 6052:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6053:                          '&nbsp;</td>';
 6054:         } elsif ($colsleft == 1) {
 6055:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 6056:         }
 6057:         $datatable .= '</tr></table></td></tr>';
 6058:         $$rowtotal ++;
 6059:     } elsif ($position eq 'middle') {
 6060:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 6061:         my @posstypes;
 6062:         if (ref($types) eq 'ARRAY') {
 6063:             @posstypes = @{$types};
 6064:         }
 6065:         unless (grep(/^default$/,@posstypes)) {
 6066:             push(@posstypes,'default');
 6067:         }
 6068:         my %usertypeshash;
 6069:         if (ref($usertypes) eq 'HASH') {
 6070:             %usertypeshash = %{$usertypes};
 6071:         }
 6072:         $usertypeshash{'default'} = $othertitle;
 6073:         foreach my $status (@posstypes) {
 6074:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 6075:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 6076:             $$rowtotal ++;
 6077:         }
 6078:     } else {
 6079:         my %choices = &Apache::lonlocal::texthash (
 6080:                           'cancreate_email' => 'Non-institutional username (via e-mail verification)',
 6081:                                                   );
 6082:         my @toggles = sort(keys(%choices));
 6083:         my %defaultchecked = (
 6084:                                'cancreate_email' => 'off',
 6085:                              );
 6086:         my $customclass = 'LC_selfcreate_email';
 6087:         my $classprefix = 'LC_canmodify_emailusername_';
 6088:         my $optionsprefix = 'LC_options_emailusername_';
 6089:         my $display = 'none';
 6090:         my $rowstyle = 'display:none';
 6091:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 6092:             $display = 'block';
 6093:             $rowstyle = 'display:table-row';
 6094:         }
 6095:         my $onclick = "toggleRows(this.form,'cancreate_email','selfassign','$customclass','$classprefix','$optionsprefix');";
 6096:         ($datatable,$$rowtotal) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 6097:                                                      \%choices,$$rowtotal,$onclick);
 6098:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal,$customclass,
 6099:                                          $rowstyle);
 6100:         $$rowtotal ++;
 6101:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal,$customclass,
 6102:                                       $rowstyle);
 6103:         $$rowtotal ++;
 6104:         my (@ordered,@posstypes,%usertypeshash);
 6105:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 6106:         my ($emailrules,$emailruleorder) =
 6107:             &Apache::lonnet::inst_userrules($dom,'email');
 6108:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 6109:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 6110:         if (ref($types) eq 'ARRAY') {
 6111:             @posstypes = @{$types};
 6112:         }
 6113:         if (@posstypes) {
 6114:             unless (grep(/^default$/,@posstypes)) {
 6115:                 push(@posstypes,'default');
 6116:             }
 6117:             if (ref($usertypes) eq 'HASH') {
 6118:                 %usertypeshash = %{$usertypes};
 6119:             }
 6120:             my $currassign;
 6121:             if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 6122:                 $currassign = {
 6123:                                   selfassign => $domdefaults{'inststatusguest'},
 6124:                               };
 6125:                 @ordered = @{$domdefaults{'inststatusguest'}};
 6126:             } else {
 6127:                 $currassign = { selfassign => [] };
 6128:             }
 6129:             my $onclicktypes = "toggleDataRow(this.form,'selfassign','$customclass','$optionsprefix',);".
 6130:                                "toggleDataRow(this.form,'selfassign','$customclass','$classprefix',1);";
 6131:             $datatable .= &insttypes_row($currassign,$types,$usertypes,$dom,
 6132:                                          $numinrow,$othertitle,'selfassign',
 6133:                                          $rowtotal,$onclicktypes,$customclass,
 6134:                                          $rowstyle);
 6135:             $$rowtotal ++;
 6136:             $usertypeshash{'default'} = $othertitle;
 6137:             foreach my $status (@posstypes) {
 6138:                 my $css_class;
 6139:                 if ($$rowtotal%2) {
 6140:                     $css_class = 'LC_odd_row ';
 6141:                 }
 6142:                 $css_class .= $customclass;
 6143:                 my $rowid = $optionsprefix.$status;
 6144:                 my $hidden = 1;
 6145:                 my $currstyle = 'display:none';
 6146:                 if (grep(/^\Q$status\E$/,@ordered)) {
 6147:                     $currstyle = $rowstyle;
 6148:                     $hidden = 0; 
 6149:                 }
 6150:                 $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 6151:                                              $emailrules,$emailruleorder,$settings,$status,$rowid,
 6152:                                              $usertypeshash{$status},$css_class,$currstyle,$intdom);
 6153:                 unless ($hidden) {
 6154:                     $$rowtotal ++;
 6155:                 }
 6156:             }
 6157:         } else {
 6158:             my $css_class;
 6159:             if ($$rowtotal%2) {
 6160:                 $css_class = 'LC_odd_row ';
 6161:             }
 6162:             $css_class .= $customclass;
 6163:             $usertypeshash{'default'} = $othertitle;
 6164:             $datatable .= &noninst_users($processing,$emailverified,$emailoptions,$emaildomain,
 6165:                                          $emailrules,$emailruleorder,$settings,'default','',
 6166:                                          $othertitle,$css_class,$rowstyle,$intdom);
 6167:             $$rowtotal ++;
 6168:         }
 6169:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 6170:         $numinrow = 1;
 6171:         if (@posstypes) {
 6172:             foreach my $status (@posstypes) {
 6173:                 my $rowid = $classprefix.$status;
 6174:                 my $datarowstyle = 'display:none';
 6175:                 if (grep(/^\Q$status\E$/,@ordered)) { 
 6176:                     $datarowstyle = $rowstyle; 
 6177:                 }
 6178:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 6179:                                                        $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 6180:                                                        $infotitles,$rowid,$customclass,$datarowstyle);
 6181:                 unless ($datarowstyle eq 'display:none') {
 6182:                     $$rowtotal ++;
 6183:                 }
 6184:             }
 6185:         } else {
 6186:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_default',$settings,
 6187:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,
 6188:                                                    $infotitles,'',$customclass,$rowstyle);
 6189:         }
 6190:     }
 6191:     return $datatable;
 6192: }
 6193: 
 6194: sub selfcreate_javascript {
 6195:     return <<"ENDSCRIPT";
 6196: 
 6197: <script type="text/javascript">
 6198: // <![CDATA[
 6199: 
 6200: function toggleRows(form,radio,checkbox,target,prefix,altprefix) {
 6201:     var x = document.getElementsByClassName(target);
 6202:     var insttypes = 0;
 6203:     var insttypeRegExp = new RegExp(prefix);
 6204:     if ((x.length != undefined) && (x.length > 0)) {
 6205:         if (form.elements[radio].length != undefined) {
 6206:             for (var i=0; i<form.elements[radio].length; i++) {
 6207:                 if (form.elements[radio][i].checked) {
 6208:                     if (form.elements[radio][i].value == 1) {
 6209:                         for (var j=0; j<x.length; j++) {
 6210:                             if (x[j].id == 'undefined') {
 6211:                                 x[j].style.display = 'table-row';
 6212:                             } else if (insttypeRegExp.test(x[j].id)) {
 6213:                                 insttypes ++;
 6214:                             } else {
 6215:                                 x[j].style.display = 'table-row';
 6216:                             }
 6217:                         }
 6218:                     } else {
 6219:                         for (var j=0; j<x.length; j++) {
 6220:                             x[j].style.display = 'none';
 6221:                         }
 6222:                     }
 6223:                     break;
 6224:                 }
 6225:             }
 6226:             if (insttypes > 0) {
 6227:                 toggleDataRow(form,checkbox,target,altprefix);
 6228:                 toggleDataRow(form,checkbox,target,prefix,1);
 6229:             }
 6230:         }
 6231:     }
 6232:     return;
 6233: }
 6234: 
 6235: function toggleDataRow(form,checkbox,target,prefix,docount) {
 6236:     if (form.elements[checkbox].length != undefined) {
 6237:         var count = 0;
 6238:         if (docount) {
 6239:             for (var i=0; i<form.elements[checkbox].length; i++) {
 6240:                 if (form.elements[checkbox][i].checked) {
 6241:                     count ++;
 6242:                 }
 6243:             }
 6244:         }
 6245:         for (var i=0; i<form.elements[checkbox].length; i++) {
 6246:             var type = form.elements[checkbox][i].value;
 6247:             if (document.getElementById(prefix+type)) {
 6248:                 if (form.elements[checkbox][i].checked) {
 6249:                     document.getElementById(prefix+type).style.display = 'table-row';
 6250:                     if (count % 2 == 1) {
 6251:                         document.getElementById(prefix+type).className = target+' LC_odd_row';
 6252:                     } else {
 6253:                         document.getElementById(prefix+type).className = target;
 6254:                     }
 6255:                     count ++;
 6256:                 } else {
 6257:                     document.getElementById(prefix+type).style.display = 'none';
 6258:                 }
 6259:             }
 6260:         }
 6261:     }
 6262:     return;
 6263: }
 6264: 
 6265: function toggleEmailOptions(form,radio,prefix,altprefix,status) {
 6266:     var caller = radio+'_'+status;
 6267:     if (form.elements[caller].length != undefined) {
 6268:         for (var i=0; i<form.elements[caller].length; i++) {
 6269:             if (form.elements[caller][i].checked) {
 6270:                 if (document.getElementById(altprefix+'_inst_'+status)) {
 6271:                     var curr = form.elements[caller][i].value;
 6272:                     if (prefix) {
 6273:                         document.getElementById(prefix+'_'+status).style.display = 'none';
 6274:                     }
 6275:                     document.getElementById(altprefix+'_inst_'+status).style.display = 'none';
 6276:                     document.getElementById(altprefix+'_noninst_'+status).style.display = 'none';
 6277:                     if (curr == 'custom') {
 6278:                         if (prefix) { 
 6279:                             document.getElementById(prefix+'_'+status).style.display = 'inline';
 6280:                         }
 6281:                     } else if (curr == 'inst') {
 6282:                         document.getElementById(altprefix+'_inst_'+status).style.display = 'inline';
 6283:                     } else if (curr == 'noninst') {
 6284:                         document.getElementById(altprefix+'_noninst_'+status).style.display = 'inline';
 6285:                     }
 6286:                     break;
 6287:                 }
 6288:             }
 6289:         }
 6290:     }
 6291: }
 6292: 
 6293: // ]]>
 6294: </script>
 6295: 
 6296: ENDSCRIPT
 6297: }
 6298: 
 6299: sub noninst_users {
 6300:     my ($processing,$emailverified,$emailoptions,$emaildomain,$emailrules,
 6301:         $emailruleorder,$settings,$type,$rowid,$typetitle,$css_class,$rowstyle,$intdom) = @_; 
 6302:     my $class = 'LC_left_item';
 6303:     if ($css_class) {
 6304:         $css_class = ' class="'.$css_class.'"'; 
 6305:     }
 6306:     if ($rowid) {
 6307:         $rowid = ' id="'.$rowid.'"';
 6308:     }
 6309:     if ($rowstyle) {
 6310:         $rowstyle = ' style="'.$rowstyle.'"';
 6311:     }
 6312:     my ($output,$description);
 6313:     if ($type eq 'default') {
 6314:         $description = &mt('Requests for: [_1]',$typetitle);
 6315:     } else {
 6316:         $description = &mt('Requests for: [_1] (status self-reported)',$typetitle);
 6317:     }
 6318:     $output = '<tr'.$css_class.$rowid.$rowstyle.'>'.
 6319:               "<td>$description</td>\n".           
 6320:               '<td class="'.$class.'" colspan="2">'.
 6321:               '<table><tr>';
 6322:     my %headers = &Apache::lonlocal::texthash( 
 6323:               approve  => 'Processing',
 6324:               email    => 'E-mail',
 6325:               username => 'Username',
 6326:     );
 6327:     foreach my $item ('approve','email','username') {
 6328:         $output .= '<th>'.$headers{$item}.'</th>';
 6329:     }
 6330:     $output .= '</tr><tr>';
 6331:     foreach my $item ('approve','email','username') {
 6332:         $output .= '<td style="vertical-align: top">';
 6333:         my (%choices,@options,$hashref,$defoption,$name,$onclick,$hascustom);
 6334:         if ($item eq 'approve') {
 6335:             %choices = &Apache::lonlocal::texthash (
 6336:                                                      automatic => 'Automatically approved',
 6337:                                                      approval  => 'Queued for approval',
 6338:                                                    );
 6339:             @options = ('automatic','approval');
 6340:             $hashref = $processing;
 6341:             $defoption = 'automatic';
 6342:             $name = 'cancreate_emailprocess_'.$type;
 6343:         } elsif ($item eq 'email') {
 6344:             %choices = &Apache::lonlocal::texthash (
 6345:                                                      any     => 'Any e-mail',
 6346:                                                      inst    => 'Institutional only',
 6347:                                                      noninst => 'Non-institutional only',
 6348:                                                      custom  => 'Custom restrictions',
 6349:                                                    );
 6350:             @options = ('any','inst','noninst');
 6351:             my $showcustom;
 6352:             if (ref($emailrules) eq 'HASH') {
 6353:                 if (keys(%{$emailrules}) > 0) {
 6354:                     push(@options,'custom');
 6355:                     $showcustom = 'cancreate_emailrule';
 6356:                     if (ref($settings) eq 'HASH') {
 6357:                         if (ref($settings->{'email_rule'}) eq 'ARRAY') {
 6358:                             foreach my $rule (@{$settings->{'email_rule'}}) {
 6359:                                 if (exists($emailrules->{$rule})) {
 6360:                                     $hascustom ++;
 6361:                                 }
 6362:                             }
 6363:                         } elsif (ref($settings->{'email_rule'}) eq 'HASH') {
 6364:                             if (ref($settings->{'email_rule'}{$type}) eq 'ARRAY') {
 6365:                                 foreach my $rule (@{$settings->{'email_rule'}{$type}}) {
 6366:                                     if (exists($emailrules->{$rule})) {
 6367:                                         $hascustom ++;
 6368:                                     }
 6369:                                 }
 6370:                             }
 6371:                         }
 6372:                     }
 6373:                 }
 6374:             }
 6375:             $onclick = ' onclick="toggleEmailOptions(this.form,'."'cancreate_emailoptions','$showcustom',".
 6376:                                                      "'cancreate_emaildomain','$type'".');"';
 6377:             $hashref = $emailoptions;
 6378:             $defoption = 'any';
 6379:             $name = 'cancreate_emailoptions_'.$type;
 6380:         } elsif ($item eq 'username') {
 6381:             %choices = &Apache::lonlocal::texthash (
 6382:                                                      all    => 'Same as e-mail',
 6383:                                                      first  => 'Omit @domain',
 6384:                                                      free   => 'Free to choose',
 6385:                                                    );
 6386:             @options = ('all','first','free');
 6387:             $hashref = $emailverified;
 6388:             $defoption = 'all';
 6389:             $name = 'cancreate_usernameoptions_'.$type;
 6390:         }
 6391:         foreach my $option (@options) {
 6392:             my $checked;
 6393:             if (ref($hashref) eq 'HASH') {
 6394:                 if ($type eq '') {
 6395:                     if (!exists($hashref->{'default'})) {
 6396:                         if ($option eq $defoption) {
 6397:                             $checked = ' checked="checked"';
 6398:                         }
 6399:                     } else {
 6400:                         if ($hashref->{'default'} eq $option) {
 6401:                             $checked = ' checked="checked"';
 6402:                         }
 6403:                     }
 6404:                 } else {
 6405:                     if (!exists($hashref->{$type})) {
 6406:                         if ($option eq $defoption) {
 6407:                             $checked = ' checked="checked"';
 6408:                         }
 6409:                     } else {
 6410:                         if ($hashref->{$type} eq $option) {
 6411:                             $checked = ' checked="checked"';
 6412:                         }
 6413:                     }
 6414:                 }
 6415:             } elsif (($item eq 'email') && ($hascustom)) {
 6416:                 if ($option eq 'custom') {
 6417:                     $checked = ' checked="checked"';
 6418:                 }
 6419:             } elsif ($option eq $defoption) {
 6420:                 $checked = ' checked="checked"';
 6421:             }
 6422:             $output .= '<span class="LC_nobreak"><label>'.
 6423:                        '<input type="radio" name="'.$name.'"'.
 6424:                        $checked.' value="'.$option.'"'.$onclick.' />'.
 6425:                        $choices{$option}.'</label></span><br />';
 6426:             if ($item eq 'email') {
 6427:                 if ($option eq 'custom') {
 6428:                     my $id = 'cancreate_emailrule_'.$type;
 6429:                     my $display = 'none';
 6430:                     if ($checked) {
 6431:                         $display = 'inline';
 6432:                     }
 6433:                     my $numinrow = 2;
 6434:                     $output .= '<fieldset id="'.$id.'" style="display:'.$display.';">'.
 6435:                                '<legend>'.&mt('Disallow').'</legend><table>'.
 6436:                                &user_formats_row('email',$settings,$emailrules,
 6437:                                                  $emailruleorder,$numinrow,'',$type);
 6438:                               '</table></fieldset>';
 6439:                 } elsif (($option eq 'inst') || ($option eq 'noninst')) {
 6440:                     my %text = &Apache::lonlocal::texthash (
 6441:                                                              inst    => 'must end:',
 6442:                                                              noninst => 'cannot end:',
 6443:                                                            );
 6444:                     my $value;
 6445:                     if (ref($emaildomain) eq 'HASH') {
 6446:                         if (ref($emaildomain->{$type}) eq 'HASH') {
 6447:                             $value = $emaildomain->{$type}->{$option}; 
 6448:                         }
 6449:                     }
 6450:                     if ($value eq '') {
 6451:                         $value = '@'.$intdom;
 6452:                     }
 6453:                     my $condition = 'cancreate_emaildomain_'.$option.'_'.$type;
 6454:                     my $display = 'none';
 6455:                     if ($checked) {
 6456:                         $display = 'inline';
 6457:                     }
 6458:                     $output .= '<div id="'.$condition.'" style="display:'.$display.';">'.
 6459:                                '<span class="LC_domprefs_email">'.$text{$option}.'</span> '.
 6460:                                '<input type="text" name="'.$condition.'" value="'.$value.'" size="10" />'.
 6461:                                '</div>';
 6462:                 }
 6463:             }
 6464:         }
 6465:         $output .= '</td>'."\n";
 6466:     }
 6467:     $output .= "</tr></table></td></tr>\n";
 6468:     return $output;
 6469: }
 6470: 
 6471: sub captcha_choice {
 6472:     my ($context,$settings,$itemcount,$customcss,$rowstyle) = @_;
 6473:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 6474:         $vertext,$currver);
 6475:     my %lt = &captcha_phrases();
 6476:     $keyentry = 'hidden';
 6477:     if ($context eq 'cancreate') {
 6478:         $rowname = &mt('CAPTCHA validation');
 6479:     } elsif ($context eq 'login') {
 6480:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 6481:     }
 6482:     if (ref($settings) eq 'HASH') {
 6483:         if ($settings->{'captcha'}) {
 6484:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 6485:         } else {
 6486:             $checked{'original'} = ' checked="checked"';
 6487:         }
 6488:         if ($settings->{'captcha'} eq 'recaptcha') {
 6489:             $pubtext = $lt{'pub'};
 6490:             $privtext = $lt{'priv'};
 6491:             $keyentry = 'text';
 6492:             $vertext = $lt{'ver'};
 6493:             $currver = $settings->{'recaptchaversion'};
 6494:             if ($currver ne '2') {
 6495:                 $currver = 1;
 6496:             }
 6497:         }
 6498:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 6499:             $currpub = $settings->{'recaptchakeys'}{'public'};
 6500:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 6501:         }
 6502:     } else {
 6503:         $checked{'original'} = ' checked="checked"';
 6504:     }
 6505:     my $css_class;
 6506:     if ($itemcount%2) {
 6507:         $css_class = 'LC_odd_row';
 6508:     }
 6509:     if ($customcss) {
 6510:         $css_class .= " $customcss";
 6511:     }
 6512:     $css_class =~ s/^\s+//;
 6513:     if ($css_class) {
 6514:         $css_class = ' class="'.$css_class.'"';
 6515:     }
 6516:     if ($rowstyle) {
 6517:         $css_class .= ' style="'.$rowstyle.'"';
 6518:     }
 6519:     my $output = '<tr'.$css_class.'>'.
 6520:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 6521:                  '<table><tr><td>'."\n";
 6522:     foreach my $option ('original','recaptcha','notused') {
 6523:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 6524:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 6525:                    $lt{$option}.'</label></span>';
 6526:         unless ($option eq 'notused') {
 6527:             $output .= ('&nbsp;'x2)."\n";
 6528:         }
 6529:     }
 6530: #
 6531: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 6532: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 6533: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 6534: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 6535: #
 6536:     $output .= '</td></tr>'."\n".
 6537:                '<tr><td class="LC_zero_height">'."\n".
 6538:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 6539:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 6540:                $currpub.'" size="40" /></span><br />'."\n".
 6541:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 6542:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 6543:                $currpriv.'" size="40" /></span><br />'.
 6544:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 6545:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 6546:                $currver.'" size="3" /></span><br />'.
 6547:                '</td></tr></table>'."\n".
 6548:                '</td></tr>';
 6549:     return $output;
 6550: }
 6551: 
 6552: sub user_formats_row {
 6553:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount,$status) = @_;
 6554:     my $output;
 6555:     my %text = (
 6556:                    'username' => 'new usernames',
 6557:                    'id'       => 'IDs',
 6558:                );
 6559:     unless ($type eq 'email') {
 6560:         my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 6561:         $output = '<tr '.$css_class.'>'.
 6562:                   '<td><span class="LC_nobreak">'.
 6563:                   &mt("Format rules to check for $text{$type}: ").
 6564:                   '</td><td class="LC_left_item" colspan="2"><table>';
 6565:     }
 6566:     my $rem;
 6567:     if (ref($ruleorder) eq 'ARRAY') {
 6568:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 6569:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 6570:                 my $rem = $i%($numinrow);
 6571:                 if ($rem == 0) {
 6572:                     if ($i > 0) {
 6573:                         $output .= '</tr>';
 6574:                     }
 6575:                     $output .= '<tr>';
 6576:                 }
 6577:                 my $check = ' ';
 6578:                 if (ref($settings) eq 'HASH') {
 6579:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 6580:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 6581:                             $check = ' checked="checked" ';
 6582:                         }
 6583:                     } elsif ((ref($settings->{$type.'_rule'}) eq 'HASH') && ($status ne '')) {
 6584:                         if (ref($settings->{$type.'_rule'}->{$status}) eq 'ARRAY') {
 6585:                             if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}->{$status}})) {
 6586:                                 $check = ' checked="checked" ';
 6587:                             }
 6588:                         }
 6589:                     }
 6590:                 }
 6591:                 my $name = $type.'_rule';
 6592:                 if ($type eq 'email') {
 6593:                     $name .= '_'.$status;
 6594:                 }
 6595:                 $output .= '<td class="LC_left_item">'.
 6596:                            '<span class="LC_nobreak"><label>'.
 6597:                            '<input type="checkbox" name="'.$name.'" '.
 6598:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 6599:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 6600:             }
 6601:         }
 6602:         $rem = @{$ruleorder}%($numinrow);
 6603:     }
 6604:     my $colsleft;
 6605:     if ($rem) {
 6606:         $colsleft = $numinrow - $rem;
 6607:     }
 6608:     if ($colsleft > 1 ) {
 6609:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6610:                    '&nbsp;</td>';
 6611:     } elsif ($colsleft == 1) {
 6612:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 6613:     }
 6614:     $output .= '</tr></table>';
 6615:     unless ($type eq 'email') {
 6616:         $output .= '</td></tr>';
 6617:     }
 6618:     return $output;
 6619: }
 6620: 
 6621: sub usercreation_types {
 6622:     my %lt = &Apache::lonlocal::texthash (
 6623:                     author     => 'When adding a co-author',
 6624:                     course     => 'When adding a user to a course',
 6625:                     requestcrs => 'When requesting a course',
 6626:                     any        => 'Any',
 6627:                     official   => 'Institutional only ',
 6628:                     unofficial => 'Non-institutional only',
 6629:                     none       => 'None',
 6630:     );
 6631:     return %lt;
 6632: }
 6633: 
 6634: sub selfcreation_types {
 6635:     my %lt = &Apache::lonlocal::texthash (
 6636:                     selfcreate => 'User creates own account',
 6637:                     any        => 'Any',
 6638:                     official   => 'Institutional only ',
 6639:                     unofficial => 'Non-institutional only',
 6640:                     email      => 'E-mail address',
 6641:                     login      => 'Institutional Login',
 6642:                     sso        => 'SSO',
 6643:              );
 6644: }
 6645: 
 6646: sub authtype_names {
 6647:     my %lt = &Apache::lonlocal::texthash(
 6648:                       int    => 'Internal',
 6649:                       krb4   => 'Kerberos 4',
 6650:                       krb5   => 'Kerberos 5',
 6651:                       loc    => 'Local',
 6652:                   );
 6653:     return %lt;
 6654: }
 6655: 
 6656: sub context_names {
 6657:     my %context_title = &Apache::lonlocal::texthash(
 6658:        author => 'Creating users when an Author',
 6659:        course => 'Creating users when in a course',
 6660:        domain => 'Creating users when a Domain Coordinator',
 6661:     );
 6662:     return %context_title;
 6663: }
 6664: 
 6665: sub print_usermodification {
 6666:     my ($position,$dom,$settings,$rowtotal) = @_;
 6667:     my $numinrow = 4;
 6668:     my ($context,$datatable,$rowcount);
 6669:     if ($position eq 'top') {
 6670:         $rowcount = 0;
 6671:         $context = 'author'; 
 6672:         foreach my $role ('ca','aa') {
 6673:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6674:                                                    $numinrow,$rowcount);
 6675:             $$rowtotal ++;
 6676:             $rowcount ++;
 6677:         }
 6678:     } elsif ($position eq 'bottom') {
 6679:         $context = 'course';
 6680:         $rowcount = 0;
 6681:         foreach my $role ('st','ep','ta','in','cr') {
 6682:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6683:                                                    $numinrow,$rowcount);
 6684:             $$rowtotal ++;
 6685:             $rowcount ++;
 6686:         }
 6687:     }
 6688:     return $datatable;
 6689: }
 6690: 
 6691: sub print_defaults {
 6692:     my ($position,$dom,$settings,$rowtotal) = @_;
 6693:     my $rownum = 0;
 6694:     my ($datatable,$css_class,$titles);
 6695:     unless ($position eq 'bottom') {
 6696:         $titles = &defaults_titles($dom);
 6697:     }
 6698:     if ($position eq 'top') {
 6699:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 6700:                      'datelocale_def','portal_def');
 6701:         my %defaults;
 6702:         if (ref($settings) eq 'HASH') {
 6703:             %defaults = %{$settings};
 6704:         } else {
 6705:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6706:             foreach my $item (@items) {
 6707:                 $defaults{$item} = $domdefaults{$item};
 6708:             }
 6709:         }
 6710:         foreach my $item (@items) {
 6711:             if ($rownum%2) {
 6712:                 $css_class = '';
 6713:             } else {
 6714:                 $css_class = ' class="LC_odd_row" ';
 6715:             }
 6716:             $datatable .= '<tr'.$css_class.'>'.
 6717:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 6718:                           '</span></td><td class="LC_right_item" colspan="3">';
 6719:             if ($item eq 'auth_def') {
 6720:                 my @authtypes = ('internal','krb4','krb5','localauth');
 6721:                 my %shortauth = (
 6722:                                  internal => 'int',
 6723:                                  krb4 => 'krb4',
 6724:                                  krb5 => 'krb5',
 6725:                                  localauth  => 'loc'
 6726:                                 );
 6727:                 my %authnames = &authtype_names();
 6728:                 foreach my $auth (@authtypes) {
 6729:                     my $checked = ' ';
 6730:                     if ($defaults{$item} eq $auth) {
 6731:                         $checked = ' checked="checked" ';
 6732:                     }
 6733:                     $datatable .= '<label><input type="radio" name="'.$item.
 6734:                                   '" value="'.$auth.'"'.$checked.'/>'.
 6735:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 6736:                 }
 6737:             } elsif ($item eq 'timezone_def') {
 6738:                 my $includeempty = 1;
 6739:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 6740:             } elsif ($item eq 'datelocale_def') {
 6741:                 my $includeempty = 1;
 6742:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 6743:             } elsif ($item eq 'lang_def') {
 6744:                 my $includeempty = 1;
 6745:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 6746:             } else {
 6747:                 my $size;
 6748:                 if ($item eq 'portal_def') {
 6749:                     $size = ' size="25"';
 6750:                 }
 6751:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6752:                               $defaults{$item}.'"'.$size.' />';
 6753:             }
 6754:             $datatable .= '</td></tr>';
 6755:             $rownum ++;
 6756:         }
 6757:     } elsif ($position eq 'middle') {
 6758:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 6759:         my %defaults;
 6760:         if (ref($settings) eq 'HASH') {
 6761:             %defaults = %{$settings};
 6762:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 6763:                 $defaults{'intauth_cost'} = 10;
 6764:             }
 6765:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 6766:                 $defaults{'intauth_check'} = 0;
 6767:             }
 6768:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 6769:                 $defaults{'intauth_switch'} = 0;
 6770:             }
 6771:         } else {
 6772:             %defaults = (
 6773:                           'intauth_cost'   => 10,
 6774:                           'intauth_check'  => 0,
 6775:                           'intauth_switch' => 0,
 6776:                         );
 6777:         }
 6778:         foreach my $item (@items) {
 6779:             if ($rownum%2) {
 6780:                 $css_class = '';
 6781:             } else {
 6782:                 $css_class = ' class="LC_odd_row" ';
 6783:             }
 6784:             $datatable .= '<tr'.$css_class.'>'.
 6785:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 6786:                           '</span></td><td class="LC_left_item" colspan="3">';
 6787:             if ($item eq 'intauth_switch') {
 6788:                 my @options = (0,1,2);
 6789:                 my %optiondesc = &Apache::lonlocal::texthash (
 6790:                                    0 => 'No',
 6791:                                    1 => 'Yes',
 6792:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 6793:                                  );
 6794:                 $datatable .= '<table width="100%">';
 6795:                 foreach my $option (@options) {
 6796:                     my $checked = ' ';
 6797:                     if ($defaults{$item} eq $option) {
 6798:                         $checked = ' checked="checked"';
 6799:                     }
 6800:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6801:                                   '<label><input type="radio" name="'.$item.
 6802:                                   '" value="'.$option.'"'.$checked.' />'.
 6803:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6804:                 }
 6805:                 $datatable .= '</table>';
 6806:             } elsif ($item eq 'intauth_check') {
 6807:                 my @options = (0,1,2);
 6808:                 my %optiondesc = &Apache::lonlocal::texthash (
 6809:                                    0 => 'No',
 6810:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 6811:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 6812:                                  );
 6813:                 $datatable .= '<table wisth="100%">';
 6814:                 foreach my $option (@options) {
 6815:                     my $checked = ' ';
 6816:                     my $onclick;
 6817:                     if ($defaults{$item} eq $option) {
 6818:                         $checked = ' checked="checked"';
 6819:                     }
 6820:                     if ($option == 2) {
 6821:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 6822:                     }
 6823:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6824:                                   '<label><input type="radio" name="'.$item.
 6825:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 6826:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6827:                 }
 6828:                 $datatable .= '</table>';
 6829:             } else {
 6830:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6831:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />'; 
 6832:             }
 6833:             $datatable .= '</td></tr>';
 6834:             $rownum ++;
 6835:         }
 6836:     } else {
 6837:         my %defaults;
 6838:         if (ref($settings) eq 'HASH') {
 6839:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 6840:                 my $maxnum = @{$settings->{'inststatusorder'}};
 6841:                 for (my $i=0; $i<$maxnum; $i++) {
 6842:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 6843:                     my $item = $settings->{'inststatusorder'}->[$i];
 6844:                     my $title = $settings->{'inststatustypes'}->{$item};
 6845:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 6846:                     $datatable .= '<tr'.$css_class.'>'.
 6847:                                   '<td><span class="LC_nobreak">'.
 6848:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 6849:                     for (my $k=0; $k<=$maxnum; $k++) {
 6850:                         my $vpos = $k+1;
 6851:                         my $selstr;
 6852:                         if ($k == $i) {
 6853:                             $selstr = ' selected="selected" ';
 6854:                         }
 6855:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6856:                     }
 6857:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 6858:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 6859:                                   &mt('delete').'</span></td>'.
 6860:                                   '<td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.&mt('Name displayed:').
 6861:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 6862:                                   '</span></td></tr>';
 6863:                 }
 6864:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 6865:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 6866:                 $datatable .= '<tr '.$css_class.'>'.
 6867:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 6868:                 for (my $k=0; $k<=$maxnum; $k++) {
 6869:                     my $vpos = $k+1;
 6870:                     my $selstr;
 6871:                     if ($k == $maxnum) {
 6872:                         $selstr = ' selected="selected" ';
 6873:                     }
 6874:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6875:                 }
 6876:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 6877:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 6878:                               '&nbsp;'.&mt('(new)').
 6879:                               '</span></td><td class="LC_left_item" colspan="2"><span class="LC_nobreak">'.
 6880:                               &mt('Name displayed:').
 6881:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 6882:                               '</tr>'."\n";
 6883:                 $rownum ++;
 6884:             }
 6885:         }
 6886:     }
 6887:     $$rowtotal += $rownum;
 6888:     return $datatable;
 6889: }
 6890: 
 6891: sub get_languages_hash {
 6892:     my %langchoices;
 6893:     foreach my $id (&Apache::loncommon::languageids()) {
 6894:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 6895:         if ($code ne '') {
 6896:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 6897:         }
 6898:     }
 6899:     return %langchoices;
 6900: }
 6901: 
 6902: sub defaults_titles {
 6903:     my ($dom) = @_;
 6904:     my %titles = &Apache::lonlocal::texthash (
 6905:                    'auth_def'      => 'Default authentication type',
 6906:                    'auth_arg_def'  => 'Default authentication argument',
 6907:                    'lang_def'      => 'Default language',
 6908:                    'timezone_def'  => 'Default timezone',
 6909:                    'datelocale_def' => 'Default locale for dates',
 6910:                    'portal_def'     => 'Portal/Default URL',
 6911:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 6912:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 6913:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 6914:                  );
 6915:     if ($dom) {
 6916:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 6917:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 6918:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 6919:         $protocol = 'http' if ($protocol ne 'https');
 6920:         if ($uint_dom) {
 6921:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 6922:                                          $uint_dom);
 6923:         }
 6924:     }
 6925:     return (\%titles);
 6926: }
 6927: 
 6928: sub print_scantronformat {
 6929:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 6930:     my $itemcount = 1;
 6931:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 6932:         %confhash);
 6933:     my $switchserver = &check_switchserver($dom,$confname);
 6934:     my %lt = &Apache::lonlocal::texthash (
 6935:                 default => 'Default bubblesheet format file error',
 6936:                 custom  => 'Custom bubblesheet format file error',
 6937:              );
 6938:     my %scantronfiles = (
 6939:         default => 'default.tab',
 6940:         custom => 'custom.tab',
 6941:     );
 6942:     foreach my $key (keys(%scantronfiles)) {
 6943:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 6944:                               .$scantronfiles{$key};
 6945:     }
 6946:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 6947:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 6948:         if (!$switchserver) {
 6949:             my $servadm = $r->dir_config('lonAdmEMail');
 6950:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 6951:             if ($configuserok eq 'ok') {
 6952:                 if ($author_ok eq 'ok') {
 6953:                     my %legacyfile = (
 6954:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 6955:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 6956:                     );
 6957:                     my %md5chk;
 6958:                     foreach my $type (keys(%legacyfile)) {
 6959:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 6960:                         chomp($md5chk{$type});
 6961:                     }
 6962:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 6963:                         foreach my $type (keys(%legacyfile)) {
 6964:                             ($scantronurls{$type},my $error) = 
 6965:                                 &legacy_scantronformat($r,$dom,$confname,
 6966:                                                  $type,$legacyfile{$type},
 6967:                                                  $scantronurls{$type},
 6968:                                                  $scantronfiles{$type});
 6969:                             if ($error ne '') {
 6970:                                 $error{$type} = $error;
 6971:                             }
 6972:                         }
 6973:                         if (keys(%error) == 0) {
 6974:                             $is_custom = 1;
 6975:                             $confhash{'scantron'}{'scantronformat'} = 
 6976:                                 $scantronurls{'custom'};
 6977:                             my $putresult = 
 6978:                                 &Apache::lonnet::put_dom('configuration',
 6979:                                                          \%confhash,$dom);
 6980:                             if ($putresult ne 'ok') {
 6981:                                 $error{'custom'} = 
 6982:                                     '<span class="LC_error">'.
 6983:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6984:                             }
 6985:                         }
 6986:                     } else {
 6987:                         ($scantronurls{'default'},my $error) =
 6988:                             &legacy_scantronformat($r,$dom,$confname,
 6989:                                           'default',$legacyfile{'default'},
 6990:                                           $scantronurls{'default'},
 6991:                                           $scantronfiles{'default'});
 6992:                         if ($error eq '') {
 6993:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 6994:                             my $putresult =
 6995:                                 &Apache::lonnet::put_dom('configuration',
 6996:                                                          \%confhash,$dom);
 6997:                             if ($putresult ne 'ok') {
 6998:                                 $error{'default'} =
 6999:                                     '<span class="LC_error">'.
 7000:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 7001:                             }
 7002:                         } else {
 7003:                             $error{'default'} = $error;
 7004:                         }
 7005:                     }
 7006:                 }
 7007:             }
 7008:         } else {
 7009:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 7010:         }
 7011:     }
 7012:     if (ref($settings) eq 'HASH') {
 7013:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 7014:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 7015:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 7016:                 $scantronurl = '';
 7017:             } else {
 7018:                 $scantronurl = $settings->{'scantronformat'};
 7019:             }
 7020:             $is_custom = 1;
 7021:         } else {
 7022:             $scantronurl = $scantronurls{'default'};
 7023:         }
 7024:     } else {
 7025:         if ($is_custom) {
 7026:             $scantronurl = $scantronurls{'custom'};
 7027:         } else {
 7028:             $scantronurl = $scantronurls{'default'};
 7029:         }
 7030:     }
 7031:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7032:     $datatable .= '<tr'.$css_class.'>';
 7033:     if (!$is_custom) {
 7034:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 7035:                       '<span class="LC_nobreak">';
 7036:         if ($scantronurl) {
 7037:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 7038:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 7039:         } else {
 7040:             $datatable = &mt('File unavailable for display');
 7041:         }
 7042:         $datatable .= '</span></td>';
 7043:         if (keys(%error) == 0) { 
 7044:             $datatable .= '<td style="vertical-align: bottom">';
 7045:             if (!$switchserver) {
 7046:                 $datatable .= &mt('Upload:').'<br />';
 7047:             }
 7048:         } else {
 7049:             my $errorstr;
 7050:             foreach my $key (sort(keys(%error))) {
 7051:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 7052:             }
 7053:             $datatable .= '<td>'.$errorstr;
 7054:         }
 7055:     } else {
 7056:         if (keys(%error) > 0) {
 7057:             my $errorstr;
 7058:             foreach my $key (sort(keys(%error))) {
 7059:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 7060:             } 
 7061:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 7062:         } elsif ($scantronurl) {
 7063:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 7064:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 7065:             $datatable .= '<td><span class="LC_nobreak">'.
 7066:                           $link.
 7067:                           '<label><input type="checkbox" name="scantronformat_del"'.
 7068:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 7069:                           '<td><span class="LC_nobreak">&nbsp;'.
 7070:                           &mt('Replace:').'</span><br />';
 7071:         }
 7072:     }
 7073:     if (keys(%error) == 0) {
 7074:         if ($switchserver) {
 7075:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 7076:         } else {
 7077:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 7078:                          '<input type="file" name="scantronformat" /></span>';
 7079:         }
 7080:     }
 7081:     $datatable .= '</td></tr>';
 7082:     $$rowtotal ++;
 7083:     return $datatable;
 7084: }
 7085: 
 7086: sub legacy_scantronformat {
 7087:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 7088:     my ($url,$error);
 7089:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 7090:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 7091:         (my $result,$url) =
 7092:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 7093:                          '','',$newfile);
 7094:         if ($result ne 'ok') {
 7095:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 7096:         }
 7097:     }
 7098:     return ($url,$error);
 7099: }
 7100: 
 7101: sub print_coursecategories {
 7102:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 7103:     my $datatable;
 7104:     if ($position eq 'top') {
 7105:         my (%checked);
 7106:         my @catitems = ('unauth','auth');
 7107:         my @cattypes = ('std','domonly','codesrch','none');
 7108:         $checked{'unauth'} = 'std';
 7109:         $checked{'auth'} = 'std';
 7110:         if (ref($settings) eq 'HASH') {
 7111:             foreach my $type (@cattypes) {
 7112:                 if ($type eq $settings->{'unauth'}) {
 7113:                     $checked{'unauth'} = $type;
 7114:                 }
 7115:                 if ($type eq $settings->{'auth'}) {
 7116:                     $checked{'auth'} = $type;
 7117:                 }
 7118:             }
 7119:         }
 7120:         my %lt = &Apache::lonlocal::texthash (
 7121:                                                unauth   => 'Catalog type for unauthenticated users',
 7122:                                                auth     => 'Catalog type for authenticated users',
 7123:                                                none     => 'No catalog',
 7124:                                                std      => 'Standard catalog',
 7125:                                                domonly  => 'Domain-only catalog',
 7126:                                                codesrch => "Code search form",
 7127:                                              );
 7128:        my $itemcount = 0;
 7129:        foreach my $item (@catitems) {
 7130:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 7131:            $datatable .= '<tr '.$css_class.'>'.
 7132:                          '<td>'.$lt{$item}.'</td>'.
 7133:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 7134:            foreach my $type (@cattypes) {
 7135:                my $ischecked;
 7136:                if ($checked{$item} eq $type) {
 7137:                    $ischecked=' checked="checked"';
 7138:                }
 7139:                $datatable .= '<label>'.
 7140:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 7141:                              ' />'.$lt{$type}.'</label>&nbsp;';
 7142:            }
 7143:            $datatable .= '</td></tr>';
 7144:            $itemcount ++;
 7145:         }
 7146:         $$rowtotal += $itemcount;
 7147:     } elsif ($position eq 'middle') {
 7148:         my $toggle_cats_crs = ' ';
 7149:         my $toggle_cats_dom = ' checked="checked" ';
 7150:         my $can_cat_crs = ' ';
 7151:         my $can_cat_dom = ' checked="checked" ';
 7152:         my $toggle_catscomm_comm = ' ';
 7153:         my $toggle_catscomm_dom = ' checked="checked" ';
 7154:         my $can_catcomm_comm = ' ';
 7155:         my $can_catcomm_dom = ' checked="checked" ';
 7156:         my $toggle_catsplace_place = ' ';
 7157:         my $toggle_catsplace_dom = ' checked="checked" ';
 7158:         my $can_catplace_place = ' ';
 7159:         my $can_catplace_dom = ' checked="checked" ';
 7160: 
 7161:         if (ref($settings) eq 'HASH') {
 7162:             if ($settings->{'togglecats'} eq 'crs') {
 7163:                 $toggle_cats_crs = $toggle_cats_dom;
 7164:                 $toggle_cats_dom = ' ';
 7165:             }
 7166:             if ($settings->{'categorize'} eq 'crs') {
 7167:                 $can_cat_crs = $can_cat_dom;
 7168:                 $can_cat_dom = ' ';
 7169:             }
 7170:             if ($settings->{'togglecatscomm'} eq 'comm') {
 7171:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 7172:                 $toggle_catscomm_dom = ' ';
 7173:             }
 7174:             if ($settings->{'categorizecomm'} eq 'comm') {
 7175:                 $can_catcomm_comm = $can_catcomm_dom;
 7176:                 $can_catcomm_dom = ' ';
 7177:             }
 7178:             if ($settings->{'togglecatsplace'} eq 'place') {
 7179:                 $toggle_catsplace_place = $toggle_catsplace_dom;
 7180:                 $toggle_catsplace_dom = ' ';
 7181:             }
 7182:             if ($settings->{'categorizeplace'} eq 'place') {
 7183:                 $can_catplace_place = $can_catplace_dom;
 7184:                 $can_catplace_dom = ' ';
 7185:             }
 7186:         }
 7187:         my %title = &Apache::lonlocal::texthash (
 7188:                      togglecats      => 'Show/Hide a course in catalog',
 7189:                      togglecatscomm  => 'Show/Hide a community in catalog',
 7190:                      togglecatsplace => 'Show/Hide a placement test in catalog',
 7191:                      categorize      => 'Assign a category to a course',
 7192:                      categorizecomm  => 'Assign a category to a community',
 7193:                      categorizeplace => 'Assign a category to a placement test',
 7194:                     );
 7195:         my %level = &Apache::lonlocal::texthash (
 7196:                      dom   => 'Set in Domain',
 7197:                      crs   => 'Set in Course',
 7198:                      comm  => 'Set in Community',
 7199:                      place => 'Set in Placement Test',
 7200:                     );
 7201:         $datatable = '<tr class="LC_odd_row">'.
 7202:                   '<td>'.$title{'togglecats'}.'</td>'.
 7203:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 7204:                   '<input type="radio" name="togglecats"'.
 7205:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7206:                   '<label><input type="radio" name="togglecats"'.
 7207:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 7208:                   '</tr><tr>'.
 7209:                   '<td>'.$title{'categorize'}.'</td>'.
 7210:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 7211:                   '<label><input type="radio" name="categorize"'.
 7212:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7213:                   '<label><input type="radio" name="categorize"'.
 7214:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 7215:                   '</tr><tr class="LC_odd_row">'.
 7216:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 7217:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 7218:                   '<input type="radio" name="togglecatscomm"'.
 7219:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7220:                   '<label><input type="radio" name="togglecatscomm"'.
 7221:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 7222:                   '</tr><tr>'.
 7223:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 7224:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 7225:                   '<label><input type="radio" name="categorizecomm"'.
 7226:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7227:                   '<label><input type="radio" name="categorizecomm"'.
 7228:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 7229:                   '</tr><tr>'.
 7230:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
 7231:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 7232:                   '<input type="radio" name="togglecatsplace"'.
 7233:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7234:                   '<label><input type="radio" name="togglecatscomm"'.
 7235:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
 7236:                   '</tr><tr>'.
 7237:                   '<td>'.$title{'categorizeplace'}.'</td>'.
 7238:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 7239:                   '<label><input type="radio" name="categorizeplace"'.
 7240:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 7241:                   '<label><input type="radio" name="categorizeplace"'.
 7242:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
 7243:                   '</tr>';
 7244:         $$rowtotal += 6;
 7245:     } else {
 7246:         my $css_class;
 7247:         my $itemcount = 1;
 7248:         my $cathash; 
 7249:         if (ref($settings) eq 'HASH') {
 7250:             $cathash = $settings->{'cats'};
 7251:         }
 7252:         if (ref($cathash) eq 'HASH') {
 7253:             my (@cats,@trails,%allitems,%idx,@jsarray);
 7254:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 7255:                                                    \%allitems,\%idx,\@jsarray);
 7256:             my $maxdepth = scalar(@cats);
 7257:             my $colattrib = '';
 7258:             if ($maxdepth > 2) {
 7259:                 $colattrib = ' colspan="2" ';
 7260:             }
 7261:             my @path;
 7262:             if (@cats > 0) {
 7263:                 if (ref($cats[0]) eq 'ARRAY') {
 7264:                     my $numtop = @{$cats[0]};
 7265:                     my $maxnum = $numtop;
 7266:                     my %default_names = (
 7267:                           instcode    => &mt('Official courses'),
 7268:                           communities => &mt('Communities'),
 7269:                           placement   => &mt('Placement Tests'),
 7270:                     );
 7271: 
 7272:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 7273:                         ($cathash->{'instcode::0'} eq '') ||
 7274:                         (!grep(/^communities$/,@{$cats[0]})) || 
 7275:                         ($cathash->{'communities::0'} eq '') ||
 7276:                         (!grep(/^placement$/,@{$cats[0]})) ||
 7277:                         ($cathash->{'placement::0'} eq '')) {
 7278:                         $maxnum ++;
 7279:                     }
 7280:                     my $lastidx;
 7281:                     for (my $i=0; $i<$numtop; $i++) {
 7282:                         my $parent = $cats[0][$i];
 7283:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7284:                         my $item = &escape($parent).'::0';
 7285:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 7286:                         $lastidx = $idx{$item};
 7287:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7288:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 7289:                         for (my $k=0; $k<=$maxnum; $k++) {
 7290:                             my $vpos = $k+1;
 7291:                             my $selstr;
 7292:                             if ($k == $i) {
 7293:                                 $selstr = ' selected="selected" ';
 7294:                             }
 7295:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7296:                         }
 7297:                         $datatable .= '</select></span></td><td>';
 7298:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
 7299:                             $datatable .=  '<span class="LC_nobreak">'
 7300:                                            .$default_names{$parent}.'</span>';
 7301:                             if ($parent eq 'instcode') {
 7302:                                 $datatable .= '<br /><span class="LC_nobreak">('
 7303:                                               .&mt('with institutional codes')
 7304:                                               .')</span></td><td'.$colattrib.'>';
 7305:                             } else {
 7306:                                 $datatable .= '<table><tr><td>';
 7307:                             }
 7308:                             $datatable .= '<span class="LC_nobreak">'
 7309:                                           .'<label><input type="radio" name="'
 7310:                                           .$parent.'" value="1" checked="checked" />'
 7311:                                           .&mt('Display').'</label>';
 7312:                             if ($parent eq 'instcode') {
 7313:                                 $datatable .= '&nbsp;';
 7314:                             } else {
 7315:                                 $datatable .= '</span></td></tr><tr><td>'
 7316:                                               .'<span class="LC_nobreak">';
 7317:                             }
 7318:                             $datatable .= '<label><input type="radio" name="'
 7319:                                           .$parent.'" value="0" />'
 7320:                                           .&mt('Do not display').'</label></span>';
 7321:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
 7322:                                 $datatable .= '</td></tr></table>';
 7323:                             }
 7324:                             $datatable .= '</td>';
 7325:                         } else {
 7326:                             $datatable .= $parent
 7327:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 7328:                                           .'<input type="checkbox" name="deletecategory" '
 7329:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 7330:                         }
 7331:                         my $depth = 1;
 7332:                         push(@path,$parent);
 7333:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 7334:                         pop(@path);
 7335:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 7336:                         $itemcount ++;
 7337:                     }
 7338:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7339:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 7340:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 7341:                     for (my $k=0; $k<=$maxnum; $k++) {
 7342:                         my $vpos = $k+1;
 7343:                         my $selstr;
 7344:                         if ($k == $numtop) {
 7345:                             $selstr = ' selected="selected" ';
 7346:                         }
 7347:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7348:                     }
 7349:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 7350:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 7351:                                   .'</tr>'."\n";
 7352:                     $itemcount ++;
 7353:                     foreach my $default ('instcode','communities','placement') {
 7354:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 7355:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7356:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 7357:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 7358:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 7359:                             for (my $k=0; $k<=$maxnum; $k++) {
 7360:                                 my $vpos = $k+1;
 7361:                                 my $selstr;
 7362:                                 if ($k == $maxnum) {
 7363:                                     $selstr = ' selected="selected" ';
 7364:                                 }
 7365:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 7366:                             }
 7367:                             $datatable .= '</select></span></td>'.
 7368:                                           '<td><span class="LC_nobreak">'.
 7369:                                           $default_names{$default}.'</span>';
 7370:                             if ($default eq 'instcode') {
 7371:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 7372:                                               .&mt('with institutional codes').')</span>';
 7373:                             }
 7374:                             $datatable .= '</td>'
 7375:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 7376:                                           .&mt('Display').'</label>&nbsp;'
 7377:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 7378:                                           .&mt('Do not display').'</label></span></td></tr>';
 7379:                         }
 7380:                     }
 7381:                 }
 7382:             } else {
 7383:                 $datatable .= &initialize_categories($itemcount);
 7384:             }
 7385:         } else {
 7386:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 7387:                           .&initialize_categories($itemcount);
 7388:         }
 7389:         $$rowtotal += $itemcount;
 7390:     }
 7391:     return $datatable;
 7392: }
 7393: 
 7394: sub print_serverstatuses {
 7395:     my ($dom,$settings,$rowtotal) = @_;
 7396:     my $datatable;
 7397:     my @pages = &serverstatus_pages();
 7398:     my (%namedaccess,%machineaccess);
 7399:     foreach my $type (@pages) {
 7400:         $namedaccess{$type} = '';
 7401:         $machineaccess{$type}= '';
 7402:     }
 7403:     if (ref($settings) eq 'HASH') {
 7404:         foreach my $type (@pages) {
 7405:             if (exists($settings->{$type})) {
 7406:                 if (ref($settings->{$type}) eq 'HASH') {
 7407:                     foreach my $key (keys(%{$settings->{$type}})) {
 7408:                         if ($key eq 'namedusers') {
 7409:                             $namedaccess{$type} = $settings->{$type}->{$key};
 7410:                         } elsif ($key eq 'machines') {
 7411:                             $machineaccess{$type} = $settings->{$type}->{$key};
 7412:                         }
 7413:                     }
 7414:                 }
 7415:             }
 7416:         }
 7417:     }
 7418:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 7419:     my $rownum = 0;
 7420:     my $css_class;
 7421:     foreach my $type (@pages) {
 7422:         $rownum ++;
 7423:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 7424:         $datatable .= '<tr'.$css_class.'>'.
 7425:                       '<td><span class="LC_nobreak">'.
 7426:                       $titles->{$type}.'</span></td>'.
 7427:                       '<td class="LC_left_item">'.
 7428:                       '<input type="text" name="'.$type.'_namedusers" '.
 7429:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 7430:                       '<td class="LC_right_item">'.
 7431:                       '<span class="LC_nobreak">'.
 7432:                       '<input type="text" name="'.$type.'_machines" '.
 7433:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 7434:                       '</td></tr>'."\n";
 7435:     }
 7436:     $$rowtotal += $rownum;
 7437:     return $datatable;
 7438: }
 7439: 
 7440: sub serverstatus_pages {
 7441:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 7442:             'checksums','clusterstatus','certstatus','metadata_keywords',
 7443:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
 7444:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
 7445: }
 7446: 
 7447: sub defaults_javascript {
 7448:     my ($settings) = @_;
 7449:     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.');
 7450:     my $intauthcost = &mt('Warning: bcrypt encryption cost for internal authentication must be an integer.');
 7451:     &js_escape(\$intauthcheck);
 7452:     &js_escape(\$intauthcost);
 7453:     my $intauthjs = <<"ENDSCRIPT";
 7454: 
 7455: function warnIntAuth(field) {
 7456:     if (field.name == 'intauth_check') {
 7457:         if (field.value == '2') {
 7458:             alert('$intauthcheck');
 7459:         }
 7460:     }
 7461:     if (field.name == 'intauth_cost') {
 7462:         field.value.replace(/\s/g,'');
 7463:         if (field.value != '') {
 7464:             var regexdigit=/^\\d+\$/;
 7465:             if (!regexdigit.test(field.value)) {
 7466:                 alert('$intauthcost');
 7467:             }
 7468:         }
 7469:     }
 7470:     return;
 7471: }
 7472: 
 7473: ENDSCRIPT
 7474: 
 7475:     if (ref($settings) ne 'HASH') {
 7476:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 7477:     }
 7478:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 7479:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 7480:         if ($maxnum eq '') {
 7481:             $maxnum = 0;
 7482:         }
 7483:         $maxnum ++;
 7484:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 7485:         return <<"ENDSCRIPT";
 7486: <script type="text/javascript">
 7487: // <![CDATA[
 7488: function reorderTypes(form,caller) {
 7489:     var changedVal;
 7490: $jstext 
 7491:     var newpos = 'addinststatus_pos';
 7492:     var current = new Array;
 7493:     var maxh = $maxnum;
 7494:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 7495:     var oldVal;
 7496:     if (caller == newpos) {
 7497:         changedVal = newitemVal;
 7498:     } else {
 7499:         var curritem = 'inststatus_pos_'+caller;
 7500:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 7501:         current[newitemVal] = newpos;
 7502:     }
 7503:     for (var i=0; i<inststatuses.length; i++) {
 7504:         if (inststatuses[i] != caller) {
 7505:             var elementName = 'inststatus_pos_'+inststatuses[i];
 7506:             if (form.elements[elementName]) {
 7507:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 7508:                 current[currVal] = elementName;
 7509:             }
 7510:         }
 7511:     }
 7512:     for (var j=0; j<maxh; j++) {
 7513:         if (current[j] == undefined) {
 7514:             oldVal = j;
 7515:         }
 7516:     }
 7517:     if (oldVal < changedVal) {
 7518:         for (var k=oldVal+1; k<=changedVal ; k++) {
 7519:            var elementName = current[k];
 7520:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 7521:         }
 7522:     } else {
 7523:         for (var k=changedVal; k<oldVal; k++) {
 7524:             var elementName = current[k];
 7525:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 7526:         }
 7527:     }
 7528:     return;
 7529: }
 7530: 
 7531: $intauthjs
 7532: 
 7533: // ]]>
 7534: </script>
 7535: 
 7536: ENDSCRIPT
 7537:     } else {
 7538:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 7539:     }
 7540: }
 7541: 
 7542: sub coursecategories_javascript {
 7543:     my ($settings) = @_;
 7544:     my ($output,$jstext,$cathash);
 7545:     if (ref($settings) eq 'HASH') {
 7546:         $cathash = $settings->{'cats'};
 7547:     }
 7548:     if (ref($cathash) eq 'HASH') {
 7549:         my (@cats,@jsarray,%idx);
 7550:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 7551:         if (@jsarray > 0) {
 7552:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 7553:             for (my $i=0; $i<@jsarray; $i++) {
 7554:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 7555:                     my $catstr = join('","',@{$jsarray[$i]});
 7556:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 7557:                 }
 7558:             }
 7559:         }
 7560:     } else {
 7561:         $jstext  = '    var categories = Array(1);'."\n".
 7562:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 7563:     }
 7564:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 7565:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 7566:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
 7567:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 7568:     &js_escape(\$instcode_reserved);
 7569:     &js_escape(\$communities_reserved);
 7570:     &js_escape(\$placement_reserved);
 7571:     &js_escape(\$choose_again);
 7572:     $output = <<"ENDSCRIPT";
 7573: <script type="text/javascript">
 7574: // <![CDATA[
 7575: function reorderCats(form,parent,item,idx) {
 7576:     var changedVal;
 7577: $jstext
 7578:     var newpos = 'addcategory_pos';
 7579:     if (parent == '') {
 7580:         var has_instcode = 0;
 7581:         var maxtop = categories[idx].length;
 7582:         for (var j=0; j<maxtop; j++) {
 7583:             if (categories[idx][j] == 'instcode::0') {
 7584:                 has_instcode == 1;
 7585:             }
 7586:         }
 7587:         if (has_instcode == 0) {
 7588:             categories[idx][maxtop] = 'instcode_pos';
 7589:         }
 7590:     } else {
 7591:         newpos += '_'+parent;
 7592:     }
 7593:     var maxh = 1 + categories[idx].length;
 7594:     var current = new Array;
 7595:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 7596:     if (item == newpos) {
 7597:         changedVal = newitemVal;
 7598:     } else {
 7599:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 7600:         current[newitemVal] = newpos;
 7601:     }
 7602:     for (var i=0; i<categories[idx].length; i++) {
 7603:         var elementName = categories[idx][i];
 7604:         if (elementName != item) {
 7605:             if (form.elements[elementName]) {
 7606:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 7607:                 current[currVal] = elementName;
 7608:             }
 7609:         }
 7610:     }
 7611:     var oldVal;
 7612:     for (var j=0; j<maxh; j++) {
 7613:         if (current[j] == undefined) {
 7614:             oldVal = j;
 7615:         }
 7616:     }
 7617:     if (oldVal < changedVal) {
 7618:         for (var k=oldVal+1; k<=changedVal ; k++) {
 7619:            var elementName = current[k];
 7620:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 7621:         }
 7622:     } else {
 7623:         for (var k=changedVal; k<oldVal; k++) {
 7624:             var elementName = current[k];
 7625:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 7626:         }
 7627:     }
 7628:     return;
 7629: }
 7630: 
 7631: function categoryCheck(form) {
 7632:     if (form.elements['addcategory_name'].value == 'instcode') {
 7633:         alert('$instcode_reserved\\n$choose_again');
 7634:         return false;
 7635:     }
 7636:     if (form.elements['addcategory_name'].value == 'communities') {
 7637:         alert('$communities_reserved\\n$choose_again');
 7638:         return false;
 7639:     }
 7640:     if (form.elements['addcategory_name'].value == 'placement') {
 7641:         alert('$placement_reserved\\n$choose_again');
 7642:         return false;
 7643:     }
 7644:     return true;
 7645: }
 7646: 
 7647: // ]]>
 7648: </script>
 7649: 
 7650: ENDSCRIPT
 7651:     return $output;
 7652: }
 7653: 
 7654: sub initialize_categories {
 7655:     my ($itemcount) = @_;
 7656:     my ($datatable,$css_class,$chgstr);
 7657:     my %default_names = (
 7658:                       instcode    => 'Official courses (with institutional codes)',
 7659:                       communities => 'Communities',
 7660:                       placement   => 'Placement Tests',
 7661:                         );
 7662:     my $select0 = ' selected="selected"';
 7663:     my $select1 = '';
 7664:     foreach my $default ('instcode','communities','placement') {
 7665:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7666:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 7667:         if (($default eq 'communities') || ($default eq 'placement')) {
 7668:             $select1 = $select0;
 7669:             $select0 = '';
 7670:         }
 7671:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7672:                      .'<select name="'.$default.'_pos">'
 7673:                      .'<option value="0"'.$select0.'>1</option>'
 7674:                      .'<option value="1"'.$select1.'>2</option>'
 7675:                      .'<option value="2">3</option></select>&nbsp;'
 7676:                      .$default_names{$default}
 7677:                      .'</span></td><td><span class="LC_nobreak">'
 7678:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 7679:                      .&mt('Display').'</label>&nbsp;<label>'
 7680:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 7681:                  .'</label></span></td></tr>';
 7682:         $itemcount ++;
 7683:     }
 7684:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7685:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 7686:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7687:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 7688:                   .'<option value="0">1</option>'
 7689:                   .'<option value="1">2</option>'
 7690:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 7691:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 7692:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 7693:     return $datatable;
 7694: }
 7695: 
 7696: sub build_category_rows {
 7697:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 7698:     my ($text,$name,$item,$chgstr);
 7699:     if (ref($cats) eq 'ARRAY') {
 7700:         my $maxdepth = scalar(@{$cats});
 7701:         if (ref($cats->[$depth]) eq 'HASH') {
 7702:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 7703:                 my $numchildren = @{$cats->[$depth]{$parent}};
 7704:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7705:                 $text .= '<td><table class="LC_data_table">';
 7706:                 my ($idxnum,$parent_name,$parent_item);
 7707:                 my $higher = $depth - 1;
 7708:                 if ($higher == 0) {
 7709:                     $parent_name = &escape($parent).'::'.$higher;
 7710:                 } else {
 7711:                     if (ref($path) eq 'ARRAY') {
 7712:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7713:                     }
 7714:                 }
 7715:                 $parent_item = 'addcategory_pos_'.$parent_name;
 7716:                 for (my $j=0; $j<=$numchildren; $j++) {
 7717:                     if ($j < $numchildren) {
 7718:                         $name = $cats->[$depth]{$parent}[$j];
 7719:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 7720:                         $idxnum = $idx->{$item};
 7721:                     } else {
 7722:                         $name = $parent_name;
 7723:                         $item = $parent_item;
 7724:                     }
 7725:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 7726:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 7727:                     for (my $i=0; $i<=$numchildren; $i++) {
 7728:                         my $vpos = $i+1;
 7729:                         my $selstr;
 7730:                         if ($j == $i) {
 7731:                             $selstr = ' selected="selected" ';
 7732:                         }
 7733:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 7734:                     }
 7735:                     $text .= '</select>&nbsp;';
 7736:                     if ($j < $numchildren) {
 7737:                         my $deeper = $depth+1;
 7738:                         $text .= $name.'&nbsp;'
 7739:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 7740:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 7741:                         if(ref($path) eq 'ARRAY') {
 7742:                             push(@{$path},$name);
 7743:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 7744:                             pop(@{$path});
 7745:                         }
 7746:                     } else {
 7747:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 7748:                         if ($j == $numchildren) {
 7749:                             $text .= $name;
 7750:                         } else {
 7751:                             $text .= $item;
 7752:                         }
 7753:                         $text .= '" value="" />';
 7754:                     }
 7755:                     $text .= '</td></tr>';
 7756:                 }
 7757:                 $text .= '</table></td>';
 7758:             } else {
 7759:                 my $higher = $depth-1;
 7760:                 if ($higher == 0) {
 7761:                     $name = &escape($parent).'::'.$higher;
 7762:                 } else {
 7763:                     if (ref($path) eq 'ARRAY') {
 7764:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7765:                     }
 7766:                 }
 7767:                 my $colspan;
 7768:                 if ($parent ne 'instcode') {
 7769:                     $colspan = $maxdepth - $depth - 1;
 7770:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 7771:                 }
 7772:             }
 7773:         }
 7774:     }
 7775:     return $text;
 7776: }
 7777: 
 7778: sub modifiable_userdata_row {
 7779:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref,
 7780:         $rowid,$customcss,$rowstyle) = @_;
 7781:     my ($role,$rolename,$statustype);
 7782:     $role = $item;
 7783:     if ($context eq 'cancreate') {
 7784:         if ($item =~ /^(emailusername)_(.+)$/) {
 7785:             $role = $1;
 7786:             $statustype = $2;
 7787:             if (ref($usertypes) eq 'HASH') {
 7788:                 if ($usertypes->{$statustype}) {
 7789:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 7790:                 } else {
 7791:                     $rolename = &mt('Data provided by user');
 7792:                 }
 7793:             }
 7794:         }
 7795:     } elsif ($context eq 'selfcreate') {
 7796:         if (ref($usertypes) eq 'HASH') {
 7797:             $rolename = $usertypes->{$role};
 7798:         } else {
 7799:             $rolename = $role;
 7800:         }
 7801:     } else {
 7802:         if ($role eq 'cr') {
 7803:             $rolename = &mt('Custom role');
 7804:         } else {
 7805:             $rolename = &Apache::lonnet::plaintext($role);
 7806:         }
 7807:     }
 7808:     my (@fields,%fieldtitles);
 7809:     if (ref($fieldsref) eq 'ARRAY') {
 7810:         @fields = @{$fieldsref};
 7811:     } else {
 7812:         @fields = ('lastname','firstname','middlename','generation',
 7813:                    'permanentemail','id');
 7814:     }
 7815:     if ((ref($titlesref) eq 'HASH')) {
 7816:         %fieldtitles = %{$titlesref};
 7817:     } else {
 7818:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7819:     }
 7820:     my $output;
 7821:     my $css_class;
 7822:     if ($rowcount%2) {
 7823:         $css_class = 'LC_odd_row';
 7824:     }
 7825:     if ($customcss) {
 7826:         $css_class .= " $customcss";
 7827:     }
 7828:     $css_class =~ s/^\s+//;
 7829:     if ($css_class) {
 7830:         $css_class = ' class="'.$css_class.'"';
 7831:     }
 7832:     if ($rowstyle) {
 7833:         $css_class .= ' style="'.$rowstyle.'"';
 7834:     }
 7835:     if ($rowid) {
 7836:         $rowid = ' id="'.$rowid.'"';
 7837:     }
 7838:     $output = '<tr '.$css_class.$rowid.'>'.
 7839:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 7840:               '<td class="LC_left_item" colspan="2"><table>';
 7841:     my $rem;
 7842:     my %checks;
 7843:     if (ref($settings) eq 'HASH') {
 7844:         if (ref($settings->{$context}) eq 'HASH') {
 7845:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 7846:                 my $hashref = $settings->{$context}->{$role};
 7847:                 if ($role eq 'emailusername') {
 7848:                     if ($statustype) {
 7849:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 7850:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 7851:                             if (ref($hashref) eq 'HASH') { 
 7852:                                 foreach my $field (@fields) {
 7853:                                     if ($hashref->{$field}) {
 7854:                                         $checks{$field} = $hashref->{$field};
 7855:                                     }
 7856:                                 }
 7857:                             }
 7858:                         }
 7859:                     }
 7860:                 } else {
 7861:                     if (ref($hashref) eq 'HASH') {
 7862:                         foreach my $field (@fields) {
 7863:                             if ($hashref->{$field}) {
 7864:                                 $checks{$field} = ' checked="checked" ';
 7865:                             }
 7866:                         }
 7867:                     }
 7868:                 }
 7869:             }
 7870:         }
 7871:     }
 7872:  
 7873:     my $total = scalar(@fields);
 7874:     for (my $i=0; $i<$total; $i++) {
 7875:         $rem = $i%($numinrow);
 7876:         if ($rem == 0) {
 7877:             if ($i > 0) {
 7878:                 $output .= '</tr>';
 7879:             }
 7880:             $output .= '<tr>';
 7881:         }
 7882:         my $check = ' ';
 7883:         unless ($role eq 'emailusername') {
 7884:             if (exists($checks{$fields[$i]})) {
 7885:                 $check = $checks{$fields[$i]}
 7886:             } else {
 7887:                 if ($role eq 'st') {
 7888:                     if (ref($settings) ne 'HASH') {
 7889:                         $check = ' checked="checked" '; 
 7890:                     }
 7891:                 }
 7892:             }
 7893:         }
 7894:         $output .= '<td class="LC_left_item">'.
 7895:                    '<span class="LC_nobreak">';
 7896:         if ($role eq 'emailusername') {
 7897:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 7898:                 $checks{$fields[$i]} = 'omit';
 7899:             }
 7900:             foreach my $option ('required','optional','omit') {
 7901:                 my $checked='';
 7902:                 if ($checks{$fields[$i]} eq $option) {
 7903:                     $checked='checked="checked" ';
 7904:                 }
 7905:                 $output .= '<label>'.
 7906:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 7907:                            &mt($option).'</label>'.('&nbsp;' x2);
 7908:             }
 7909:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 7910:         } else {
 7911:             $output .= '<label>'.
 7912:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 7913:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 7914:                        '</label>';
 7915:         }
 7916:         $output .= '</span></td>';
 7917:     }
 7918:     $rem = $total%$numinrow;
 7919:     my $colsleft;
 7920:     if ($rem) {
 7921:         $colsleft = $numinrow - $rem;
 7922:     }
 7923:     if ($colsleft > 1) {
 7924:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7925:                    '&nbsp;</td>';
 7926:     } elsif ($colsleft == 1) {
 7927:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 7928:     }
 7929:     $output .= '</tr></table></td></tr>';
 7930:     return $output;
 7931: }
 7932: 
 7933: sub insttypes_row {
 7934:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rowtotal,$onclick,
 7935:         $customcss,$rowstyle) = @_;
 7936:     my %lt = &Apache::lonlocal::texthash (
 7937:                       cansearch => 'Users allowed to search',
 7938:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 7939:                       lockablenames => 'User preference to lock name',
 7940:                       selfassign    => 'Self-reportable affiliations',
 7941:                       overrides     => "Override domain's helpdesk settings based on requester's affiliation",
 7942:              );
 7943:     my $showdom;
 7944:     if ($context eq 'cansearch') {
 7945:         $showdom = ' ('.$dom.')';
 7946:     }
 7947:     my $class = 'LC_left_item';
 7948:     if ($context eq 'statustocreate') {
 7949:         $class = 'LC_right_item';
 7950:     }
 7951:     my $css_class;
 7952:     if ($$rowtotal%2) {
 7953:         $css_class = 'LC_odd_row';
 7954:     }
 7955:     if ($customcss) {
 7956:         $css_class .= ' '.$customcss;
 7957:     }
 7958:     $css_class =~ s/^\s+//;
 7959:     if ($css_class) {
 7960:         $css_class = ' class="'.$css_class.'"';
 7961:     }
 7962:     if ($rowstyle) {
 7963:         $css_class .= ' style="'.$rowstyle.'"';
 7964:     }
 7965:     if ($onclick) {
 7966:         $onclick = 'onclick="'.$onclick.'" ';
 7967:     }
 7968:     my $output = '<tr'.$css_class.'>'.
 7969:                  '<td>'.$lt{$context}.$showdom.
 7970:                  '</td><td class="'.$class.'" colspan="2"><table>';
 7971:     my $rem;
 7972:     if (ref($types) eq 'ARRAY') {
 7973:         for (my $i=0; $i<@{$types}; $i++) {
 7974:             if (defined($usertypes->{$types->[$i]})) {
 7975:                 my $rem = $i%($numinrow);
 7976:                 if ($rem == 0) {
 7977:                     if ($i > 0) {
 7978:                         $output .= '</tr>';
 7979:                     }
 7980:                     $output .= '<tr>';
 7981:                 }
 7982:                 my $check = ' ';
 7983:                 if (ref($settings) eq 'HASH') {
 7984:                     if (ref($settings->{$context}) eq 'ARRAY') {
 7985:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 7986:                             $check = ' checked="checked" ';
 7987:                         }
 7988:                     } elsif (ref($settings->{$context}) eq 'HASH') {
 7989:                         if (ref($settings->{$context}->{$types->[$i]}) eq 'HASH') {
 7990:                             $check = ' checked="checked" ';
 7991:                         }
 7992:                     } elsif ($context eq 'statustocreate') {
 7993:                         $check = ' checked="checked" ';
 7994:                     }
 7995:                 }
 7996:                 $output .= '<td class="LC_left_item">'.
 7997:                            '<span class="LC_nobreak"><label>'.
 7998:                            '<input type="checkbox" name="'.$context.'" '.
 7999:                            'value="'.$types->[$i].'"'.$check.$onclick.' />'.
 8000:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 8001:             }
 8002:         }
 8003:         $rem = @{$types}%($numinrow);
 8004:     }
 8005:     my $colsleft = $numinrow - $rem;
 8006:     if ($context eq 'overrides') {
 8007:         if ($colsleft > 1) {
 8008:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 8009:         } else {
 8010:             $output .= '<td class="LC_left_item">';
 8011:         }
 8012:         $output .= '&nbsp;';  
 8013:     } else {
 8014:         if (($rem == 0) && (@{$types} > 0)) {
 8015:             $output .= '<tr>';
 8016:         }
 8017:         if ($colsleft > 1) {
 8018:             $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 8019:         } else {
 8020:             $output .= '<td class="LC_left_item">';
 8021:         }
 8022:         my $defcheck = ' ';
 8023:         if (ref($settings) eq 'HASH') {  
 8024:             if (ref($settings->{$context}) eq 'ARRAY') {
 8025:                 if (grep(/^default$/,@{$settings->{$context}})) {
 8026:                     $defcheck = ' checked="checked" ';
 8027:                 }
 8028:             } elsif ($context eq 'statustocreate') {
 8029:                 $defcheck = ' checked="checked" ';
 8030:             }
 8031:         }
 8032:         $output .= '<span class="LC_nobreak"><label>'.
 8033:                    '<input type="checkbox" name="'.$context.'" '.
 8034:                    'value="default"'.$defcheck.$onclick.' />'.
 8035:                    $othertitle.'</label></span>';
 8036:     }
 8037:     $output .= '</td></tr></table></td></tr>';
 8038:     return $output;
 8039: }
 8040: 
 8041: sub sorted_searchtitles {
 8042:     my %searchtitles = &Apache::lonlocal::texthash(
 8043:                          'uname' => 'username',
 8044:                          'lastname' => 'last name',
 8045:                          'lastfirst' => 'last name, first name',
 8046:                      );
 8047:     my @titleorder = ('uname','lastname','lastfirst');
 8048:     return (\%searchtitles,\@titleorder);
 8049: }
 8050: 
 8051: sub sorted_searchtypes {
 8052:     my %srchtypes_desc = (
 8053:                            exact    => 'is exact match',
 8054:                            contains => 'contains ..',
 8055:                            begins   => 'begins with ..',
 8056:                          );
 8057:     my @srchtypeorder = ('exact','begins','contains');
 8058:     return (\%srchtypes_desc,\@srchtypeorder);
 8059: }
 8060: 
 8061: sub usertype_update_row {
 8062:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 8063:     my $datatable;
 8064:     my $numinrow = 4;
 8065:     foreach my $type (@{$types}) {
 8066:         if (defined($usertypes->{$type})) {
 8067:             $$rownums ++;
 8068:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 8069:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 8070:                           '</td><td class="LC_left_item"><table>';
 8071:             for (my $i=0; $i<@{$fields}; $i++) {
 8072:                 my $rem = $i%($numinrow);
 8073:                 if ($rem == 0) {
 8074:                     if ($i > 0) {
 8075:                         $datatable .= '</tr>';
 8076:                     }
 8077:                     $datatable .= '<tr>';
 8078:                 }
 8079:                 my $check = ' ';
 8080:                 if (ref($settings) eq 'HASH') {
 8081:                     if (ref($settings->{'fields'}) eq 'HASH') {
 8082:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 8083:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 8084:                                 $check = ' checked="checked" ';
 8085:                             }
 8086:                         }
 8087:                     }
 8088:                 }
 8089: 
 8090:                 if ($i == @{$fields}-1) {
 8091:                     my $colsleft = $numinrow - $rem;
 8092:                     if ($colsleft > 1) {
 8093:                         $datatable .= '<td colspan="'.$colsleft.'">';
 8094:                     } else {
 8095:                         $datatable .= '<td>';
 8096:                     }
 8097:                 } else {
 8098:                     $datatable .= '<td>';
 8099:                 }
 8100:                 $datatable .= '<span class="LC_nobreak"><label>'.
 8101:                               '<input type="checkbox" name="updateable_'.$type.
 8102:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 8103:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 8104:             }
 8105:             $datatable .= '</tr></table></td></tr>';
 8106:         }
 8107:     }
 8108:     return $datatable;
 8109: }
 8110: 
 8111: sub modify_login {
 8112:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 8113:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 8114:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 8115:     %title = ( coursecatalog => 'Display course catalog',
 8116:                adminmail => 'Display administrator E-mail address',
 8117:                helpdesk  => 'Display "Contact Helpdesk" link',
 8118:                newuser => 'Link for visitors to create a user account',
 8119:                loginheader => 'Log-in box header');
 8120:     @offon = ('off','on');
 8121:     if (ref($domconfig{login}) eq 'HASH') {
 8122:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 8123:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 8124:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 8125:             }
 8126:         }
 8127:     }
 8128:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 8129:                                            \%domconfig,\%loginhash);
 8130:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 8131:     foreach my $item (@toggles) {
 8132:         $loginhash{login}{$item} = $env{'form.'.$item};
 8133:     }
 8134:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 8135:     if (ref($colchanges{'login'}) eq 'HASH') {  
 8136:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 8137:                                          \%loginhash);
 8138:     }
 8139: 
 8140:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 8141:     my %domservers = &Apache::lonnet::get_servers($dom);
 8142:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 8143:     if (keys(%servers) > 1) {
 8144:         foreach my $lonhost (keys(%servers)) {
 8145:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 8146:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 8147:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 8148:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 8149:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 8150:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 8151:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 8152:                         $changes{'loginvia'}{$lonhost} = 1;
 8153:                     } else {
 8154:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 8155:                         $changes{'loginvia'}{$lonhost} = 1;
 8156:                     }
 8157:                 } else {
 8158:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 8159:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 8160:                         $changes{'loginvia'}{$lonhost} = 1;
 8161:                     }
 8162:                 }
 8163:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 8164:                     foreach my $item (@loginvia_attribs) {
 8165:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 8166:                     }
 8167:                 } else {
 8168:                     foreach my $item (@loginvia_attribs) {
 8169:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 8170:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 8171:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 8172:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 8173:                                 $new = '/';
 8174:                             }
 8175:                         }
 8176:                         if (($item eq 'custompath') && 
 8177:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 8178:                             $new = '';
 8179:                         }
 8180:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 8181:                             $changes{'loginvia'}{$lonhost} = 1;
 8182:                         }
 8183:                         if ($item eq 'exempt') {
 8184:                             $new = &check_exempt_addresses($new);
 8185:                         }
 8186:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 8187:                     }
 8188:                 }
 8189:             } else {
 8190:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 8191:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 8192:                     $changes{'loginvia'}{$lonhost} = 1;
 8193:                     foreach my $item (@loginvia_attribs) {
 8194:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 8195:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 8196:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 8197:                                 $new = '/';
 8198:                             }
 8199:                         }
 8200:                         if (($item eq 'custompath') && 
 8201:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 8202:                             $new = '';
 8203:                         }
 8204:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 8205:                     }
 8206:                 }
 8207:             }
 8208:         }
 8209:     }
 8210: 
 8211:     my $servadm = $r->dir_config('lonAdmEMail');
 8212:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 8213:     if (ref($domconfig{'login'}) eq 'HASH') {
 8214:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 8215:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 8216:                 if ($lang eq 'nolang') {
 8217:                     push(@currlangs,$lang);
 8218:                 } elsif (defined($langchoices{$lang})) {
 8219:                     push(@currlangs,$lang);
 8220:                 } else {
 8221:                     next;
 8222:                 }
 8223:             }
 8224:         }
 8225:     }
 8226:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 8227:     if (@currlangs > 0) {
 8228:         foreach my $lang (@currlangs) {
 8229:             if (grep(/^\Q$lang\E$/,@delurls)) {
 8230:                 $changes{'helpurl'}{$lang} = 1;
 8231:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 8232:                 $changes{'helpurl'}{$lang} = 1;
 8233:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 8234:                 push(@newlangs,$lang);
 8235:             } else {
 8236:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 8237:             }
 8238:         }
 8239:     }
 8240:     unless (grep(/^nolang$/,@currlangs)) {
 8241:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 8242:             $changes{'helpurl'}{'nolang'} = 1;
 8243:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 8244:             push(@newlangs,'nolang');
 8245:         }
 8246:     }
 8247:     if ($env{'form.loginhelpurl_add_lang'}) {
 8248:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 8249:             ($env{'form.loginhelpurl_add_file.filename'})) {
 8250:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 8251:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 8252:         }
 8253:     }
 8254:     if ((@newlangs > 0) || ($addedfile)) {
 8255:         my $error;
 8256:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8257:         if ($configuserok eq 'ok') {
 8258:             if ($switchserver) {
 8259:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 8260:             } elsif ($author_ok eq 'ok') {
 8261:                 my @allnew = @newlangs;
 8262:                 if ($addedfile ne '') {
 8263:                     push(@allnew,$addedfile);
 8264:                 }
 8265:                 foreach my $lang (@allnew) {
 8266:                     my $formelem = 'loginhelpurl_'.$lang;
 8267:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 8268:                         $formelem = 'loginhelpurl_add_file';
 8269:                     }
 8270:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 8271:                                                                "help/$lang",'','',$newfile{$lang});
 8272:                     if ($result eq 'ok') {
 8273:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 8274:                         $changes{'helpurl'}{$lang} = 1;
 8275:                     } else {
 8276:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 8277:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 8278:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 8279:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 8280:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 8281:                         }
 8282:                     }
 8283:                 }
 8284:             } else {
 8285:                 $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);
 8286:             }
 8287:         } else {
 8288:             $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);
 8289:         }
 8290:         if ($error) {
 8291:             &Apache::lonnet::logthis($error);
 8292:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8293:         }
 8294:     }
 8295: 
 8296:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 8297:     if (ref($domconfig{'login'}) eq 'HASH') {
 8298:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 8299:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 8300:                 if ($domservers{$lonhost}) {
 8301:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 8302:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 8303:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 8304:                     }
 8305:                 }
 8306:             }
 8307:         }
 8308:     }
 8309:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 8310:     foreach my $lonhost (sort(keys(%domservers))) {
 8311:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 8312:             $changes{'headtag'}{$lonhost} = 1;
 8313:         } else {
 8314:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 8315:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 8316:             }
 8317:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 8318:                 push(@newhosts,$lonhost);
 8319:             } elsif ($currheadtagurls{$lonhost}) {
 8320:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 8321:                 if ($currexempt{$lonhost}) {
 8322:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 8323:                         $changes{'headtag'}{$lonhost} = 1;
 8324:                     }
 8325:                 } elsif ($possexempt{$lonhost}) {
 8326:                     $changes{'headtag'}{$lonhost} = 1;
 8327:                 }
 8328:                 if ($possexempt{$lonhost}) {
 8329:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 8330:                 }
 8331:             }
 8332:         }
 8333:     }
 8334:     if (@newhosts) {
 8335:         my $error;
 8336:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8337:         if ($configuserok eq 'ok') {
 8338:             if ($switchserver) {
 8339:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 8340:             } elsif ($author_ok eq 'ok') {
 8341:                 foreach my $lonhost (@newhosts) {
 8342:                     my $formelem = 'loginheadtag_'.$lonhost;
 8343:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 8344:                                                                           "login/headtag/$lonhost",'','',
 8345:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 8346:                     if ($result eq 'ok') {
 8347:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 8348:                         $changes{'headtag'}{$lonhost} = 1;
 8349:                         if ($possexempt{$lonhost}) {
 8350:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 8351:                         }
 8352:                     } else {
 8353:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 8354:                                            $newheadtagurls{$lonhost},$result);
 8355:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 8356:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 8357:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 8358:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 8359:                         }
 8360:                     }
 8361:                 }
 8362:             } else {
 8363:                 $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);
 8364:             }
 8365:         } else {
 8366:             $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);
 8367:         }
 8368:         if ($error) {
 8369:             &Apache::lonnet::logthis($error);
 8370:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8371:         }
 8372:     }
 8373:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 8374: 
 8375:     my $defaulthelpfile = '/adm/loginproblems.html';
 8376:     my $defaulttext = &mt('Default in use');
 8377: 
 8378:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 8379:                                              $dom);
 8380:     if ($putresult eq 'ok') {
 8381:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 8382:         my %defaultchecked = (
 8383:                     'coursecatalog' => 'on',
 8384:                     'helpdesk'      => 'on',
 8385:                     'adminmail'     => 'off',
 8386:                     'newuser'       => 'off',
 8387:         );
 8388:         if (ref($domconfig{'login'}) eq 'HASH') {
 8389:             foreach my $item (@toggles) {
 8390:                 if ($defaultchecked{$item} eq 'on') { 
 8391:                     if (($domconfig{'login'}{$item} eq '0') &&
 8392:                         ($env{'form.'.$item} eq '1')) {
 8393:                         $changes{$item} = 1;
 8394:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 8395:                               $domconfig{'login'}{$item} eq '1') &&
 8396:                              ($env{'form.'.$item} eq '0')) {
 8397:                         $changes{$item} = 1;
 8398:                     }
 8399:                 } elsif ($defaultchecked{$item} eq 'off') {
 8400:                     if (($domconfig{'login'}{$item} eq '1') &&
 8401:                         ($env{'form.'.$item} eq '0')) {
 8402:                         $changes{$item} = 1;
 8403:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 8404:                               $domconfig{'login'}{$item} eq '0') &&
 8405:                              ($env{'form.'.$item} eq '1')) {
 8406:                         $changes{$item} = 1;
 8407:                     }
 8408:                 }
 8409:             }
 8410:         }
 8411:         if (keys(%changes) > 0 || $colchgtext) {
 8412:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8413:             if (ref($lastactref) eq 'HASH') {
 8414:                 $lastactref->{'domainconfig'} = 1;
 8415:             }
 8416:             $resulttext = &mt('Changes made:').'<ul>';
 8417:             foreach my $item (sort(keys(%changes))) {
 8418:                 if ($item eq 'loginvia') {
 8419:                     if (ref($changes{$item}) eq 'HASH') {
 8420:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 8421:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 8422:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 8423:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 8424:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 8425:                                     $protocol = 'http' if ($protocol ne 'https');
 8426:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 8427: 
 8428:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 8429:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 8430:                                     } else {
 8431:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 8432:                                     }
 8433:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 8434:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 8435:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 8436:                                     }
 8437:                                     $resulttext .= '</li>';
 8438:                                 } else {
 8439:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 8440:                                 }
 8441:                             } else {
 8442:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 8443:                             }
 8444:                         }
 8445:                         $resulttext .= '</ul></li>';
 8446:                     }
 8447:                 } elsif ($item eq 'helpurl') {
 8448:                     if (ref($changes{$item}) eq 'HASH') {
 8449:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 8450:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 8451:                                 my ($chg,$link);
 8452:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 8453:                                 if ($lang eq 'nolang') {
 8454:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 8455:                                 } else {
 8456:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 8457:                                 }
 8458:                                 $resulttext .= '<li>'.$chg.'</li>';
 8459:                             } else {
 8460:                                 my $chg;
 8461:                                 if ($lang eq 'nolang') {
 8462:                                     $chg = &mt('custom log-in help file for no preferred language');
 8463:                                 } else {
 8464:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 8465:                                 }
 8466:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 8467:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 8468:                                                       '?inhibitmenu=yes',$chg,600,500).
 8469:                                                '</li>';
 8470:                             }
 8471:                         }
 8472:                     }
 8473:                 } elsif ($item eq 'headtag') {
 8474:                     if (ref($changes{$item}) eq 'HASH') {
 8475:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 8476:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 8477:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 8478:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 8479:                                 $resulttext .= '<li><a href="'.
 8480:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 8481:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 8482:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 8483:                                 if ($possexempt{$lonhost}) {
 8484:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 8485:                                 } else {
 8486:                                     $resulttext .= &mt('included for any client IP');
 8487:                                 }
 8488:                                 $resulttext .= '</li>';
 8489:                             }
 8490:                         }
 8491:                     }
 8492:                 } elsif ($item eq 'captcha') {
 8493:                     if (ref($loginhash{'login'}) eq 'HASH') {
 8494:                         my $chgtxt;
 8495:                         if ($loginhash{'login'}{$item} eq 'notused') {
 8496:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 8497:                         } else {
 8498:                             my %captchas = &captcha_phrases();
 8499:                             if ($captchas{$loginhash{'login'}{$item}}) {
 8500:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 8501:                             } else {
 8502:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 8503:                             }
 8504:                         }
 8505:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 8506:                     }
 8507:                 } elsif ($item eq 'recaptchakeys') {
 8508:                     if (ref($loginhash{'login'}) eq 'HASH') {
 8509:                         my ($privkey,$pubkey);
 8510:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 8511:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 8512:                             $privkey = $loginhash{'login'}{$item}{'private'};
 8513:                         }
 8514:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 8515:                         if (!$pubkey) {
 8516:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 8517:                         } else {
 8518:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 8519:                         }
 8520:                         if (!$privkey) {
 8521:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 8522:                         } else {
 8523:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 8524:                         }
 8525:                         $chgtxt .= '</ul>';
 8526:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 8527:                     }
 8528:                 } elsif ($item eq 'recaptchaversion') {
 8529:                     if (ref($loginhash{'login'}) eq 'HASH') {
 8530:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 8531:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 8532:                                            '</li>';
 8533:                         }
 8534:                     }
 8535:                 } else {
 8536:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 8537:                 }
 8538:             }
 8539:             $resulttext .= $colchgtext.'</ul>';
 8540:         } else {
 8541:             $resulttext = &mt('No changes made to log-in page settings');
 8542:         }
 8543:     } else {
 8544:         $resulttext = '<span class="LC_error">'.
 8545: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8546:     }
 8547:     if ($errors) {
 8548:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 8549:                        $errors.'</ul>';
 8550:     }
 8551:     return $resulttext;
 8552: }
 8553: 
 8554: sub check_exempt_addresses {
 8555:     my ($iplist) = @_;
 8556:     $iplist =~ s/^\s+//;
 8557:     $iplist =~ s/\s+$//;
 8558:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 8559:     my (@okips,$new);
 8560:     foreach my $ip (@poss_ips) {
 8561:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 8562:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 8563:                 push(@okips,$ip);
 8564:             }
 8565:         }
 8566:     }
 8567:     if (@okips > 0) {
 8568:         $new = join(',',@okips);
 8569:     } else {
 8570:         $new = '';
 8571:     }
 8572:     return $new;
 8573: }
 8574: 
 8575: sub color_font_choices {
 8576:     my %choices =
 8577:         &Apache::lonlocal::texthash (
 8578:             img => "Header",
 8579:             bgs => "Background colors",
 8580:             links => "Link colors",
 8581:             images => "Images",
 8582:             font => "Font color",
 8583:             fontmenu => "Font menu",
 8584:             pgbg => "Page",
 8585:             tabbg => "Header",
 8586:             sidebg => "Border",
 8587:             link => "Link",
 8588:             alink => "Active link",
 8589:             vlink => "Visited link",
 8590:         );
 8591:     return %choices;
 8592: }
 8593: 
 8594: sub modify_rolecolors {
 8595:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 8596:     my ($resulttext,%rolehash);
 8597:     $rolehash{'rolecolors'} = {};
 8598:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 8599:         if ($domconfig{'rolecolors'} eq '') {
 8600:             $domconfig{'rolecolors'} = {};
 8601:         }
 8602:     }
 8603:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 8604:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 8605:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 8606:                                              $dom);
 8607:     if ($putresult eq 'ok') {
 8608:         if (keys(%changes) > 0) {
 8609:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8610:             if (ref($lastactref) eq 'HASH') {
 8611:                 $lastactref->{'domainconfig'} = 1;
 8612:             }
 8613:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 8614:                                              $rolehash{'rolecolors'});
 8615:         } else {
 8616:             $resulttext = &mt('No changes made to default color schemes');
 8617:         }
 8618:     } else {
 8619:         $resulttext = '<span class="LC_error">'.
 8620: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8621:     }
 8622:     if ($errors) {
 8623:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 8624:                        $errors.'</ul>';
 8625:     }
 8626:     return $resulttext;
 8627: }
 8628: 
 8629: sub modify_colors {
 8630:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 8631:     my (%changes,%choices);
 8632:     my @bgs;
 8633:     my @links = ('link','alink','vlink');
 8634:     my @logintext;
 8635:     my @images;
 8636:     my $servadm = $r->dir_config('lonAdmEMail');
 8637:     my $errors;
 8638:     my %defaults;
 8639:     foreach my $role (@{$roles}) {
 8640:         if ($role eq 'login') {
 8641:             %choices = &login_choices();
 8642:             @logintext = ('textcol','bgcol');
 8643:         } else {
 8644:             %choices = &color_font_choices();
 8645:         }
 8646:         if ($role eq 'login') {
 8647:             @images = ('img','logo','domlogo','login');
 8648:             @bgs = ('pgbg','mainbg','sidebg');
 8649:         } else {
 8650:             @images = ('img');
 8651:             @bgs = ('pgbg','tabbg','sidebg');
 8652:         }
 8653:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 8654:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 8655:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 8656:         }
 8657:         if ($role eq 'login') {
 8658:             foreach my $item (@logintext) {
 8659:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8660:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8661:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8662:                 }
 8663:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 8664:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8665:                 }
 8666:             }
 8667:         } else {
 8668:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 8669:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 8670:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 8671:             }
 8672:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 8673:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 8674:             }
 8675:         }
 8676:         foreach my $item (@bgs) {
 8677:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8678:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8679:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8680:             }
 8681:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 8682:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8683:             }
 8684:         }
 8685:         foreach my $item (@links) {
 8686:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8687:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8688:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8689:             }
 8690:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 8691:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8692:             }
 8693:         }
 8694:         my ($configuserok,$author_ok,$switchserver) = 
 8695:             &config_check($dom,$confname,$servadm);
 8696:         my ($width,$height) = &thumb_dimensions();
 8697:         if (ref($domconfig->{$role}) ne 'HASH') {
 8698:             $domconfig->{$role} = {};
 8699:         }
 8700:         foreach my $img (@images) {
 8701:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 8702:                 if (defined($env{'form.login_showlogo_'.$img})) {
 8703:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 8704:                 } else { 
 8705:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 8706:                 }
 8707:             } 
 8708: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 8709: 		 && !defined($domconfig->{$role}{$img})
 8710: 		 && !$env{'form.'.$role.'_del_'.$img}
 8711: 		 && $env{'form.'.$role.'_import_'.$img}) {
 8712: 		# import the old configured image from the .tab setting
 8713: 		# if they haven't provided a new one 
 8714: 		$domconfig->{$role}{$img} = 
 8715: 		    $env{'form.'.$role.'_import_'.$img};
 8716: 	    }
 8717:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 8718:                 my $error;
 8719:                 if ($configuserok eq 'ok') {
 8720:                     if ($switchserver) {
 8721:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 8722:                     } else {
 8723:                         if ($author_ok eq 'ok') {
 8724:                             my ($result,$logourl) = 
 8725:                                 &publishlogo($r,'upload',$role.'_'.$img,
 8726:                                            $dom,$confname,$img,$width,$height);
 8727:                             if ($result eq 'ok') {
 8728:                                 $confhash->{$role}{$img} = $logourl;
 8729:                                 $changes{$role}{'images'}{$img} = 1;
 8730:                             } else {
 8731:                                 $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);
 8732:                             }
 8733:                         } else {
 8734:                             $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);
 8735:                         }
 8736:                     }
 8737:                 } else {
 8738:                     $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);
 8739:                 }
 8740:                 if ($error) {
 8741:                     &Apache::lonnet::logthis($error);
 8742:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8743:                 }
 8744:             } elsif ($domconfig->{$role}{$img} ne '') {
 8745:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 8746:                     my $error;
 8747:                     if ($configuserok eq 'ok') {
 8748: # is confname an author?
 8749:                         if ($switchserver eq '') {
 8750:                             if ($author_ok eq 'ok') {
 8751:                                 my ($result,$logourl) = 
 8752:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 8753:                                             $dom,$confname,$img,$width,$height);
 8754:                                 if ($result eq 'ok') {
 8755:                                     $confhash->{$role}{$img} = $logourl;
 8756: 				    $changes{$role}{'images'}{$img} = 1;
 8757:                                 }
 8758:                             }
 8759:                         }
 8760:                     }
 8761:                 }
 8762:             }
 8763:         }
 8764:         if (ref($domconfig) eq 'HASH') {
 8765:             if (ref($domconfig->{$role}) eq 'HASH') {
 8766:                 foreach my $img (@images) {
 8767:                     if ($domconfig->{$role}{$img} ne '') {
 8768:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8769:                             $confhash->{$role}{$img} = '';
 8770:                             $changes{$role}{'images'}{$img} = 1;
 8771:                         } else {
 8772:                             if ($confhash->{$role}{$img} eq '') {
 8773:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 8774:                             }
 8775:                         }
 8776:                     } else {
 8777:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8778:                             $confhash->{$role}{$img} = '';
 8779:                             $changes{$role}{'images'}{$img} = 1;
 8780:                         } 
 8781:                     }
 8782:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 8783:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 8784:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 8785:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 8786:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 8787:                             }
 8788:                         } else {
 8789:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8790:                                 $changes{$role}{'showlogo'}{$img} = 1;
 8791:                             }
 8792:                         }
 8793:                     }
 8794:                 }
 8795:                 if ($domconfig->{$role}{'font'} ne '') {
 8796:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 8797:                         $changes{$role}{'font'} = 1;
 8798:                     }
 8799:                 } else {
 8800:                     if ($confhash->{$role}{'font'}) {
 8801:                         $changes{$role}{'font'} = 1;
 8802:                     }
 8803:                 }
 8804:                 if ($role ne 'login') {
 8805:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 8806:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 8807:                             $changes{$role}{'fontmenu'} = 1;
 8808:                         }
 8809:                     } else {
 8810:                         if ($confhash->{$role}{'fontmenu'}) {
 8811:                             $changes{$role}{'fontmenu'} = 1;
 8812:                         }
 8813:                     }
 8814:                 }
 8815:                 foreach my $item (@bgs) {
 8816:                     if ($domconfig->{$role}{$item} ne '') {
 8817:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8818:                             $changes{$role}{'bgs'}{$item} = 1;
 8819:                         } 
 8820:                     } else {
 8821:                         if ($confhash->{$role}{$item}) {
 8822:                             $changes{$role}{'bgs'}{$item} = 1;
 8823:                         }
 8824:                     }
 8825:                 }
 8826:                 foreach my $item (@links) {
 8827:                     if ($domconfig->{$role}{$item} ne '') {
 8828:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8829:                             $changes{$role}{'links'}{$item} = 1;
 8830:                         }
 8831:                     } else {
 8832:                         if ($confhash->{$role}{$item}) {
 8833:                             $changes{$role}{'links'}{$item} = 1;
 8834:                         }
 8835:                     }
 8836:                 }
 8837:                 foreach my $item (@logintext) {
 8838:                     if ($domconfig->{$role}{$item} ne '') {
 8839:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8840:                             $changes{$role}{'logintext'}{$item} = 1;
 8841:                         }
 8842:                     } else {
 8843:                         if ($confhash->{$role}{$item}) {
 8844:                             $changes{$role}{'logintext'}{$item} = 1;
 8845:                         }
 8846:                     }
 8847:                 }
 8848:             } else {
 8849:                 &default_change_checker($role,\@images,\@links,\@bgs,
 8850:                                         \@logintext,$confhash,\%changes); 
 8851:             }
 8852:         } else {
 8853:             &default_change_checker($role,\@images,\@links,\@bgs,
 8854:                                     \@logintext,$confhash,\%changes); 
 8855:         }
 8856:     }
 8857:     return ($errors,%changes);
 8858: }
 8859: 
 8860: sub config_check {
 8861:     my ($dom,$confname,$servadm) = @_;
 8862:     my ($configuserok,$author_ok,$switchserver,%currroles);
 8863:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 8864:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 8865:                                                    $confname,$servadm);
 8866:     if ($configuserok eq 'ok') {
 8867:         $switchserver = &check_switchserver($dom,$confname);
 8868:         if ($switchserver eq '') {
 8869:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 8870:         }
 8871:     }
 8872:     return ($configuserok,$author_ok,$switchserver);
 8873: }
 8874: 
 8875: sub default_change_checker {
 8876:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 8877:     foreach my $item (@{$links}) {
 8878:         if ($confhash->{$role}{$item}) {
 8879:             $changes->{$role}{'links'}{$item} = 1;
 8880:         }
 8881:     }
 8882:     foreach my $item (@{$bgs}) {
 8883:         if ($confhash->{$role}{$item}) {
 8884:             $changes->{$role}{'bgs'}{$item} = 1;
 8885:         }
 8886:     }
 8887:     foreach my $item (@{$logintext}) {
 8888:         if ($confhash->{$role}{$item}) {
 8889:             $changes->{$role}{'logintext'}{$item} = 1;
 8890:         }
 8891:     }
 8892:     foreach my $img (@{$images}) {
 8893:         if ($env{'form.'.$role.'_del_'.$img}) {
 8894:             $confhash->{$role}{$img} = '';
 8895:             $changes->{$role}{'images'}{$img} = 1;
 8896:         }
 8897:         if ($role eq 'login') {
 8898:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8899:                 $changes->{$role}{'showlogo'}{$img} = 1;
 8900:             }
 8901:         }
 8902:     }
 8903:     if ($confhash->{$role}{'font'}) {
 8904:         $changes->{$role}{'font'} = 1;
 8905:     }
 8906: }
 8907: 
 8908: sub display_colorchgs {
 8909:     my ($dom,$changes,$roles,$confhash) = @_;
 8910:     my (%choices,$resulttext);
 8911:     if (!grep(/^login$/,@{$roles})) {
 8912:         $resulttext = &mt('Changes made:').'<br />';
 8913:     }
 8914:     foreach my $role (@{$roles}) {
 8915:         if ($role eq 'login') {
 8916:             %choices = &login_choices();
 8917:         } else {
 8918:             %choices = &color_font_choices();
 8919:         }
 8920:         if (ref($changes->{$role}) eq 'HASH') {
 8921:             if ($role ne 'login') {
 8922:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 8923:             }
 8924:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 8925:                 if ($role ne 'login') {
 8926:                     $resulttext .= '<ul>';
 8927:                 }
 8928:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 8929:                     if ($role ne 'login') {
 8930:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 8931:                     }
 8932:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 8933:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 8934:                             if ($confhash->{$role}{$key}{$item}) {
 8935:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 8936:                             } else {
 8937:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 8938:                             }
 8939:                         } elsif ($confhash->{$role}{$item} eq '') {
 8940:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 8941:                         } else {
 8942:                             my $newitem = $confhash->{$role}{$item};
 8943:                             if ($key eq 'images') {
 8944:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" style="vertical-align: bottom" />';
 8945:                             }
 8946:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 8947:                         }
 8948:                     }
 8949:                     if ($role ne 'login') {
 8950:                         $resulttext .= '</ul></li>';
 8951:                     }
 8952:                 } else {
 8953:                     if ($confhash->{$role}{$key} eq '') {
 8954:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 8955:                     } else {
 8956:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 8957:                     }
 8958:                 }
 8959:                 if ($role ne 'login') {
 8960:                     $resulttext .= '</ul>';
 8961:                 }
 8962:             }
 8963:         }
 8964:     }
 8965:     return $resulttext;
 8966: }
 8967: 
 8968: sub thumb_dimensions {
 8969:     return ('200','50');
 8970: }
 8971: 
 8972: sub check_dimensions {
 8973:     my ($inputfile) = @_;
 8974:     my ($fullwidth,$fullheight);
 8975:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 8976:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 8977:             my $imageinfo = <PIPE>;
 8978:             if (!close(PIPE)) {
 8979:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 8980:             }
 8981:             chomp($imageinfo);
 8982:             my ($fullsize) = 
 8983:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 8984:             if ($fullsize) {
 8985:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 8986:             }
 8987:         }
 8988:     }
 8989:     return ($fullwidth,$fullheight);
 8990: }
 8991: 
 8992: sub check_configuser {
 8993:     my ($uhome,$dom,$confname,$servadm) = @_;
 8994:     my ($configuserok,%currroles);
 8995:     if ($uhome eq 'no_host') {
 8996:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 8997:         my $configpass = &LONCAPA::Enrollment::create_password();
 8998:         $configuserok = 
 8999:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 9000:                              $configpass,'','','','','',undef,$servadm);
 9001:     } else {
 9002:         $configuserok = 'ok';
 9003:         %currroles = 
 9004:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 9005:     }
 9006:     return ($configuserok,%currroles);
 9007: }
 9008: 
 9009: sub check_authorstatus {
 9010:     my ($dom,$confname,%currroles) = @_;
 9011:     my $author_ok;
 9012:     if (!$currroles{':'.$dom.':au'}) {
 9013:         my $start = time;
 9014:         my $end = 0;
 9015:         $author_ok = 
 9016:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 9017:                                         'au',$end,$start,'','','domconfig');
 9018:     } else {
 9019:         $author_ok = 'ok';
 9020:     }
 9021:     return $author_ok;
 9022: }
 9023: 
 9024: sub publishlogo {
 9025:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 9026:     my ($output,$fname,$logourl,$madethumb);
 9027:     if ($action eq 'upload') {
 9028:         $fname=$env{'form.'.$formname.'.filename'};
 9029:         chop($env{'form.'.$formname});
 9030:     } else {
 9031:         ($fname) = ($formname =~ /([^\/]+)$/);
 9032:     }
 9033:     if ($savefileas ne '') {
 9034:         $fname = $savefileas;
 9035:     }
 9036:     $fname=&Apache::lonnet::clean_filename($fname);
 9037: # See if there is anything left
 9038:     unless ($fname) { return ('error: no uploaded file'); }
 9039:     $fname="$subdir/$fname";
 9040:     my $docroot=$r->dir_config('lonDocRoot');
 9041:     my $filepath="$docroot/priv";
 9042:     my $relpath = "$dom/$confname";
 9043:     my ($fnamepath,$file,$fetchthumb);
 9044:     $file=$fname;
 9045:     if ($fname=~m|/|) {
 9046:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 9047:     }
 9048:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 9049:     my $count;
 9050:     for ($count=5;$count<=$#parts;$count++) {
 9051:         $filepath.="/$parts[$count]";
 9052:         if ((-e $filepath)!=1) {
 9053:             mkdir($filepath,02770);
 9054:         }
 9055:     }
 9056:     # Check for bad extension and disallow upload
 9057:     if ($file=~/\.(\w+)$/ &&
 9058:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 9059:         $output = 
 9060:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 9061:     } elsif ($file=~/\.(\w+)$/ &&
 9062:         !defined(&Apache::loncommon::fileembstyle($1))) {
 9063:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 9064:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 9065:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 9066:     } elsif (-d "$filepath/$file") {
 9067:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 9068:     } else {
 9069:         my $source = $filepath.'/'.$file;
 9070:         my $logfile;
 9071:         if (!open($logfile,">>",$source.'.log')) {
 9072:             return (&mt('No write permission to Authoring Space'));
 9073:         }
 9074:         print $logfile
 9075: "\n================= Publish ".localtime()." ================\n".
 9076: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 9077: # Save the file
 9078:         if (!open(FH,">",$source)) {
 9079:             &Apache::lonnet::logthis('Failed to create '.$source);
 9080:             return (&mt('Failed to create file'));
 9081:         }
 9082:         if ($action eq 'upload') {
 9083:             if (!print FH ($env{'form.'.$formname})) {
 9084:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 9085:                 return (&mt('Failed to write file'));
 9086:             }
 9087:         } else {
 9088:             my $original = &Apache::lonnet::filelocation('',$formname);
 9089:             if(!copy($original,$source)) {
 9090:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 9091:                 return (&mt('Failed to write file'));
 9092:             }
 9093:         }
 9094:         close(FH);
 9095:         chmod(0660, $source); # Permissions to rw-rw---.
 9096: 
 9097:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 9098:         my $copyfile=$targetdir.'/'.$file;
 9099: 
 9100:         my @parts=split(/\//,$targetdir);
 9101:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 9102:         for (my $count=5;$count<=$#parts;$count++) {
 9103:             $path.="/$parts[$count]";
 9104:             if (!-e $path) {
 9105:                 print $logfile "\nCreating directory ".$path;
 9106:                 mkdir($path,02770);
 9107:             }
 9108:         }
 9109:         my $versionresult;
 9110:         if (-e $copyfile) {
 9111:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 9112:         } else {
 9113:             $versionresult = 'ok';
 9114:         }
 9115:         if ($versionresult eq 'ok') {
 9116:             if (copy($source,$copyfile)) {
 9117:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 9118:                 $output = 'ok';
 9119:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 9120:                 push(@{$modified_urls},[$copyfile,$source]);
 9121:                 my $metaoutput = 
 9122:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 9123:                 unless ($registered_cleanup) {
 9124:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 9125:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 9126:                     $registered_cleanup=1;
 9127:                 }
 9128:             } else {
 9129:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 9130:                 $output = &mt('Failed to copy file to RES space').", $!";
 9131:             }
 9132:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 9133:                 my $inputfile = $filepath.'/'.$file;
 9134:                 my $outfile = $filepath.'/'.'tn-'.$file;
 9135:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 9136:                 if ($fullwidth ne '' && $fullheight ne '') { 
 9137:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 9138:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 9139:                         my @args = ('convert','-sample',$thumbsize,$inputfile,$outfile);
 9140:                         system({$args[0]} @args);
 9141:                         chmod(0660, $filepath.'/tn-'.$file);
 9142:                         if (-e $outfile) {
 9143:                             my $copyfile=$targetdir.'/tn-'.$file;
 9144:                             if (copy($outfile,$copyfile)) {
 9145:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 9146:                                 my $thumb_metaoutput = 
 9147:                                     &write_metadata($dom,$confname,$formname,
 9148:                                                     $targetdir,'tn-'.$file,$logfile);
 9149:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 9150:                                 unless ($registered_cleanup) {
 9151:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 9152:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 9153:                                     $registered_cleanup=1;
 9154:                                 }
 9155:                                 $madethumb = 1;
 9156:                             } else {
 9157:                                 print $logfile "\nUnable to write ".$copyfile.
 9158:                                                ':'.$!."\n";
 9159:                             }
 9160:                         }
 9161:                     }
 9162:                 }
 9163:             }
 9164:         } else {
 9165:             $output = $versionresult;
 9166:         }
 9167:     }
 9168:     return ($output,$logourl,$madethumb);
 9169: }
 9170: 
 9171: sub logo_versioning {
 9172:     my ($targetdir,$file,$logfile) = @_;
 9173:     my $target = $targetdir.'/'.$file;
 9174:     my ($maxversion,$fn,$extn,$output);
 9175:     $maxversion = 0;
 9176:     if ($file =~ /^(.+)\.(\w+)$/) {
 9177:         $fn=$1;
 9178:         $extn=$2;
 9179:     }
 9180:     opendir(DIR,$targetdir);
 9181:     while (my $filename=readdir(DIR)) {
 9182:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 9183:             $maxversion=($1>$maxversion)?$1:$maxversion;
 9184:         }
 9185:     }
 9186:     $maxversion++;
 9187:     print $logfile "\nCreating old version ".$maxversion."\n";
 9188:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 9189:     if (copy($target,$copyfile)) {
 9190:         print $logfile "Copied old target to ".$copyfile."\n";
 9191:         $copyfile=$copyfile.'.meta';
 9192:         if (copy($target.'.meta',$copyfile)) {
 9193:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 9194:             $output = 'ok';
 9195:         } else {
 9196:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 9197:             $output = &mt('Failed to copy old meta').", $!, ";
 9198:         }
 9199:     } else {
 9200:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 9201:         $output = &mt('Failed to copy old target').", $!, ";
 9202:     }
 9203:     return $output;
 9204: }
 9205: 
 9206: sub write_metadata {
 9207:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 9208:     my (%metadatafields,%metadatakeys,$output);
 9209:     $metadatafields{'title'}=$formname;
 9210:     $metadatafields{'creationdate'}=time;
 9211:     $metadatafields{'lastrevisiondate'}=time;
 9212:     $metadatafields{'copyright'}='public';
 9213:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 9214:                                          $env{'user.domain'};
 9215:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 9216:     $metadatafields{'domain'}=$dom;
 9217:     {
 9218:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 9219:         my $mfh;
 9220:         if (open($mfh,">",$targetdir.'/'.$file.'.meta')) {
 9221:             foreach (sort(keys(%metadatafields))) {
 9222:                 unless ($_=~/\./) {
 9223:                     my $unikey=$_;
 9224:                     $unikey=~/^([A-Za-z]+)/;
 9225:                     my $tag=$1;
 9226:                     $tag=~tr/A-Z/a-z/;
 9227:                     print $mfh "\n\<$tag";
 9228:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 9229:                         my $value=$metadatafields{$unikey.'.'.$_};
 9230:                         $value=~s/\"/\'\'/g;
 9231:                         print $mfh ' '.$_.'="'.$value.'"';
 9232:                     }
 9233:                     print $mfh '>'.
 9234:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 9235:                             .'</'.$tag.'>';
 9236:                 }
 9237:             }
 9238:             $output = 'ok';
 9239:             print $logfile "\nWrote metadata";
 9240:             close($mfh);
 9241:         } else {
 9242:             print $logfile "\nFailed to open metadata file";
 9243:             $output = &mt('Could not write metadata');
 9244:         }
 9245:     }
 9246:     return $output;
 9247: }
 9248: 
 9249: sub notifysubscribed {
 9250:     foreach my $targetsource (@{$modified_urls}){
 9251:         next unless (ref($targetsource) eq 'ARRAY');
 9252:         my ($target,$source)=@{$targetsource};
 9253:         if ($source ne '') {
 9254:             if (open(my $logfh,">>",$source.'.log')) {
 9255:                 print $logfh "\nCleanup phase: Notifications\n";
 9256:                 my @subscribed=&subscribed_hosts($target);
 9257:                 foreach my $subhost (@subscribed) {
 9258:                     print $logfh "\nNotifying host ".$subhost.':';
 9259:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 9260:                     print $logfh $reply;
 9261:                 }
 9262:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 9263:                 foreach my $subhost (@subscribedmeta) {
 9264:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 9265:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 9266:                                                         $subhost);
 9267:                     print $logfh $reply;
 9268:                 }
 9269:                 print $logfh "\n============ Done ============\n";
 9270:                 close($logfh);
 9271:             }
 9272:         }
 9273:     }
 9274:     return OK;
 9275: }
 9276: 
 9277: sub subscribed_hosts {
 9278:     my ($target) = @_;
 9279:     my @subscribed;
 9280:     if (open(my $fh,"<","$target.subscription")) {
 9281:         while (my $subline=<$fh>) {
 9282:             if ($subline =~ /^($match_lonid):/) {
 9283:                 my $host = $1;
 9284:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 9285:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 9286:                         push(@subscribed,$host);
 9287:                     }
 9288:                 }
 9289:             }
 9290:         }
 9291:     }
 9292:     return @subscribed;
 9293: }
 9294: 
 9295: sub check_switchserver {
 9296:     my ($dom,$confname) = @_;
 9297:     my ($allowed,$switchserver);
 9298:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 9299:     if ($home eq 'no_host') {
 9300:         $home = &Apache::lonnet::domain($dom,'primary');
 9301:     }
 9302:     my @ids=&Apache::lonnet::current_machine_ids();
 9303:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 9304:     if (!$allowed) {
 9305: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 9306:     }
 9307:     return $switchserver;
 9308: }
 9309: 
 9310: sub modify_quotas {
 9311:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 9312:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 9313:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 9314:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 9315:         $validationfieldsref);
 9316:     if ($action eq 'quotas') {
 9317:         $context = 'tools'; 
 9318:     } else {
 9319:         $context = $action;
 9320:     }
 9321:     if ($context eq 'requestcourses') {
 9322:         @usertools = ('official','unofficial','community','textbook','placement');
 9323:         @options =('norequest','approval','validate','autolimit');
 9324:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 9325:         %titles = &courserequest_titles();
 9326:         $toolregexp = join('|',@usertools);
 9327:         %conditions = &courserequest_conditions();
 9328:         $confname = $dom.'-domainconfig';
 9329:         my $servadm = $r->dir_config('lonAdmEMail');
 9330:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9331:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 9332:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 9333:     } elsif ($context eq 'requestauthor') {
 9334:         @usertools = ('author');
 9335:         %titles = &authorrequest_titles();
 9336:     } else {
 9337:         @usertools = ('aboutme','blog','webdav','portfolio');
 9338:         %titles = &tool_titles();
 9339:     }
 9340:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9341:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 9342:     foreach my $key (keys(%env)) {
 9343:         if ($context eq 'requestcourses') {
 9344:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 9345:                 my $item = $1;
 9346:                 my $type = $2;
 9347:                 if ($type =~ /^limit_(.+)/) {
 9348:                     $limithash{$item}{$1} = $env{$key};
 9349:                 } else {
 9350:                     $confhash{$item}{$type} = $env{$key};
 9351:                 }
 9352:             }
 9353:         } elsif ($context eq 'requestauthor') {
 9354:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 9355:                 $confhash{$1} = $env{$key};
 9356:             }
 9357:         } else {
 9358:             if ($key =~ /^form\.quota_(.+)$/) {
 9359:                 $confhash{'defaultquota'}{$1} = $env{$key};
 9360:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 9361:                 $confhash{'authorquota'}{$1} = $env{$key};
 9362:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 9363:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 9364:             }
 9365:         }
 9366:     }
 9367:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 9368:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 9369:         @approvalnotify = sort(@approvalnotify);
 9370:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 9371:         my @crstypes = ('official','unofficial','community','textbook','placement');
 9372:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 9373:         foreach my $type (@hasuniquecode) {
 9374:             if (grep(/^\Q$type\E$/,@crstypes)) {
 9375:                 $confhash{'uniquecode'}{$type} = 1;
 9376:             }
 9377:         }
 9378:         my (%newbook,%allpos);
 9379:         if ($context eq 'requestcourses') {
 9380:             foreach my $type ('textbooks','templates') {
 9381:                 @{$allpos{$type}} = (); 
 9382:                 my $invalid;
 9383:                 if ($type eq 'textbooks') {
 9384:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 9385:                 } else {
 9386:                     $invalid = &mt('Invalid LON-CAPA course for template');
 9387:                 }
 9388:                 if ($env{'form.'.$type.'_addbook'}) {
 9389:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 9390:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 9391:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 9392:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 9393:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 9394:                         } else {
 9395:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 9396:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 9397:                             $position =~ s/\D+//g;
 9398:                             if ($position ne '') {
 9399:                                 $allpos{$type}[$position] = $newbook{$type};
 9400:                             }
 9401:                         }
 9402:                     } else {
 9403:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 9404:                     }
 9405:                 }
 9406:             } 
 9407:         }
 9408:         if (ref($domconfig{$action}) eq 'HASH') {
 9409:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 9410:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 9411:                     $changes{'notify'}{'approval'} = 1;
 9412:                 }
 9413:             } else {
 9414:                 if ($confhash{'notify'}{'approval'}) {
 9415:                     $changes{'notify'}{'approval'} = 1;
 9416:                 }
 9417:             }
 9418:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 9419:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 9420:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 9421:                         unless ($confhash{'uniquecode'}{$crstype}) {
 9422:                             $changes{'uniquecode'} = 1;
 9423:                         }
 9424:                     }
 9425:                     unless ($changes{'uniquecode'}) {
 9426:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 9427:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 9428:                                 $changes{'uniquecode'} = 1;
 9429:                             }
 9430:                         }
 9431:                     }
 9432:                } else {
 9433:                    $changes{'uniquecode'} = 1;
 9434:                }
 9435:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 9436:                 $changes{'uniquecode'} = 1;
 9437:             }
 9438:             if ($context eq 'requestcourses') {
 9439:                 foreach my $type ('textbooks','templates') {
 9440:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 9441:                         my %deletions;
 9442:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 9443:                         if (@todelete) {
 9444:                             map { $deletions{$_} = 1; } @todelete;
 9445:                         }
 9446:                         my %imgdeletions;
 9447:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 9448:                         if (@todeleteimages) {
 9449:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 9450:                         }
 9451:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 9452:                         for (my $i=0; $i<=$maxnum; $i++) {
 9453:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 9454:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 9455:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 9456:                                 if ($deletions{$key}) {
 9457:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 9458:                                         #FIXME need to obsolete item in RES space
 9459:                                     }
 9460:                                     next;
 9461:                                 } else {
 9462:                                     my $newpos = $env{'form.'.$itemid};
 9463:                                     $newpos =~ s/\D+//g;
 9464:                                     foreach my $item ('subject','title','publisher','author') {
 9465:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9466:                                                  ($type eq 'templates'));
 9467:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 9468:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 9469:                                             $changes{$type}{$key} = 1;
 9470:                                         }
 9471:                                     }
 9472:                                     $allpos{$type}[$newpos] = $key;
 9473:                                 }
 9474:                                 if ($imgdeletions{$key}) {
 9475:                                     $changes{$type}{$key} = 1;
 9476:                                     #FIXME need to obsolete item in RES space
 9477:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 9478:                                     my ($cdom,$cnum) = split(/_/,$key);
 9479:                                     if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
 9480:                                         $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
 9481:                                     } else {
 9482:                                         my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 9483:                                                                                       $cdom,$cnum,$type,$configuserok,
 9484:                                                                                       $switchserver,$author_ok);
 9485:                                         if ($imgurl) {
 9486:                                             $confhash{$type}{$key}{'image'} = $imgurl;
 9487:                                             $changes{$type}{$key} = 1; 
 9488:                                         }
 9489:                                         if ($error) {
 9490:                                             &Apache::lonnet::logthis($error);
 9491:                                             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9492:                                         }
 9493:                                     }
 9494:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 9495:                                     $confhash{$type}{$key}{'image'} = 
 9496:                                         $domconfig{$action}{$type}{$key}{'image'};
 9497:                                 }
 9498:                             }
 9499:                         }
 9500:                     }
 9501:                 }
 9502:             }
 9503:         } else {
 9504:             if ($confhash{'notify'}{'approval'}) {
 9505:                 $changes{'notify'}{'approval'} = 1;
 9506:             }
 9507:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 9508:                 $changes{'uniquecode'} = 1;
 9509:             }
 9510:         }
 9511:         if ($context eq 'requestcourses') {
 9512:             foreach my $type ('textbooks','templates') {
 9513:                 if ($newbook{$type}) {
 9514:                     $changes{$type}{$newbook{$type}} = 1;
 9515:                     foreach my $item ('subject','title','publisher','author') {
 9516:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9517:                                  ($type eq 'template'));
 9518:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 9519:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 9520:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 9521:                         }
 9522:                     }
 9523:                     if ($type eq 'textbooks') {
 9524:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 9525:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 9526:                             if (&Apache::lonnet::homeserver($cnum,$cdom) eq 'no_host') {
 9527:                                 $errors .= '<li><span class="LC_error">'.&mt('Image not saved: could not find textbook course').'</li>';
 9528:                             } else {
 9529:                                 my ($imageurl,$error) =
 9530:                                     &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 9531:                                                             $configuserok,$switchserver,$author_ok);
 9532:                                 if ($imageurl) {
 9533:                                     $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 9534:                                 }
 9535:                                 if ($error) {
 9536:                                     &Apache::lonnet::logthis($error);
 9537:                                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9538:                                 }
 9539:                             }
 9540:                         }
 9541:                     }
 9542:                 }
 9543:                 if (@{$allpos{$type}} > 0) {
 9544:                     my $idx = 0;
 9545:                     foreach my $item (@{$allpos{$type}}) {
 9546:                         if ($item ne '') {
 9547:                             $confhash{$type}{$item}{'order'} = $idx;
 9548:                             if (ref($domconfig{$action}) eq 'HASH') {
 9549:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 9550:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 9551:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 9552:                                             $changes{$type}{$item} = 1;
 9553:                                         }
 9554:                                     }
 9555:                                 }
 9556:                             }
 9557:                             $idx ++;
 9558:                         }
 9559:                     }
 9560:                 }
 9561:             }
 9562:             if (ref($validationitemsref) eq 'ARRAY') {
 9563:                 foreach my $item (@{$validationitemsref}) {
 9564:                     if ($item eq 'fields') {
 9565:                         my @changed;
 9566:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 9567:                         if (@{$confhash{'validation'}{$item}} > 0) {
 9568:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 9569:                         }
 9570:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9571:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9572:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 9573:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 9574:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
 9575:                                 } else {
 9576:                                     @changed = @{$confhash{'validation'}{$item}};
 9577:                                 }
 9578:                             } else {
 9579:                                 @changed = @{$confhash{'validation'}{$item}};
 9580:                             }
 9581:                         } else {
 9582:                             @changed = @{$confhash{'validation'}{$item}};
 9583:                         }
 9584:                         if (@changed) {
 9585:                             if ($confhash{'validation'}{$item}) {
 9586:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 9587:                             } else {
 9588:                                 $changes{'validation'}{$item} = &mt('None');
 9589:                             }
 9590:                         }
 9591:                     } else {
 9592:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 9593:                         if ($item eq 'markup') {
 9594:                             if ($env{'form.requestcourses_validation_'.$item}) {
 9595:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 9596:                             }
 9597:                         }
 9598:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9599:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9600:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 9601:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9602:                                 }
 9603:                             } else {
 9604:                                 if ($confhash{'validation'}{$item} ne '') {
 9605:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9606:                                 }
 9607:                             }
 9608:                         } else {
 9609:                             if ($confhash{'validation'}{$item} ne '') {
 9610:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9611:                             }
 9612:                         }
 9613:                     }
 9614:                 }
 9615:             }
 9616:             if ($env{'form.validationdc'}) {
 9617:                 my $newval = $env{'form.validationdc'};
 9618:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 9619:                 if (exists($domcoords{$newval})) {
 9620:                     $confhash{'validation'}{'dc'} = $newval;
 9621:                 }
 9622:             }
 9623:             if (ref($confhash{'validation'}) eq 'HASH') {
 9624:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9625:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9626:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9627:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9628:                                 if ($confhash{'validation'}{'dc'} eq '') {
 9629:                                     $changes{'validation'}{'dc'} = &mt('None');
 9630:                                 } else {
 9631:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9632:                                 }
 9633:                             }
 9634:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
 9635:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9636:                         }
 9637:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 9638:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9639:                     }
 9640:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 9641:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9642:                 }
 9643:             } else {
 9644:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9645:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9646:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9647:                             $changes{'validation'}{'dc'} = &mt('None');
 9648:                         }
 9649:                     }
 9650:                 }
 9651:             }
 9652:         }
 9653:     } else {
 9654:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 9655:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 9656:     }
 9657:     foreach my $item (@usertools) {
 9658:         foreach my $type (@{$types},'default','_LC_adv') {
 9659:             my $unset; 
 9660:             if ($context eq 'requestcourses') {
 9661:                 $unset = '0';
 9662:                 if ($type eq '_LC_adv') {
 9663:                     $unset = '';
 9664:                 }
 9665:                 if ($confhash{$item}{$type} eq 'autolimit') {
 9666:                     $confhash{$item}{$type} .= '=';
 9667:                     unless ($limithash{$item}{$type} =~ /\D/) {
 9668:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 9669:                     }
 9670:                 }
 9671:             } elsif ($context eq 'requestauthor') {
 9672:                 $unset = '0';
 9673:                 if ($type eq '_LC_adv') {
 9674:                     $unset = '';
 9675:                 }
 9676:             } else {
 9677:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 9678:                     $confhash{$item}{$type} = 1;
 9679:                 } else {
 9680:                     $confhash{$item}{$type} = 0;
 9681:                 }
 9682:             }
 9683:             if (ref($domconfig{$action}) eq 'HASH') {
 9684:                 if ($action eq 'requestauthor') {
 9685:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 9686:                         $changes{$type} = 1;
 9687:                     }
 9688:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 9689:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 9690:                         $changes{$item}{$type} = 1;
 9691:                     }
 9692:                 } else {
 9693:                     if ($context eq 'requestcourses') {
 9694:                         if ($confhash{$item}{$type} ne $unset) {
 9695:                             $changes{$item}{$type} = 1;
 9696:                         }
 9697:                     } else {
 9698:                         if (!$confhash{$item}{$type}) {
 9699:                             $changes{$item}{$type} = 1;
 9700:                         }
 9701:                     }
 9702:                 }
 9703:             } else {
 9704:                 if ($context eq 'requestcourses') {
 9705:                     if ($confhash{$item}{$type} ne $unset) {
 9706:                         $changes{$item}{$type} = 1;
 9707:                     }
 9708:                 } elsif ($context eq 'requestauthor') {
 9709:                     if ($confhash{$type} ne $unset) {
 9710:                         $changes{$type} = 1;
 9711:                     }
 9712:                 } else {
 9713:                     if (!$confhash{$item}{$type}) {
 9714:                         $changes{$item}{$type} = 1;
 9715:                     }
 9716:                 }
 9717:             }
 9718:         }
 9719:     }
 9720:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 9721:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 9722:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 9723:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 9724:                     if (exists($confhash{'defaultquota'}{$key})) {
 9725:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 9726:                             $changes{'defaultquota'}{$key} = 1;
 9727:                         }
 9728:                     } else {
 9729:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 9730:                     }
 9731:                 }
 9732:             } else {
 9733:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 9734:                     if (exists($confhash{'defaultquota'}{$key})) {
 9735:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 9736:                             $changes{'defaultquota'}{$key} = 1;
 9737:                         }
 9738:                     } else {
 9739:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 9740:                     }
 9741:                 }
 9742:             }
 9743:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9744:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 9745:                     if (exists($confhash{'authorquota'}{$key})) {
 9746:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 9747:                             $changes{'authorquota'}{$key} = 1;
 9748:                         }
 9749:                     } else {
 9750:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 9751:                     }
 9752:                 }
 9753:             }
 9754:         }
 9755:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 9756:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 9757:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9758:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 9759:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 9760:                             $changes{'defaultquota'}{$key} = 1;
 9761:                         }
 9762:                     } else {
 9763:                         if (!exists($domconfig{'quotas'}{$key})) {
 9764:                             $changes{'defaultquota'}{$key} = 1;
 9765:                         }
 9766:                     }
 9767:                 } else {
 9768:                     $changes{'defaultquota'}{$key} = 1;
 9769:                 }
 9770:             }
 9771:         }
 9772:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 9773:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 9774:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9775:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9776:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 9777:                             $changes{'authorquota'}{$key} = 1;
 9778:                         }
 9779:                     } else {
 9780:                         $changes{'authorquota'}{$key} = 1;
 9781:                     }
 9782:                 } else {
 9783:                     $changes{'authorquota'}{$key} = 1;
 9784:                 }
 9785:             }
 9786:         }
 9787:     }
 9788: 
 9789:     if ($context eq 'requestauthor') {
 9790:         $domdefaults{'requestauthor'} = \%confhash;
 9791:     } else {
 9792:         foreach my $key (keys(%confhash)) {
 9793:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 9794:                 $domdefaults{$key} = $confhash{$key};
 9795:             }
 9796:         }
 9797:     }
 9798: 
 9799:     my %quotahash = (
 9800:                       $action => { %confhash }
 9801:                     );
 9802:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 9803:                                              $dom);
 9804:     if ($putresult eq 'ok') {
 9805:         if (keys(%changes) > 0) {
 9806:             my $cachetime = 24*60*60;
 9807:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9808:             if (ref($lastactref) eq 'HASH') {
 9809:                 $lastactref->{'domdefaults'} = 1;
 9810:             }
 9811:             $resulttext = &mt('Changes made:').'<ul>';
 9812:             unless (($context eq 'requestcourses') ||
 9813:                     ($context eq 'requestauthor')) {
 9814:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 9815:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 9816:                     foreach my $type (@{$types},'default') {
 9817:                         if (defined($changes{'defaultquota'}{$type})) {
 9818:                             my $typetitle = $usertypes->{$type};
 9819:                             if ($type eq 'default') {
 9820:                                 $typetitle = $othertitle;
 9821:                             }
 9822:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 9823:                         }
 9824:                     }
 9825:                     $resulttext .= '</ul></li>';
 9826:                 }
 9827:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 9828:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 9829:                     foreach my $type (@{$types},'default') {
 9830:                         if (defined($changes{'authorquota'}{$type})) {
 9831:                             my $typetitle = $usertypes->{$type};
 9832:                             if ($type eq 'default') {
 9833:                                 $typetitle = $othertitle;
 9834:                             }
 9835:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 9836:                         }
 9837:                     }
 9838:                     $resulttext .= '</ul></li>';
 9839:                 }
 9840:             }
 9841:             my %newenv;
 9842:             foreach my $item (@usertools) {
 9843:                 my (%haschgs,%inconf);
 9844:                 if ($context eq 'requestauthor') {
 9845:                     %haschgs = %changes;
 9846:                     %inconf = %confhash;
 9847:                 } else {
 9848:                     if (ref($changes{$item}) eq 'HASH') {
 9849:                         %haschgs = %{$changes{$item}};
 9850:                     }
 9851:                     if (ref($confhash{$item}) eq 'HASH') {
 9852:                         %inconf = %{$confhash{$item}};
 9853:                     }
 9854:                 }
 9855:                 if (keys(%haschgs) > 0) {
 9856:                     my $newacc = 
 9857:                         &Apache::lonnet::usertools_access($env{'user.name'},
 9858:                                                           $env{'user.domain'},
 9859:                                                           $item,'reload',$context);
 9860:                     if (($context eq 'requestcourses') ||
 9861:                         ($context eq 'requestauthor')) {
 9862:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 9863:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 9864:                         }
 9865:                     } else {
 9866:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 9867:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 9868:                         }
 9869:                     }
 9870:                     unless ($context eq 'requestauthor') {
 9871:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 9872:                     }
 9873:                     foreach my $type (@{$types},'default','_LC_adv') {
 9874:                         if ($haschgs{$type}) {
 9875:                             my $typetitle = $usertypes->{$type};
 9876:                             if ($type eq 'default') {
 9877:                                 $typetitle = $othertitle;
 9878:                             } elsif ($type eq '_LC_adv') {
 9879:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 9880:                             }
 9881:                             if ($inconf{$type}) {
 9882:                                 if ($context eq 'requestcourses') {
 9883:                                     my $cond;
 9884:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 9885:                                         if ($1 eq '') {
 9886:                                             $cond = &mt('(Automatic processing of any request).');
 9887:                                         } else {
 9888:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 9889:                                         }
 9890:                                     } else { 
 9891:                                         $cond = $conditions{$inconf{$type}};
 9892:                                     }
 9893:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 9894:                                 } elsif ($context eq 'requestauthor') {
 9895:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 9896:                                                              $titles{$inconf{$type}},$typetitle);
 9897: 
 9898:                                 } else {
 9899:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 9900:                                 }
 9901:                             } else {
 9902:                                 if ($type eq '_LC_adv') {
 9903:                                     if ($inconf{$type} eq '0') {
 9904:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9905:                                     } else { 
 9906:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 9907:                                     }
 9908:                                 } else {
 9909:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9910:                                 }
 9911:                             }
 9912:                         }
 9913:                     }
 9914:                     unless ($context eq 'requestauthor') {
 9915:                         $resulttext .= '</ul></li>';
 9916:                     }
 9917:                 }
 9918:             }
 9919:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 9920:                 if (ref($changes{'notify'}) eq 'HASH') {
 9921:                     if ($changes{'notify'}{'approval'}) {
 9922:                         if (ref($confhash{'notify'}) eq 'HASH') {
 9923:                             if ($confhash{'notify'}{'approval'}) {
 9924:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 9925:                             } else {
 9926:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 9927:                             }
 9928:                         }
 9929:                     }
 9930:                 }
 9931:             }
 9932:             if ($action eq 'requestcourses') {
 9933:                 my @offon = ('off','on');
 9934:                 if ($changes{'uniquecode'}) {
 9935:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 9936:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 9937:                         $resulttext .= '<li>'.
 9938:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 9939:                                        '</li>';
 9940:                     } else {
 9941:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 9942:                                        '</li>';
 9943:                     }
 9944:                 }
 9945:                 foreach my $type ('textbooks','templates') {
 9946:                     if (ref($changes{$type}) eq 'HASH') {
 9947:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 9948:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 9949:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 9950:                             my $coursetitle = $coursehash{'description'};
 9951:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 9952:                             $resulttext .= '<li>';
 9953:                             foreach my $item ('subject','title','publisher','author') {
 9954:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9955:                                          ($type eq 'templates'));
 9956:                                 my $name = $item.':';
 9957:                                 $name =~ s/^(\w)/\U$1/;
 9958:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 9959:                             }
 9960:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 9961:                             if ($type eq 'textbooks') {
 9962:                                 if ($confhash{$type}{$key}{'image'}) {
 9963:                                     $resulttext .= ' '.&mt('Image: [_1]',
 9964:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 9965:                                                    ' alt="Textbook cover" />').'<br />';
 9966:                                 }
 9967:                             }
 9968:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 9969:                         }
 9970:                         $resulttext .= '</ul></li>';
 9971:                     }
 9972:                 }
 9973:                 if (ref($changes{'validation'}) eq 'HASH') {
 9974:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 9975:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 9976:                         foreach my $item (@{$validationitemsref}) {
 9977:                             if (exists($changes{'validation'}{$item})) {
 9978:                                 if ($item eq 'markup') {
 9979:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9980:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 9981:                                 } else {
 9982:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9983:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 9984:                                 }
 9985:                             }
 9986:                         }
 9987:                         if (exists($changes{'validation'}{'dc'})) {
 9988:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 9989:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 9990:                         }
 9991:                     }
 9992:                 }
 9993:             }
 9994:             $resulttext .= '</ul>';
 9995:             if (keys(%newenv)) {
 9996:                 &Apache::lonnet::appenv(\%newenv);
 9997:             }
 9998:         } else {
 9999:             if ($context eq 'requestcourses') {
10000:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
10001:             } elsif ($context eq 'requestauthor') {
10002:                 $resulttext = &mt('No changes made to rights to request author space.');
10003:             } else {
10004:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
10005:             }
10006:         }
10007:     } else {
10008:         $resulttext = '<span class="LC_error">'.
10009: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10010:     }
10011:     if ($errors) {
10012:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
10013:                        '<ul>'.$errors.'</ul></p>';
10014:     }
10015:     return $resulttext;
10016: }
10017: 
10018: sub process_textbook_image {
10019:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
10020:     my $filename = $env{'form.'.$caller.'.filename'};
10021:     my ($error,$url);
10022:     my ($width,$height) = (50,50);
10023:     if ($configuserok eq 'ok') {
10024:         if ($switchserver) {
10025:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
10026:                          $switchserver);
10027:         } elsif ($author_ok eq 'ok') {
10028:             my ($result,$imageurl) =
10029:                 &publishlogo($r,'upload',$caller,$dom,$confname,
10030:                              "$type/$cdom/$cnum/cover",$width,$height);
10031:             if ($result eq 'ok') {
10032:                 $url = $imageurl;
10033:             } else {
10034:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
10035:             }
10036:         } else {
10037:             $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);
10038:         }
10039:     } else {
10040:         $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);
10041:     }
10042:     return ($url,$error);
10043: }
10044: 
10045: sub modify_ltitools {
10046:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
10047:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
10048:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
10049:     my $confname = $dom.'-domainconfig';
10050:     my $servadm = $r->dir_config('lonAdmEMail');
10051:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
10052:     my (%posslti,%possfield);
10053:     my @courseroles = ('cc','in','ta','ep','st');
10054:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
10055:     map { $posslti{$_} = 1; } @ltiroles;
10056:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
10057:     map { $possfield{$_} = 1; } @allfields;
10058:     my %lt = &ltitools_names(); 
10059:     if ($env{'form.ltitools_add'}) {
10060:         my $title = $env{'form.ltitools_add_title'};
10061:         $title =~ s/(`)/'/g;
10062:         ($newid,my $error) = &get_ltitools_id($dom,$title);
10063:         if ($newid) {
10064:             my $position = $env{'form.ltitools_add_pos'};
10065:             $position =~ s/\D+//g;
10066:             if ($position ne '') {
10067:                 $allpos[$position] = $newid;
10068:             }
10069:             $changes{$newid} = 1;
10070:             foreach my $item ('title','url','key','secret') {
10071:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
10072:                 if ($env{'form.ltitools_add_'.$item}) {
10073:                     if (($item eq 'key') || ($item eq 'secret')) {
10074:                         $encconfig{$newid}{$item} = $env{'form.ltitools_add_'.$item};
10075:                     } else {
10076:                         $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
10077:                     }
10078:                 }
10079:             }
10080:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
10081:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
10082:             }
10083:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
10084:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
10085:             }
10086:             foreach my $item ('width','height','linktext','explanation') {
10087:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
10088:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
10089:                 if (($item eq 'width') || ($item eq 'height')) {
10090:                     if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
10091:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
10092:                     }
10093:                 } else {
10094:                     if ($env{'form.ltitools_add_'.$item} ne '') {
10095:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item}; 
10096:                     }
10097:                 }
10098:             }
10099:             if ($env{'form.ltitools_add_target'} eq 'window') {
10100:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
10101:             } elsif ($env{'form.ltitools_add_target'} eq 'tab') {
10102:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
10103:             } else {
10104:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
10105:             }
10106:             foreach my $item ('passback','roster') {
10107:                 if ($env{'form.ltitools_add_'.$item}) {
10108:                     $confhash{$newid}{$item} = 1;
10109:                 }
10110:             }
10111:             if ($env{'form.ltitools_add_image.filename'} ne '') {
10112:                 my ($imageurl,$error) =
10113:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$newid,
10114:                                             $configuserok,$switchserver,$author_ok);
10115:                 if ($imageurl) {
10116:                     $confhash{$newid}{'image'} = $imageurl;
10117:                 }
10118:                 if ($error) {
10119:                     &Apache::lonnet::logthis($error);
10120:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10121:                 }
10122:             }
10123:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
10124:             foreach my $field (@fields) {
10125:                 if ($possfield{$field}) {
10126:                     if ($field eq 'roles') {
10127:                         foreach my $role (@courseroles) {
10128:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
10129:                             if (($choice ne '') && ($posslti{$choice})) {
10130:                                 $confhash{$newid}{'roles'}{$role} = $choice;
10131:                                 if ($role eq 'cc') {
10132:                                     $confhash{$newid}{'roles'}{'co'} = $choice; 
10133:                                 }
10134:                             }
10135:                         }
10136:                     } else {
10137:                         $confhash{$newid}{'fields'}{$field} = 1;
10138:                     }
10139:                 }
10140:             }
10141:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
10142:             foreach my $item (@courseconfig) {
10143:                 $confhash{$newid}{'crsconf'}{$item} = 1;
10144:             }
10145:             if ($env{'form.ltitools_add_custom'}) {
10146:                 my $name = $env{'form.ltitools_add_custom_name'};
10147:                 my $value = $env{'form.ltitools_add_custom_value'};
10148:                 $value =~ s/(`)/'/g;
10149:                 $name =~ s/(`)/'/g;
10150:                 $confhash{$newid}{'custom'}{$name} = $value;
10151:             }
10152:         } else {
10153:             my $error = &mt('Failed to acquire unique ID for new external tool');   
10154:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10155:         }
10156:     }
10157:     if (ref($domconfig{$action}) eq 'HASH') {
10158:         my %deletions;
10159:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
10160:         if (@todelete) {
10161:             map { $deletions{$_} = 1; } @todelete;
10162:         }
10163:         my %customadds;
10164:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
10165:         if (@newcustom) {
10166:             map { $customadds{$_} = 1; } @newcustom;
10167:         } 
10168:         my %imgdeletions;
10169:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
10170:         if (@todeleteimages) {
10171:             map { $imgdeletions{$_} = 1; } @todeleteimages;
10172:         }
10173:         my $maxnum = $env{'form.ltitools_maxnum'};
10174:         for (my $i=0; $i<=$maxnum; $i++) {
10175:             my $itemid = $env{'form.ltitools_id_'.$i};
10176:             $itemid =~ s/\D+//g;
10177:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
10178:                 if ($deletions{$itemid}) {
10179:                     if ($domconfig{$action}{$itemid}{'image'}) {
10180:                         #FIXME need to obsolete item in RES space
10181:                     }
10182:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
10183:                     next;
10184:                 } else {
10185:                     my $newpos = $env{'form.ltitools_'.$itemid};
10186:                     $newpos =~ s/\D+//g;
10187:                     foreach my $item ('title','url') {
10188:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
10189:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
10190:                             $changes{$itemid} = 1;
10191:                         }
10192:                     }
10193:                     foreach my $item ('key','secret') {
10194:                         $encconfig{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
10195:                         if ($domconfig{$action}{$itemid}{$item} ne $encconfig{$itemid}{$item}) {
10196:                             $changes{$itemid} = 1;
10197:                         }
10198:                     }
10199:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
10200:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
10201:                     }
10202:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
10203:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
10204:                     }
10205:                     foreach my $size ('width','height') {
10206:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
10207:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
10208:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
10209:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
10210:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10211:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
10212:                                     $changes{$itemid} = 1;
10213:                                 }
10214:                             } else {
10215:                                 $changes{$itemid} = 1;
10216:                             }
10217:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10218:                             if ($domconfig{$action}{$itemid}{'display'}{$size} ne '') {
10219:                                 $changes{$itemid} = 1;
10220:                             }
10221:                         }
10222:                     }
10223:                     foreach my $item ('linktext','explanation') {
10224:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/^\s+//;
10225:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/\s+$//;
10226:                         if ($env{'form.ltitools_'.$item.'_'.$i} ne '') {
10227:                             $confhash{$itemid}{'display'}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
10228:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10229:                                 if ($domconfig{$action}{$itemid}{'display'}{$item} ne $confhash{$itemid}{'display'}{$item}) {
10230:                                     $changes{$itemid} = 1;
10231:                                 }
10232:                             } else {
10233:                                 $changes{$itemid} = 1;
10234:                             }
10235:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10236:                             if ($domconfig{$action}{$itemid}{'display'}{$item} ne '') {
10237:                                 $changes{$itemid} = 1;
10238:                             }
10239:                         }
10240:                     }
10241:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
10242:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
10243:                     } elsif ($env{'form.ltitools_target_'.$i} eq 'tab') {
10244:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
10245:                     } else {
10246:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
10247:                     }
10248:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
10249:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
10250:                             $changes{$itemid} = 1;
10251:                         }
10252:                     } else {
10253:                         $changes{$itemid} = 1;
10254:                     }
10255:                     foreach my $extra ('passback','roster') {
10256:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
10257:                             $confhash{$itemid}{$extra} = 1;
10258:                         }
10259:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
10260:                             $changes{$itemid} = 1;
10261:                         }
10262:                     }
10263:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
10264:                     foreach my $item ('label','title','target','linktext','explanation','append') {
10265:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
10266:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
10267:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
10268:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
10269:                                     $changes{$itemid} = 1;
10270:                                 }
10271:                             } else {
10272:                                 $changes{$itemid} = 1;
10273:                             }
10274:                         }
10275:                     }
10276:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
10277:                     foreach my $field (@fields) {
10278:                         if ($possfield{$field}) {
10279:                             if ($field eq 'roles') {
10280:                                 foreach my $role (@courseroles) {
10281:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
10282:                                     if (($choice ne '') && ($posslti{$choice})) {
10283:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
10284:                                         if ($role eq 'cc') {
10285:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
10286:                                         }
10287:                                     }
10288:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
10289:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
10290:                                             $changes{$itemid} = 1;
10291:                                         }
10292:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
10293:                                         $changes{$itemid} = 1;
10294:                                     }
10295:                                 }
10296:                             } else {
10297:                                 $confhash{$itemid}{'fields'}{$field} = 1;
10298:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
10299:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
10300:                                         $changes{$itemid} = 1;
10301:                                     }
10302:                                 } else {
10303:                                     $changes{$itemid} = 1;
10304:                                 }
10305:                             }
10306:                         }
10307:                     }
10308:                     $allpos[$newpos] = $itemid;
10309:                 }
10310:                 if ($imgdeletions{$itemid}) {
10311:                     $changes{$itemid} = 1;
10312:                     #FIXME need to obsolete item in RES space
10313:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
10314:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
10315:                                                                  $itemid,$configuserok,$switchserver,
10316:                                                                  $author_ok);
10317:                     if ($imgurl) {
10318:                         $confhash{$itemid}{'image'} = $imgurl;
10319:                         $changes{$itemid} = 1;
10320:                     }
10321:                     if ($error) {
10322:                         &Apache::lonnet::logthis($error);
10323:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
10324:                     }
10325:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
10326:                     $confhash{$itemid}{'image'} =
10327:                        $domconfig{$action}{$itemid}{'image'};
10328:                 }
10329:                 if ($customadds{$i}) {
10330:                     my $name = $env{'form.ltitools_custom_name_'.$i};
10331:                     $name =~ s/(`)/'/g;
10332:                     $name =~ s/^\s+//;
10333:                     $name =~ s/\s+$//;
10334:                     my $value = $env{'form.ltitools_custom_value_'.$i};
10335:                     $value =~ s/(`)/'/g;
10336:                     $value =~ s/^\s+//;
10337:                     $value =~ s/\s+$//;
10338:                     if ($name ne '') {
10339:                         $confhash{$itemid}{'custom'}{$name} = $value;
10340:                         $changes{$itemid} = 1;
10341:                     }
10342:                 }
10343:                 my %customdels;
10344:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i); 
10345:                 if (@customdeletions) {
10346:                     $changes{$itemid} = 1;
10347:                 }
10348:                 map { $customdels{$_} = 1; } @customdeletions;
10349:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
10350:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
10351:                         unless ($customdels{$key}) {
10352:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
10353:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i}; 
10354:                             }
10355:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
10356:                                 $changes{$itemid} = 1;
10357:                             }
10358:                         }
10359:                     }
10360:                 }
10361:                 unless ($changes{$itemid}) {
10362:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
10363:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
10364:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
10365:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
10366:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
10367:                                         $changes{$itemid} = 1;
10368:                                         last;
10369:                                     }
10370:                                 }
10371:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
10372:                                 $changes{$itemid} = 1;
10373:                             }
10374:                         }
10375:                         last if ($changes{$itemid});
10376:                     }
10377:                 }
10378:             }
10379:         }
10380:     }
10381:     if (@allpos > 0) {
10382:         my $idx = 0;
10383:         foreach my $itemid (@allpos) {
10384:             if ($itemid ne '') {
10385:                 $confhash{$itemid}{'order'} = $idx;
10386:                 if (ref($domconfig{$action}) eq 'HASH') {
10387:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
10388:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
10389:                             $changes{$itemid} = 1;
10390:                         }
10391:                     }
10392:                 }
10393:                 $idx ++;
10394:             }
10395:         }
10396:     }
10397:     my %ltitoolshash = (
10398:                           $action => { %confhash }
10399:                        );
10400:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
10401:                                              $dom);
10402:     if ($putresult eq 'ok') {
10403:         my %ltienchash = (
10404:                              $action => { %encconfig }
10405:                          );
10406:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
10407:         if (keys(%changes) > 0) {
10408:             my $cachetime = 24*60*60;
10409:             my %ltiall = %confhash;
10410:             foreach my $id (keys(%ltiall)) {
10411:                 if (ref($encconfig{$id}) eq 'HASH') {
10412:                     foreach my $item ('key','secret') {
10413:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
10414:                     }
10415:                 }
10416:             }
10417:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%ltiall,$cachetime);
10418:             if (ref($lastactref) eq 'HASH') {
10419:                 $lastactref->{'ltitools'} = 1;
10420:             }
10421:             $resulttext = &mt('Changes made:').'<ul>';
10422:             my %bynum;
10423:             foreach my $itemid (sort(keys(%changes))) {
10424:                 my $position = $confhash{$itemid}{'order'};
10425:                 $bynum{$position} = $itemid;
10426:             }
10427:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
10428:                 my $itemid = $bynum{$pos}; 
10429:                 if (ref($confhash{$itemid}) ne 'HASH') {
10430:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
10431:                 } else {
10432:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
10433:                     if ($confhash{$itemid}{'image'}) {
10434:                         $resulttext .= '&nbsp;'.
10435:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
10436:                                        ' alt="'.&mt('Tool Provider icon').'" />';
10437:                     }
10438:                     $resulttext .= '</li><ul>';
10439:                     my $position = $pos + 1;
10440:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
10441:                     foreach my $item ('version','msgtype','url') {
10442:                         if ($confhash{$itemid}{$item} ne '') {
10443:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
10444:                         }
10445:                     }
10446:                     if ($encconfig{$itemid}{'key'} ne '') {
10447:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
10448:                     }
10449:                     if ($encconfig{$itemid}{'secret'} ne '') {
10450:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
10451:                         my $num = length($encconfig{$itemid}{'secret'});
10452:                         $resulttext .= ('*'x$num).'</li>';
10453:                     }
10454:                     $resulttext .= '<li>'.&mt('Configurable in course:');
10455:                     my @possconfig = ('label','title','target','linktext','explanation','append');
10456:                     my $numconfig = 0; 
10457:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') { 
10458:                         foreach my $item (@possconfig) {
10459:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
10460:                                 $numconfig ++;
10461:                                 $resulttext .= ' "'.$lt{'crs'.$item}.'"';
10462:                             }
10463:                         }
10464:                     }
10465:                     if (!$numconfig) {
10466:                         $resulttext .= &mt('None');
10467:                     }
10468:                     $resulttext .= '</li>';
10469:                     foreach my $item ('passback','roster') {
10470:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
10471:                         if ($confhash{$itemid}{$item}) {
10472:                             $resulttext .= &mt('Yes');
10473:                         } else {
10474:                             $resulttext .= &mt('No');
10475:                         }
10476:                         $resulttext .= '</li>';
10477:                     }
10478:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
10479:                         my $displaylist;
10480:                         if ($confhash{$itemid}{'display'}{'target'}) {
10481:                             $displaylist = &mt('Display target').':&nbsp;'.
10482:                                            $confhash{$itemid}{'display'}{'target'}.',';
10483:                         }
10484:                         foreach my $size ('width','height') { 
10485:                             if ($confhash{$itemid}{'display'}{$size}) {
10486:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
10487:                                                 $confhash{$itemid}{'display'}{$size}.',';
10488:                             }
10489:                         }
10490:                         if ($displaylist) {
10491:                             $displaylist =~ s/,$//;
10492:                             $resulttext .= '<li>'.$displaylist.'</li>';
10493:                         }
10494:                         foreach my $item ('linktext','explanation') {
10495:                             if ($confhash{$itemid}{'display'}{$item}) {
10496:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{'display'}{$item}.'</li>';
10497:                             }
10498:                         }
10499:                     }
10500:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
10501:                         my $fieldlist;
10502:                         foreach my $field (@allfields) {
10503:                             if ($confhash{$itemid}{'fields'}{$field}) {
10504:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
10505:                             }
10506:                         }
10507:                         if ($fieldlist) {
10508:                             $fieldlist =~ s/,$//;
10509:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
10510:                         }
10511:                     }
10512:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
10513:                         my $rolemaps;
10514:                         foreach my $role (@courseroles) {
10515:                             if ($confhash{$itemid}{'roles'}{$role}) {
10516:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
10517:                                              $confhash{$itemid}{'roles'}{$role}.',';
10518:                             }
10519:                         }
10520:                         if ($rolemaps) {
10521:                             $rolemaps =~ s/,$//; 
10522:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
10523:                         }
10524:                     }
10525:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
10526:                         my $customlist;
10527:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
10528:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
10529:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
10530:                             } 
10531:                         }
10532:                         if ($customlist) {
10533:                             $resulttext .= '<li>'.&mt('Custom items').': '.$customlist.'</li>';
10534:                         }
10535:                     } 
10536:                     $resulttext .= '</ul></li>';
10537:                 }
10538:             }
10539:             $resulttext .= '</ul>';
10540:         } else {
10541:             $resulttext = &mt('No changes made.');
10542:         }
10543:     } else {
10544:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
10545:     }
10546:     if ($errors) {
10547:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
10548:                        $errors.'</ul>';
10549:     }
10550:     return $resulttext;
10551: }
10552: 
10553: sub process_ltitools_image {
10554:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
10555:     my $filename = $env{'form.'.$caller.'.filename'};
10556:     my ($error,$url);
10557:     my ($width,$height) = (21,21);
10558:     if ($configuserok eq 'ok') {
10559:         if ($switchserver) {
10560:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
10561:                          $switchserver);
10562:         } elsif ($author_ok eq 'ok') {
10563:             my ($result,$imageurl,$madethumb) =
10564:                 &publishlogo($r,'upload',$caller,$dom,$confname,
10565:                              "ltitools/$itemid/icon",$width,$height);
10566:             if ($result eq 'ok') {
10567:                 if ($madethumb) {
10568:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
10569:                     my $imagethumb = "$path/tn-".$imagefile;
10570:                     $url = $imagethumb;
10571:                 } else {
10572:                     $url = $imageurl;
10573:                 }
10574:             } else {
10575:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
10576:             }
10577:         } else {
10578:             $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);
10579:         }
10580:     } else {
10581:         $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);
10582:     }
10583:     return ($url,$error);
10584: }
10585: 
10586: sub get_ltitools_id {
10587:     my ($cdom,$title) = @_;
10588:     # get lock on ltitools db
10589:     my $lockhash = {
10590:                       lock => $env{'user.name'}.
10591:                               ':'.$env{'user.domain'},
10592:                    };
10593:     my $tries = 0;
10594:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
10595:     my ($id,$error);
10596:  
10597:     while (($gotlock ne 'ok') && ($tries<10)) {
10598:         $tries ++;
10599:         sleep (0.1);
10600:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
10601:     }
10602:     if ($gotlock eq 'ok') {
10603:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
10604:         if ($currids{'lock'}) {
10605:             delete($currids{'lock'});
10606:             if (keys(%currids)) {
10607:                 my @curr = sort { $a <=> $b } keys(%currids);
10608:                 if ($curr[-1] =~ /^\d+$/) {
10609:                     $id = 1 + $curr[-1];
10610:                 }
10611:             } else {
10612:                 $id = 1;
10613:             }
10614:             if ($id) {
10615:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
10616:                     $error = 'nostore';
10617:                 }
10618:             } else {
10619:                 $error = 'nonumber';
10620:             }
10621:         }
10622:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
10623:     } else {
10624:         $error = 'nolock';
10625:     }
10626:     return ($id,$error);
10627: }
10628: 
10629: sub modify_autoenroll {
10630:     my ($dom,$lastactref,%domconfig) = @_;
10631:     my ($resulttext,%changes);
10632:     my %currautoenroll;
10633:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
10634:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
10635:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
10636:         }
10637:     }
10638:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
10639:     my %title = ( run => 'Auto-enrollment active',
10640:                   sender => 'Sender for notification messages',
10641:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
10642:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
10643:     my @offon = ('off','on');
10644:     my $sender_uname = $env{'form.sender_uname'};
10645:     my $sender_domain = $env{'form.sender_domain'};
10646:     if ($sender_domain eq '') {
10647:         $sender_uname = '';
10648:     } elsif ($sender_uname eq '') {
10649:         $sender_domain = '';
10650:     }
10651:     my $coowners = $env{'form.autoassign_coowners'};
10652:     my $failsafe = $env{'form.autoenroll_failsafe'};
10653:     $failsafe =~ s{^\s+|\s+$}{}g;
10654:     if ($failsafe =~ /\D/) {
10655:         undef($failsafe);
10656:     }
10657:     my %autoenrollhash =  (
10658:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
10659:                                        'sender_uname' => $sender_uname,
10660:                                        'sender_domain' => $sender_domain,
10661:                                        'co-owners' => $coowners,
10662:                                        'autofailsafe' => $failsafe,
10663:                                 }
10664:                      );
10665:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
10666:                                              $dom);
10667:     if ($putresult eq 'ok') {
10668:         if (exists($currautoenroll{'run'})) {
10669:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
10670:                  $changes{'run'} = 1;
10671:              }
10672:         } elsif ($autorun) {
10673:             if ($env{'form.autoenroll_run'} ne '1') {
10674:                  $changes{'run'} = 1;
10675:             }
10676:         }
10677:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
10678:             $changes{'sender'} = 1;
10679:         }
10680:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
10681:             $changes{'sender'} = 1;
10682:         }
10683:         if ($currautoenroll{'co-owners'} ne '') {
10684:             if ($currautoenroll{'co-owners'} ne $coowners) {
10685:                 $changes{'coowners'} = 1;
10686:             }
10687:         } elsif ($coowners) {
10688:             $changes{'coowners'} = 1;
10689:         }
10690:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
10691:             $changes{'autofailsafe'} = 1;
10692:         }
10693:         if (keys(%changes) > 0) {
10694:             $resulttext = &mt('Changes made:').'<ul>';
10695:             if ($changes{'run'}) {
10696:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
10697:             }
10698:             if ($changes{'sender'}) {
10699:                 if ($sender_uname eq '' || $sender_domain eq '') {
10700:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
10701:                 } else {
10702:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
10703:                 }
10704:             }
10705:             if ($changes{'coowners'}) {
10706:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
10707:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
10708:                 if (ref($lastactref) eq 'HASH') {
10709:                     $lastactref->{'domainconfig'} = 1;
10710:                 }
10711:             }
10712:             if ($changes{'autofailsafe'}) {
10713:                 if ($failsafe ne '') {
10714:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
10715:                 } else {
10716:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
10717:                 }
10718:                 &Apache::lonnet::get_domain_defaults($dom,1);
10719:                 if (ref($lastactref) eq 'HASH') {
10720:                     $lastactref->{'domdefaults'} = 1;
10721:                 }
10722:             }
10723:             $resulttext .= '</ul>';
10724:         } else {
10725:             $resulttext = &mt('No changes made to auto-enrollment settings');
10726:         }
10727:     } else {
10728:         $resulttext = '<span class="LC_error">'.
10729: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10730:     }
10731:     return $resulttext;
10732: }
10733: 
10734: sub modify_autoupdate {
10735:     my ($dom,%domconfig) = @_;
10736:     my ($resulttext,%currautoupdate,%fields,%changes);
10737:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
10738:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
10739:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
10740:         }
10741:     }
10742:     my @offon = ('off','on');
10743:     my %title = &Apache::lonlocal::texthash (
10744:                    run => 'Auto-update:',
10745:                    classlists => 'Updates to user information in classlists?'
10746:                 );
10747:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10748:     my %fieldtitles = &Apache::lonlocal::texthash (
10749:                         id => 'Student/Employee ID',
10750:                         permanentemail => 'E-mail address',
10751:                         lastname => 'Last Name',
10752:                         firstname => 'First Name',
10753:                         middlename => 'Middle Name',
10754:                         generation => 'Generation',
10755:                       );
10756:     $othertitle = &mt('All users');
10757:     if (keys(%{$usertypes}) >  0) {
10758:         $othertitle = &mt('Other users');
10759:     }
10760:     foreach my $key (keys(%env)) {
10761:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
10762:             my ($usertype,$item) = ($1,$2);
10763:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
10764:                 if ($usertype eq 'default') {   
10765:                     push(@{$fields{$1}},$2);
10766:                 } elsif (ref($types) eq 'ARRAY') {
10767:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
10768:                         push(@{$fields{$1}},$2);
10769:                     }
10770:                 }
10771:             }
10772:         }
10773:     }
10774:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
10775:     @lockablenames = sort(@lockablenames);
10776:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
10777:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
10778:         if (@changed) {
10779:             $changes{'lockablenames'} = 1;
10780:         }
10781:     } else {
10782:         if (@lockablenames) {
10783:             $changes{'lockablenames'} = 1;
10784:         }
10785:     }
10786:     my %updatehash = (
10787:                       autoupdate => { run => $env{'form.autoupdate_run'},
10788:                                       classlists => $env{'form.classlists'},
10789:                                       fields => {%fields},
10790:                                       lockablenames => \@lockablenames,
10791:                                     }
10792:                      );
10793:     foreach my $key (keys(%currautoupdate)) {
10794:         if (($key eq 'run') || ($key eq 'classlists')) {
10795:             if (exists($updatehash{autoupdate}{$key})) {
10796:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
10797:                     $changes{$key} = 1;
10798:                 }
10799:             }
10800:         } elsif ($key eq 'fields') {
10801:             if (ref($currautoupdate{$key}) eq 'HASH') {
10802:                 foreach my $item (@{$types},'default') {
10803:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
10804:                         my $change = 0;
10805:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
10806:                             if (!exists($fields{$item})) {
10807:                                 $change = 1;
10808:                                 last;
10809:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
10810:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
10811:                                     $change = 1;
10812:                                     last;
10813:                                 }
10814:                             }
10815:                         }
10816:                         if ($change) {
10817:                             push(@{$changes{$key}},$item);
10818:                         }
10819:                     } 
10820:                 }
10821:             }
10822:         } elsif ($key eq 'lockablenames') {
10823:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
10824:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
10825:                 if (@changed) {
10826:                     $changes{'lockablenames'} = 1;
10827:                 }
10828:             } else {
10829:                 if (@lockablenames) {
10830:                     $changes{'lockablenames'} = 1;
10831:                 }
10832:             }
10833:         }
10834:     }
10835:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
10836:         if (@lockablenames) {
10837:             $changes{'lockablenames'} = 1;
10838:         }
10839:     }
10840:     foreach my $item (@{$types},'default') {
10841:         if (defined($fields{$item})) {
10842:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
10843:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
10844:                     my $change = 0;
10845:                     if (ref($fields{$item}) eq 'ARRAY') {
10846:                         foreach my $type (@{$fields{$item}}) {
10847:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
10848:                                 $change = 1;
10849:                                 last;
10850:                             }
10851:                         }
10852:                     }
10853:                     if ($change) {
10854:                         push(@{$changes{'fields'}},$item);
10855:                     }
10856:                 } else {
10857:                     push(@{$changes{'fields'}},$item);
10858:                 }
10859:             } else {
10860:                 push(@{$changes{'fields'}},$item);
10861:             }
10862:         }
10863:     }
10864:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
10865:                                              $dom);
10866:     if ($putresult eq 'ok') {
10867:         if (keys(%changes) > 0) {
10868:             $resulttext = &mt('Changes made:').'<ul>';
10869:             foreach my $key (sort(keys(%changes))) {
10870:                 if ($key eq 'lockablenames') {
10871:                     $resulttext .= '<li>';
10872:                     if (@lockablenames) {
10873:                         $usertypes->{'default'} = $othertitle;
10874:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
10875:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
10876:                     } else {
10877:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
10878:                     }
10879:                     $resulttext .= '</li>';
10880:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
10881:                     foreach my $item (@{$changes{$key}}) {
10882:                         my @newvalues;
10883:                         foreach my $type (@{$fields{$item}}) {
10884:                             push(@newvalues,$fieldtitles{$type});
10885:                         }
10886:                         my $newvaluestr;
10887:                         if (@newvalues > 0) {
10888:                             $newvaluestr = join(', ',@newvalues);
10889:                         } else {
10890:                             $newvaluestr = &mt('none');
10891:                         }
10892:                         if ($item eq 'default') {
10893:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
10894:                         } else {
10895:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
10896:                         }
10897:                     }
10898:                 } else {
10899:                     my $newvalue;
10900:                     if ($key eq 'run') {
10901:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
10902:                     } else {
10903:                         $newvalue = $offon[$env{'form.'.$key}];
10904:                     }
10905:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
10906:                 }
10907:             }
10908:             $resulttext .= '</ul>';
10909:         } else {
10910:             $resulttext = &mt('No changes made to autoupdates');
10911:         }
10912:     } else {
10913:         $resulttext = '<span class="LC_error">'.
10914: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10915:     }
10916:     return $resulttext;
10917: }
10918: 
10919: sub modify_autocreate {
10920:     my ($dom,%domconfig) = @_;
10921:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
10922:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
10923:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
10924:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
10925:         }
10926:     }
10927:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
10928:                  req => 'Auto-creation of validated requests for official courses',
10929:                  xmldc => 'Identity of course creator of courses from XML files',
10930:                );
10931:     my @types = ('xml','req');
10932:     foreach my $item (@types) {
10933:         $newvals{$item} = $env{'form.autocreate_'.$item};
10934:         $newvals{$item} =~ s/\D//g;
10935:         $newvals{$item} = 0 if ($newvals{$item} eq '');
10936:     }
10937:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
10938:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
10939:     unless (exists($domcoords{$newvals{'xmldc'}})) {
10940:         $newvals{'xmldc'} = '';
10941:     } 
10942:     %autocreatehash =  (
10943:                         autocreate => { xml => $newvals{'xml'},
10944:                                         req => $newvals{'req'},
10945:                                       }
10946:                        );
10947:     if ($newvals{'xmldc'} ne '') {
10948:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
10949:     }
10950:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
10951:                                              $dom);
10952:     if ($putresult eq 'ok') {
10953:         my @items = @types;
10954:         if ($newvals{'xml'}) {
10955:             push(@items,'xmldc');
10956:         }
10957:         foreach my $item (@items) {
10958:             if (exists($currautocreate{$item})) {
10959:                 if ($currautocreate{$item} ne $newvals{$item}) {
10960:                     $changes{$item} = 1;
10961:                 }
10962:             } elsif ($newvals{$item}) {
10963:                 $changes{$item} = 1;
10964:             }
10965:         }
10966:         if (keys(%changes) > 0) {
10967:             my @offon = ('off','on'); 
10968:             $resulttext = &mt('Changes made:').'<ul>';
10969:             foreach my $item (@types) {
10970:                 if ($changes{$item}) {
10971:                     my $newtxt = $offon[$newvals{$item}];
10972:                     $resulttext .= '<li>'.
10973:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
10974:                                        '<b>','</b>').
10975:                                    '</li>';
10976:                 }
10977:             }
10978:             if ($changes{'xmldc'}) {
10979:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
10980:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
10981:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
10982:             }
10983:             $resulttext .= '</ul>';
10984:         } else {
10985:             $resulttext = &mt('No changes made to auto-creation settings');
10986:         }
10987:     } else {
10988:         $resulttext = '<span class="LC_error">'.
10989:             &mt('An error occurred: [_1]',$putresult).'</span>';
10990:     }
10991:     return $resulttext;
10992: }
10993: 
10994: sub modify_directorysrch {
10995:     my ($dom,$lastactref,%domconfig) = @_;
10996:     my ($resulttext,%changes);
10997:     my %currdirsrch;
10998:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
10999:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
11000:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
11001:         }
11002:     }
11003:     my %title = ( available => 'Institutional directory search available',
11004:                   localonly => 'Other domains can search institution',
11005:                   lcavailable => 'LON-CAPA directory search available',
11006:                   lclocalonly => 'Other domains can search LON-CAPA domain',
11007:                   searchby => 'Search types',
11008:                   searchtypes => 'Search latitude');
11009:     my @offon = ('off','on');
11010:     my @otherdoms = ('Yes','No');
11011: 
11012:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
11013:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
11014:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
11015: 
11016:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11017:     if (keys(%{$usertypes}) == 0) {
11018:         @cansearch = ('default');
11019:     } else {
11020:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
11021:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
11022:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
11023:                     push(@{$changes{'cansearch'}},$type);
11024:                 }
11025:             }
11026:             foreach my $type (@cansearch) {
11027:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
11028:                     push(@{$changes{'cansearch'}},$type);
11029:                 }
11030:             }
11031:         } else {
11032:             push(@{$changes{'cansearch'}},@cansearch);
11033:         }
11034:     }
11035: 
11036:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
11037:         foreach my $by (@{$currdirsrch{'searchby'}}) {
11038:             if (!grep(/^\Q$by\E$/,@searchby)) {
11039:                 push(@{$changes{'searchby'}},$by);
11040:             }
11041:         }
11042:         foreach my $by (@searchby) {
11043:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
11044:                 push(@{$changes{'searchby'}},$by);
11045:             }
11046:         }
11047:     } else {
11048:         push(@{$changes{'searchby'}},@searchby);
11049:     }
11050: 
11051:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
11052:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
11053:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
11054:                 push(@{$changes{'searchtypes'}},$type);
11055:             }
11056:         }
11057:         foreach my $type (@searchtypes) {
11058:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
11059:                 push(@{$changes{'searchtypes'}},$type);
11060:             }
11061:         }
11062:     } else {
11063:         if (exists($currdirsrch{'searchtypes'})) {
11064:             foreach my $type (@searchtypes) {  
11065:                 if ($type ne $currdirsrch{'searchtypes'}) { 
11066:                     push(@{$changes{'searchtypes'}},$type);
11067:                 }
11068:             }
11069:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
11070:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
11071:             }   
11072:         } else {
11073:             push(@{$changes{'searchtypes'}},@searchtypes); 
11074:         }
11075:     }
11076: 
11077:     my %dirsrch_hash =  (
11078:             directorysrch => { available => $env{'form.dirsrch_available'},
11079:                                cansearch => \@cansearch,
11080:                                localonly => $env{'form.dirsrch_instlocalonly'},
11081:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
11082:                                lcavailable => $env{'form.dirsrch_domavailable'},
11083:                                searchby => \@searchby,
11084:                                searchtypes => \@searchtypes,
11085:                              }
11086:             );
11087:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
11088:                                              $dom);
11089:     if ($putresult eq 'ok') {
11090:         if (exists($currdirsrch{'available'})) {
11091:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
11092:                  $changes{'available'} = 1;
11093:              }
11094:         } else {
11095:             if ($env{'form.dirsrch_available'} eq '1') {
11096:                 $changes{'available'} = 1;
11097:             }
11098:         }
11099:         if (exists($currdirsrch{'lcavailable'})) {
11100:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
11101:                 $changes{'lcavailable'} = 1;
11102:             }
11103:         } else {
11104:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
11105:                 $changes{'lcavailable'} = 1;
11106:             }
11107:         }
11108:         if (exists($currdirsrch{'localonly'})) {
11109:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
11110:                 $changes{'localonly'} = 1;
11111:             }
11112:         } else {
11113:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
11114:                 $changes{'localonly'} = 1;
11115:             }
11116:         }
11117:         if (exists($currdirsrch{'lclocalonly'})) {
11118:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
11119:                 $changes{'lclocalonly'} = 1;
11120:             }
11121:         } else {
11122:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
11123:                 $changes{'lclocalonly'} = 1;
11124:             }
11125:         }
11126:         if (keys(%changes) > 0) {
11127:             $resulttext = &mt('Changes made:').'<ul>';
11128:             if ($changes{'available'}) {
11129:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
11130:             }
11131:             if ($changes{'lcavailable'}) {
11132:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
11133:             }
11134:             if ($changes{'localonly'}) {
11135:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
11136:             }
11137:             if ($changes{'lclocalonly'}) {
11138:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
11139:             }
11140:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
11141:                 my $chgtext;
11142:                 if (ref($usertypes) eq 'HASH') {
11143:                     if (keys(%{$usertypes}) > 0) {
11144:                         foreach my $type (@{$types}) {
11145:                             if (grep(/^\Q$type\E$/,@cansearch)) {
11146:                                 $chgtext .= $usertypes->{$type}.'; ';
11147:                             }
11148:                         }
11149:                         if (grep(/^default$/,@cansearch)) {
11150:                             $chgtext .= $othertitle;
11151:                         } else {
11152:                             $chgtext =~ s/\; $//;
11153:                         }
11154:                         $resulttext .=
11155:                             '<li>'.
11156:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
11157:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
11158:                             '</li>';
11159:                     }
11160:                 }
11161:             }
11162:             if (ref($changes{'searchby'}) eq 'ARRAY') {
11163:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
11164:                 my $chgtext;
11165:                 foreach my $type (@{$titleorder}) {
11166:                     if (grep(/^\Q$type\E$/,@searchby)) {
11167:                         if (defined($searchtitles->{$type})) {
11168:                             $chgtext .= $searchtitles->{$type}.'; ';
11169:                         }
11170:                     }
11171:                 }
11172:                 $chgtext =~ s/\; $//;
11173:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
11174:             }
11175:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
11176:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
11177:                 my $chgtext;
11178:                 foreach my $type (@{$srchtypeorder}) {
11179:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
11180:                         if (defined($srchtypes_desc->{$type})) {
11181:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
11182:                         }
11183:                     }
11184:                 }
11185:                 $chgtext =~ s/\; $//;
11186:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
11187:             }
11188:             $resulttext .= '</ul>';
11189:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
11190:             if (ref($lastactref) eq 'HASH') {
11191:                 $lastactref->{'directorysrch'} = 1;
11192:             }
11193:         } else {
11194:             $resulttext = &mt('No changes made to directory search settings');
11195:         }
11196:     } else {
11197:         $resulttext = '<span class="LC_error">'.
11198:                       &mt('An error occurred: [_1]',$putresult).'</span>';
11199:     }
11200:     return $resulttext;
11201: }
11202: 
11203: sub modify_contacts {
11204:     my ($dom,$lastactref,%domconfig) = @_;
11205:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
11206:     if (ref($domconfig{'contacts'}) eq 'HASH') {
11207:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
11208:             $currsetting{$key} = $domconfig{'contacts'}{$key};
11209:         }
11210:     }
11211:     my (%others,%to,%bcc,%includestr,%includeloc);
11212:     my @contacts = ('supportemail','adminemail');
11213:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
11214:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail');
11215:     my @toggles = ('reporterrors','reportupdates');
11216:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
11217:     foreach my $type (@mailings) {
11218:         @{$newsetting{$type}} = 
11219:             &Apache::loncommon::get_env_multiple('form.'.$type);
11220:         foreach my $item (@contacts) {
11221:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
11222:                 $contacts_hash{contacts}{$type}{$item} = 1;
11223:             } else {
11224:                 $contacts_hash{contacts}{$type}{$item} = 0;
11225:             }
11226:         }
11227:         $others{$type} = $env{'form.'.$type.'_others'};
11228:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
11229:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11230:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
11231:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
11232:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
11233:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
11234:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
11235:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
11236:             }
11237:         }
11238:     }
11239:     foreach my $item (@contacts) {
11240:         $to{$item} = $env{'form.'.$item};
11241:         $contacts_hash{'contacts'}{$item} = $to{$item};
11242:     }
11243:     foreach my $item (@toggles) {
11244:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
11245:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
11246:         }
11247:     }
11248:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
11249:         foreach my $field (@{$fields}) {
11250:             if (ref($possoptions->{$field}) eq 'ARRAY') {
11251:                 my $value = $env{'form.helpform_'.$field};
11252:                 $value =~ s/^\s+|\s+$//g;
11253:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
11254:                     $contacts_hash{'contacts'}{'helpform'}{$field} = $value;
11255:                     if ($field eq 'screenshot') {
11256:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
11257:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
11258:                             $contacts_hash{'contacts'}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
11259:                         }
11260:                     }
11261:                 }
11262:             }
11263:         }
11264:     }
11265:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11266:     my (@statuses,%usertypeshash,@overrides);
11267:     if ((ref($types) eq 'ARRAY') && (@{$types} > 0)) {
11268:         @statuses = @{$types};
11269:         if (ref($usertypes) eq 'HASH') {
11270:             %usertypeshash = %{$usertypes};
11271:         }
11272:     }
11273:     if (@statuses) {
11274:         my @possoverrides = &Apache::loncommon::get_env_multiple('form.overrides');
11275:         foreach my $type (@possoverrides) {
11276:             if (($type ne '') && (grep(/^\Q$type\E$/,@statuses))) {
11277:                 push(@overrides,$type);
11278:             }
11279:         }
11280:         if (@overrides) {
11281:             foreach my $type (@overrides) {
11282:                 my @standard = &Apache::loncommon::get_env_multiple('form.override_'.$type);
11283:                 foreach my $item (@contacts) {
11284:                     if (grep(/^\Q$item\E$/,@standard)) {
11285:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 1;
11286:                         $newsetting{'override_'.$type}{$item} = 1;
11287:                     } else {
11288:                         $contacts_hash{'contacts'}{'overrides'}{$type}{$item} = 0;
11289:                         $newsetting{'override_'.$type}{$item} = 0;
11290:                     }
11291:                 }
11292:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'others'} = $env{'form.override_'.$type.'_others'};
11293:                 $contacts_hash{'contacts'}{'overrides'}{$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
11294:                 $newsetting{'override_'.$type}{'others'} = $env{'form.override_'.$type.'_others'};
11295:                 $newsetting{'override_'.$type}{'bcc'} = $env{'form.override_'.$type.'_bcc'};
11296:                 if (($env{'form.override_'.$type.'_includestr'} ne '') && ($env{'form.override_'.$type.'_includeloc'} =~ /^s|b$/)) {
11297:                     $includestr{$type} = $env{'form.override_'.$type.'_includestr'};
11298:                     $includeloc{$type} = $env{'form.override_'.$type.'_includeloc'};
11299:                     $contacts_hash{'contacts'}{'overrides'}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
11300:                     $newsetting{'override_'.$type}{'include'} = $contacts_hash{'contacts'}{'overrides'}{$type}{'include'};
11301:                 }
11302:             }     
11303:         }
11304:     }
11305:     if (keys(%currsetting) > 0) {
11306:         foreach my $item (@contacts) {
11307:             if ($to{$item} ne $currsetting{$item}) {
11308:                 $changes{$item} = 1;
11309:             }
11310:         }
11311:         foreach my $type (@mailings) {
11312:             foreach my $item (@contacts) {
11313:                 if (ref($currsetting{$type}) eq 'HASH') {
11314:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
11315:                         push(@{$changes{$type}},$item);
11316:                     }
11317:                 } else {
11318:                     push(@{$changes{$type}},@{$newsetting{$type}});
11319:                 }
11320:             }
11321:             if ($others{$type} ne $currsetting{$type}{'others'}) {
11322:                 push(@{$changes{$type}},'others');
11323:             }
11324:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11325:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
11326:                     push(@{$changes{$type}},'bcc'); 
11327:                 }
11328:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
11329:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
11330:                     push(@{$changes{$type}},'include');
11331:                 }
11332:             }
11333:         }
11334:         if (ref($fields) eq 'ARRAY') {
11335:             if (ref($currsetting{'helpform'}) eq 'HASH') {
11336:                 foreach my $field (@{$fields}) {
11337:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
11338:                         push(@{$changes{'helpform'}},$field);
11339:                     }
11340:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
11341:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
11342:                             push(@{$changes{'helpform'}},'maxsize');
11343:                         }
11344:                     }
11345:                 }
11346:             } else {
11347:                 foreach my $field (@{$fields}) {
11348:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
11349:                         push(@{$changes{'helpform'}},$field);
11350:                     }
11351:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
11352:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
11353:                             push(@{$changes{'helpform'}},'maxsize');
11354:                         }
11355:                     }
11356:                 }
11357:             }
11358:         }
11359:         if (@statuses) {
11360:             if (ref($currsetting{'overrides'}) eq 'HASH') { 
11361:                 foreach my $key (keys(%{$currsetting{'overrides'}})) {
11362:                     if (ref($currsetting{'overrides'}{$key}) eq 'HASH') {
11363:                         if (ref($newsetting{'override_'.$key}) eq 'HASH') {
11364:                             foreach my $item (@contacts,'bcc','others','include') {
11365:                                 if ($currsetting{'overrides'}{$key}{$item} ne $newsetting{'override_'.$key}{$item}) { 
11366:                                     push(@{$changes{'overrides'}},$key);
11367:                                     last;
11368:                                 }
11369:                             }
11370:                         } else {
11371:                             push(@{$changes{'overrides'}},$key);
11372:                         }
11373:                     }
11374:                 }
11375:                 foreach my $key (@overrides) {
11376:                     unless (exists($currsetting{'overrides'}{$key})) {
11377:                         push(@{$changes{'overrides'}},$key);
11378:                     }
11379:                 }
11380:             } else {
11381:                 foreach my $key (@overrides) {
11382:                     push(@{$changes{'overrides'}},$key); 
11383:                 }
11384:             }
11385:         }
11386:     } else {
11387:         my %default;
11388:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
11389:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
11390:         $default{'errormail'} = 'adminemail';
11391:         $default{'packagesmail'} = 'adminemail';
11392:         $default{'helpdeskmail'} = 'supportemail';
11393:         $default{'otherdomsmail'} = 'supportemail';
11394:         $default{'lonstatusmail'} = 'adminemail';
11395:         $default{'requestsmail'} = 'adminemail';
11396:         $default{'updatesmail'} = 'adminemail';
11397:         foreach my $item (@contacts) {
11398:            if ($to{$item} ne $default{$item}) {
11399:                $changes{$item} = 1;
11400:            }
11401:         }
11402:         foreach my $type (@mailings) {
11403:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
11404:                 push(@{$changes{$type}},@{$newsetting{$type}});
11405:             }
11406:             if ($others{$type} ne '') {
11407:                 push(@{$changes{$type}},'others');
11408:             }
11409:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11410:                 if ($bcc{$type} ne '') {
11411:                     push(@{$changes{$type}},'bcc');
11412:                 }
11413:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
11414:                     push(@{$changes{$type}},'include');
11415:                 }
11416:             }
11417:         }
11418:         if (ref($fields) eq 'ARRAY') {
11419:             foreach my $field (@{$fields}) {
11420:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
11421:                     push(@{$changes{'helpform'}},$field);
11422:                 }
11423:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
11424:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
11425:                         push(@{$changes{'helpform'}},'maxsize');
11426:                     }
11427:                 }
11428:             }
11429:         }
11430:     }
11431:     foreach my $item (@toggles) {
11432:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
11433:             $changes{$item} = 1;
11434:         } elsif ((!$env{'form.'.$item}) &&
11435:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
11436:             $changes{$item} = 1;
11437:         }
11438:     }
11439:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
11440:                                              $dom);
11441:     if ($putresult eq 'ok') {
11442:         if (keys(%changes) > 0) {
11443:             &Apache::loncommon::devalidate_domconfig_cache($dom);
11444:             if (ref($lastactref) eq 'HASH') {
11445:                 $lastactref->{'domainconfig'} = 1;
11446:             }
11447:             my ($titles,$short_titles)  = &contact_titles();
11448:             $resulttext = &mt('Changes made:').'<ul>';
11449:             foreach my $item (@contacts) {
11450:                 if ($changes{$item}) {
11451:                     $resulttext .= '<li>'.$titles->{$item}.
11452:                                     &mt(' set to: ').
11453:                                     '<span class="LC_cusr_emph">'.
11454:                                     $to{$item}.'</span></li>';
11455:                 }
11456:             }
11457:             foreach my $type (@mailings) {
11458:                 if (ref($changes{$type}) eq 'ARRAY') {
11459:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11460:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
11461:                     } else {
11462:                         $resulttext .= '<li>'.$titles->{$type}.': ';
11463:                     }
11464:                     my @text;
11465:                     foreach my $item (@{$newsetting{$type}}) {
11466:                         push(@text,$short_titles->{$item});
11467:                     }
11468:                     if ($others{$type} ne '') {
11469:                         push(@text,$others{$type});
11470:                     }
11471:                     if (@text) {
11472:                         $resulttext .= '<span class="LC_cusr_emph">'.
11473:                                        join(', ',@text).'</span>';
11474:                     }
11475:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
11476:                         if ($bcc{$type} ne '') {
11477:                             my $bcctext;
11478:                             if (@text) {
11479:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
11480:                             } else {
11481:                                 $bcctext = '(Bcc)';
11482:                             }
11483:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
11484:                         } elsif (!@text) {
11485:                             $resulttext .= &mt('No one');
11486:                         }   
11487:                         if ($includestr{$type} ne '') {
11488:                             if ($includeloc{$type} eq 'b') {
11489:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
11490:                             } elsif ($includeloc{$type} eq 's') {
11491:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
11492:                             }
11493:                         }
11494:                     } elsif (!@text) {
11495:                         $resulttext .= &mt('No recipients');
11496:                     }
11497:                     $resulttext .= '</li>';
11498:                 }
11499:             }
11500:             if (ref($changes{'overrides'}) eq 'ARRAY') {
11501:                 my @deletions;
11502:                 foreach my $type (@{$changes{'overrides'}}) {
11503:                     if ($usertypeshash{$type}) {
11504:                         if (grep(/^\Q$type\E/,@overrides)) {
11505:                             $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation set for [_1]",
11506:                                                       $usertypeshash{$type}).'<ul><li>';
11507:                             if (ref($newsetting{'override_'.$type}) eq 'HASH') {
11508:                                 my @text;
11509:                                 foreach my $item (@contacts) {
11510:                                     if ($newsetting{'override_'.$type}{$item}) { 
11511:                                         push(@text,$short_titles->{$item});
11512:                                     }
11513:                                 }
11514:                                 if ($newsetting{'override_'.$type}{'others'} ne '') {
11515:                                     push(@text,$newsetting{'override_'.$type}{'others'});
11516:                                 }
11517:   
11518:                                 if (@text) {
11519:                                     $resulttext .= &mt('Helpdesk e-mail sent to: [_1]',
11520:                                                        '<span class="LC_cusr_emph">'.join(', ',@text).'</span>');
11521:                                 }
11522:                                 if ($newsetting{'override_'.$type}{'bcc'} ne '') {
11523:                                     my $bcctext;
11524:                                     if (@text) {
11525:                                         $bcctext = '&nbsp;'.&mt('with Bcc to');
11526:                                     } else {
11527:                                         $bcctext = '(Bcc)';
11528:                                     }
11529:                                     $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$newsetting{'override_'.$type}{'bcc'}.'</span>';
11530:                                 } elsif (!@text) {
11531:                                      $resulttext .= &mt('Helpdesk e-mail sent to no one');
11532:                                 }
11533:                                 $resulttext .= '</li>';
11534:                                 if ($newsetting{'override_'.$type}{'include'} ne '') {
11535:                                     my ($loc,$str) = split(/:/,$newsetting{'override_'.$type}{'include'});
11536:                                     if ($loc eq 'b') {
11537:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail body:').' '.&unescape($str).'</li>';
11538:                                     } elsif ($loc eq 's') {
11539:                                         $resulttext .= '<li>'.&mt('Text automatically added to e-mail subject:').' '.&unescape($str).'</li>';
11540:                                     }
11541:                                 }
11542:                             }
11543:                             $resulttext .= '</li></ul></li>';
11544:                         } else {
11545:                             push(@deletions,$usertypeshash{$type});
11546:                         }
11547:                     }
11548:                 }
11549:                 if (@deletions) {
11550:                     $resulttext .= '<li>'.&mt("Overrides based on requester's affiliation discontinued for: [_1]",
11551:                                               join(', ',@deletions)).'</li>';
11552:                 }
11553:             }
11554:             my @offon = ('off','on');
11555:             if ($changes{'reporterrors'}) {
11556:                 $resulttext .= '<li>'.
11557:                                &mt('E-mail error reports to [_1] set to "'.
11558:                                    $offon[$env{'form.reporterrors'}].'".',
11559:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
11560:                                        &mt('LON-CAPA core group - MSU'),600,500)).
11561:                                '</li>';
11562:             }
11563:             if ($changes{'reportupdates'}) {
11564:                 $resulttext .= '<li>'.
11565:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
11566:                                     $offon[$env{'form.reportupdates'}].'".',
11567:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
11568:                                         &mt('LON-CAPA core group - MSU'),600,500)).
11569:                                 '</li>';
11570:             }
11571:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
11572:                 my (@optional,@required,@unused,$maxsizechg);
11573:                 foreach my $field (@{$changes{'helpform'}}) {
11574:                     if ($field eq 'maxsize') {
11575:                         $maxsizechg = 1;
11576:                         next;
11577:                     }
11578:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
11579:                         push(@optional,$field);
11580:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
11581:                         push(@unused,$field);
11582:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
11583:                         push(@required,$field);
11584:                     }
11585:                 }
11586:                 if (@optional) {
11587:                     $resulttext .= '<li>'.
11588:                                    &mt('Help form fields changed to "Optional": [_1].',
11589:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
11590:                                    '</li>';
11591:                 }
11592:                 if (@required) {
11593:                     $resulttext .= '<li>'.
11594:                                    &mt('Help form fields changed to "Required": [_1].',
11595:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
11596:                                    '</li>';
11597:                 }
11598:                 if (@unused) {
11599:                     $resulttext .= '<li>'.
11600:                                    &mt('Help form fields changed to "Not shown": [_1].',
11601:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
11602:                                    '</li>';
11603:                 }
11604:                 if ($maxsizechg) {
11605:                     $resulttext .= '<li>'.
11606:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
11607:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
11608:                                    '</li>';
11609:                 }
11610:             }
11611:             $resulttext .= '</ul>';
11612:         } else {
11613:             $resulttext = &mt('No changes made to contacts and form settings');
11614:         }
11615:     } else {
11616:         $resulttext = '<span class="LC_error">'.
11617:             &mt('An error occurred: [_1].',$putresult).'</span>';
11618:     }
11619:     return $resulttext;
11620: }
11621: 
11622: sub modify_usercreation {
11623:     my ($dom,%domconfig) = @_;
11624:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
11625:     my $warningmsg;
11626:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
11627:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
11628:             if ($key eq 'cancreate') {
11629:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
11630:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
11631:                         if (($item eq 'requestcrs') || ($item eq 'course') || ($item eq 'author')) {
11632:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11633:                         } else {
11634:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11635:                         }
11636:                     }
11637:                 }
11638:             } elsif ($key eq 'email_rule') {
11639:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
11640:             } else {
11641:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
11642:             }
11643:         }
11644:     }
11645:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
11646:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
11647:     my @contexts = ('author','course','requestcrs');
11648:     foreach my $item(@contexts) {
11649:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
11650:     }
11651:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11652:         foreach my $item (@contexts) {
11653:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
11654:                 push(@{$changes{'cancreate'}},$item);
11655:             }
11656:         }
11657:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
11658:         foreach my $item (@contexts) {
11659:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
11660:                 if ($cancreate{$item} ne 'any') {
11661:                     push(@{$changes{'cancreate'}},$item);
11662:                 }
11663:             } else {
11664:                 if ($cancreate{$item} ne 'none') {
11665:                     push(@{$changes{'cancreate'}},$item);
11666:                 }
11667:             }
11668:         }
11669:     } else {
11670:         foreach my $item (@contexts)  {
11671:             push(@{$changes{'cancreate'}},$item);
11672:         }
11673:     }
11674: 
11675:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
11676:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
11677:             if (!grep(/^\Q$type\E$/,@username_rule)) {
11678:                 push(@{$changes{'username_rule'}},$type);
11679:             }
11680:         }
11681:         foreach my $type (@username_rule) {
11682:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
11683:                 push(@{$changes{'username_rule'}},$type);
11684:             }
11685:         }
11686:     } else {
11687:         push(@{$changes{'username_rule'}},@username_rule);
11688:     }
11689: 
11690:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
11691:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
11692:             if (!grep(/^\Q$type\E$/,@id_rule)) {
11693:                 push(@{$changes{'id_rule'}},$type);
11694:             }
11695:         }
11696:         foreach my $type (@id_rule) {
11697:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
11698:                 push(@{$changes{'id_rule'}},$type);
11699:             }
11700:         }
11701:     } else {
11702:         push(@{$changes{'id_rule'}},@id_rule);
11703:     }
11704: 
11705:     my @authen_contexts = ('author','course','domain');
11706:     my @authtypes = ('int','krb4','krb5','loc');
11707:     my %authhash;
11708:     foreach my $item (@authen_contexts) {
11709:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
11710:         foreach my $auth (@authtypes) {
11711:             if (grep(/^\Q$auth\E$/,@authallowed)) {
11712:                 $authhash{$item}{$auth} = 1;
11713:             } else {
11714:                 $authhash{$item}{$auth} = 0;
11715:             }
11716:         }
11717:     }
11718:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
11719:         foreach my $item (@authen_contexts) {
11720:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
11721:                 foreach my $auth (@authtypes) {
11722:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
11723:                         push(@{$changes{'authtypes'}},$item);
11724:                         last;
11725:                     }
11726:                 }
11727:             }
11728:         }
11729:     } else {
11730:         foreach my $item (@authen_contexts) {
11731:             push(@{$changes{'authtypes'}},$item);
11732:         }
11733:     }
11734: 
11735:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
11736:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
11737:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
11738:     $save_usercreate{'id_rule'} = \@id_rule;
11739:     $save_usercreate{'username_rule'} = \@username_rule,
11740:     $save_usercreate{'authtypes'} = \%authhash;
11741: 
11742:     my %usercreation_hash =  (
11743:         usercreation     => \%save_usercreate,
11744:     );
11745: 
11746:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
11747:                                              $dom);
11748: 
11749:     if ($putresult eq 'ok') {
11750:         if (keys(%changes) > 0) {
11751:             $resulttext = &mt('Changes made:').'<ul>';
11752:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
11753:                 my %lt = &usercreation_types();
11754:                 foreach my $type (@{$changes{'cancreate'}}) {
11755:                     my $chgtext = $lt{$type}.', ';
11756:                     if ($cancreate{$type} eq 'none') {
11757:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
11758:                     } elsif ($cancreate{$type} eq 'any') {
11759:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
11760:                     } elsif ($cancreate{$type} eq 'official') {
11761:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
11762:                     } elsif ($cancreate{$type} eq 'unofficial') {
11763:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
11764:                     }
11765:                     $resulttext .= '<li>'.$chgtext.'</li>';
11766:                 }
11767:             }
11768:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
11769:                 my ($rules,$ruleorder) = 
11770:                     &Apache::lonnet::inst_userrules($dom,'username');
11771:                 my $chgtext = '<ul>';
11772:                 foreach my $type (@username_rule) {
11773:                     if (ref($rules->{$type}) eq 'HASH') {
11774:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
11775:                     }
11776:                 }
11777:                 $chgtext .= '</ul>';
11778:                 if (@username_rule > 0) {
11779:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
11780:                 } else {
11781:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
11782:                 }
11783:             }
11784:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
11785:                 my ($idrules,$idruleorder) = 
11786:                     &Apache::lonnet::inst_userrules($dom,'id');
11787:                 my $chgtext = '<ul>';
11788:                 foreach my $type (@id_rule) {
11789:                     if (ref($idrules->{$type}) eq 'HASH') {
11790:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
11791:                     }
11792:                 }
11793:                 $chgtext .= '</ul>';
11794:                 if (@id_rule > 0) {
11795:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
11796:                 } else {
11797:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
11798:                 }
11799:             }
11800:             my %authname = &authtype_names();
11801:             my %context_title = &context_names();
11802:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
11803:                 my $chgtext = '<ul>';
11804:                 foreach my $type (@{$changes{'authtypes'}}) {
11805:                     my @allowed;
11806:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
11807:                     foreach my $auth (@authtypes) {
11808:                         if ($authhash{$type}{$auth}) {
11809:                             push(@allowed,$authname{$auth});
11810:                         }
11811:                     }
11812:                     if (@allowed > 0) {
11813:                         $chgtext .= join(', ',@allowed).'</li>';
11814:                     } else {
11815:                         $chgtext .= &mt('none').'</li>';
11816:                     }
11817:                 }
11818:                 $chgtext .= '</ul>';
11819:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
11820:                 $resulttext .= '</li>';
11821:             }
11822:             $resulttext .= '</ul>';
11823:         } else {
11824:             $resulttext = &mt('No changes made to user creation settings');
11825:         }
11826:     } else {
11827:         $resulttext = '<span class="LC_error">'.
11828:             &mt('An error occurred: [_1]',$putresult).'</span>';
11829:     }
11830:     if ($warningmsg ne '') {
11831:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
11832:     }
11833:     return $resulttext;
11834: }
11835: 
11836: sub modify_selfcreation {
11837:     my ($dom,$lastactref,%domconfig) = @_;
11838:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%curr_inststatus,%changes,%cancreate);
11839:     my (%save_usercreate,%save_usermodify,%save_inststatus,@types,%usertypes);
11840:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11841:     my ($othertitle,$usertypesref,$typesref) = &Apache::loncommon::sorted_inst_types($dom);
11842:     if (ref($typesref) eq 'ARRAY') {
11843:         @types = @{$typesref};
11844:     }
11845:     if (ref($usertypesref) eq 'HASH') {
11846:         %usertypes = %{$usertypesref};
11847:     }
11848:     $usertypes{'default'} = $othertitle;
11849: #
11850: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
11851: #
11852:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
11853:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
11854:             if ($key eq 'cancreate') {
11855:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
11856:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
11857:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
11858:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
11859:                             ($item eq 'recaptchaversion') || ($item eq 'notify') ||
11860:                             ($item eq 'emailusername') || ($item eq 'shibenv') ||
11861:                             ($item eq 'selfcreateprocessing') || ($item eq 'emailverified') ||
11862:                             ($item eq 'emailoptions') || ($item eq 'emaildomain')) {
11863:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11864:                         } else {
11865:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11866:                         }
11867:                     }
11868:                 }
11869:             } elsif ($key eq 'email_rule') {
11870:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
11871:             } else {
11872:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
11873:             }
11874:         }
11875:     }
11876: #
11877: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
11878: #
11879:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
11880:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
11881:             if ($key eq 'selfcreate') {
11882:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
11883:             } else {
11884:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
11885:             }
11886:         }
11887:     }
11888: #
11889: # Retrieve current domain configuration for institutional status types from $domconfig{'inststatus'}.
11890: #
11891:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
11892:         foreach my $key (keys(%{$domconfig{'inststatus'}})) {
11893:             if ($key eq 'inststatusguest') {
11894:                 $curr_inststatus{$key} = $domconfig{'inststatus'}{$key};
11895:             } else {
11896:                 $save_inststatus{$key} = $domconfig{'inststatus'}{$key};
11897:             }
11898:         }
11899:     }
11900: 
11901:     my @contexts = ('selfcreate');
11902:     @{$cancreate{'selfcreate'}} = ();
11903:     %{$cancreate{'emailusername'}} = ();
11904:     if (@types) {
11905:         @{$cancreate{'statustocreate'}} = ();
11906:     }
11907:     %{$cancreate{'selfcreateprocessing'}} = ();
11908:     %{$cancreate{'shibenv'}} = ();
11909:     %{$cancreate{'emailverified'}} = ();
11910:     %{$cancreate{'emailoptions'}} = ();
11911:     %{$cancreate{'emaildomain'}} = ();
11912:     my %selfcreatetypes = (
11913:                              sso   => 'users authenticated by institutional single sign on',
11914:                              login => 'users authenticated by institutional log-in',
11915:                              email => 'users verified by e-mail',
11916:                           );
11917: #
11918: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
11919: # is permitted.
11920: #
11921:     my ($emailrules,$emailruleorder) = &Apache::lonnet::inst_userrules($dom,'email');
11922: 
11923:     my (@statuses,%email_rule);
11924:     foreach my $item ('login','sso','email') {
11925:         if ($item eq 'email') {
11926:             if ($env{'form.cancreate_email'}) {
11927:                 if (@types) {
11928:                     my @poss_statuses = &Apache::loncommon::get_env_multiple('form.selfassign');
11929:                     foreach my $status (@poss_statuses) {
11930:                         if (grep(/^\Q$status\E$/,(@types,'default'))) {
11931:                             push(@statuses,$status);
11932:                         }
11933:                     }
11934:                     $save_inststatus{'inststatusguest'} = \@statuses;
11935:                 } else {
11936:                     push(@statuses,'default');
11937:                 }
11938:                 if (@statuses) {
11939:                     my %curr_rule;
11940:                     if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
11941:                         foreach my $type (@statuses) {
11942:                             $curr_rule{$type} = $curr_usercreation{'email_rule'};
11943:                         }
11944:                     } elsif (ref($curr_usercreation{'email_rule'}) eq 'HASH') {
11945:                         foreach my $type (@statuses) {
11946:                             $curr_rule{$type} = $curr_usercreation{'email_rule'}{$type};
11947:                         }
11948:                     }
11949:                     push(@{$cancreate{'selfcreate'}},'email');
11950:                     push(@contexts,('selfcreateprocessing','emailverified','emailoptions'));
11951:                     my %curremaildom;
11952:                     if (ref($curr_usercreation{'cancreate'}{'emaildomain'}) eq 'HASH') {
11953:                         %curremaildom = %{$curr_usercreation{'cancreate'}{'emaildomain'}};
11954:                     }
11955:                     foreach my $type (@statuses) {
11956:                         if ($env{'form.cancreate_emailprocess_'.$type} =~ /^(?:approval|automatic)$/) {
11957:                             $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
11958:                         }
11959:                         if ($env{'form.cancreate_usernameoptions_'.$type} =~ /^(?:all|first|free)$/) {
11960:                             $cancreate{'emailverified'}{$type} = $env{'form.cancreate_usernameoptions_'.$type};
11961:                         }
11962:                         if ($env{'form.cancreate_emailoptions_'.$type} =~ /^(any|inst|noninst|custom)$/) {
11963: #
11964: # Retrieve rules (if any) governing types of e-mail address which may be used to verify a username.
11965: #
11966:                             my $chosen = $1;
11967:                             if (($chosen eq 'inst') || ($chosen eq 'noninst')) {
11968:                                 my $emaildom;
11969:                                 if ($env{'form.cancreate_emaildomain_'.$chosen.'_'.$type} =~ /^\@[^\@]+$/) {
11970:                                     $emaildom = $env{'form.cancreate_emaildomain_'.$chosen.'_'.$type}; 
11971:                                     $cancreate{'emaildomain'}{$type}{$chosen} = $emaildom;
11972:                                     if (ref($curremaildom{$type}) eq 'HASH') {
11973:                                         if (exists($curremaildom{$type}{$chosen})) {
11974:                                             if ($curremaildom{$type}{$chosen} ne $emaildom) {
11975:                                                 push(@{$changes{'cancreate'}},'emaildomain');
11976:                                             }
11977:                                         } elsif ($emaildom ne '') {
11978:                                             push(@{$changes{'cancreate'}},'emaildomain');
11979:                                         }
11980:                                     } elsif ($emaildom ne '') {
11981:                                         push(@{$changes{'cancreate'}},'emaildomain');
11982:                                     } 
11983:                                 }
11984:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
11985:                             } elsif ($chosen eq 'custom') {
11986:                                 my @possemail_rules = &Apache::loncommon::get_env_multiple('form.email_rule_'.$type);
11987:                                 $email_rule{$type} = [];
11988:                                 if (ref($emailrules) eq 'HASH') {
11989:                                     foreach my $rule (@possemail_rules) {
11990:                                         if (exists($emailrules->{$rule})) {
11991:                                             push(@{$email_rule{$type}},$rule);
11992:                                         }
11993:                                     }
11994:                                 }
11995:                                 if (@{$email_rule{$type}}) {
11996:                                     $cancreate{'emailoptions'}{$type} = 'custom';
11997:                                     if (ref($curr_rule{$type}) eq 'ARRAY') {
11998:                                         if (@{$curr_rule{$type}} > 0) {
11999:                                             foreach my $rule (@{$curr_rule{$type}}) {
12000:                                                 if (!grep(/^\Q$rule\E$/,@{$email_rule{$type}})) {
12001:                                                     push(@{$changes{'email_rule'}},$type);
12002:                                                 }
12003:                                             }
12004:                                         }
12005:                                         foreach my $type (@{$email_rule{$type}}) {
12006:                                             if (!grep(/^\Q$type\E$/,@{$curr_rule{$type}})) {
12007:                                                 push(@{$changes{'email_rule'}},$type);
12008:                                             }
12009:                                         }
12010:                                     } else {
12011:                                         push(@{$changes{'email_rule'}},$type);
12012:                                     }
12013:                                 }
12014:                             } else {
12015:                                 $cancreate{'emailoptions'}{$type} = $env{'form.cancreate_emailoptions_'.$type};
12016:                             }
12017:                         }
12018:                     }
12019:                     if (@types) {
12020:                         if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
12021:                             my @changed = &Apache::loncommon::compare_arrays(\@statuses,$curr_inststatus{'inststatusguest'});
12022:                             if (@changed) {
12023:                                 push(@{$changes{'inststatus'}},'inststatusguest');
12024:                             }
12025:                         } else {
12026:                             push(@{$changes{'inststatus'}},'inststatusguest');
12027:                         }
12028:                     }
12029:                 } else {
12030:                     delete($env{'form.cancreate_email'});
12031:                     if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
12032:                         if (@{$curr_inststatus{'inststatusguest'}} > 0) {
12033:                             push(@{$changes{'inststatus'}},'inststatusguest');
12034:                         }
12035:                     }
12036:                 }
12037:             } else {
12038:                 $save_inststatus{'inststatusguest'} = [];
12039:                 if (ref($curr_inststatus{'inststatusguest'}) eq 'ARRAY') {
12040:                     if (@{$curr_inststatus{'inststatusguest'}} > 0) {
12041:                         push(@{$changes{'inststatus'}},'inststatusguest');
12042:                     }
12043:                 }
12044:             }
12045:         } else {
12046:             if ($env{'form.cancreate_'.$item}) {
12047:                 push(@{$cancreate{'selfcreate'}},$item);
12048:             }
12049:         }
12050:     }
12051:     my (%userinfo,%savecaptcha);
12052:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
12053: #
12054: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
12055: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
12056: #
12057: 
12058:     if ($env{'form.cancreate_email'}) {
12059:         push(@contexts,'emailusername');
12060:         if (@statuses) {
12061:             foreach my $type (@statuses) {
12062:                 if (ref($infofields) eq 'ARRAY') {
12063:                     foreach my $field (@{$infofields}) {
12064:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
12065:                             $cancreate{'emailusername'}{$type}{$field} = $1;
12066:                         }
12067:                     }
12068:                 }
12069:             }
12070:         }
12071: #
12072: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
12073: # queued requests for self-creation of account verified by e-mail.
12074: #
12075: 
12076:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
12077:         @approvalnotify = sort(@approvalnotify);
12078:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
12079:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
12080:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
12081:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
12082:                     push(@{$changes{'cancreate'}},'notify');
12083:                 }
12084:             } else {
12085:                 if ($cancreate{'notify'}{'approval'}) {
12086:                     push(@{$changes{'cancreate'}},'notify');
12087:                 }
12088:             }
12089:         } elsif ($cancreate{'notify'}{'approval'}) {
12090:             push(@{$changes{'cancreate'}},'notify');
12091:         }
12092: 
12093:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
12094:     }
12095: #  
12096: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
12097: # institutional log-in.
12098: #
12099:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
12100:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
12101:                ($domdefaults{'auth_def'} eq 'localauth'))) {
12102:             $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.').' '.
12103:                           &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.');
12104:         }
12105:     }
12106:     my @fields = ('lastname','firstname','middlename','generation',
12107:                   'permanentemail','id');
12108:     my @shibfields = (@fields,'inststatus');
12109:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
12110: #
12111: # Where usernames may created for institutional log-in and/or institutional single sign on:
12112: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
12113: # may self-create accounts 
12114: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
12115: # which the user may supply, if institutional data is unavailable.
12116: #
12117:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
12118:         if (@types) {
12119:             @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
12120:             push(@contexts,'statustocreate');
12121:             foreach my $type (@types) {
12122:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
12123:                 foreach my $field (@fields) {
12124:                     if (grep(/^\Q$field\E$/,@modifiable)) {
12125:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
12126:                     } else {
12127:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
12128:                     }
12129:                 }
12130:             }
12131:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
12132:                 foreach my $type (@types) {
12133:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
12134:                         foreach my $field (@fields) {
12135:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
12136:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
12137:                                 push(@{$changes{'selfcreate'}},$type);
12138:                                 last;
12139:                             }
12140:                         }
12141:                     }
12142:                 }
12143:             } else {
12144:                 foreach my $type (@types) {
12145:                     push(@{$changes{'selfcreate'}},$type);
12146:                 }
12147:             }
12148:         }
12149:         foreach my $field (@shibfields) {
12150:             if ($env{'form.shibenv_'.$field} ne '') {
12151:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
12152:             }
12153:         }
12154:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
12155:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
12156:                 foreach my $field (@shibfields) {
12157:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
12158:                         push(@{$changes{'cancreate'}},'shibenv');
12159:                     }
12160:                 }
12161:             } else {
12162:                 foreach my $field (@shibfields) {
12163:                     if ($env{'form.shibenv_'.$field}) {
12164:                         push(@{$changes{'cancreate'}},'shibenv');
12165:                         last;
12166:                     }
12167:                 }
12168:             }
12169:         }
12170:     }
12171:     foreach my $item (@contexts) {
12172:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
12173:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
12174:                 if (ref($cancreate{$item}) eq 'ARRAY') {
12175:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
12176:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
12177:                             push(@{$changes{'cancreate'}},$item);
12178:                         }
12179:                     }
12180:                 }
12181:             }
12182:             if (ref($cancreate{$item}) eq 'ARRAY') {
12183:                 foreach my $type (@{$cancreate{$item}}) {
12184:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
12185:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
12186:                             push(@{$changes{'cancreate'}},$item);
12187:                         }
12188:                     }
12189:                 }
12190:             }
12191:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
12192:             if (ref($cancreate{$item}) eq 'HASH') {
12193:                 foreach my $type (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
12194:                     if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
12195:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$type}})) {
12196:                             unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
12197:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
12198:                                     push(@{$changes{'cancreate'}},$item);
12199:                                 }
12200:                             }
12201:                         }
12202:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
12203:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
12204:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
12205:                                 push(@{$changes{'cancreate'}},$item);
12206:                             }
12207:                         }
12208:                     }
12209:                 }
12210:                 foreach my $type (keys(%{$cancreate{$item}})) {
12211:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
12212:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
12213:                             if (ref($curr_usercreation{'cancreate'}{$item}{$type}) eq 'HASH') {
12214:                                 unless ($curr_usercreation{'cancreate'}{$item}{$type}{$field} eq $cancreate{$item}{$type}{$field}) {
12215:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
12216:                                         push(@{$changes{'cancreate'}},$item);
12217:                                     }
12218:                                 }
12219:                             } else {
12220:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
12221:                                     push(@{$changes{'cancreate'}},$item);
12222:                                 }
12223:                             }
12224:                         }
12225:                     } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
12226:                         if ($cancreate{$item}{$type} ne $curr_usercreation{'cancreate'}{$item}{$type}) {
12227:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
12228:                                 push(@{$changes{'cancreate'}},$item);
12229:                             }
12230:                         }
12231:                     }
12232:                 }
12233:             }
12234:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
12235:             if (ref($cancreate{$item}) eq 'ARRAY') {
12236:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
12237:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
12238:                         push(@{$changes{'cancreate'}},$item);
12239:                     }
12240:                 }
12241:             }
12242:         } elsif (($item eq 'selfcreateprocessing') || ($item eq 'emailverified') || ($item eq 'emailoptions')) {
12243:             if (ref($cancreate{$item}) eq 'HASH') {
12244:                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
12245:                     push(@{$changes{'cancreate'}},$item);
12246:                 }
12247:             }
12248:         } elsif ($item eq 'emailusername') {
12249:             if (ref($cancreate{$item}) eq 'HASH') {
12250:                 foreach my $type (keys(%{$cancreate{$item}})) {
12251:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
12252:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
12253:                             if ($cancreate{$item}{$type}{$field}) {
12254:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
12255:                                     push(@{$changes{'cancreate'}},$item);
12256:                                 }
12257:                                 last;
12258:                             }
12259:                         }
12260:                     }
12261:                 }
12262:             }
12263:         }
12264:     }
12265: #
12266: # Populate %save_usercreate hash with updates to self-creation configuration.
12267: #
12268:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
12269:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
12270:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
12271:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
12272:     if (ref($cancreate{'notify'}) eq 'HASH') {
12273:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
12274:     }
12275:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
12276:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
12277:     }
12278:     if (ref($cancreate{'emailverified'}) eq 'HASH') {
12279:         $save_usercreate{'cancreate'}{'emailverified'} = $cancreate{'emailverified'};
12280:     }
12281:     if (ref($cancreate{'emailoptions'}) eq 'HASH') {
12282:         $save_usercreate{'cancreate'}{'emailoptions'} = $cancreate{'emailoptions'};
12283:     }
12284:     if (ref($cancreate{'emaildomain'}) eq 'HASH') {
12285:         $save_usercreate{'cancreate'}{'emaildomain'} = $cancreate{'emaildomain'};
12286:     }
12287:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
12288:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
12289:     }
12290:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
12291:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
12292:     }
12293:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
12294:     $save_usercreate{'email_rule'} = \%email_rule;
12295: 
12296:     my %userconfig_hash = (
12297:             usercreation     => \%save_usercreate,
12298:             usermodification => \%save_usermodify,
12299:             inststatus       => \%save_inststatus,
12300:     );
12301: 
12302:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
12303:                                              $dom);
12304: #
12305: # Accumulate details of changes to domain configuration for self-creation of usernames in $resulttext
12306: #
12307:     if ($putresult eq 'ok') {
12308:         if (keys(%changes) > 0) {
12309:             $resulttext = &mt('Changes made:').'<ul>';
12310:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
12311:                 my %lt = &selfcreation_types();
12312:                 foreach my $type (@{$changes{'cancreate'}}) {
12313:                     my $chgtext = '';
12314:                     if ($type eq 'selfcreate') {
12315:                         if (@{$cancreate{$type}} == 0) {
12316:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
12317:                         } else {
12318:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
12319:                                         '<ul>';
12320:                             foreach my $case (@{$cancreate{$type}}) {
12321:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
12322:                             }
12323:                             $chgtext .= '</ul>';
12324:                             if (ref($cancreate{$type}) eq 'ARRAY') {
12325:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
12326:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
12327:                                         if (@{$cancreate{'statustocreate'}} == 0) {
12328:                                             $chgtext .= '<span class="LC_warning">'.
12329:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts via log-in or single sign-on.").
12330:                                                         '</span><br />';
12331:                                         }
12332:                                     }
12333:                                 }
12334:                                 if (grep(/^email$/,@{$cancreate{$type}})) {
12335:                                     if (!@statuses) {
12336:                                         $chgtext .= '<span class="LC_warning">'.
12337:                                                     &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.").
12338:                                                     '</span><br />';
12339: 
12340:                                     }
12341:                                 }
12342:                             }
12343:                         }
12344:                     } elsif ($type eq 'shibenv') {
12345:                         if (keys(%{$cancreate{$type}}) == 0) {
12346:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information').'<br />'; 
12347:                         } else {
12348:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
12349:                                         '<ul>';
12350:                             foreach my $field (@shibfields) {
12351:                                 next if ($cancreate{$type}{$field} eq '');
12352:                                 if ($field eq 'inststatus') {
12353:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
12354:                                 } else {
12355:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
12356:                                 }
12357:                             }
12358:                             $chgtext .= '</ul>';
12359:                         }
12360:                     } elsif ($type eq 'statustocreate') {
12361:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
12362:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
12363:                             if (@{$cancreate{'selfcreate'}} > 0) {
12364:                                 if (@{$cancreate{'statustocreate'}} == 0) {
12365:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
12366:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
12367:                                         $chgtext .= '<br />'.
12368:                                                     '<span class="LC_warning">'.
12369:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
12370:                                                     '</span>';
12371:                                     }
12372:                                 } elsif (keys(%usertypes) > 0) {
12373:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
12374:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
12375:                                     } else {
12376:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
12377:                                     }
12378:                                     $chgtext .= '<ul>';
12379:                                     foreach my $case (@{$cancreate{$type}}) {
12380:                                         if ($case eq 'default') {
12381:                                             $chgtext .= '<li>'.$othertitle.'</li>';
12382:                                         } else {
12383:                                             $chgtext .= '<li>'.$usertypes{$case}.'</li>';
12384:                                         }
12385:                                     }
12386:                                     $chgtext .= '</ul>';
12387:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
12388:                                         $chgtext .= '<span class="LC_warning">'.
12389:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
12390:                                                     '</span>';
12391:                                     }
12392:                                 }
12393:                             } else {
12394:                                 if (@{$cancreate{$type}} == 0) {
12395:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
12396:                                 } else {
12397:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
12398:                                 }
12399:                             }
12400:                             $chgtext .= '<br />';
12401:                         }
12402:                     } elsif ($type eq 'selfcreateprocessing') {
12403:                         my %choices = &Apache::lonlocal::texthash (
12404:                                                                     automatic => 'Automatic approval',
12405:                                                                     approval  => 'Queued for approval',
12406:                                                                   );
12407:                         if (@types) {
12408:                             if (@statuses) {
12409:                                 $chgtext .= &mt('Processing of requests to create account with e-mail verification set as follows:'). 
12410:                                             '<ul>';
12411:                                 foreach my $status (@statuses) {
12412:                                     if ($status eq 'default') {
12413:                                         $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
12414:                                     } else {
12415:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$status}}.'</li>';
12416:                                     }
12417:                                 }
12418:                                 $chgtext .= '</ul>';
12419:                             }
12420:                         } else {
12421:                             $chgtext .= &mt('Processing of requests to create account with e-mail verification set to: "[_1]"',
12422:                                             $choices{$cancreate{'selfcreateprocessing'}{'default'}});
12423:                         }
12424:                     } elsif ($type eq 'emailverified') {
12425:                         my %options = &Apache::lonlocal::texthash (
12426:                                                                     all   => 'Same as e-mail',
12427:                                                                     first => 'Omit @domain',
12428:                                                                     free  => 'Free to choose',
12429:                                                                   );
12430:                         if (@types) {
12431:                             if (@statuses) {
12432:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, username is set as follows:').
12433:                                             '<ul>';
12434:                                 foreach my $status (@statuses) {
12435:                                     if ($type eq 'default') {
12436:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
12437:                                     } else {
12438:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailverified'}{$status}}.'</li>';
12439:                                     }
12440:                                 }
12441:                                 $chgtext .= '</ul>';
12442:                             }
12443:                         } else {
12444:                             $chgtext .= &mt("For self-created accounts verified by e-mail address, user's username is: '[_1]'",
12445:                                             $options{$cancreate{'emailverified'}{'default'}});
12446:                         }
12447:                     } elsif ($type eq 'emailoptions') {
12448:                         my %options = &Apache::lonlocal::texthash (
12449:                                                                     any     => 'Any e-mail',
12450:                                                                     inst    => 'Institutional only',
12451:                                                                     noninst => 'Non-institutional only',
12452:                                                                     custom  => 'Custom restrictions',
12453:                                                                   );
12454:                         if (@types) {
12455:                             if (@statuses) {
12456:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, requirements for e-mail address are as follows:').
12457:                                             '<ul>';
12458:                                 foreach my $status (@statuses) {
12459:                                     if ($type eq 'default') {
12460:                                         $chgtext .= '<li>'.$othertitle.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
12461:                                     } else {
12462:                                         $chgtext .= '<li>'.$usertypes{$status}.' -- '.$options{$cancreate{'emailoptions'}{$status}}.'</li>';
12463:                                     }
12464:                                 }
12465:                                 $chgtext .= '</ul>';
12466:                             }
12467:                         } else {
12468:                             if ($cancreate{'emailoptions'}{'default'} eq 'any') {
12469:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, any e-mail may be used');
12470:                             } else {
12471:                                 $chgtext .= &mt('For self-created accounts verified by e-mail address, e-mail restricted to: "[_1]"',
12472:                                                 $options{$cancreate{'emailoptions'}{'default'}});
12473:                             }
12474:                         }
12475:                     } elsif ($type eq 'emaildomain') {
12476:                         my $output;
12477:                         if (@statuses) {
12478:                             foreach my $type (@statuses) {
12479:                                 if (ref($cancreate{'emaildomain'}{$type}) eq 'HASH') {
12480:                                     if ($cancreate{'emailoptions'}{$type} eq 'inst') {
12481:                                         if ($type eq 'default') {
12482:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
12483:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
12484:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
12485:                                             } else {
12486:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address needs to end: [_1]",
12487:                                                                                         $cancreate{'emaildomain'}{$type}{'inst'}).'</li>';
12488:                                             }
12489:                                         } else {
12490:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
12491:                                                 ($cancreate{'emaildomain'}{$type}{'inst'} eq '')) {
12492:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
12493:                                             } else {
12494:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address needs to end: [_1]",
12495:                                                                                               $cancreate{'emaildomain'}{$type}{'inst'}).'</li>'; 
12496:                                             }
12497:                                         }
12498:                                     } elsif ($cancreate{'emailoptions'}{$type} eq 'noninst') {
12499:                                         if ($type eq 'default') {
12500:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
12501:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
12502:                                                 $output = '<li>'.$othertitle.' -- '.&mt('No restriction on e-mail domain').'</li>';
12503:                                             } else {
12504:                                                 $output = '<li>'.$othertitle.' -- '.&mt("User's e-mail address must not end: [_1]",
12505:                                                                                         $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';
12506:                                             }
12507:                                         } else {
12508:                                             if ((ref($cancreate{'emaildomain'}{$type}) ne 'HASH') ||
12509:                                                 ($cancreate{'emaildomain'}{$type}{'noninst'} eq '')) {
12510:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt('No restriction on e-mail domain').'</li>';
12511:                                             } else {
12512:                                                 $output = '<li>'.$usertypes{$type}.' -- '.&mt("User's e-mail address must not end: [_1]",
12513:                                                                                                 $cancreate{'emaildomain'}{$type}{'noninst'}).'</li>';   
12514:                                             }
12515:                                         }
12516:                                     }
12517:                                 }
12518:                             }
12519:                         }
12520:                         if ($output ne '') {
12521:                             $chgtext .= &mt('For self-created accounts verified by e-mail address:').
12522:                                         '<ul>'.$output.'</ul>';
12523:                         }
12524:                     } elsif ($type eq 'captcha') {
12525:                         if ($savecaptcha{$type} eq 'notused') {
12526:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
12527:                         } else {
12528:                             my %captchas = &captcha_phrases();
12529:                             if ($captchas{$savecaptcha{$type}}) {
12530:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
12531:                             } else {
12532:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
12533:                             }
12534:                         }
12535:                     } elsif ($type eq 'recaptchakeys') {
12536:                         my ($privkey,$pubkey);
12537:                         if (ref($savecaptcha{$type}) eq 'HASH') {
12538:                             $pubkey = $savecaptcha{$type}{'public'};
12539:                             $privkey = $savecaptcha{$type}{'private'};
12540:                         }
12541:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
12542:                         if (!$pubkey) {
12543:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
12544:                         } else {
12545:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
12546:                         }
12547:                         if (!$privkey) {
12548:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
12549:                         } else {
12550:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
12551:                         }
12552:                         $chgtext .= '</ul>';
12553:                     } elsif ($type eq 'recaptchaversion') {
12554:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
12555:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
12556:                         }
12557:                     } elsif ($type eq 'emailusername') {
12558:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
12559:                             if (@statuses) {
12560:                                 foreach my $type (@statuses) {
12561:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
12562:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
12563:                                             $chgtext .= &mt('When self-creating account with e-mail verification, the following information will be provided by [_1]:',"'$usertypes{$type}'").
12564:                                                     '<ul>';
12565:                                             foreach my $field (@{$infofields}) {
12566:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
12567:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
12568:                                                 }
12569:                                             }
12570:                                             $chgtext .= '</ul>';
12571:                                         } else {
12572:                                             $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
12573:                                         }
12574:                                     } else {
12575:                                         $chgtext .= &mt('When self creating account with e-mail verification, no information besides e-mail address will be provided by [_1].',"'$usertypes{$type}'").'<br />';
12576:                                     }
12577:                                 }
12578:                             }
12579:                         }
12580:                     } elsif ($type eq 'notify') {
12581:                         my $numapprove = 0;
12582:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
12583:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
12584:                                 if ($cancreate{'notify'}{'approval'}) {
12585:                                     $chgtext .= &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
12586:                                     $numapprove ++;
12587:                                 }
12588:                             }
12589:                         }
12590:                         unless ($numapprove) {
12591:                             $chgtext .= &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
12592:                         }
12593:                     }
12594:                     if ($chgtext) {
12595:                         $resulttext .= '<li>'.$chgtext.'</li>';
12596:                     }
12597:                 }
12598:             }
12599:             if ((ref($changes{'email_rule'}) eq 'ARRAY') && (@{$changes{'email_rule'}} > 0)) {
12600:                 my ($emailrules,$emailruleorder) =
12601:                     &Apache::lonnet::inst_userrules($dom,'email');
12602:                 foreach my $type (@{$changes{'email_rule'}}) {
12603:                     if (ref($email_rule{$type}) eq 'ARRAY') {
12604:                         my $chgtext = '<ul>';
12605:                         foreach my $rule (@{$email_rule{$type}}) {
12606:                             if (ref($emailrules->{$rule}) eq 'HASH') {
12607:                                 $chgtext .= '<li>'.$emailrules->{$rule}{'name'}.'</li>';
12608:                             }
12609:                         }
12610:                         $chgtext .= '</ul>';
12611:                         my $typename;
12612:                         if (@types) {
12613:                             if ($type eq 'default') {
12614:                                 $typename = $othertitle;
12615:                             } else {
12616:                                 $typename = $usertypes{$type};
12617:                             } 
12618:                             $chgtext .= &mt('(Affiliation: [_1])',$typename);
12619:                         }
12620:                         if (@{$email_rule{$type}} > 0) {
12621:                             $resulttext .= '<li>'.
12622:                                            &mt('Accounts may not be created by users verified by e-mail, for e-mail addresses of the following types: ',
12623:                                                $usertypes{$type}).
12624:                                            $chgtext.
12625:                                            '</li>';
12626:                         } else {
12627:                             $resulttext .= '<li>'.
12628:                                            &mt('There are now no restrictions on e-mail addresses which may be used for verification when a user requests an account.').
12629:                                            '</li>'.
12630:                                            &mt('(Affiliation: [_1])',$typename);
12631:                         }
12632:                     }
12633:                 }
12634:             }
12635:             if (ref($changes{'inststatus'}) eq 'ARRAY') {
12636:                 if (ref($save_inststatus{'inststatusguest'}) eq 'ARRAY') {
12637:                     if (@{$save_inststatus{'inststatusguest'}} > 0) {
12638:                         my $chgtext = '<ul>';
12639:                         foreach my $type (@{$save_inststatus{'inststatusguest'}}) {
12640:                             $chgtext .= '<li>'.$usertypes{$type}.'</li>';
12641:                         }
12642:                         $chgtext .= '</ul>';
12643:                         $resulttext .= '<li>'.
12644:                                        &mt('A user will self-report one of the following affiliations when requesting an account verified by e-mail: ').
12645:                                           $chgtext.
12646:                                        '</li>';
12647:                     } else {
12648:                         $resulttext .= '<li>'.
12649:                                        &mt('No affiliations available for self-reporting when requesting an account verified by e-mail.').
12650:                                        '</li>';
12651:                     }
12652:                 }
12653:             }
12654:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
12655:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
12656:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
12657:                 foreach my $type (@{$changes{'selfcreate'}}) {
12658:                     my $typename = $type;
12659:                     if (keys(%usertypes) > 0) {
12660:                         if ($usertypes{$type} ne '') {
12661:                             $typename = $usertypes{$type};
12662:                         }
12663:                     }
12664:                     my @modifiable;
12665:                     $resulttext .= '<li>'.
12666:                                     &mt('Self-creation of account by users with status: [_1]',
12667:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
12668:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
12669:                     foreach my $field (@fields) {
12670:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
12671:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
12672:                         }
12673:                     }
12674:                     if (@modifiable > 0) {
12675:                         $resulttext .= join(', ',@modifiable);
12676:                     } else {
12677:                         $resulttext .= &mt('none');
12678:                     }
12679:                     $resulttext .= '</li>';
12680:                 }
12681:                 $resulttext .= '</ul></li>';
12682:             }
12683:             $resulttext .= '</ul>';
12684:             my $cachetime = 24*60*60;
12685:             $domdefaults{'inststatusguest'} = $save_inststatus{'inststatusguest'};
12686:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12687:             if (ref($lastactref) eq 'HASH') {
12688:                 $lastactref->{'domdefaults'} = 1;
12689:             }
12690:         } else {
12691:             $resulttext = &mt('No changes made to self-creation settings');
12692:         }
12693:     } else {
12694:         $resulttext = '<span class="LC_error">'.
12695:             &mt('An error occurred: [_1]',$putresult).'</span>';
12696:     }
12697:     if ($warningmsg ne '') {
12698:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
12699:     }
12700:     return $resulttext;
12701: }
12702: 
12703: sub process_captcha {
12704:     my ($container,$changes,$newsettings,$current) = @_;
12705:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
12706:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
12707:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
12708:         $newsettings->{'captcha'} = 'original';
12709:     }
12710:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
12711:         if ($container eq 'cancreate') {
12712:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
12713:                 push(@{$changes->{'cancreate'}},'captcha');
12714:             } elsif (!defined($changes->{'cancreate'})) {
12715:                 $changes->{'cancreate'} = ['captcha'];
12716:             }
12717:         } else {
12718:             $changes->{'captcha'} = 1;
12719:         }
12720:     }
12721:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
12722:     if ($newsettings->{'captcha'} eq 'recaptcha') {
12723:         $newpub = $env{'form.'.$container.'_recaptchapub'};
12724:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
12725:         $newpub =~ s/[^\w\-]//g;
12726:         $newpriv =~ s/[^\w\-]//g;
12727:         $newsettings->{'recaptchakeys'} = {
12728:                                              public  => $newpub,
12729:                                              private => $newpriv,
12730:                                           };
12731:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
12732:         $newversion =~ s/\D//g;
12733:         if ($newversion ne '2') {
12734:             $newversion = 1;
12735:         }
12736:         $newsettings->{'recaptchaversion'} = $newversion;
12737:     }
12738:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
12739:         $currpub = $current->{'recaptchakeys'}{'public'};
12740:         $currpriv = $current->{'recaptchakeys'}{'private'};
12741:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
12742:             $newsettings->{'recaptchakeys'} = {
12743:                                                  public  => '',
12744:                                                  private => '',
12745:                                               }
12746:         }
12747:     }
12748:     if ($current->{'captcha'} eq 'recaptcha') {
12749:         $currversion = $current->{'recaptchaversion'};
12750:         if ($currversion ne '2') {
12751:             $currversion = 1;
12752:         }
12753:     }
12754:     if ($currversion ne $newversion) {
12755:         if ($container eq 'cancreate') {
12756:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
12757:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
12758:             } elsif (!defined($changes->{'cancreate'})) {
12759:                 $changes->{'cancreate'} = ['recaptchaversion'];
12760:             }
12761:         } else {
12762:             $changes->{'recaptchaversion'} = 1;
12763:         }
12764:     }
12765:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
12766:         if ($container eq 'cancreate') {
12767:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
12768:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
12769:             } elsif (!defined($changes->{'cancreate'})) {
12770:                 $changes->{'cancreate'} = ['recaptchakeys'];
12771:             }
12772:         } else {
12773:             $changes->{'recaptchakeys'} = 1;
12774:         }
12775:     }
12776:     return;
12777: }
12778: 
12779: sub modify_usermodification {
12780:     my ($dom,%domconfig) = @_;
12781:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
12782:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
12783:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
12784:             if ($key eq 'selfcreate') {
12785:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
12786:             } else {  
12787:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
12788:             }
12789:         }
12790:     }
12791:     my @contexts = ('author','course');
12792:     my %context_title = (
12793:                            author => 'In author context',
12794:                            course => 'In course context',
12795:                         );
12796:     my @fields = ('lastname','firstname','middlename','generation',
12797:                   'permanentemail','id');
12798:     my %roles = (
12799:                   author => ['ca','aa'],
12800:                   course => ['st','ep','ta','in','cr'],
12801:                 );
12802:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
12803:     foreach my $context (@contexts) {
12804:         foreach my $role (@{$roles{$context}}) {
12805:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
12806:             foreach my $item (@fields) {
12807:                 if (grep(/^\Q$item\E$/,@modifiable)) {
12808:                     $modifyhash{$context}{$role}{$item} = 1;
12809:                 } else {
12810:                     $modifyhash{$context}{$role}{$item} = 0;
12811:                 }
12812:             }
12813:         }
12814:         if (ref($curr_usermodification{$context}) eq 'HASH') {
12815:             foreach my $role (@{$roles{$context}}) {
12816:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
12817:                     foreach my $field (@fields) {
12818:                         if ($modifyhash{$context}{$role}{$field} ne 
12819:                                 $curr_usermodification{$context}{$role}{$field}) {
12820:                             push(@{$changes{$context}},$role);
12821:                             last;
12822:                         }
12823:                     }
12824:                 }
12825:             }
12826:         } else {
12827:             foreach my $context (@contexts) {
12828:                 foreach my $role (@{$roles{$context}}) {
12829:                     push(@{$changes{$context}},$role);
12830:                 }
12831:             }
12832:         }
12833:     }
12834:     my %usermodification_hash =  (
12835:                                    usermodification => \%modifyhash,
12836:                                  );
12837:     my $putresult = &Apache::lonnet::put_dom('configuration',
12838:                                              \%usermodification_hash,$dom);
12839:     if ($putresult eq 'ok') {
12840:         if (keys(%changes) > 0) {
12841:             $resulttext = &mt('Changes made: ').'<ul>';
12842:             foreach my $context (@contexts) {
12843:                 if (ref($changes{$context}) eq 'ARRAY') {
12844:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
12845:                     if (ref($changes{$context}) eq 'ARRAY') {
12846:                         foreach my $role (@{$changes{$context}}) {
12847:                             my $rolename;
12848:                             if ($role eq 'cr') {
12849:                                 $rolename = &mt('Custom');
12850:                             } else {
12851:                                 $rolename = &Apache::lonnet::plaintext($role);
12852:                             }
12853:                             my @modifiable;
12854:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
12855:                             foreach my $field (@fields) {
12856:                                 if ($modifyhash{$context}{$role}{$field}) {
12857:                                     push(@modifiable,$fieldtitles{$field});
12858:                                 }
12859:                             }
12860:                             if (@modifiable > 0) {
12861:                                 $resulttext .= join(', ',@modifiable);
12862:                             } else {
12863:                                 $resulttext .= &mt('none'); 
12864:                             }
12865:                             $resulttext .= '</li>';
12866:                         }
12867:                         $resulttext .= '</ul></li>';
12868:                     }
12869:                 }
12870:             }
12871:             $resulttext .= '</ul>';
12872:         } else {
12873:             $resulttext = &mt('No changes made to user modification settings');
12874:         }
12875:     } else {
12876:         $resulttext = '<span class="LC_error">'.
12877:             &mt('An error occurred: [_1]',$putresult).'</span>';
12878:     }
12879:     return $resulttext;
12880: }
12881: 
12882: sub modify_defaults {
12883:     my ($dom,$lastactref,%domconfig) = @_;
12884:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
12885:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12886:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
12887:                  'portal_def','intauth_cost','intauth_check','intauth_switch');
12888:     my @authtypes = ('internal','krb4','krb5','localauth');
12889:     foreach my $item (@items) {
12890:         $newvalues{$item} = $env{'form.'.$item};
12891:         if ($item eq 'auth_def') {
12892:             if ($newvalues{$item} ne '') {
12893:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
12894:                     push(@errors,$item);
12895:                 }
12896:             }
12897:         } elsif ($item eq 'lang_def') {
12898:             if ($newvalues{$item} ne '') {
12899:                 if ($newvalues{$item} =~ /^(\w+)/) {
12900:                     my $langcode = $1;
12901:                     if ($langcode ne 'x_chef') {
12902:                         if (code2language($langcode) eq '') {
12903:                             push(@errors,$item);
12904:                         }
12905:                     }
12906:                 } else {
12907:                     push(@errors,$item);
12908:                 }
12909:             }
12910:         } elsif ($item eq 'timezone_def') {
12911:             if ($newvalues{$item} ne '') {
12912:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
12913:                     push(@errors,$item);   
12914:                 }
12915:             }
12916:         } elsif ($item eq 'datelocale_def') {
12917:             if ($newvalues{$item} ne '') {
12918:                 my @datelocale_ids = DateTime::Locale->ids();
12919:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
12920:                     push(@errors,$item);
12921:                 }
12922:             }
12923:         } elsif ($item eq 'portal_def') {
12924:             if ($newvalues{$item} ne '') {
12925:                 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])\/?$/) {
12926:                     push(@errors,$item);
12927:                 }
12928:             }
12929:         } elsif ($item eq 'intauth_cost') {
12930:             if ($newvalues{$item} ne '') {
12931:                 if ($newvalues{$item} =~ /\D/) {
12932:                     push(@errors,$item);
12933:                 }
12934:             }
12935:         } elsif ($item eq 'intauth_check') {
12936:             if ($newvalues{$item} ne '') {
12937:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
12938:                     push(@errors,$item);
12939:                 }
12940:             }
12941:         } elsif ($item eq 'intauth_switch') {
12942:             if ($newvalues{$item} ne '') {
12943:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
12944:                     push(@errors,$item);
12945:                 }
12946:             }
12947:         }
12948:         if (grep(/^\Q$item\E$/,@errors)) {
12949:             $newvalues{$item} = $domdefaults{$item};
12950:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
12951:             $changes{$item} = 1;
12952:         }
12953:         $domdefaults{$item} = $newvalues{$item};
12954:     }
12955:     my %defaults_hash = (
12956:                          defaults => \%newvalues,
12957:                         );
12958:     my $title = &defaults_titles();
12959: 
12960:     my $currinststatus;
12961:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
12962:         $currinststatus = $domconfig{'inststatus'};
12963:     } else {
12964:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12965:         $currinststatus = {
12966:                              inststatustypes => $usertypes,
12967:                              inststatusorder => $types,
12968:                              inststatusguest => [],
12969:                           };
12970:     }
12971:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
12972:     my @allpos;
12973:     my %alltypes;
12974:     my @inststatusguest;
12975:     if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
12976:         foreach my $type (@{$currinststatus->{'inststatusguest'}}) {
12977:             unless (grep(/^\Q$type\E$/,@todelete)) {
12978:                 push(@inststatusguest,$type);
12979:             }
12980:         }
12981:     }
12982:     my ($currtitles,$currorder);
12983:     if (ref($currinststatus) eq 'HASH') {
12984:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
12985:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
12986:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
12987:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
12988:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
12989:                     }
12990:                 }
12991:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
12992:                     my $position = $env{'form.inststatus_pos_'.$type};
12993:                     $position =~ s/\D+//g;
12994:                     $allpos[$position] = $type;
12995:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
12996:                     $alltypes{$type} =~ s/`//g;
12997:                 }
12998:             }
12999:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
13000:             $currtitles =~ s/,$//;
13001:         }
13002:     }
13003:     if ($env{'form.addinststatus'}) {
13004:         my $newtype = $env{'form.addinststatus'};
13005:         $newtype =~ s/\W//g;
13006:         unless (exists($alltypes{$newtype})) {
13007:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
13008:             $alltypes{$newtype} =~ s/`//g; 
13009:             my $position = $env{'form.addinststatus_pos'};
13010:             $position =~ s/\D+//g;
13011:             if ($position ne '') {
13012:                 $allpos[$position] = $newtype;
13013:             }
13014:         }
13015:     }
13016:     my @orderedstatus;
13017:     foreach my $type (@allpos) {
13018:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
13019:             push(@orderedstatus,$type);
13020:         }
13021:     }
13022:     foreach my $type (keys(%alltypes)) {
13023:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
13024:             delete($alltypes{$type});
13025:         }
13026:     }
13027:     $defaults_hash{'inststatus'} = {
13028:                                      inststatustypes => \%alltypes,
13029:                                      inststatusorder => \@orderedstatus,
13030:                                      inststatusguest => \@inststatusguest,
13031:                                    };
13032:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
13033:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
13034:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
13035:         }
13036:     }
13037:     if ($currorder ne join(',',@orderedstatus)) {
13038:         $changes{'inststatus'}{'inststatusorder'} = 1;
13039:     }
13040:     my $newtitles;
13041:     foreach my $item (@orderedstatus) {
13042:         $newtitles .= $alltypes{$item}.',';
13043:     }
13044:     $newtitles =~ s/,$//;
13045:     if ($currtitles ne $newtitles) {
13046:         $changes{'inststatus'}{'inststatustypes'} = 1;
13047:     }
13048:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
13049:                                              $dom);
13050:     if ($putresult eq 'ok') {
13051:         if (keys(%changes) > 0) {
13052:             $resulttext = &mt('Changes made:').'<ul>';
13053:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
13054:             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";
13055:             foreach my $item (sort(keys(%changes))) {
13056:                 if ($item eq 'inststatus') {
13057:                     if (ref($changes{'inststatus'}) eq 'HASH') {
13058:                         if (@orderedstatus) {
13059:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
13060:                             foreach my $type (@orderedstatus) { 
13061:                                 $resulttext .= $alltypes{$type}.', ';
13062:                             }
13063:                             $resulttext =~ s/, $//;
13064:                             $resulttext .= '</li>';
13065:                         } else {
13066:                             $resulttext .= '<li>'.&mt('Institutional user status types deleted').'</li>'; 
13067:                         }
13068:                     }
13069:                 } else {
13070:                     my $value = $env{'form.'.$item};
13071:                     if ($value eq '') {
13072:                         $value = &mt('none');
13073:                     } elsif ($item eq 'auth_def') {
13074:                         my %authnames = &authtype_names();
13075:                         my %shortauth = (
13076:                                           internal   => 'int',
13077:                                           krb4       => 'krb4',
13078:                                           krb5       => 'krb5',
13079:                                           localauth  => 'loc',
13080:                         );
13081:                         $value = $authnames{$shortauth{$value}};
13082:                     } elsif ($item eq 'intauth_switch') {
13083:                         my %optiondesc = &Apache::lonlocal::texthash (
13084:                                             0 => 'No',
13085:                                             1 => 'Yes',
13086:                                             2 => 'Yes, and copy existing passwd file to passwd.bak file',
13087:                                          );
13088:                         if ($value =~ /^(0|1|2)$/) {
13089:                             $value = $optiondesc{$value};
13090:                         } else {
13091:                             $value = &mt('none -- defaults to No');
13092:                         }
13093:                     } elsif ($item eq 'intauth_check') {
13094:                         my %optiondesc = &Apache::lonlocal::texthash (
13095:                                              0 => 'No',
13096:                                              1 => 'Yes, allow login then update passwd file using default cost (if higher)',
13097:                                              2 => 'Yes, disallow login if stored cost is less than domain default',
13098:                                          );
13099:                         if ($value =~ /^(0|1|2)$/) {
13100:                             $value = $optiondesc{$value};
13101:                         } else {
13102:                             $value = &mt('none -- defaults to No');
13103:                         }
13104:                     }
13105:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
13106:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
13107:                 }
13108:             }
13109:             $resulttext .= '</ul>';
13110:             $mailmsgtext .= "\n";
13111:             my $cachetime = 24*60*60;
13112:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13113:             if (ref($lastactref) eq 'HASH') {
13114:                 $lastactref->{'domdefaults'} = 1;
13115:             }
13116:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
13117:                 my $notify = 1;
13118:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
13119:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
13120:                         $notify = 0;
13121:                     }
13122:                 }
13123:                 if ($notify) {
13124:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
13125:                                                "LON-CAPA Domain Settings Change - $dom",
13126:                                                $mailmsgtext);
13127:                 }
13128:             }
13129:         } else {
13130:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
13131:         }
13132:     } else {
13133:         $resulttext = '<span class="LC_error">'.
13134:             &mt('An error occurred: [_1]',$putresult).'</span>';
13135:     }
13136:     if (@errors > 0) {
13137:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
13138:         foreach my $item (@errors) {
13139:             $resulttext .= ' "'.$title->{$item}.'",';
13140:         }
13141:         $resulttext =~ s/,$//;
13142:     }
13143:     return $resulttext;
13144: }
13145: 
13146: sub modify_scantron {
13147:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
13148:     my ($resulttext,%confhash,%changes,$errors);
13149:     my $custom = 'custom.tab';
13150:     my $default = 'default.tab';
13151:     my $servadm = $r->dir_config('lonAdmEMail');
13152:     my ($configuserok,$author_ok,$switchserver) = 
13153:         &config_check($dom,$confname,$servadm);
13154:     if ($env{'form.scantronformat.filename'} ne '') {
13155:         my $error;
13156:         if ($configuserok eq 'ok') {
13157:             if ($switchserver) {
13158:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
13159:             } else {
13160:                 if ($author_ok eq 'ok') {
13161:                     my ($result,$scantronurl) =
13162:                         &publishlogo($r,'upload','scantronformat',$dom,
13163:                                      $confname,'scantron','','',$custom);
13164:                     if ($result eq 'ok') {
13165:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
13166:                         $changes{'scantronformat'} = 1;
13167:                     } else {
13168:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
13169:                     }
13170:                 } else {
13171:                     $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);
13172:                 }
13173:             }
13174:         } else {
13175:             $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);
13176:         }
13177:         if ($error) {
13178:             &Apache::lonnet::logthis($error);
13179:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
13180:         }
13181:     }
13182:     if (ref($domconfig{'scantron'}) eq 'HASH') {
13183:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
13184:             if ($env{'form.scantronformat_del'}) {
13185:                 $confhash{'scantron'}{'scantronformat'} = '';
13186:                 $changes{'scantronformat'} = 1;
13187:             }
13188:         }
13189:     }
13190:     if (keys(%confhash) > 0) {
13191:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
13192:                                                  $dom);
13193:         if ($putresult eq 'ok') {
13194:             if (keys(%changes) > 0) {
13195:                 if (ref($confhash{'scantron'}) eq 'HASH') {
13196:                     $resulttext = &mt('Changes made:').'<ul>';
13197:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
13198:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
13199:                     } else {
13200:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
13201:                     }
13202:                     $resulttext .= '</ul>';
13203:                 } else {
13204:                     $resulttext = &mt('Changes made to bubblesheet format file.');
13205:                 }
13206:                 $resulttext .= '</ul>';
13207:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
13208:                 if (ref($lastactref) eq 'HASH') {
13209:                     $lastactref->{'domainconfig'} = 1;
13210:                 }
13211:             } else {
13212:                 $resulttext = &mt('No changes made to bubblesheet format file');
13213:             }
13214:         } else {
13215:             $resulttext = '<span class="LC_error">'.
13216:                 &mt('An error occurred: [_1]',$putresult).'</span>';
13217:         }
13218:     } else {
13219:         $resulttext = &mt('No changes made to bubblesheet format file'); 
13220:     }
13221:     if ($errors) {
13222:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
13223:                        $errors.'</ul>';
13224:     }
13225:     return $resulttext;
13226: }
13227: 
13228: sub modify_coursecategories {
13229:     my ($dom,$lastactref,%domconfig) = @_;
13230:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
13231:         $cathash);
13232:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
13233:     my @catitems = ('unauth','auth');
13234:     my @cattypes = ('std','domonly','codesrch','none');
13235:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
13236:         $cathash = $domconfig{'coursecategories'}{'cats'};
13237:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
13238:             $changes{'togglecats'} = 1;
13239:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
13240:         }
13241:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
13242:             $changes{'categorize'} = 1;
13243:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
13244:         }
13245:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
13246:             $changes{'togglecatscomm'} = 1;
13247:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
13248:         }
13249:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
13250:             $changes{'categorizecomm'} = 1;
13251:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
13252: 
13253:         }
13254:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
13255:             $changes{'togglecatsplace'} = 1;
13256:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
13257:         }
13258:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
13259:             $changes{'categorizeplace'} = 1;
13260:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
13261:         }
13262:         foreach my $item (@catitems) {
13263:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
13264:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
13265:                     $changes{$item} = 1;
13266:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
13267:                 }
13268:             }
13269:         }
13270:     } else {
13271:         $changes{'togglecats'} = 1;
13272:         $changes{'categorize'} = 1;
13273:         $changes{'togglecatscomm'} = 1;
13274:         $changes{'categorizecomm'} = 1;
13275:         $changes{'togglecatsplace'} = 1;
13276:         $changes{'categorizeplace'} = 1;
13277:         $domconfig{'coursecategories'} = {
13278:                                              togglecats => $env{'form.togglecats'},
13279:                                              categorize => $env{'form.categorize'},
13280:                                              togglecatscomm => $env{'form.togglecatscomm'},
13281:                                              categorizecomm => $env{'form.categorizecomm'},
13282:                                              togglecatsplace => $env{'form.togglecatsplace'},
13283:                                              categorizeplace => $env{'form.categorizeplace'},
13284:                                          };
13285:         foreach my $item (@catitems) {
13286:             if ($env{'form.coursecat_'.$item} ne 'std') {
13287:                 $changes{$item} = 1;
13288:             }
13289:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
13290:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
13291:             }
13292:         }
13293:     }
13294:     if (ref($cathash) eq 'HASH') {
13295:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
13296:             push (@deletecategory,'instcode::0');
13297:         }
13298:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
13299:             push(@deletecategory,'communities::0');
13300:         }
13301:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
13302:             push(@deletecategory,'placement::0');
13303:         }
13304:     }
13305:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
13306:     if (ref($cathash) eq 'HASH') {
13307:         if (@deletecategory > 0) {
13308:             #FIXME Need to remove category from all courses using a deleted category 
13309:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
13310:             foreach my $item (@deletecategory) {
13311:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
13312:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
13313:                     $deletions{$item} = 1;
13314:                     &recurse_cat_deletes($item,$cathash,\%deletions);
13315:                 }
13316:             }
13317:         }
13318:         foreach my $item (keys(%{$cathash})) {
13319:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
13320:             if ($cathash->{$item} ne $env{'form.'.$item}) {
13321:                 $reorderings{$item} = 1;
13322:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
13323:             }
13324:             if ($env{'form.addcategory_name_'.$item} ne '') {
13325:                 my $newcat = $env{'form.addcategory_name_'.$item};
13326:                 my $newdepth = $depth+1;
13327:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
13328:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
13329:                 $adds{$newitem} = 1; 
13330:             }
13331:             if ($env{'form.subcat_'.$item} ne '') {
13332:                 my $newcat = $env{'form.subcat_'.$item};
13333:                 my $newdepth = $depth+1;
13334:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
13335:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
13336:                 $adds{$newitem} = 1;
13337:             }
13338:         }
13339:     }
13340:     if ($env{'form.instcode'} eq '1') {
13341:         if (ref($cathash) eq 'HASH') {
13342:             my $newitem = 'instcode::0';
13343:             if ($cathash->{$newitem} eq '') {  
13344:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
13345:                 $adds{$newitem} = 1;
13346:             }
13347:         } else {
13348:             my $newitem = 'instcode::0';
13349:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
13350:             $adds{$newitem} = 1;
13351:         }
13352:     }
13353:     if ($env{'form.communities'} eq '1') {
13354:         if (ref($cathash) eq 'HASH') {
13355:             my $newitem = 'communities::0';
13356:             if ($cathash->{$newitem} eq '') {
13357:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
13358:                 $adds{$newitem} = 1;
13359:             }
13360:         } else {
13361:             my $newitem = 'communities::0';
13362:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
13363:             $adds{$newitem} = 1;
13364:         }
13365:     }
13366:     if ($env{'form.placement'} eq '1') {
13367:         if (ref($cathash) eq 'HASH') {
13368:             my $newitem = 'placement::0';
13369:             if ($cathash->{$newitem} eq '') {
13370:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
13371:                 $adds{$newitem} = 1;
13372:             }
13373:         } else {
13374:             my $newitem = 'placement::0';
13375:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
13376:             $adds{$newitem} = 1;
13377:         }
13378:     }
13379:     if ($env{'form.addcategory_name'} ne '') {
13380:         if (($env{'form.addcategory_name'} ne 'instcode') &&
13381:             ($env{'form.addcategory_name'} ne 'communities') &&
13382:             ($env{'form.addcategory_name'} ne 'placement')) {
13383:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
13384:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
13385:             $adds{$newitem} = 1;
13386:         }
13387:     }
13388:     my $putresult;
13389:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
13390:         if (keys(%deletions) > 0) {
13391:             foreach my $key (keys(%deletions)) {
13392:                 if ($predelallitems{$key} ne '') {
13393:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
13394:                 }
13395:             }
13396:         }
13397:         my (@chkcats,@chktrails,%chkallitems);
13398:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
13399:         if (ref($chkcats[0]) eq 'ARRAY') {
13400:             my $depth = 0;
13401:             my $chg = 0;
13402:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
13403:                 my $name = $chkcats[0][$i];
13404:                 my $item;
13405:                 if ($name eq '') {
13406:                     $chg ++;
13407:                 } else {
13408:                     $item = &escape($name).'::0';
13409:                     if ($chg) {
13410:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
13411:                     }
13412:                     $depth ++; 
13413:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
13414:                     $depth --;
13415:                 }
13416:             }
13417:         }
13418:     }
13419:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
13420:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
13421:         if ($putresult eq 'ok') {
13422:             my %title = (
13423:                          togglecats     => 'Show/Hide a course in catalog',
13424:                          categorize     => 'Assign a category to a course',
13425:                          togglecatscomm => 'Show/Hide a community in catalog',
13426:                          categorizecomm => 'Assign a category to a community',
13427:                         );
13428:             my %level = (
13429:                          dom  => 'set in Domain ("Modify Course/Community")',
13430:                          crs  => 'set in Course ("Course Configuration")',
13431:                          comm => 'set in Community ("Community Configuration")',
13432:                          none     => 'No catalog',
13433:                          std      => 'Standard catalog',
13434:                          domonly  => 'Domain-only catalog',
13435:                          codesrch => 'Code search form',
13436:                         );
13437:             $resulttext = &mt('Changes made:').'<ul>';
13438:             if ($changes{'togglecats'}) {
13439:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
13440:             }
13441:             if ($changes{'categorize'}) {
13442:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
13443:             }
13444:             if ($changes{'togglecatscomm'}) {
13445:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
13446:             }
13447:             if ($changes{'categorizecomm'}) {
13448:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
13449:             }
13450:             if ($changes{'unauth'}) {
13451:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
13452:             }
13453:             if ($changes{'auth'}) {
13454:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
13455:             }
13456:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
13457:                 my $cathash;
13458:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
13459:                     $cathash = $domconfig{'coursecategories'}{'cats'};
13460:                 } else {
13461:                     $cathash = {};
13462:                 } 
13463:                 my (@cats,@trails,%allitems);
13464:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
13465:                 if (keys(%deletions) > 0) {
13466:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
13467:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
13468:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
13469:                     }
13470:                     $resulttext .= '</ul></li>';
13471:                 }
13472:                 if (keys(%reorderings) > 0) {
13473:                     my %sort_by_trail;
13474:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
13475:                     foreach my $key (keys(%reorderings)) {
13476:                         if ($allitems{$key} ne '') {
13477:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
13478:                         }
13479:                     }
13480:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
13481:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
13482:                     }
13483:                     $resulttext .= '</ul></li>';
13484:                 }
13485:                 if (keys(%adds) > 0) {
13486:                     my %sort_by_trail;
13487:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
13488:                     foreach my $key (keys(%adds)) {
13489:                         if ($allitems{$key} ne '') {
13490:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
13491:                         }
13492:                     }
13493:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
13494:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
13495:                     }
13496:                     $resulttext .= '</ul></li>';
13497:                 }
13498:             }
13499:             $resulttext .= '</ul>';
13500:             if ($changes{'unauth'} || $changes{'auth'}) {
13501:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
13502:                 if ($changes{'auth'}) {
13503:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
13504:                 }
13505:                 if ($changes{'unauth'}) {
13506:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
13507:                 }
13508:                 my $cachetime = 24*60*60;
13509:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13510:                 if (ref($lastactref) eq 'HASH') {
13511:                     $lastactref->{'domdefaults'} = 1;
13512:                 }
13513:             }
13514:         } else {
13515:             $resulttext = '<span class="LC_error">'.
13516:                           &mt('An error occurred: [_1]',$putresult).'</span>';
13517:         }
13518:     } else {
13519:         $resulttext = &mt('No changes made to course and community categories');
13520:     }
13521:     return $resulttext;
13522: }
13523: 
13524: sub modify_serverstatuses {
13525:     my ($dom,%domconfig) = @_;
13526:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
13527:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
13528:         %currserverstatus = %{$domconfig{'serverstatuses'}};
13529:     }
13530:     my @pages = &serverstatus_pages();
13531:     foreach my $type (@pages) {
13532:         $newserverstatus{$type}{'namedusers'} = '';
13533:         $newserverstatus{$type}{'machines'} = '';
13534:         if (defined($env{'form.'.$type.'_namedusers'})) {
13535:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
13536:             my @okusers;
13537:             foreach my $user (@users) {
13538:                 my ($uname,$udom) = split(/:/,$user);
13539:                 if (($udom =~ /^$match_domain$/) &&   
13540:                     (&Apache::lonnet::domain($udom)) &&
13541:                     ($uname =~ /^$match_username$/)) {
13542:                     if (!grep(/^\Q$user\E/,@okusers)) {
13543:                         push(@okusers,$user);
13544:                     }
13545:                 }
13546:             }
13547:             if (@okusers > 0) {
13548:                  @okusers = sort(@okusers);
13549:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
13550:             }
13551:         }
13552:         if (defined($env{'form.'.$type.'_machines'})) {
13553:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
13554:             my @okmachines;
13555:             foreach my $ip (@machines) {
13556:                 my @parts = split(/\./,$ip);
13557:                 next if (@parts < 4);
13558:                 my $badip = 0;
13559:                 for (my $i=0; $i<4; $i++) {
13560:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
13561:                         $badip = 1;
13562:                         last;
13563:                     }
13564:                 }
13565:                 if (!$badip) {
13566:                     push(@okmachines,$ip);     
13567:                 }
13568:             }
13569:             @okmachines = sort(@okmachines);
13570:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
13571:         }
13572:     }
13573:     my %serverstatushash =  (
13574:                                 serverstatuses => \%newserverstatus,
13575:                             );
13576:     foreach my $type (@pages) {
13577:         foreach my $setting ('namedusers','machines') {
13578:             my (@current,@new);
13579:             if (ref($currserverstatus{$type}) eq 'HASH') {
13580:                 if ($currserverstatus{$type}{$setting} ne '') { 
13581:                     @current = split(/,/,$currserverstatus{$type}{$setting});
13582:                 }
13583:             }
13584:             if ($newserverstatus{$type}{$setting} ne '') {
13585:                 @new = split(/,/,$newserverstatus{$type}{$setting});
13586:             }
13587:             if (@current > 0) {
13588:                 if (@new > 0) {
13589:                     foreach my $item (@current) {
13590:                         if (!grep(/^\Q$item\E$/,@new)) {
13591:                             $changes{$type}{$setting} = 1;
13592:                             last;
13593:                         }
13594:                     }
13595:                     foreach my $item (@new) {
13596:                         if (!grep(/^\Q$item\E$/,@current)) {
13597:                             $changes{$type}{$setting} = 1;
13598:                             last;
13599:                         }
13600:                     }
13601:                 } else {
13602:                     $changes{$type}{$setting} = 1;
13603:                 }
13604:             } elsif (@new > 0) {
13605:                 $changes{$type}{$setting} = 1;
13606:             }
13607:         }
13608:     }
13609:     if (keys(%changes) > 0) {
13610:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
13611:         my $putresult = &Apache::lonnet::put_dom('configuration',
13612:                                                  \%serverstatushash,$dom);
13613:         if ($putresult eq 'ok') {
13614:             $resulttext .= &mt('Changes made:').'<ul>';
13615:             foreach my $type (@pages) {
13616:                 if (ref($changes{$type}) eq 'HASH') {
13617:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
13618:                     if ($changes{$type}{'namedusers'}) {
13619:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
13620:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
13621:                         } else {
13622:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
13623:                         }
13624:                     }
13625:                     if ($changes{$type}{'machines'}) {
13626:                         if ($newserverstatus{$type}{'machines'} eq '') {
13627:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
13628:                         } else {
13629:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
13630:                         }
13631: 
13632:                     }
13633:                     $resulttext .= '</ul></li>';
13634:                 }
13635:             }
13636:             $resulttext .= '</ul>';
13637:         } else {
13638:             $resulttext = '<span class="LC_error">'.
13639:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
13640: 
13641:         }
13642:     } else {
13643:         $resulttext = &mt('No changes made to access to server status pages');
13644:     }
13645:     return $resulttext;
13646: }
13647: 
13648: sub modify_helpsettings {
13649:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
13650:     my ($resulttext,$errors,%changes,%helphash);
13651:     my %defaultchecked = ('submitbugs' => 'on');
13652:     my @offon = ('off','on');
13653:     my @toggles = ('submitbugs');
13654:     my %current = ('submitbugs' => '',
13655:                    'adhoc'      => {},
13656:                   );
13657:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
13658:         %current = %{$domconfig{'helpsettings'}};
13659:     }
13660:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13661:     foreach my $item (@toggles) {
13662:         if ($defaultchecked{$item} eq 'on') { 
13663:             if ($current{$item} eq '') {
13664:                 if ($env{'form.'.$item} eq '0') {
13665:                     $changes{$item} = 1;
13666:                 }
13667:             } elsif ($current{$item} ne $env{'form.'.$item}) {
13668:                 $changes{$item} = 1;
13669:             }
13670:         } elsif ($defaultchecked{$item} eq 'off') {
13671:             if ($current{$item} eq '') {
13672:                 if ($env{'form.'.$item} eq '1') {
13673:                     $changes{$item} = 1;
13674:                 }
13675:             } elsif ($current{$item} ne $env{'form.'.$item}) {
13676:                 $changes{$item} = 1;
13677:             }
13678:         }
13679:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
13680:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
13681:         }
13682:     }
13683:     my $maxnum = $env{'form.helproles_maxnum'};
13684:     my $confname = $dom.'-domainconfig';
13685:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
13686:     my (@allpos,%newsettings,%changedprivs,$newrole);
13687:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
13688:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
13689:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
13690:     my %lt = &Apache::lonlocal::texthash(
13691:                     s      => 'system',
13692:                     d      => 'domain',
13693:                     order  => 'Display order',
13694:                     access => 'Role usage',
13695:                     all    => 'All with domain helpdesk or helpdesk assistant role',
13696:                     dh     => 'All with domain helpdesk role',
13697:                     da     => 'All with domain helpdesk assistant role',
13698:                     none   => 'None',
13699:                     status => 'Determined based on institutional status',
13700:                     inc    => 'Include all, but exclude specific personnel',
13701:                     exc    => 'Exclude all, but include specific personnel',
13702:     );
13703:     for (my $num=0; $num<=$maxnum; $num++) {
13704:         my ($prefix,$identifier,$rolename,%curr);
13705:         if ($num == $maxnum) {
13706:             next unless ($env{'form.newcusthelp'} == $maxnum);
13707:             $identifier = 'custhelp'.$num;
13708:             $prefix = 'helproles_'.$num;
13709:             $rolename = $env{'form.custhelpname'.$num};
13710:             $rolename=~s/[^A-Za-z0-9]//gs;
13711:             next if ($rolename eq '');
13712:             next if (exists($existing{'rolesdef_'.$rolename}));
13713:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
13714:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
13715:                                                      $newprivs{'c'},$confname,$dom);
13716:             if ($result ne 'ok') {
13717:                 $errors .= '<li><span class="LC_error">'.
13718:                            &mt('An error occurred storing the new custom role: [_1]',
13719:                            $result).'</span></li>';
13720:                 next;
13721:             } else {
13722:                 $changedprivs{$rolename} = \%newprivs;
13723:                 $newrole = $rolename;
13724:             }
13725:         } else {
13726:             $prefix = 'helproles_'.$num;
13727:             $rolename = $env{'form.'.$prefix};
13728:             next if ($rolename eq '');
13729:             next unless (exists($existing{'rolesdef_'.$rolename}));
13730:             $identifier = 'custhelp'.$num;
13731:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
13732:             my %currprivs;
13733:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
13734:                 split(/\_/,$existing{'rolesdef_'.$rolename});
13735:             foreach my $level ('c','d','s') {
13736:                 if ($newprivs{$level} ne $currprivs{$level}) {
13737:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
13738:                                                              $newprivs{'c'},$confname,$dom);
13739:                     if ($result ne 'ok') {
13740:                         $errors .= '<li><span class="LC_error">'.
13741:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
13742:                                        $rolename,$result).'</span></li>';
13743:                     } else {
13744:                         $changedprivs{$rolename} = \%newprivs;
13745:                     }
13746:                     last;
13747:                 }
13748:             }
13749:             if (ref($current{'adhoc'}) eq 'HASH') {
13750:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
13751:                     %curr = %{$current{'adhoc'}{$rolename}};
13752:                 }
13753:             }
13754:         }
13755:         my $newpos = $env{'form.'.$prefix.'_pos'};
13756:         $newpos =~ s/\D+//g;
13757:         $allpos[$newpos] = $rolename;
13758:         my $newdesc = $env{'form.'.$prefix.'_desc'};
13759:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
13760:         if ($curr{'desc'}) {
13761:             if ($curr{'desc'} ne $newdesc) {
13762:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
13763:                 $newsettings{$rolename}{'desc'} = $newdesc;
13764:             }
13765:         } elsif ($newdesc ne '') {
13766:             $changes{'customrole'}{$rolename}{'desc'} = 1;
13767:             $newsettings{$rolename}{'desc'} = $newdesc;
13768:         }
13769:         my $access = $env{'form.'.$prefix.'_access'};
13770:         if (grep(/^\Q$access\E$/,@accesstypes)) {
13771:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
13772:             if ($access eq 'status') {
13773:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
13774:                 if (scalar(@statuses) == 0) {
13775:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
13776:                 } else {
13777:                     my (@shownstatus,$numtypes);
13778:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
13779:                     if (ref($types) eq 'ARRAY') {
13780:                         $numtypes = scalar(@{$types});
13781:                         foreach my $type (sort(@statuses)) {
13782:                             if ($type eq 'default') {
13783:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
13784:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
13785:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
13786:                                 push(@shownstatus,$usertypes->{$type});
13787:                             }
13788:                         }
13789:                     }
13790:                     if (grep(/^default$/,@statuses)) {
13791:                         push(@shownstatus,$othertitle);
13792:                     }
13793:                     if (scalar(@shownstatus) == 1+$numtypes) {
13794:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
13795:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
13796:                     } else {
13797:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
13798:                         if (ref($curr{'status'}) eq 'ARRAY') {
13799:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
13800:                             if (@diffs) {
13801:                                 $changes{'customrole'}{$rolename}{$access} = 1;
13802:                             }
13803:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
13804:                             $changes{'customrole'}{$rolename}{$access} = 1;
13805:                         }
13806:                     }
13807:                 }
13808:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
13809:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
13810:                 my @newspecstaff;
13811:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
13812:                 foreach my $person (sort(@personnel)) {
13813:                     if ($domhelpdesk{$person}) {
13814:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
13815:                     }
13816:                 }
13817:                 if (ref($curr{$access}) eq 'ARRAY') {
13818:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
13819:                     if (@diffs) {
13820:                         $changes{'customrole'}{$rolename}{$access} = 1;
13821:                     }
13822:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
13823:                     $changes{'customrole'}{$rolename}{$access} = 1;
13824:                 }
13825:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
13826:                     my ($uname,$udom) = split(/:/,$person);
13827:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
13828:                 }
13829:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
13830:             }
13831:         } else {
13832:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
13833:         }
13834:         unless ($curr{'access'} eq $access) {
13835:             $changes{'customrole'}{$rolename}{'access'} = 1;
13836:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
13837:         }
13838:     }
13839:     if (@allpos > 0) {
13840:         my $idx = 0;
13841:         foreach my $rolename (@allpos) {
13842:             if ($rolename ne '') {
13843:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
13844:                 if (ref($current{'adhoc'}) eq 'HASH') {
13845:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
13846:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
13847:                             $changes{'customrole'}{$rolename}{'order'} = 1;
13848:                             $newsettings{$rolename}{'order'} = $idx+1;
13849:                         }
13850:                     }
13851:                 }
13852:                 $idx ++;
13853:             }
13854:         }
13855:     }
13856:     my $putresult;
13857:     if (keys(%changes) > 0) {
13858:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
13859:         if ($putresult eq 'ok') {
13860:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
13861:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
13862:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
13863:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
13864:                 }
13865:             }
13866:             my $cachetime = 24*60*60;
13867:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13868:             if (ref($lastactref) eq 'HASH') {
13869:                 $lastactref->{'domdefaults'} = 1;
13870:             }
13871:         } else {
13872:             $errors .= '<li><span class="LC_error">'.
13873:                        &mt('An error occurred storing the settings: [_1]',
13874:                            $putresult).'</span></li>';
13875:         }
13876:     }
13877:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
13878:         $resulttext = &mt('Changes made:').'<ul>';
13879:         my (%shownprivs,@levelorder);
13880:         @levelorder = ('c','d','s');
13881:         if ((keys(%changes)) && ($putresult eq 'ok')) {
13882:             foreach my $item (sort(keys(%changes))) {
13883:                 if ($item eq 'submitbugs') {
13884:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
13885:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
13886:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
13887:                 } elsif ($item eq 'customrole') {
13888:                     if (ref($changes{'customrole'}) eq 'HASH') {
13889:                         my @keyorder = ('order','desc','access','status','exc','inc');
13890:                         my %keytext = &Apache::lonlocal::texthash(
13891:                                                                    order  => 'Order',
13892:                                                                    desc   => 'Role description',
13893:                                                                    access => 'Role usage',
13894:                                                                    status => 'Allowed institutional types',
13895:                                                                    exc    => 'Allowed personnel',
13896:                                                                    inc    => 'Disallowed personnel',
13897:                         );
13898:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
13899:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
13900:                                 if ($role eq $newrole) {
13901:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
13902:                                                               $role).'<ul>';
13903:                                 } else {
13904:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
13905:                                                               $role).'<ul>';
13906:                                 }
13907:                                 foreach my $key (@keyorder) {
13908:                                     if ($changes{'customrole'}{$role}{$key}) {
13909:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
13910:                                                                   $keytext{$key},$newsettings{$role}{$key}).
13911:                                                        '</li>';
13912:                                     }
13913:                                 }
13914:                                 if (ref($changedprivs{$role}) eq 'HASH') {
13915:                                     $shownprivs{$role} = 1;
13916:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
13917:                                     foreach my $level (@levelorder) {
13918:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
13919:                                             next if ($item eq '');
13920:                                             my ($priv) = split(/\&/,$item,2);
13921:                                             if (&Apache::lonnet::plaintext($priv)) {
13922:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
13923:                                                 unless ($level eq 'c') {
13924:                                                     $resulttext .= ' ('.$lt{$level}.')';
13925:                                                 }
13926:                                                 $resulttext .= '</li>';
13927:                                             }
13928:                                         }
13929:                                     }
13930:                                     $resulttext .= '</ul>';
13931:                                 }
13932:                                 $resulttext .= '</ul></li>';
13933:                             }
13934:                         }
13935:                     }
13936:                 }
13937:             }
13938:         }
13939:         if (keys(%changedprivs)) {
13940:             foreach my $role (sort(keys(%changedprivs))) {
13941:                 unless ($shownprivs{$role}) {
13942:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
13943:                                               $role).'<ul>'.
13944:                                    '<li>'.&mt('Privileges set to :').'<ul>';
13945:                     foreach my $level (@levelorder) {
13946:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
13947:                             next if ($item eq '');
13948:                             my ($priv) = split(/\&/,$item,2);
13949:                             if (&Apache::lonnet::plaintext($priv)) {
13950:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
13951:                                 unless ($level eq 'c') {
13952:                                     $resulttext .= ' ('.$lt{$level}.')';
13953:                                 }
13954:                                 $resulttext .= '</li>';
13955:                             }
13956:                         }
13957:                     }
13958:                     $resulttext .= '</ul></li></ul></li>';
13959:                 }
13960:             }
13961:         }
13962:         $resulttext .= '</ul>';
13963:     } else {
13964:         $resulttext = &mt('No changes made to help settings');
13965:     }
13966:     if ($errors) {
13967:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
13968:                                     $errors.'</ul>';
13969:     }
13970:     return $resulttext;
13971: }
13972: 
13973: sub modify_coursedefaults {
13974:     my ($dom,$lastactref,%domconfig) = @_;
13975:     my ($resulttext,$errors,%changes,%defaultshash);
13976:     my %defaultchecked = (
13977:                            'canuse_pdfforms' => 'off',
13978:                            'uselcmath'       => 'on',
13979:                            'usejsme'         => 'on'
13980:                          );
13981:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme');
13982:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
13983:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
13984:                    'mysqltables_official','mysqltables_unofficial','mysqltables_community',
13985:                    'mysqltables_textbook','mysqltables_placement');
13986:     my @types = ('official','unofficial','community','textbook','placement');
13987:     my %staticdefaults = (
13988:                            anonsurvey_threshold => 10,
13989:                            uploadquota          => 500,
13990:                            postsubmit           => 60,
13991:                            mysqltables          => 172800,
13992:                          );
13993:     my %texoptions = (
13994:                         MathJax  => 'MathJax',
13995:                         mimetex  => &mt('Convert to Images'),
13996:                         tth      => &mt('TeX to HTML'),
13997:                      );
13998:     $defaultshash{'coursedefaults'} = {};
13999: 
14000:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
14001:         if ($domconfig{'coursedefaults'} eq '') {
14002:             $domconfig{'coursedefaults'} = {};
14003:         }
14004:     }
14005: 
14006:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
14007:         foreach my $item (@toggles) {
14008:             if ($defaultchecked{$item} eq 'on') {
14009:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
14010:                     ($env{'form.'.$item} eq '0')) {
14011:                     $changes{$item} = 1;
14012:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
14013:                     $changes{$item} = 1;
14014:                 }
14015:             } elsif ($defaultchecked{$item} eq 'off') {
14016:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
14017:                     ($env{'form.'.$item} eq '1')) {
14018:                     $changes{$item} = 1;
14019:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
14020:                     $changes{$item} = 1;
14021:                 }
14022:             }
14023:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
14024:         }
14025:         foreach my $item (@numbers) {
14026:             my ($currdef,$newdef);
14027:             $newdef = $env{'form.'.$item};
14028:             if ($item eq 'anonsurvey_threshold') {
14029:                 $currdef = $domconfig{'coursedefaults'}{$item};
14030:                 $newdef =~ s/\D//g;
14031:                 if ($newdef eq '' || $newdef < 1) {
14032:                     $newdef = 1;
14033:                 }
14034:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
14035:             } else {
14036:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
14037:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
14038:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
14039:                 }
14040:                 $newdef =~ s/[^\w.\-]//g;
14041:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
14042:             }
14043:             if ($currdef ne $newdef) {
14044:                 my $staticdef;
14045:                 if ($item eq 'anonsurvey_threshold') {
14046:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
14047:                         $changes{$item} = 1;
14048:                     }
14049:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
14050:                     my $setting = $1;
14051:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
14052:                         $changes{$setting} = 1;
14053:                     }
14054:                 }
14055:             }
14056:         }
14057:         my $texengine;
14058:         if ($env{'form.texengine'} =~ /^(MathJax|mimetex|tth)$/) {
14059:             $texengine = $env{'form.texengine'};
14060:             if ($defaultshash{'coursedefaults'}{'texengine'} eq '') {
14061:                 unless ($texengine eq 'MathJax') {
14062:                     $changes{'texengine'} = 1;
14063:                 }
14064:             } elsif ($defaultshash{'coursedefaults'}{'texengine'} ne $texengine) {
14065:                 $changes{'texengine'} = 1;
14066:             }
14067:         }
14068:         if ($texengine ne '') {
14069:             $defaultshash{'coursedefaults'}{'texengine'} = $texengine;
14070:         }
14071:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
14072:         my @currclonecode;
14073:         if (ref($currclone) eq 'HASH') {
14074:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
14075:                 @currclonecode = @{$currclone->{'instcode'}};
14076:             }
14077:         }
14078:         my $newclone;
14079:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
14080:             $newclone = $env{'form.canclone'};
14081:         }
14082:         if ($newclone eq 'instcode') {
14083:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
14084:             my (%codedefaults,@code_order,@clonecode);
14085:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
14086:                                                     \@code_order);
14087:             foreach my $item (@code_order) {
14088:                 if (grep(/^\Q$item\E$/,@newcodes)) {
14089:                     push(@clonecode,$item);
14090:                 }
14091:             }
14092:             if (@clonecode) {
14093:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
14094:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
14095:                 if (@diffs) {
14096:                     $changes{'canclone'} = 1;
14097:                 }
14098:             } else {
14099:                 $newclone eq '';
14100:             }
14101:         } elsif ($newclone ne '') {
14102:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
14103:         }
14104:         if ($newclone ne $currclone) {
14105:             $changes{'canclone'} = 1;
14106:         }
14107:         my %credits;
14108:         foreach my $type (@types) {
14109:             unless ($type eq 'community') {
14110:                 $credits{$type} = $env{'form.'.$type.'_credits'};
14111:                 $credits{$type} =~ s/[^\d.]+//g;
14112:             }
14113:         }
14114:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
14115:             ($env{'form.coursecredits'} eq '1')) {
14116:             $changes{'coursecredits'} = 1;
14117:             foreach my $type (keys(%credits)) {
14118:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
14119:             }
14120:         } else {
14121:             if ($env{'form.coursecredits'} eq '1') {
14122:                 foreach my $type (@types) {
14123:                     unless ($type eq 'community') {
14124:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
14125:                             $changes{'coursecredits'} = 1;
14126:                         }
14127:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
14128:                     }
14129:                 }
14130:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
14131:                 foreach my $type (@types) {
14132:                     unless ($type eq 'community') {
14133:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
14134:                             $changes{'coursecredits'} = 1;
14135:                             last;
14136:                         }
14137:                     }
14138:                 }
14139:             }
14140:         }
14141:         if ($env{'form.postsubmit'} eq '1') {
14142:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
14143:             my %currtimeout;
14144:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
14145:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
14146:                     $changes{'postsubmit'} = 1;
14147:                 }
14148:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
14149:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
14150:                 }
14151:             } else {
14152:                 $changes{'postsubmit'} = 1;
14153:             }
14154:             foreach my $type (@types) {
14155:                 my $timeout = $env{'form.'.$type.'_timeout'};
14156:                 $timeout =~ s/\D//g;
14157:                 if ($timeout == $staticdefaults{'postsubmit'}) {
14158:                     $timeout = '';
14159:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
14160:                     $timeout = '0';
14161:                 }
14162:                 unless ($timeout eq '') {
14163:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
14164:                 }
14165:                 if (exists($currtimeout{$type})) {
14166:                     if ($timeout ne $currtimeout{$type}) {
14167:                         $changes{'postsubmit'} = 1;
14168:                     }
14169:                 } elsif ($timeout ne '') {
14170:                     $changes{'postsubmit'} = 1;
14171:                 }
14172:             }
14173:         } else {
14174:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
14175:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
14176:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
14177:                     $changes{'postsubmit'} = 1;
14178:                 }
14179:             } else {
14180:                 $changes{'postsubmit'} = 1;
14181:             }
14182:         }
14183:     }
14184:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
14185:                                              $dom);
14186:     if ($putresult eq 'ok') {
14187:         if (keys(%changes) > 0) {
14188:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14189:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
14190:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
14191:                 ($changes{'canclone'}) || ($changes{'mysqltables'}) || ($changes{'texengine'})) {
14192:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme','texengine') {
14193:                     if ($changes{$item}) {
14194:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
14195:                     }
14196:                 }
14197:                 if ($changes{'coursecredits'}) {
14198:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
14199:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
14200:                             $domdefaults{$type.'credits'} =
14201:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
14202:                         }
14203:                     }
14204:                 }
14205:                 if ($changes{'postsubmit'}) {
14206:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
14207:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
14208:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
14209:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
14210:                                 $domdefaults{$type.'postsubtimeout'} =
14211:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
14212:                             }
14213:                         }
14214:                     }
14215:                 }
14216:                 if ($changes{'uploadquota'}) {
14217:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
14218:                         foreach my $type (@types) {
14219:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
14220:                         }
14221:                     }
14222:                 }
14223:                 if ($changes{'canclone'}) {
14224:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
14225:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
14226:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
14227:                             if (@clonecodes) {
14228:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
14229:                             }
14230:                         }
14231:                     } else {
14232:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
14233:                     }
14234:                 }
14235:                 my $cachetime = 24*60*60;
14236:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14237:                 if (ref($lastactref) eq 'HASH') {
14238:                     $lastactref->{'domdefaults'} = 1;
14239:                 }
14240:             }
14241:             $resulttext = &mt('Changes made:').'<ul>';
14242:             foreach my $item (sort(keys(%changes))) {
14243:                 if ($item eq 'canuse_pdfforms') {
14244:                     if ($env{'form.'.$item} eq '1') {
14245:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
14246:                     } else {
14247:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
14248:                     }
14249:                 } elsif ($item eq 'uselcmath') {
14250:                     if ($env{'form.'.$item} eq '1') {
14251:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
14252:                     } else {
14253:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
14254:                     }
14255:                 } elsif ($item eq 'usejsme') {
14256:                     if ($env{'form.'.$item} eq '1') {
14257:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
14258:                     } else {
14259:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
14260:                     }
14261:                 } elsif ($item eq 'texengine') {
14262:                     if ($defaultshash{'coursedefaults'}{'texengine'} ne '') {
14263:                         $resulttext .= '<li>'.&mt('Default method to display mathematics set to: "[_1]"',
14264:                                                   $texoptions{$defaultshash{'coursedefaults'}{'texengine'}}).'</li>';
14265:                     }
14266:                 } elsif ($item eq 'anonsurvey_threshold') {
14267:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
14268:                 } elsif ($item eq 'uploadquota') {
14269:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
14270:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
14271:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
14272:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
14273:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
14274:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
14275:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
14276:                                        '</ul>'.
14277:                                        '</li>';
14278:                     } else {
14279:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
14280:                     }
14281:                 } elsif ($item eq 'mysqltables') {
14282:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
14283:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
14284:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
14285:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
14286:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
14287:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
14288:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
14289:                                        '</ul>'.
14290:                                        '</li>';
14291:                     } else {
14292:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
14293:                     }
14294:                 } elsif ($item eq 'postsubmit') {
14295:                     if ($domdefaults{'postsubmit'} eq 'off') {
14296:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
14297:                     } else {
14298:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
14299:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
14300:                             $resulttext .= &mt('durations:').'<ul>';
14301:                             foreach my $type (@types) {
14302:                                 $resulttext .= '<li>';
14303:                                 my $timeout;
14304:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
14305:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
14306:                                 }
14307:                                 my $display;
14308:                                 if ($timeout eq '0') {
14309:                                     $display = &mt('unlimited');
14310:                                 } elsif ($timeout eq '') {
14311:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
14312:                                 } else {
14313:                                     $display = &mt('[quant,_1,second]',$timeout);
14314:                                 }
14315:                                 if ($type eq 'community') {
14316:                                     $resulttext .= &mt('Communities');
14317:                                 } elsif ($type eq 'official') {
14318:                                     $resulttext .= &mt('Official courses');
14319:                                 } elsif ($type eq 'unofficial') {
14320:                                     $resulttext .= &mt('Unofficial courses');
14321:                                 } elsif ($type eq 'textbook') {
14322:                                     $resulttext .= &mt('Textbook courses');
14323:                                 } elsif ($type eq 'placement') {
14324:                                     $resulttext .= &mt('Placement tests');
14325:                                 }
14326:                                 $resulttext .= ' -- '.$display.'</li>';
14327:                             }
14328:                             $resulttext .= '</ul>';
14329:                         }
14330:                         $resulttext .= '</li>';
14331:                     }
14332:                 } elsif ($item eq 'coursecredits') {
14333:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
14334:                         if (($domdefaults{'officialcredits'} eq '') &&
14335:                             ($domdefaults{'unofficialcredits'} eq '') &&
14336:                             ($domdefaults{'textbookcredits'} eq '')) {
14337:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
14338:                         } else {
14339:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
14340:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
14341:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
14342:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
14343:                                            '</ul>'.
14344:                                            '</li>';
14345:                         }
14346:                     } else {
14347:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
14348:                     }
14349:                 } elsif ($item eq 'canclone') {
14350:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
14351:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
14352:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
14353:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
14354:                         }
14355:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
14356:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
14357:                     } else {
14358:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
14359:                     }
14360:                 }
14361:             }
14362:             $resulttext .= '</ul>';
14363:         } else {
14364:             $resulttext = &mt('No changes made to course defaults');
14365:         }
14366:     } else {
14367:         $resulttext = '<span class="LC_error">'.
14368:             &mt('An error occurred: [_1]',$putresult).'</span>';
14369:     }
14370:     return $resulttext;
14371: }
14372: 
14373: sub modify_selfenrollment {
14374:     my ($dom,$lastactref,%domconfig) = @_;
14375:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
14376:     my @types = ('official','unofficial','community','textbook','placement');
14377:     my %titles = &tool_titles();
14378:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
14379:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
14380:     $ordered{'default'} = ['types','registered','approval','limit'];
14381: 
14382:     my (%roles,%shown,%toplevel);
14383:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
14384: 
14385:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
14386:         if ($domconfig{'selfenrollment'} eq '') {
14387:             $domconfig{'selfenrollment'} = {};
14388:         }
14389:     }
14390:     %toplevel = (
14391:                   admin      => 'Configuration Rights',
14392:                   default    => 'Default settings',
14393:                   validation => 'Validation of self-enrollment requests',
14394:                 );
14395:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
14396: 
14397:     if (ref($ordered{'admin'}) eq 'ARRAY') {
14398:         foreach my $item (@{$ordered{'admin'}}) {
14399:             foreach my $type (@types) {
14400:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
14401:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
14402:                 } else {
14403:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
14404:                 }
14405:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
14406:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
14407:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
14408:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
14409:                             push(@{$changes{'admin'}{$type}},$item);
14410:                         }
14411:                     } else {
14412:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
14413:                             push(@{$changes{'admin'}{$type}},$item);
14414:                         }
14415:                     }
14416:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
14417:                     push(@{$changes{'admin'}{$type}},$item);
14418:                 }
14419:             }
14420:         }
14421:     }
14422: 
14423:     foreach my $item (@{$ordered{'default'}}) {
14424:         foreach my $type (@types) {
14425:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
14426:             if ($item eq 'types') {
14427:                 unless (($value eq 'all') || ($value eq 'dom')) {
14428:                     $value = '';
14429:                 }
14430:             } elsif ($item eq 'registered') {
14431:                 unless ($value eq '1') {
14432:                     $value = 0;
14433:                 }
14434:             } elsif ($item eq 'approval') {
14435:                 unless ($value =~ /^[012]$/) {
14436:                     $value = 0;
14437:                 }
14438:             } else {
14439:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
14440:                     $value = 'none';
14441:                 }
14442:             }
14443:             $selfenrollhash{'default'}{$type}{$item} = $value;
14444:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
14445:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
14446:                     if ($selfenrollhash{'default'}{$type}{$item} ne
14447:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
14448:                          push(@{$changes{'default'}{$type}},$item);
14449:                     }
14450:                 } else {
14451:                     push(@{$changes{'default'}{$type}},$item);
14452:                 }
14453:             } else {
14454:                 push(@{$changes{'default'}{$type}},$item);
14455:             }
14456:             if ($item eq 'limit') {
14457:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
14458:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
14459:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
14460:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
14461:                     }
14462:                 } else {
14463:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
14464:                 }
14465:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
14466:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
14467:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
14468:                          push(@{$changes{'default'}{$type}},'cap');
14469:                     }
14470:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
14471:                     push(@{$changes{'default'}{$type}},'cap');
14472:                 }
14473:             }
14474:         }
14475:     }
14476: 
14477:     foreach my $item (@{$itemsref}) {
14478:         if ($item eq 'fields') {
14479:             my @changed;
14480:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
14481:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
14482:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
14483:             }
14484:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
14485:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
14486:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
14487:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
14488:                 } else {
14489:                     @changed = @{$selfenrollhash{'validation'}{$item}};
14490:                 }
14491:             } else {
14492:                 @changed = @{$selfenrollhash{'validation'}{$item}};
14493:             }
14494:             if (@changed) {
14495:                 if ($selfenrollhash{'validation'}{$item}) { 
14496:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
14497:                 } else {
14498:                     $changes{'validation'}{$item} = &mt('None');
14499:                 }
14500:             }
14501:         } else {
14502:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
14503:             if ($item eq 'markup') {
14504:                if ($env{'form.selfenroll_validation_'.$item}) {
14505:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
14506:                }
14507:             }
14508:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
14509:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
14510:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
14511:                 }
14512:             }
14513:         }
14514:     }
14515: 
14516:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
14517:                                              $dom);
14518:     if ($putresult eq 'ok') {
14519:         if (keys(%changes) > 0) {
14520:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14521:             $resulttext = &mt('Changes made:').'<ul>';
14522:             foreach my $key ('admin','default','validation') {
14523:                 if (ref($changes{$key}) eq 'HASH') {
14524:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
14525:                     if ($key eq 'validation') {
14526:                         foreach my $item (@{$itemsref}) {
14527:                             if (exists($changes{$key}{$item})) {
14528:                                 if ($item eq 'markup') {
14529:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
14530:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
14531:                                 } else {  
14532:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
14533:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
14534:                                 }
14535:                             }
14536:                         }
14537:                     } else {
14538:                         foreach my $type (@types) {
14539:                             if ($type eq 'community') {
14540:                                 $roles{'1'} = &mt('Community personnel');
14541:                             } else {
14542:                                 $roles{'1'} = &mt('Course personnel');
14543:                             }
14544:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
14545:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
14546:                                     if ($key eq 'admin') {
14547:                                         my @mgrdc = ();
14548:                                         if (ref($ordered{$key}) eq 'ARRAY') {
14549:                                             foreach my $item (@{$ordered{'admin'}}) {
14550:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
14551:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
14552:                                                         push(@mgrdc,$item);
14553:                                                     }
14554:                                                 }
14555:                                             }
14556:                                             if (@mgrdc) {
14557:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
14558:                                             } else {
14559:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
14560:                                             }
14561:                                         }
14562:                                     } else {
14563:                                         if (ref($ordered{$key}) eq 'ARRAY') {
14564:                                             foreach my $item (@{$ordered{$key}}) {
14565:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
14566:                                                     $domdefaults{$type.'selfenroll'.$item} =
14567:                                                         $selfenrollhash{$key}{$type}{$item};
14568:                                                 }
14569:                                             }
14570:                                         }
14571:                                     }
14572:                                 }
14573:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
14574:                                 foreach my $item (@{$ordered{$key}}) {
14575:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
14576:                                         $resulttext .= '<li>';
14577:                                         if ($key eq 'admin') {
14578:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
14579:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
14580:                                         } else {
14581:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
14582:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
14583:                                         }
14584:                                         $resulttext .= '</li>';
14585:                                     }
14586:                                 }
14587:                                 $resulttext .= '</ul></li>';
14588:                             }
14589:                         }
14590:                         $resulttext .= '</ul></li>'; 
14591:                     }
14592:                 }
14593:             }
14594:             if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
14595:                 my $cachetime = 24*60*60;
14596:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14597:                 if (ref($lastactref) eq 'HASH') {
14598:                     $lastactref->{'domdefaults'} = 1;
14599:                 }
14600:             }
14601:             $resulttext .= '</ul>';
14602:         } else {
14603:             $resulttext = &mt('No changes made to self-enrollment settings');
14604:         }
14605:     } else {
14606:         $resulttext = '<span class="LC_error">'.
14607:             &mt('An error occurred: [_1]',$putresult).'</span>';
14608:     }
14609:     return $resulttext;
14610: }
14611: 
14612: sub modify_usersessions {
14613:     my ($dom,$lastactref,%domconfig) = @_;
14614:     my @hostingtypes = ('version','excludedomain','includedomain');
14615:     my @offloadtypes = ('primary','default');
14616:     my %types = (
14617:                   remote => \@hostingtypes,
14618:                   hosted => \@hostingtypes,
14619:                   spares => \@offloadtypes,
14620:                 );
14621:     my @prefixes = ('remote','hosted','spares');
14622:     my @lcversions = &Apache::lonnet::all_loncaparevs();
14623:     my (%by_ip,%by_location,@intdoms,@instdoms);
14624:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
14625:     my @locations = sort(keys(%by_location));
14626:     my (%defaultshash,%changes);
14627:     foreach my $prefix (@prefixes) {
14628:         $defaultshash{'usersessions'}{$prefix} = {};
14629:     }
14630:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14631:     my $resulttext;
14632:     my %iphost = &Apache::lonnet::get_iphost();
14633:     foreach my $prefix (@prefixes) {
14634:         next if ($prefix eq 'spares');
14635:         foreach my $type (@{$types{$prefix}}) {
14636:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
14637:             if ($type eq 'version') {
14638:                 my $value = $env{'form.'.$prefix.'_'.$type};
14639:                 my $okvalue;
14640:                 if ($value ne '') {
14641:                     if (grep(/^\Q$value\E$/,@lcversions)) {
14642:                         $okvalue = $value;
14643:                     }
14644:                 }
14645:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
14646:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
14647:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
14648:                             if ($inuse == 0) {
14649:                                 $changes{$prefix}{$type} = 1;
14650:                             } else {
14651:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
14652:                                     $changes{$prefix}{$type} = 1;
14653:                                 }
14654:                                 if ($okvalue ne '') {
14655:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
14656:                                 } 
14657:                             }
14658:                         } else {
14659:                             if (($inuse == 1) && ($okvalue ne '')) {
14660:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
14661:                                 $changes{$prefix}{$type} = 1;
14662:                             }
14663:                         }
14664:                     } else {
14665:                         if (($inuse == 1) && ($okvalue ne '')) {
14666:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
14667:                             $changes{$prefix}{$type} = 1;
14668:                         }
14669:                     }
14670:                 } else {
14671:                     if (($inuse == 1) && ($okvalue ne '')) {
14672:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
14673:                         $changes{$prefix}{$type} = 1;
14674:                     }
14675:                 }
14676:             } else {
14677:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
14678:                 my @okvals;
14679:                 foreach my $val (@vals) {
14680:                     if ($val =~ /:/) {
14681:                         my @items = split(/:/,$val);
14682:                         foreach my $item (@items) {
14683:                             if (ref($by_location{$item}) eq 'ARRAY') {
14684:                                 push(@okvals,$item);
14685:                             }
14686:                         }
14687:                     } else {
14688:                         if (ref($by_location{$val}) eq 'ARRAY') {
14689:                             push(@okvals,$val);
14690:                         }
14691:                     }
14692:                 }
14693:                 @okvals = sort(@okvals);
14694:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
14695:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
14696:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
14697:                             if ($inuse == 0) {
14698:                                 $changes{$prefix}{$type} = 1; 
14699:                             } else {
14700:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
14701:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
14702:                                 if (@changed > 0) {
14703:                                     $changes{$prefix}{$type} = 1;
14704:                                 }
14705:                             }
14706:                         } else {
14707:                             if ($inuse == 1) {
14708:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
14709:                                 $changes{$prefix}{$type} = 1;
14710:                             }
14711:                         } 
14712:                     } else {
14713:                         if ($inuse == 1) {
14714:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
14715:                             $changes{$prefix}{$type} = 1;
14716:                         }
14717:                     }
14718:                 } else {
14719:                     if ($inuse == 1) {
14720:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
14721:                         $changes{$prefix}{$type} = 1;
14722:                     }
14723:                 }
14724:             }
14725:         }
14726:     }
14727: 
14728:     my @alldoms = &Apache::lonnet::all_domains();
14729:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14730:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
14731:     my $savespares;
14732: 
14733:     foreach my $lonhost (sort(keys(%servers))) {
14734:         my $serverhomeID =
14735:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
14736:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
14737:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
14738:         my %spareschg;
14739:         foreach my $type (@{$types{'spares'}}) {
14740:             my @okspares;
14741:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
14742:             foreach my $server (@checked) {
14743:                 if (&Apache::lonnet::hostname($server) ne '') {
14744:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
14745:                         unless (grep(/^\Q$server\E$/,@okspares)) {
14746:                             push(@okspares,$server);
14747:                         }
14748:                     }
14749:                 }
14750:             }
14751:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
14752:             my $newspare;
14753:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
14754:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
14755:                     $newspare = $new;
14756:                 }
14757:             }
14758:             my @spares;
14759:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
14760:                 @spares = sort(@okspares,$newspare);
14761:             } else {
14762:                 @spares = sort(@okspares);
14763:             }
14764:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
14765:             if (ref($spareid{$lonhost}) eq 'HASH') {
14766:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
14767:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
14768:                     if (@diffs > 0) {
14769:                         $spareschg{$type} = 1;
14770:                     }
14771:                 }
14772:             }
14773:         }
14774:         if (keys(%spareschg) > 0) {
14775:             $changes{'spares'}{$lonhost} = \%spareschg;
14776:         }
14777:     }
14778:     $defaultshash{'usersessions'}{'offloadnow'} = {};
14779:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
14780:     my @okoffload;
14781:     if (@offloadnow) {
14782:         foreach my $server (@offloadnow) {
14783:             if (&Apache::lonnet::hostname($server) ne '') {
14784:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
14785:                     push(@okoffload,$server);
14786:                 }
14787:             }
14788:         }
14789:         if (@okoffload) {
14790:             foreach my $lonhost (@okoffload) {
14791:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
14792:             }
14793:         }
14794:     }
14795:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
14796:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
14797:             if (ref($changes{'spares'}) eq 'HASH') {
14798:                 if (keys(%{$changes{'spares'}}) > 0) {
14799:                     $savespares = 1;
14800:                 }
14801:             }
14802:         } else {
14803:             $savespares = 1;
14804:         }
14805:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
14806:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
14807:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
14808:                     $changes{'offloadnow'} = 1;
14809:                     last;
14810:                 }
14811:             }
14812:             unless ($changes{'offloadnow'}) {
14813:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
14814:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
14815:                         $changes{'offloadnow'} = 1;
14816:                         last;
14817:                     }
14818:                 }
14819:             }
14820:         } elsif (@okoffload) {
14821:             $changes{'offloadnow'} = 1;
14822:         }
14823:     } elsif (@okoffload) {
14824:         $changes{'offloadnow'} = 1;
14825:     }
14826:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
14827:     if ((keys(%changes) > 0) || ($savespares)) {
14828:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
14829:                                                  $dom);
14830:         if ($putresult eq 'ok') {
14831:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
14832:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
14833:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
14834:                 }
14835:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
14836:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
14837:                 }
14838:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
14839:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
14840:                 }
14841:             }
14842:             my $cachetime = 24*60*60;
14843:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14844:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
14845:             if (ref($lastactref) eq 'HASH') {
14846:                 $lastactref->{'domdefaults'} = 1;
14847:                 $lastactref->{'usersessions'} = 1;
14848:             }
14849:             if (keys(%changes) > 0) {
14850:                 my %lt = &usersession_titles();
14851:                 $resulttext = &mt('Changes made:').'<ul>';
14852:                 foreach my $prefix (@prefixes) {
14853:                     if (ref($changes{$prefix}) eq 'HASH') {
14854:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
14855:                         if ($prefix eq 'spares') {
14856:                             if (ref($changes{$prefix}) eq 'HASH') {
14857:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
14858:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
14859:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
14860:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
14861:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
14862:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
14863:                                         foreach my $type (@{$types{$prefix}}) {
14864:                                             if ($changes{$prefix}{$lonhost}{$type}) {
14865:                                                 my $offloadto = &mt('None');
14866:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
14867:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
14868:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
14869:                                                     }
14870:                                                 }
14871:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
14872:                                             }
14873:                                         }
14874:                                     }
14875:                                     $resulttext .= '</li>';
14876:                                 }
14877:                             }
14878:                         } else {
14879:                             foreach my $type (@{$types{$prefix}}) {
14880:                                 if (defined($changes{$prefix}{$type})) {
14881:                                     my $newvalue;
14882:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
14883:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
14884:                                             if ($type eq 'version') {
14885:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
14886:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
14887:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
14888:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
14889:                                                 }
14890:                                             }
14891:                                         }
14892:                                     }
14893:                                     if ($newvalue eq '') {
14894:                                         if ($type eq 'version') {
14895:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
14896:                                         } else {
14897:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
14898:                                         }
14899:                                     } else {
14900:                                         if ($type eq 'version') {
14901:                                             $newvalue .= ' '.&mt('(or later)'); 
14902:                                         }
14903:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
14904:                                     }
14905:                                 }
14906:                             }
14907:                         }
14908:                         $resulttext .= '</ul>';
14909:                     }
14910:                 }
14911:                 if ($changes{'offloadnow'}) {
14912:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
14913:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
14914:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
14915:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
14916:                                 $resulttext .= '<li>'.$lonhost.'</li>';
14917:                             }
14918:                             $resulttext .= '</ul>';
14919:                         } else {
14920:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
14921:                         }
14922:                     } else {
14923:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
14924:                     }
14925:                 }
14926:                 $resulttext .= '</ul>';
14927:             } else {
14928:                 $resulttext = $nochgmsg;
14929:             }
14930:         } else {
14931:             $resulttext = '<span class="LC_error">'.
14932:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14933:         }
14934:     } else {
14935:         $resulttext = $nochgmsg;
14936:     }
14937:     return $resulttext;
14938: }
14939: 
14940: sub modify_ssl {
14941:     my ($dom,$lastactref,%domconfig) = @_;
14942:     my (%by_ip,%by_location,@intdoms,@instdoms);
14943:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
14944:     my @locations = sort(keys(%by_location));
14945:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14946:     my (%defaultshash,%changes);
14947:     my $action = 'ssl';
14948:     my @prefixes = ('connto','connfrom','replication');
14949:     foreach my $prefix (@prefixes) {
14950:         $defaultshash{$action}{$prefix} = {};
14951:     }
14952:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14953:     my $resulttext;
14954:     my %iphost = &Apache::lonnet::get_iphost();
14955:     my @reptypes = ('certreq','nocertreq');
14956:     my @connecttypes = ('dom','intdom','other');
14957:     my %types = (
14958:                   connto      => \@connecttypes,
14959:                   connfrom    => \@connecttypes,
14960:                   replication => \@reptypes,
14961:                 );
14962:     foreach my $prefix (sort(keys(%types))) {
14963:         foreach my $type (@{$types{$prefix}}) {
14964:             if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
14965:                 my $value = 'yes';
14966:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
14967:                     $value = $env{'form.'.$prefix.'_'.$type};
14968:                 }
14969:                 if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
14970:                     if ($domconfig{$action}{$prefix}{$type} ne '') {
14971:                         if ($value ne $domconfig{$action}{$prefix}{$type}) {
14972:                             $changes{$prefix}{$type} = 1;
14973:                         }
14974:                         $defaultshash{$action}{$prefix}{$type} = $value;
14975:                     } else {
14976:                         $defaultshash{$action}{$prefix}{$type} = $value;
14977:                         $changes{$prefix}{$type} = 1;
14978:                     }
14979:                 } else {
14980:                     $defaultshash{$action}{$prefix}{$type} = $value;
14981:                     $changes{$prefix}{$type} = 1;
14982:                 }
14983:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
14984:                     delete($changes{$prefix}{$type});
14985:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
14986:                     delete($changes{$prefix}{$type});
14987:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
14988:                     delete($changes{$prefix}{$type});
14989:                 }
14990:             } elsif ($prefix eq 'replication') {
14991:                 if (@locations > 0) {
14992:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
14993:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
14994:                     my @okvals;
14995:                     foreach my $val (@vals) {
14996:                         if ($val =~ /:/) {
14997:                             my @items = split(/:/,$val);
14998:                             foreach my $item (@items) {
14999:                                 if (ref($by_location{$item}) eq 'ARRAY') {
15000:                                     push(@okvals,$item);
15001:                                 }
15002:                             }
15003:                         } else {
15004:                             if (ref($by_location{$val}) eq 'ARRAY') {
15005:                                 push(@okvals,$val);
15006:                             }
15007:                         }
15008:                     }
15009:                     @okvals = sort(@okvals);
15010:                     if (ref($domconfig{$action}) eq 'HASH') {
15011:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
15012:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
15013:                                 if ($inuse == 0) {
15014:                                     $changes{$prefix}{$type} = 1;
15015:                                 } else {
15016:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
15017:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
15018:                                     if (@changed > 0) {
15019:                                         $changes{$prefix}{$type} = 1;
15020:                                     }
15021:                                 }
15022:                             } else {
15023:                                 if ($inuse == 1) {
15024:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
15025:                                     $changes{$prefix}{$type} = 1;
15026:                                 }
15027:                             }
15028:                         } else {
15029:                             if ($inuse == 1) {
15030:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
15031:                                 $changes{$prefix}{$type} = 1;
15032:                             }
15033:                         }
15034:                     } else {
15035:                         if ($inuse == 1) {
15036:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
15037:                             $changes{$prefix}{$type} = 1;
15038:                         }
15039:                     }
15040:                 }
15041:             }
15042:         }
15043:     }
15044:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
15045:     if (keys(%changes) > 0) {
15046:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
15047:                                                  $dom);
15048:         if ($putresult eq 'ok') {
15049:             if (ref($defaultshash{$action}) eq 'HASH') {
15050:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
15051:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
15052:                 }
15053:                 if (ref($defaultshash{$action}{'connto'}) eq 'HASH') {
15054:                     $domdefaults{'connto'} = $domconfig{$action}{'connto'};
15055:                 }
15056:                 if (ref($defaultshash{$action}{'connfrom'}) eq 'HASH') {
15057:                     $domdefaults{'connfrom'} = $domconfig{$action}{'connfrom'};
15058:                 }
15059:             }
15060:             my $cachetime = 24*60*60;
15061:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15062:             if (ref($lastactref) eq 'HASH') {
15063:                 $lastactref->{'domdefaults'} = 1;
15064:             }
15065:             if (keys(%changes) > 0) {
15066:                 my %titles = &ssl_titles();
15067:                 $resulttext = &mt('Changes made:').'<ul>';
15068:                 foreach my $prefix (@prefixes) {
15069:                     if (ref($changes{$prefix}) eq 'HASH') {
15070:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
15071:                         foreach my $type (@{$types{$prefix}}) {
15072:                             if (defined($changes{$prefix}{$type})) {
15073:                                 my $newvalue;
15074:                                 if (ref($defaultshash{$action}) eq 'HASH') {
15075:                                     if (ref($defaultshash{$action}{$prefix})) {
15076:                                         if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
15077:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
15078:                                         } elsif (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
15079:                                             if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
15080:                                                 $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
15081:                                             }
15082:                                         }
15083:                                     }
15084:                                     if ($newvalue eq '') {
15085:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
15086:                                     } else {
15087:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
15088:                                     }
15089:                                 }
15090:                             }
15091:                         }
15092:                         $resulttext .= '</ul>';
15093:                     }
15094:                 }
15095:             } else {
15096:                 $resulttext = $nochgmsg;
15097:             }
15098:         } else {
15099:             $resulttext = '<span class="LC_error">'.
15100:                           &mt('An error occurred: [_1]',$putresult).'</span>';
15101:         }
15102:     } else {
15103:         $resulttext = $nochgmsg;
15104:     }
15105:     return $resulttext;
15106: }
15107: 
15108: sub modify_trust {
15109:     my ($dom,$lastactref,%domconfig) = @_;
15110:     my (%by_ip,%by_location,@intdoms,@instdoms);
15111:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
15112:     my @locations = sort(keys(%by_location));
15113:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
15114:     my @types = ('exc','inc');
15115:     my (%defaultshash,%changes);
15116:     foreach my $prefix (@prefixes) {
15117:         $defaultshash{'trust'}{$prefix} = {};
15118:     }
15119:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
15120:     my $resulttext;
15121:     foreach my $prefix (@prefixes) {
15122:         foreach my $type (@types) {
15123:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
15124:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
15125:             my @okvals;
15126:             foreach my $val (@vals) {
15127:                 if ($val =~ /:/) {
15128:                     my @items = split(/:/,$val);
15129:                     foreach my $item (@items) {
15130:                         if (ref($by_location{$item}) eq 'ARRAY') {
15131:                             push(@okvals,$item);
15132:                         }
15133:                     }
15134:                 } else {
15135:                     if (ref($by_location{$val}) eq 'ARRAY') {
15136:                         push(@okvals,$val);
15137:                     }
15138:                 }
15139:             }
15140:             @okvals = sort(@okvals);
15141:             if (ref($domconfig{'trust'}) eq 'HASH') {
15142:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
15143:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
15144:                         if ($inuse == 0) {
15145:                             $changes{$prefix}{$type} = 1;
15146:                         } else {
15147:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
15148:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
15149:                             if (@changed > 0) {
15150:                                 $changes{$prefix}{$type} = 1;
15151:                             }
15152:                         }
15153:                     } else {
15154:                         if ($inuse == 1) {
15155:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
15156:                             $changes{$prefix}{$type} = 1;
15157:                         }
15158:                     }
15159:                 } else {
15160:                     if ($inuse == 1) {
15161:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
15162:                         $changes{$prefix}{$type} = 1;
15163:                     }
15164:                 }
15165:             } else {
15166:                 if ($inuse == 1) {
15167:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
15168:                     $changes{$prefix}{$type} = 1;
15169:                 }
15170:             }
15171:         }
15172:     }
15173:     my $nochgmsg = &mt('No changes made to trust settings.');
15174:     if (keys(%changes) > 0) {
15175:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
15176:                                                  $dom);
15177:         if ($putresult eq 'ok') {
15178:             if (ref($defaultshash{'trust'}) eq 'HASH') {
15179:                 foreach my $prefix (@prefixes) {
15180:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
15181:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
15182:                     }
15183:                 }
15184:             }
15185:             my $cachetime = 24*60*60;
15186:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
15187:             if (ref($lastactref) eq 'HASH') {
15188:                 $lastactref->{'domdefaults'} = 1;
15189:             }
15190:             if (keys(%changes) > 0) {
15191:                 my %lt = &trust_titles();
15192:                 $resulttext = &mt('Changes made:').'<ul>';
15193:                 foreach my $prefix (@prefixes) {
15194:                     if (ref($changes{$prefix}) eq 'HASH') {
15195:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
15196:                         foreach my $type (@types) {
15197:                             if (defined($changes{$prefix}{$type})) {
15198:                                 my $newvalue;
15199:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
15200:                                     if (ref($defaultshash{'trust'}{$prefix})) {
15201:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
15202:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
15203:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
15204:                                             }
15205:                                         }
15206:                                     }
15207:                                 }
15208:                                 if ($newvalue eq '') {
15209:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
15210:                                 } else {
15211:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
15212:                                 }
15213:                             }
15214:                         }
15215:                         $resulttext .= '</ul>';
15216:                     }
15217:                 }
15218:                 $resulttext .= '</ul>';
15219:             } else {
15220:                 $resulttext = $nochgmsg;
15221:             }
15222:         } else {
15223:             $resulttext = '<span class="LC_error">'.
15224:                           &mt('An error occurred: [_1]',$putresult).'</span>';
15225:         }
15226:     } else {
15227:         $resulttext = $nochgmsg;
15228:     }
15229:     return $resulttext;
15230: }
15231: 
15232: sub modify_loadbalancing {
15233:     my ($dom,%domconfig) = @_;
15234:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
15235:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
15236:     my ($othertitle,$usertypes,$types) =
15237:         &Apache::loncommon::sorted_inst_types($dom);
15238:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
15239:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
15240:     my @sparestypes = ('primary','default');
15241:     my %typetitles = &sparestype_titles();
15242:     my $resulttext;
15243:     my (%currbalancer,%currtargets,%currrules,%existing);
15244:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
15245:         %existing = %{$domconfig{'loadbalancing'}};
15246:     }
15247:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
15248:                               \%currtargets,\%currrules);
15249:     my ($saveloadbalancing,%defaultshash,%changes);
15250:     my ($alltypes,$othertypes,$titles) =
15251:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
15252:     my %ruletitles = &offloadtype_text();
15253:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
15254:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
15255:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
15256:         if ($balancer eq '') {
15257:             next;
15258:         }
15259:         if (!exists($servers{$balancer})) {
15260:             if (exists($currbalancer{$balancer})) {
15261:                 push(@{$changes{'delete'}},$balancer);
15262:             }
15263:             next;
15264:         }
15265:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
15266:             push(@{$changes{'delete'}},$balancer);
15267:             next;
15268:         }
15269:         if (!exists($currbalancer{$balancer})) {
15270:             push(@{$changes{'add'}},$balancer);
15271:         }
15272:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
15273:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
15274:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
15275:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
15276:             $saveloadbalancing = 1;
15277:         }
15278:         foreach my $sparetype (@sparestypes) {
15279:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
15280:             my @offloadto;
15281:             foreach my $target (@targets) {
15282:                 if (($servers{$target}) && ($target ne $balancer)) {
15283:                     if ($sparetype eq 'default') {
15284:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
15285:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
15286:                         }
15287:                     }
15288:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
15289:                         push(@offloadto,$target);
15290:                     }
15291:                 }
15292:             }
15293:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
15294:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
15295:                     push(@offloadto,$balancer);
15296:                 }
15297:             }
15298:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
15299:         }
15300:         if (ref($currtargets{$balancer}) eq 'HASH') {
15301:             foreach my $sparetype (@sparestypes) {
15302:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
15303:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
15304:                     if (@targetdiffs > 0) {
15305:                         $changes{'curr'}{$balancer}{'targets'} = 1;
15306:                     }
15307:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
15308:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
15309:                         $changes{'curr'}{$balancer}{'targets'} = 1;
15310:                     }
15311:                 }
15312:             }
15313:         } else {
15314:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
15315:                 foreach my $sparetype (@sparestypes) {
15316:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
15317:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
15318:                             $changes{'curr'}{$balancer}{'targets'} = 1;
15319:                         }
15320:                     }
15321:                 }
15322:             }
15323:         }
15324:         my $ishomedom;
15325:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
15326:             $ishomedom = 1;
15327:         }
15328:         if (ref($alltypes) eq 'ARRAY') {
15329:             foreach my $type (@{$alltypes}) {
15330:                 my $rule;
15331:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
15332:                          (!$ishomedom)) {
15333:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
15334:                 }
15335:                 if ($rule eq 'specific') {
15336:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
15337:                     if (exists($servers{$specifiedhost})) {
15338:                         $rule = $specifiedhost;
15339:                     }
15340:                 }
15341:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
15342:                 if (ref($currrules{$balancer}) eq 'HASH') {
15343:                     if ($rule ne $currrules{$balancer}{$type}) {
15344:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
15345:                     }
15346:                 } elsif ($rule ne '') {
15347:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
15348:                 }
15349:             }
15350:         }
15351:     }
15352:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
15353:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
15354:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
15355:             $defaultshash{'loadbalancing'} = {};
15356:         }
15357:         my $putresult = &Apache::lonnet::put_dom('configuration',
15358:                                                  \%defaultshash,$dom);
15359:         if ($putresult eq 'ok') {
15360:             if (keys(%changes) > 0) {
15361:                 my %toupdate;
15362:                 if (ref($changes{'delete'}) eq 'ARRAY') {
15363:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
15364:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
15365:                         $toupdate{$balancer} = 1;
15366:                     }
15367:                 }
15368:                 if (ref($changes{'add'}) eq 'ARRAY') {
15369:                     foreach my $balancer (sort(@{$changes{'add'}})) {
15370:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
15371:                         $toupdate{$balancer} = 1;
15372:                     }
15373:                 }
15374:                 if (ref($changes{'curr'}) eq 'HASH') {
15375:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
15376:                         $toupdate{$balancer} = 1;
15377:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
15378:                             if ($changes{'curr'}{$balancer}{'targets'}) {
15379:                                 my %offloadstr;
15380:                                 foreach my $sparetype (@sparestypes) {
15381:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
15382:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
15383:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
15384:                                         }
15385:                                     }
15386:                                 }
15387:                                 if (keys(%offloadstr) == 0) {
15388:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
15389:                                 } else {
15390:                                     my $showoffload;
15391:                                     foreach my $sparetype (@sparestypes) {
15392:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
15393:                                         if (defined($offloadstr{$sparetype})) {
15394:                                             $showoffload .= $offloadstr{$sparetype};
15395:                                         } else {
15396:                                             $showoffload .= &mt('None');
15397:                                         }
15398:                                         $showoffload .= ('&nbsp;'x3);
15399:                                     }
15400:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
15401:                                 }
15402:                             }
15403:                         }
15404:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
15405:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
15406:                                 foreach my $type (@{$alltypes}) {
15407:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
15408:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
15409:                                         my $balancetext;
15410:                                         if ($rule eq '') {
15411:                                             $balancetext =  $ruletitles{'default'};
15412:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
15413:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
15414:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
15415:                                                 foreach my $sparetype (@sparestypes) {
15416:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
15417:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
15418:                                                     }
15419:                                                 }
15420:                                                 foreach my $item (@{$alltypes}) {
15421:                                                     next if ($item =~  /^_LC_ipchange/);
15422:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
15423:                                                     if ($hasrule eq 'homeserver') {
15424:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
15425:                                                     } else {
15426:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
15427:                                                             if ($servers{$hasrule}) {
15428:                                                                 $toupdate{$hasrule} = 1;
15429:                                                             }
15430:                                                         }
15431:                                                     }
15432:                                                 }
15433:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
15434:                                                     $balancetext =  $ruletitles{$rule};
15435:                                                 } else {
15436:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
15437:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
15438:                                                     if ($receiver) {
15439:                                                         $toupdate{$receiver};
15440:                                                     }
15441:                                                 }
15442:                                             } else {
15443:                                                 $balancetext =  $ruletitles{$rule};
15444:                                             }
15445:                                         } else {
15446:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
15447:                                         }
15448:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
15449:                                     }
15450:                                 }
15451:                             }
15452:                         }
15453:                         if (keys(%toupdate)) {
15454:                             my %thismachine;
15455:                             my $updatedhere;
15456:                             my $cachetime = 60*60*24;
15457:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
15458:                             foreach my $lonhost (keys(%toupdate)) {
15459:                                 if ($thismachine{$lonhost}) {
15460:                                     unless ($updatedhere) {
15461:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
15462:                                                                       $defaultshash{'loadbalancing'},
15463:                                                                       $cachetime);
15464:                                         $updatedhere = 1;
15465:                                     }
15466:                                 } else {
15467:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
15468:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
15469:                                 }
15470:                             }
15471:                         }
15472:                     }
15473:                 }
15474:                 if ($resulttext ne '') {
15475:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
15476:                 } else {
15477:                     $resulttext = $nochgmsg;
15478:                 }
15479:             } else {
15480:                 $resulttext = $nochgmsg;
15481:             }
15482:         } else {
15483:             $resulttext = '<span class="LC_error">'.
15484:                           &mt('An error occurred: [_1]',$putresult).'</span>';
15485:         }
15486:     } else {
15487:         $resulttext = $nochgmsg;
15488:     }
15489:     return $resulttext;
15490: }
15491: 
15492: sub recurse_check {
15493:     my ($chkcats,$categories,$depth,$name) = @_;
15494:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
15495:         my $chg = 0;
15496:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
15497:             my $category = $chkcats->[$depth]{$name}[$j];
15498:             my $item;
15499:             if ($category eq '') {
15500:                 $chg ++;
15501:             } else {
15502:                 my $deeper = $depth + 1;
15503:                 $item = &escape($category).':'.&escape($name).':'.$depth;
15504:                 if ($chg) {
15505:                     $categories->{$item} -= $chg;
15506:                 }
15507:                 &recurse_check($chkcats,$categories,$deeper,$category);
15508:                 $deeper --;
15509:             }
15510:         }
15511:     }
15512:     return;
15513: }
15514: 
15515: sub recurse_cat_deletes {
15516:     my ($item,$coursecategories,$deletions) = @_;
15517:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
15518:     my $subdepth = $depth + 1;
15519:     if (ref($coursecategories) eq 'HASH') {
15520:         foreach my $subitem (keys(%{$coursecategories})) {
15521:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
15522:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
15523:                 delete($coursecategories->{$subitem});
15524:                 $deletions->{$subitem} = 1;
15525:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
15526:             }
15527:         }
15528:     }
15529:     return;
15530: }
15531: 
15532: sub active_dc_picker {
15533:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
15534:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
15535:     my @domcoord = keys(%domcoords);
15536:     if (keys(%currhash)) {
15537:         foreach my $dc (keys(%currhash)) {
15538:             unless (exists($domcoords{$dc})) {
15539:                 push(@domcoord,$dc);
15540:             }
15541:         }
15542:     }
15543:     @domcoord = sort(@domcoord);
15544:     my $numdcs = scalar(@domcoord);
15545:     my $rows = 0;
15546:     my $table;
15547:     if ($numdcs > 1) {
15548:         $table = '<table>';
15549:         for (my $i=0; $i<@domcoord; $i++) {
15550:             my $rem = $i%($numinrow);
15551:             if ($rem == 0) {
15552:                 if ($i > 0) {
15553:                     $table .= '</tr>';
15554:                 }
15555:                 $table .= '<tr>';
15556:                 $rows ++;
15557:             }
15558:             my $check = '';
15559:             if ($inputtype eq 'radio') {
15560:                 if (keys(%currhash) == 0) {
15561:                     if (!$i) {
15562:                         $check = ' checked="checked"';
15563:                     }
15564:                 } elsif (exists($currhash{$domcoord[$i]})) {
15565:                     $check = ' checked="checked"';
15566:                 }
15567:             } else {
15568:                 if (exists($currhash{$domcoord[$i]})) {
15569:                     $check = ' checked="checked"';
15570:                 }
15571:             }
15572:             if ($i == @domcoord - 1) {
15573:                 my $colsleft = $numinrow - $rem;
15574:                 if ($colsleft > 1) {
15575:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
15576:                 } else {
15577:                     $table .= '<td class="LC_left_item">';
15578:                 }
15579:             } else {
15580:                 $table .= '<td class="LC_left_item">';
15581:             }
15582:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
15583:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
15584:             $table .= '<span class="LC_nobreak"><label>'.
15585:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
15586:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
15587:             if ($user ne $dcname.':'.$dcdom) {
15588:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
15589:             }
15590:             $table .= '</label></span></td>';
15591:         }
15592:         $table .= '</tr></table>';
15593:     } elsif ($numdcs == 1) {
15594:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
15595:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
15596:         if ($inputtype eq 'radio') {
15597:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
15598:             if ($user ne $dcname.':'.$dcdom) {
15599:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
15600:             }
15601:         } else {
15602:             my $check;
15603:             if (exists($currhash{$domcoord[0]})) {
15604:                 $check = ' checked="checked"';
15605:             }
15606:             $table = '<span class="LC_nobreak"><label>'.
15607:                      '<input type="checkbox" name="'.$name.'" '.
15608:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
15609:             if ($user ne $dcname.':'.$dcdom) {
15610:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
15611:             }
15612:             $table .= '</label></span>';
15613:             $rows ++;
15614:         }
15615:     }
15616:     return ($numdcs,$table,$rows);
15617: }
15618: 
15619: sub usersession_titles {
15620:     return &Apache::lonlocal::texthash(
15621:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
15622:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
15623:                spares => 'Servers offloaded to, when busy',
15624:                version => 'LON-CAPA version requirement',
15625:                excludedomain => 'Allow all, but exclude specific domains',
15626:                includedomain => 'Deny all, but include specific domains',
15627:                primary => 'Primary (checked first)',
15628:                default => 'Default',
15629:            );
15630: }
15631: 
15632: sub id_for_thisdom {
15633:     my (%servers) = @_;
15634:     my %altids;
15635:     foreach my $server (keys(%servers)) {
15636:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
15637:         if ($serverhome ne $server) {
15638:             $altids{$serverhome} = $server;
15639:         }
15640:     }
15641:     return %altids;
15642: }
15643: 
15644: sub count_servers {
15645:     my ($currbalancer,%servers) = @_;
15646:     my (@spares,$numspares);
15647:     foreach my $lonhost (sort(keys(%servers))) {
15648:         next if ($currbalancer eq $lonhost);
15649:         push(@spares,$lonhost);
15650:     }
15651:     if ($currbalancer) {
15652:         $numspares = scalar(@spares);
15653:     } else {
15654:         $numspares = scalar(@spares) - 1;
15655:     }
15656:     return ($numspares,@spares);
15657: }
15658: 
15659: sub lonbalance_targets_js {
15660:     my ($dom,$types,$servers,$settings) = @_;
15661:     my $select = &mt('Select');
15662:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
15663:     if (ref($servers) eq 'HASH') {
15664:         $alltargets = join("','",sort(keys(%{$servers})));
15665:         my @homedoms;
15666:         foreach my $server (sort(keys(%{$servers}))) {
15667:             if (&Apache::lonnet::host_domain($server) eq $dom) {
15668:                 push(@homedoms,'1');
15669:             } else {
15670:                 push(@homedoms,'0');
15671:             }
15672:         }
15673:         $allishome = join("','",@homedoms);
15674:     }
15675:     if (ref($types) eq 'ARRAY') {
15676:         if (@{$types} > 0) {
15677:             @alltypes = @{$types};
15678:         }
15679:     }
15680:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
15681:     $allinsttypes = join("','",@alltypes);
15682:     my (%currbalancer,%currtargets,%currrules,%existing);
15683:     if (ref($settings) eq 'HASH') {
15684:         %existing = %{$settings};
15685:     }
15686:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
15687:                               \%currtargets,\%currrules);
15688:     my $balancers = join("','",sort(keys(%currbalancer)));
15689:     return <<"END";
15690: 
15691: <script type="text/javascript">
15692: // <![CDATA[
15693: 
15694: currBalancers = new Array('$balancers');
15695: 
15696: function toggleTargets(balnum) {
15697:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
15698:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
15699:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
15700:     var prevbalancer = prevhostitem.value;
15701:     var baltotal = document.getElementById('loadbalancing_total').value;
15702:     prevhostitem.value = balancer;
15703:     if (prevbalancer != '') {
15704:         var prevIdx = currBalancers.indexOf(prevbalancer);
15705:         if (prevIdx != -1) {
15706:             currBalancers.splice(prevIdx,1);
15707:         }
15708:     }
15709:     if (balancer == '') {
15710:         hideSpares(balnum);
15711:     } else {
15712:         var currIdx = currBalancers.indexOf(balancer);
15713:         if (currIdx == -1) {
15714:             currBalancers.push(balancer);
15715:         }
15716:         var homedoms = new Array('$allishome');
15717:         var ishomedom = homedoms[lonhostitem.selectedIndex];
15718:         showSpares(balancer,ishomedom,balnum);
15719:     }
15720:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
15721:     return;
15722: }
15723: 
15724: function showSpares(balancer,ishomedom,balnum) {
15725:     var alltargets = new Array('$alltargets');
15726:     var insttypes = new Array('$allinsttypes');
15727:     var offloadtypes = new Array('primary','default');
15728: 
15729:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
15730:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
15731:  
15732:     for (var i=0; i<offloadtypes.length; i++) {
15733:         var count = 0;
15734:         for (var j=0; j<alltargets.length; j++) {
15735:             if (alltargets[j] != balancer) {
15736:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
15737:                 item.value = alltargets[j];
15738:                 item.style.textAlign='left';
15739:                 item.style.textFace='normal';
15740:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
15741:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
15742:                     item.disabled = '';
15743:                 } else {
15744:                     item.disabled = 'disabled';
15745:                     item.checked = false;
15746:                 }
15747:                 count ++;
15748:             }
15749:         }
15750:     }
15751:     for (var k=0; k<insttypes.length; k++) {
15752:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
15753:             if (ishomedom == 1) {
15754:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
15755:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
15756:             } else {
15757:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
15758:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
15759:             }
15760:         } else {
15761:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
15762:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
15763:         }
15764:         if ((insttypes[k] != '_LC_external') && 
15765:             ((insttypes[k] != '_LC_internetdom') ||
15766:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
15767:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
15768:             item.options.length = 0;
15769:             item.options[0] = new Option("","",true,true);
15770:             var idx = 0;
15771:             for (var m=0; m<alltargets.length; m++) {
15772:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
15773:                     idx ++;
15774:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
15775:                 }
15776:             }
15777:         }
15778:     }
15779:     return;
15780: }
15781: 
15782: function hideSpares(balnum) {
15783:     var alltargets = new Array('$alltargets');
15784:     var insttypes = new Array('$allinsttypes');
15785:     var offloadtypes = new Array('primary','default');
15786: 
15787:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
15788:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
15789: 
15790:     var total = alltargets.length - 1;
15791:     for (var i=0; i<offloadtypes; i++) {
15792:         for (var j=0; j<total; j++) {
15793:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
15794:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
15795:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
15796:         }
15797:     }
15798:     for (var k=0; k<insttypes.length; k++) {
15799:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
15800:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
15801:         if (insttypes[k] != '_LC_external') {
15802:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
15803:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
15804:         }
15805:     }
15806:     return;
15807: }
15808: 
15809: function checkOffloads(item,balnum,type) {
15810:     var alltargets = new Array('$alltargets');
15811:     var offloadtypes = new Array('primary','default');
15812:     if (item.checked) {
15813:         var total = alltargets.length - 1;
15814:         var other;
15815:         if (type == offloadtypes[0]) {
15816:             other = offloadtypes[1];
15817:         } else {
15818:             other = offloadtypes[0];
15819:         }
15820:         for (var i=0; i<total; i++) {
15821:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
15822:             if (server == item.value) {
15823:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
15824:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
15825:                 }
15826:             }
15827:         }
15828:     }
15829:     return;
15830: }
15831: 
15832: function singleServerToggle(balnum,type) {
15833:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
15834:     if (offloadtoSelIdx == 0) {
15835:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
15836:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
15837: 
15838:     } else {
15839:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
15840:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
15841:     }
15842:     return;
15843: }
15844: 
15845: function balanceruleChange(formname,balnum,type) {
15846:     if (type == '_LC_external') {
15847:         return;
15848:     }
15849:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
15850:     for (var i=0; i<typesRules.length; i++) {
15851:         if (formname.elements[typesRules[i]].checked) {
15852:             if (formname.elements[typesRules[i]].value != 'specific') {
15853:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
15854:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
15855:             } else {
15856:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
15857:             }
15858:         }
15859:     }
15860:     return;
15861: }
15862: 
15863: function balancerDeleteChange(balnum) {
15864:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
15865:     var baltotal = document.getElementById('loadbalancing_total').value;
15866:     var addtarget;
15867:     var removetarget;
15868:     var action = 'delete';
15869:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
15870:         var lonhost = hostitem.value;
15871:         var currIdx = currBalancers.indexOf(lonhost);
15872:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
15873:             if (currIdx != -1) {
15874:                 currBalancers.splice(currIdx,1);
15875:             }
15876:             addtarget = lonhost;
15877:         } else {
15878:             if (currIdx == -1) {
15879:                 currBalancers.push(lonhost);
15880:             }
15881:             removetarget = lonhost;
15882:             action = 'undelete';
15883:         }
15884:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
15885:     }
15886:     return;
15887: }
15888: 
15889: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
15890:     if (baltotal > 1) {
15891:         var offloadtypes = new Array('primary','default');
15892:         var alltargets = new Array('$alltargets');
15893:         var insttypes = new Array('$allinsttypes');
15894:         for (var i=0; i<baltotal; i++) {
15895:             if (i != balnum) {
15896:                 for (var j=0; j<offloadtypes.length; j++) {
15897:                     var total = alltargets.length - 1;
15898:                     for (var k=0; k<total; k++) {
15899:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
15900:                         var server = serveritem.value;
15901:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
15902:                             if (server == addtarget) {
15903:                                 serveritem.disabled = '';
15904:                             }
15905:                         }
15906:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
15907:                             if (server == removetarget) {
15908:                                 serveritem.disabled = 'disabled';
15909:                                 serveritem.checked = false;
15910:                             }
15911:                         }
15912:                     }
15913:                 }
15914:                 for (var j=0; j<insttypes.length; j++) {
15915:                     if (insttypes[j] != '_LC_external') {
15916:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
15917:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
15918:                             var currSel = singleserver.selectedIndex;
15919:                             var currVal = singleserver.options[currSel].value;
15920:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
15921:                                 var numoptions = singleserver.options.length;
15922:                                 var needsnew = 1;
15923:                                 for (var k=0; k<numoptions; k++) {
15924:                                     if (singleserver.options[k] == addtarget) {
15925:                                         needsnew = 0;
15926:                                         break;
15927:                                     }
15928:                                 }
15929:                                 if (needsnew == 1) {
15930:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
15931:                                 }
15932:                             }
15933:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
15934:                                 singleserver.options.length = 0;
15935:                                 if ((currVal) && (currVal != removetarget)) {
15936:                                     singleserver.options[0] = new Option("","",false,false);
15937:                                 } else {
15938:                                     singleserver.options[0] = new Option("","",true,true);
15939:                                 }
15940:                                 var idx = 0;
15941:                                 for (var m=0; m<alltargets.length; m++) {
15942:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
15943:                                         idx ++;
15944:                                         if (currVal == alltargets[m]) {
15945:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
15946:                                         } else {
15947:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
15948:                                         }
15949:                                     }
15950:                                 }
15951:                             }
15952:                         }
15953:                     }
15954:                 }
15955:             }
15956:         }
15957:     }
15958:     return;
15959: }
15960: 
15961: // ]]>
15962: </script>
15963: 
15964: END
15965: }
15966: 
15967: sub new_spares_js {
15968:     my @sparestypes = ('primary','default');
15969:     my $types = join("','",@sparestypes);
15970:     my $select = &mt('Select');
15971:     return <<"END";
15972: 
15973: <script type="text/javascript">
15974: // <![CDATA[
15975: 
15976: function updateNewSpares(formname,lonhost) {
15977:     var types = new Array('$types');
15978:     var include = new Array();
15979:     var exclude = new Array();
15980:     for (var i=0; i<types.length; i++) {
15981:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
15982:         for (var j=0; j<spareboxes.length; j++) {
15983:             if (formname.elements[spareboxes[j]].checked) {
15984:                 exclude.push(formname.elements[spareboxes[j]].value);
15985:             } else {
15986:                 include.push(formname.elements[spareboxes[j]].value);
15987:             }
15988:         }
15989:     }
15990:     for (var i=0; i<types.length; i++) {
15991:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
15992:         var selIdx = newSpare.selectedIndex;
15993:         var currnew = newSpare.options[selIdx].value;
15994:         var okSpares = new Array();
15995:         for (var j=0; j<newSpare.options.length; j++) {
15996:             var possible = newSpare.options[j].value;
15997:             if (possible != '') {
15998:                 if (exclude.indexOf(possible) == -1) {
15999:                     okSpares.push(possible);
16000:                 } else {
16001:                     if (currnew == possible) {
16002:                         selIdx = 0;
16003:                     }
16004:                 }
16005:             }
16006:         }
16007:         for (var k=0; k<include.length; k++) {
16008:             if (okSpares.indexOf(include[k]) == -1) {
16009:                 okSpares.push(include[k]);
16010:             }
16011:         }
16012:         okSpares.sort();
16013:         newSpare.options.length = 0;
16014:         if (selIdx == 0) {
16015:             newSpare.options[0] = new Option("$select","",true,true);
16016:         } else {
16017:             newSpare.options[0] = new Option("$select","",false,false);
16018:         }
16019:         for (var m=0; m<okSpares.length; m++) {
16020:             var idx = m+1;
16021:             var selThis = 0;
16022:             if (selIdx != 0) {
16023:                 if (okSpares[m] == currnew) {
16024:                     selThis = 1;
16025:                 }
16026:             }
16027:             if (selThis == 1) {
16028:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
16029:             } else {
16030:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
16031:             }
16032:         }
16033:     }
16034:     return;
16035: }
16036: 
16037: function checkNewSpares(lonhost,type) {
16038:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
16039:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
16040:     if (chosen != '') { 
16041:         var othertype;
16042:         var othernewSpare;
16043:         if (type == 'primary') {
16044:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
16045:         }
16046:         if (type == 'default') {
16047:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
16048:         }
16049:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
16050:             othernewSpare.selectedIndex = 0;
16051:         }
16052:     }
16053:     return;
16054: }
16055: 
16056: // ]]>
16057: </script>
16058: 
16059: END
16060: 
16061: }
16062: 
16063: sub common_domprefs_js {
16064:     return <<"END";
16065: 
16066: <script type="text/javascript">
16067: // <![CDATA[
16068: 
16069: function getIndicesByName(formname,item) {
16070:     var group = new Array();
16071:     for (var i=0;i<formname.elements.length;i++) {
16072:         if (formname.elements[i].name == item) {
16073:             group.push(formname.elements[i].id);
16074:         }
16075:     }
16076:     return group;
16077: }
16078: 
16079: // ]]>
16080: </script>
16081: 
16082: END
16083: 
16084: }
16085: 
16086: sub recaptcha_js {
16087:     my %lt = &captcha_phrases();
16088:     return <<"END";
16089: 
16090: <script type="text/javascript">
16091: // <![CDATA[
16092: 
16093: function updateCaptcha(caller,context) {
16094:     var privitem;
16095:     var pubitem;
16096:     var privtext;
16097:     var pubtext;
16098:     var versionitem;
16099:     var versiontext;
16100:     if (document.getElementById(context+'_recaptchapub')) {
16101:         pubitem = document.getElementById(context+'_recaptchapub');
16102:     } else {
16103:         return;
16104:     }
16105:     if (document.getElementById(context+'_recaptchapriv')) {
16106:         privitem = document.getElementById(context+'_recaptchapriv');
16107:     } else {
16108:         return;
16109:     }
16110:     if (document.getElementById(context+'_recaptchapubtxt')) {
16111:         pubtext = document.getElementById(context+'_recaptchapubtxt');
16112:     } else {
16113:         return;
16114:     }
16115:     if (document.getElementById(context+'_recaptchaprivtxt')) {
16116:         privtext = document.getElementById(context+'_recaptchaprivtxt');
16117:     } else {
16118:         return;
16119:     }
16120:     if (document.getElementById(context+'_recaptchaversion')) {
16121:         versionitem = document.getElementById(context+'_recaptchaversion');
16122:     } else {
16123:         return;
16124:     }
16125:     if (document.getElementById(context+'_recaptchavertxt')) {
16126:         versiontext = document.getElementById(context+'_recaptchavertxt');
16127:     } else {
16128:         return;
16129:     }
16130:     if (caller.checked) {
16131:         if (caller.value == 'recaptcha') {
16132:             pubitem.type = 'text';
16133:             privitem.type = 'text';
16134:             pubitem.size = '40';
16135:             privitem.size = '40';
16136:             pubtext.innerHTML = "$lt{'pub'}";
16137:             privtext.innerHTML = "$lt{'priv'}";
16138:             versionitem.type = 'text';
16139:             versionitem.size = '3';
16140:             versiontext.innerHTML = "$lt{'ver'}";
16141:         } else {
16142:             pubitem.type = 'hidden';
16143:             privitem.type = 'hidden';
16144:             versionitem.type = 'hidden';
16145:             pubtext.innerHTML = '';
16146:             privtext.innerHTML = '';
16147:             versiontext.innerHTML = '';
16148:         }
16149:     }
16150:     return;
16151: }
16152: 
16153: // ]]>
16154: </script>
16155: 
16156: END
16157: 
16158: }
16159: 
16160: sub toggle_display_js {
16161:     return <<"END";
16162: 
16163: <script type="text/javascript">
16164: // <![CDATA[
16165: 
16166: function toggleDisplay(domForm,caller) {
16167:     if (document.getElementById(caller)) {
16168:         var divitem = document.getElementById(caller);
16169:         var optionsElement = domForm.coursecredits;
16170:         var checkval = 1;
16171:         var dispval = 'block';
16172:         var selfcreateRegExp = /^cancreate_emailverified/;
16173:         if (caller == 'emailoptions') {
16174:             optionsElement = domForm.cancreate_email; 
16175:         }
16176:         if (caller == 'studentsubmission') {
16177:             optionsElement = domForm.postsubmit;
16178:         }
16179:         if (caller == 'cloneinstcode') {
16180:             optionsElement = domForm.canclone;
16181:             checkval = 'instcode';
16182:         }
16183:         if (selfcreateRegExp.test(caller)) {
16184:             optionsElement = domForm.elements[caller];
16185:             checkval = 'other';
16186:             dispval = 'inline'
16187:         }
16188:         if (optionsElement.length) {
16189:             var currval;
16190:             for (var i=0; i<optionsElement.length; i++) {
16191:                 if (optionsElement[i].checked) {
16192:                    currval = optionsElement[i].value;
16193:                 }
16194:             }
16195:             if (currval == checkval) {
16196:                 divitem.style.display = dispval;
16197:             } else {
16198:                 divitem.style.display = 'none';
16199:             }
16200:         }
16201:     }
16202:     return;
16203: }
16204: 
16205: // ]]>
16206: </script>
16207: 
16208: END
16209: 
16210: }
16211: 
16212: sub captcha_phrases {
16213:     return &Apache::lonlocal::texthash (
16214:                  priv => 'Private key',
16215:                  pub  => 'Public key',
16216:                  original  => 'original (CAPTCHA)',
16217:                  recaptcha => 'successor (ReCAPTCHA)',
16218:                  notused   => 'unused',
16219:                  ver => 'ReCAPTCHA version (1 or 2)',
16220:     );
16221: }
16222: 
16223: sub devalidate_remote_domconfs {
16224:     my ($dom,$cachekeys) = @_;
16225:     return unless (ref($cachekeys) eq 'HASH');
16226:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
16227:     my %thismachine;
16228:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
16229:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions','directorysrch');
16230:     if (keys(%servers)) {
16231:         foreach my $server (keys(%servers)) {
16232:             next if ($thismachine{$server});
16233:             my @cached;
16234:             foreach my $name (@posscached) {
16235:                 if ($cachekeys->{$name}) {
16236:                     push(@cached,&escape($name).':'.&escape($dom));
16237:                 }
16238:             }
16239:             if (@cached) {
16240:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
16241:             }
16242:         }
16243:     }
16244:     return;
16245: }
16246: 
16247: 1;

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