File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.297: download - view: text, annotated - select for diffs
Tue May 9 03:04:26 2017 UTC (7 years ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Key and Secret for LTI Tools are stored in a separate GDBM file
  (encconfig.db) on primary library server in domain so more specific domain
  trust settings can apply than is the case for regular domain configuration
  (in configuration.db).
- Storage and retrieval of data from GDBM files for domain, with namespaces
  beginning 'enc' use encryption when data are transferred.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.297 2017/05/09 03:04:26 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 => 'Assignable to e-mail usernames'}],
  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 as username',
  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(/^contacts$/,@actions)) {
  602:             $js .= &contacts_javascript();
  603:         }
  604:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  605:     } else {
  606: # check if domconfig user exists for the domain.
  607:         my $servadm = $r->dir_config('lonAdmEMail');
  608:         my ($configuserok,$author_ok,$switchserver) =
  609:             &config_check($dom,$confname,$servadm);
  610:         unless ($configuserok eq 'ok') {
  611:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  612:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  613:                           $confname).
  614:                       '<br />'
  615:             );
  616:             if ($switchserver) {
  617:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  618:                           '<br />'.
  619:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  620:                           '<br />'.
  621:                           &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).
  622:                           '<br />'.
  623:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  624:                 );
  625:             } else {
  626:                 $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.').
  627:                           '<br />'.
  628:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  629:                 );
  630:             }
  631:             $r->print(&Apache::loncommon::end_page());
  632:             return OK;
  633:         }
  634:         if (keys(%domconfig) == 0) {
  635:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  636:             my @ids=&Apache::lonnet::current_machine_ids();
  637:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  638:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  639:                 my @loginimages = ('img','logo','domlogo','login');
  640:                 my $custom_img_count = 0;
  641:                 foreach my $img (@loginimages) {
  642:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  643:                         $custom_img_count ++;
  644:                     }
  645:                 }
  646:                 foreach my $role (@roles) {
  647:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  648:                         $custom_img_count ++;
  649:                     }
  650:                 }
  651:                 if ($custom_img_count > 0) {
  652:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  653:                     my $switch_server = &check_switchserver($dom,$confname);
  654:                     $r->print(
  655:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  656:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  657:     &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 />'.
  658:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  659:                     if ($switch_server) {
  660:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  661:                     }
  662:                     $r->print(&Apache::loncommon::end_page());
  663:                     return OK;
  664:                 }
  665:             }
  666:         }
  667:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  668:     }
  669:     return OK;
  670: }
  671: 
  672: sub process_changes {
  673:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  674:     my %domconfig;
  675:     if (ref($values) eq 'HASH') {
  676:         %domconfig = %{$values};
  677:     }
  678:     my $output;
  679:     if ($action eq 'login') {
  680:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  681:     } elsif ($action eq 'rolecolors') {
  682:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  683:                                      $lastactref,%domconfig);
  684:     } elsif ($action eq 'quotas') {
  685:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  686:     } elsif ($action eq 'autoenroll') {
  687:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  688:     } elsif ($action eq 'autoupdate') {
  689:         $output = &modify_autoupdate($dom,%domconfig);
  690:     } elsif ($action eq 'autocreate') {
  691:         $output = &modify_autocreate($dom,%domconfig);
  692:     } elsif ($action eq 'directorysrch') {
  693:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  694:     } elsif ($action eq 'usercreation') {
  695:         $output = &modify_usercreation($dom,%domconfig);
  696:     } elsif ($action eq 'selfcreation') {
  697:         $output = &modify_selfcreation($dom,%domconfig);
  698:     } elsif ($action eq 'usermodification') {
  699:         $output = &modify_usermodification($dom,%domconfig);
  700:     } elsif ($action eq 'contacts') {
  701:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  702:     } elsif ($action eq 'defaults') {
  703:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  704:     } elsif ($action eq 'scantron') {
  705:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  706:     } elsif ($action eq 'coursecategories') {
  707:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  708:     } elsif ($action eq 'serverstatuses') {
  709:         $output = &modify_serverstatuses($dom,%domconfig);
  710:     } elsif ($action eq 'requestcourses') {
  711:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  712:     } elsif ($action eq 'requestauthor') {
  713:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  714:     } elsif ($action eq 'helpsettings') {
  715:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  716:     } elsif ($action eq 'coursedefaults') {
  717:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  718:     } elsif ($action eq 'selfenrollment') {
  719:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  720:     } elsif ($action eq 'usersessions') {
  721:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  722:     } elsif ($action eq 'loadbalancing') {
  723:         $output = &modify_loadbalancing($dom,%domconfig);
  724:     } elsif ($action eq 'ltitools') {
  725:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  726:     } elsif ($action eq 'ssl') {
  727:         $output = &modify_ssl($dom,$lastactref,%domconfig);
  728:     } elsif ($action eq 'trust') {
  729:         $output = &modify_trust($dom,$lastactref,%domconfig);
  730:     }
  731:     return $output;
  732: }
  733: 
  734: sub print_config_box {
  735:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  736:     my $rowtotal = 0;
  737:     my $output;
  738:     if ($action eq 'coursecategories') {
  739:         $output = &coursecategories_javascript($settings);
  740:     } elsif ($action eq 'defaults') {
  741:         $output = &defaults_javascript($settings); 
  742:     } elsif ($action eq 'helpsettings') {
  743:         my (%privs,%levelscurrent);
  744:         my %full=();
  745:         my %levels=(
  746:                      course => {},
  747:                      domain => {},
  748:                      system => {},
  749:                    );
  750:         my $context = 'domain';
  751:         my $crstype = 'Course';
  752:         my $formname = 'display';
  753:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  754:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  755:         $output =
  756:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full, 
  757:                                                       \@templateroles);
  758:     }
  759:     $output .=
  760:          '<table class="LC_nested_outer">
  761:           <tr>
  762:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  763:            &mt($item->{text}).'&nbsp;'.
  764:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  765:           '</tr>';
  766:     $rowtotal ++;
  767:     my $numheaders = 1;
  768:     if (ref($item->{'header'}) eq 'ARRAY') {
  769:         $numheaders = scalar(@{$item->{'header'}});
  770:     }
  771:     if ($numheaders > 1) {
  772:         my $colspan = '';
  773:         my $rightcolspan = '';
  774:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  775:             ($action eq 'directorysrch') ||
  776:             (($action eq 'login') && ($numheaders < 4))) {
  777:             $colspan = ' colspan="2"';
  778:         }
  779:         if ($action eq 'usersessions') {
  780:             $rightcolspan = ' colspan="3"'; 
  781:         }
  782:         $output .= '
  783:           <tr>
  784:            <td>
  785:             <table class="LC_nested">
  786:              <tr class="LC_info_row">
  787:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  788:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  789:              </tr>';
  790:         $rowtotal ++;
  791:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  792:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  793:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'ssl') ||
  794:             ($action eq 'directorysrch') || ($action eq 'trust') || ($action eq 'helpsettings') ||
  795:             ($action eq 'contacts')) {
  796:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  797:         } elsif ($action eq 'coursecategories') {
  798:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  799:         } elsif ($action eq 'login') {
  800:             if ($numheaders == 4) {
  801:                 $colspan = ' colspan="2"';
  802:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  803:             } else {
  804:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  805:             }
  806:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  807:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  808:         } elsif ($action eq 'rolecolors') {
  809:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  810:         }
  811:         $output .= '
  812:            </table>
  813:           </td>
  814:          </tr>
  815:          <tr>
  816:            <td>
  817:             <table class="LC_nested">
  818:              <tr class="LC_info_row">
  819:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  820:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  821:              </tr>';
  822:             $rowtotal ++;
  823:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  824:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  825:             ($action eq 'usersessions') || ($action eq 'coursecategories') || 
  826:             ($action eq 'trust') || ($action eq 'contacts') || ($action eq 'defaults')) {
  827:             if ($action eq 'coursecategories') {
  828:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  829:                 $colspan = ' colspan="2"';
  830:             } elsif ($action eq 'trust') {
  831:                 $output .= $item->{'print'}->('shared',$dom,$settings,\$rowtotal);
  832:             } else {
  833:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  834:             }
  835:             if ($action eq 'trust') {
  836:                 $output .= '
  837:             </table>
  838:           </td>
  839:          </tr>';
  840:                 my @trusthdrs = qw(2 3 4 5 6 7);
  841:                 my @prefixes = qw(enroll othcoau coaurem domroles catalog reqcrs);
  842:                 for (my $i=0; $i<@trusthdrs; $i++) {
  843:                     $output .= '
  844:          <tr>
  845:            <td>
  846:             <table class="LC_nested">
  847:              <tr class="LC_info_row">
  848:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col1'}).'</td>
  849:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col2'}).'</td></tr>'.
  850:                            $item->{'print'}->($prefixes[$i],$dom,$settings,\$rowtotal).'
  851:             </table>
  852:           </td>
  853:          </tr>';
  854:                 }
  855:                 $output .= '
  856:          <tr>
  857:            <td>
  858:             <table class="LC_nested">
  859:              <tr class="LC_info_row">
  860:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col1'}).'</td>
  861:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col2'}).'</td></tr>'.
  862:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  863:             } else {
  864:                 $output .= '
  865:            </table>
  866:           </td>
  867:          </tr>
  868:          <tr>
  869:            <td>
  870:             <table class="LC_nested">
  871:              <tr class="LC_info_row">
  872:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  873:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  874:              </tr>'."\n";
  875:                 if ($action eq 'coursecategories') {
  876:                     $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  877:                 } else {
  878:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  879:                 }
  880:             }
  881:             $rowtotal ++;
  882:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  883:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
  884:                  ($action eq 'helpsettings')) {
  885:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  886:         } elsif ($action eq 'ssl') {
  887:             $output .= $item->{'print'}->('connto',$dom,$settings,\$rowtotal).'
  888:             </table>
  889:           </td>
  890:          </tr>
  891:          <tr>
  892:            <td>
  893:             <table class="LC_nested">
  894:              <tr class="LC_info_row">
  895:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  896:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  897:                            $item->{'print'}->('connfrom',$dom,$settings,\$rowtotal).'
  898:             </table>
  899:           </td>
  900:          </tr>
  901:          <tr>
  902:            <td>
  903:             <table class="LC_nested">
  904:              <tr class="LC_info_row">
  905:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  906:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'.
  907:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  908:         } elsif ($action eq 'login') {
  909:             if ($numheaders == 4) {
  910:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  911:            </table>
  912:           </td>
  913:          </tr>
  914:          <tr>
  915:            <td>
  916:             <table class="LC_nested">
  917:              <tr class="LC_info_row">
  918:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  919:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  920:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  921:                 $rowtotal ++;
  922:             } else {
  923:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  924:             }
  925:             $output .= '
  926:            </table>
  927:           </td>
  928:          </tr>
  929:          <tr>
  930:            <td>
  931:             <table class="LC_nested">
  932:              <tr class="LC_info_row">';
  933:             if ($numheaders == 4) {
  934:                 $output .= '
  935:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  936:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  937:              </tr>';
  938:             } else {
  939:                 $output .= '
  940:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  941:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  942:              </tr>';
  943:             }
  944:             $rowtotal ++;
  945:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
  946:         } elsif ($action eq 'requestcourses') {
  947:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  948:             $rowtotal ++;
  949:             $output .= &print_studentcode($settings,\$rowtotal).'
  950:            </table>
  951:           </td>
  952:          </tr>
  953:          <tr>
  954:            <td>
  955:             <table class="LC_nested">
  956:              <tr class="LC_info_row">
  957:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  958:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  959:                        &textbookcourses_javascript($settings).
  960:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  961:             </table>
  962:            </td>
  963:           </tr>
  964:          <tr>
  965:            <td>
  966:             <table class="LC_nested">
  967:              <tr class="LC_info_row">
  968:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  969:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  970:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  971:             </table>
  972:            </td>
  973:           </tr>
  974:           <tr>
  975:            <td>
  976:             <table class="LC_nested">
  977:              <tr class="LC_info_row">
  978:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  979:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  980:              </tr>'.
  981:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  982:         } elsif ($action eq 'requestauthor') {
  983:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  984:             $rowtotal ++;
  985:         } elsif ($action eq 'rolecolors') {
  986:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  987:            </table>
  988:           </td>
  989:          </tr>
  990:          <tr>
  991:            <td>
  992:             <table class="LC_nested">
  993:              <tr class="LC_info_row">
  994:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  995:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  996:               <td class="LC_right_item" valign="top">'.
  997:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  998:              </tr>'.
  999:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
 1000:            </table>
 1001:           </td>
 1002:          </tr>
 1003:          <tr>
 1004:            <td>
 1005:             <table class="LC_nested">
 1006:              <tr class="LC_info_row">
 1007:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1008:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1009:              </tr>'.
 1010:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
 1011:             $rowtotal += 2;
 1012:         }
 1013:     } else {
 1014:         $output .= '
 1015:           <tr>
 1016:            <td>
 1017:             <table class="LC_nested">
 1018:              <tr class="LC_info_row">';
 1019:         if ($action eq 'login') {
 1020:             $output .= '  
 1021:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1022:         } elsif ($action eq 'serverstatuses') {
 1023:             $output .= '
 1024:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
 1025:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
 1026: 
 1027:         } else {
 1028:             $output .= '
 1029:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1030:         }
 1031:         if (defined($item->{'header'}->[0]->{'col3'})) {
 1032:             $output .= '<td class="LC_left_item" valign="top">'.
 1033:                        &mt($item->{'header'}->[0]->{'col2'});
 1034:             if ($action eq 'serverstatuses') {
 1035:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
 1036:             } 
 1037:         } else {
 1038:             $output .= '<td class="LC_right_item" valign="top">'.
 1039:                        &mt($item->{'header'}->[0]->{'col2'});
 1040:         }
 1041:         $output .= '</td>';
 1042:         if ($item->{'header'}->[0]->{'col3'}) {
 1043:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1044:                 $output .= '<td class="LC_left_item" valign="top">'.
 1045:                             &mt($item->{'header'}->[0]->{'col3'});
 1046:             } else {
 1047:                 $output .= '<td class="LC_right_item" valign="top">'.
 1048:                            &mt($item->{'header'}->[0]->{'col3'});
 1049:             }
 1050:             if ($action eq 'serverstatuses') {
 1051:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1052:             }
 1053:             $output .= '</td>';
 1054:         }
 1055:         if ($item->{'header'}->[0]->{'col4'}) {
 1056:             $output .= '<td class="LC_right_item" valign="top">'.
 1057:                        &mt($item->{'header'}->[0]->{'col4'});
 1058:         }
 1059:         $output .= '</tr>';
 1060:         $rowtotal ++;
 1061:         if ($action eq 'quotas') {
 1062:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1063:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1064:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') || 
 1065:                  ($action eq 'ltitools')) {
 1066:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1067:         } elsif ($action eq 'scantron') {
 1068:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
 1069:         }
 1070:     }
 1071:     $output .= '
 1072:    </table>
 1073:   </td>
 1074:  </tr>
 1075: </table><br />';
 1076:     return ($output,$rowtotal);
 1077: }
 1078: 
 1079: sub print_login {
 1080:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1081:     my ($css_class,$datatable);
 1082:     my %choices = &login_choices();
 1083: 
 1084:     if ($caller eq 'service') {
 1085:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1086:         my $choice = $choices{'disallowlogin'};
 1087:         $css_class = ' class="LC_odd_row"';
 1088:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1089:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1090:                       '<th>'.$choices{'server'}.'</th>'.
 1091:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1092:                       '<th>'.$choices{'custompath'}.'</th>'.
 1093:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1094:         my %disallowed;
 1095:         if (ref($settings) eq 'HASH') {
 1096:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1097:                %disallowed = %{$settings->{'loginvia'}};
 1098:             }
 1099:         }
 1100:         foreach my $lonhost (sort(keys(%servers))) {
 1101:             my $direct = 'selected="selected"';
 1102:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1103:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1104:                     $direct = '';
 1105:                 }
 1106:             }
 1107:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1108:                           '<td><select name="'.$lonhost.'_server">'.
 1109:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1110:                           '</option>';
 1111:             foreach my $hostid (sort(keys(%servers))) {
 1112:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1113:                 my $selected = '';
 1114:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1115:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1116:                         $selected = 'selected="selected"';
 1117:                     }
 1118:                 }
 1119:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1120:                               $servers{$hostid}.'</option>';
 1121:             }
 1122:             $datatable .= '</select></td>'.
 1123:                           '<td><select name="'.$lonhost.'_serverpath">';
 1124:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1125:                 my $pathname = $path;
 1126:                 if ($path eq 'custom') {
 1127:                     $pathname = &mt('Custom Path').' ->';
 1128:                 }
 1129:                 my $selected = '';
 1130:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1131:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1132:                         $selected = 'selected="selected"';
 1133:                     }
 1134:                 } elsif ($path eq '') {
 1135:                     $selected = 'selected="selected"';
 1136:                 }
 1137:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1138:             }
 1139:             $datatable .= '</select></td>';
 1140:             my ($custom,$exempt);
 1141:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1142:                 $custom = $disallowed{$lonhost}{'custompath'};
 1143:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1144:             }
 1145:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1146:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1147:                           '</tr>';
 1148:         }
 1149:         $datatable .= '</table></td></tr>';
 1150:         return $datatable;
 1151:     } elsif ($caller eq 'page') {
 1152:         my %defaultchecked = ( 
 1153:                                'coursecatalog' => 'on',
 1154:                                'helpdesk'      => 'on',
 1155:                                'adminmail'     => 'off',
 1156:                                'newuser'       => 'off',
 1157:                              );
 1158:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1159:         my (%checkedon,%checkedoff);
 1160:         foreach my $item (@toggles) {
 1161:             if ($defaultchecked{$item} eq 'on') { 
 1162:                 $checkedon{$item} = ' checked="checked" ';
 1163:                 $checkedoff{$item} = ' ';
 1164:             } elsif ($defaultchecked{$item} eq 'off') {
 1165:                 $checkedoff{$item} = ' checked="checked" ';
 1166:                 $checkedon{$item} = ' ';
 1167:             }
 1168:         }
 1169:         my @images = ('img','logo','domlogo','login');
 1170:         my @logintext = ('textcol','bgcol');
 1171:         my @bgs = ('pgbg','mainbg','sidebg');
 1172:         my @links = ('link','alink','vlink');
 1173:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1174:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1175:         my (%is_custom,%designs);
 1176:         my %defaults = (
 1177:                        font => $defaultdesign{'login.font'},
 1178:                        );
 1179:         foreach my $item (@images) {
 1180:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1181:             $defaults{'showlogo'}{$item} = 1;
 1182:         }
 1183:         foreach my $item (@bgs) {
 1184:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1185:         }
 1186:         foreach my $item (@logintext) {
 1187:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1188:         }
 1189:         foreach my $item (@links) {
 1190:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1191:         }
 1192:         if (ref($settings) eq 'HASH') {
 1193:             foreach my $item (@toggles) {
 1194:                 if ($settings->{$item} eq '1') {
 1195:                     $checkedon{$item} =  ' checked="checked" ';
 1196:                     $checkedoff{$item} = ' ';
 1197:                 } elsif ($settings->{$item} eq '0') {
 1198:                     $checkedoff{$item} =  ' checked="checked" ';
 1199:                     $checkedon{$item} = ' ';
 1200:                 }
 1201:             }
 1202:             foreach my $item (@images) {
 1203:                 if (defined($settings->{$item})) {
 1204:                     $designs{$item} = $settings->{$item};
 1205:                     $is_custom{$item} = 1;
 1206:                 }
 1207:                 if (defined($settings->{'showlogo'}{$item})) {
 1208:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1209:                 }
 1210:             }
 1211:             foreach my $item (@logintext) {
 1212:                 if ($settings->{$item} ne '') {
 1213:                     $designs{'logintext'}{$item} = $settings->{$item};
 1214:                     $is_custom{$item} = 1;
 1215:                 }
 1216:             }
 1217:             if ($settings->{'font'} ne '') {
 1218:                 $designs{'font'} = $settings->{'font'};
 1219:                 $is_custom{'font'} = 1;
 1220:             }
 1221:             foreach my $item (@bgs) {
 1222:                 if ($settings->{$item} ne '') {
 1223:                     $designs{'bgs'}{$item} = $settings->{$item};
 1224:                     $is_custom{$item} = 1;
 1225:                 }
 1226:             }
 1227:             foreach my $item (@links) {
 1228:                 if ($settings->{$item} ne '') {
 1229:                     $designs{'links'}{$item} = $settings->{$item};
 1230:                     $is_custom{$item} = 1;
 1231:                 }
 1232:             }
 1233:         } else {
 1234:             if ($designhash{$dom.'.login.font'} ne '') {
 1235:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1236:                 $is_custom{'font'} = 1;
 1237:             }
 1238:             foreach my $item (@images) {
 1239:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1240:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1241:                     $is_custom{$item} = 1;
 1242:                 }
 1243:             }
 1244:             foreach my $item (@bgs) {
 1245:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1246:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1247:                     $is_custom{$item} = 1;
 1248:                 }
 1249:             }
 1250:             foreach my $item (@links) {
 1251:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1252:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1253:                     $is_custom{$item} = 1;
 1254:                 }
 1255:             }
 1256:         }
 1257:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1258:                                                       logo => 'Institution Logo',
 1259:                                                       domlogo => 'Domain Logo',
 1260:                                                       login => 'Login box');
 1261:         my $itemcount = 1;
 1262:         foreach my $item (@toggles) {
 1263:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1264:             $datatable .=  
 1265:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1266:                 '</td><td>'.
 1267:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1268:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1269:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1270:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1271:                 '</tr>';
 1272:             $itemcount ++;
 1273:         }
 1274:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1275:         $datatable .= '</tr></table></td></tr>';
 1276:     } elsif ($caller eq 'help') {
 1277:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1278:         my $switchserver = &check_switchserver($dom,$confname);
 1279:         my $itemcount = 1;
 1280:         $defaulturl = '/adm/loginproblems.html';
 1281:         $defaulttype = 'default';
 1282:         %lt = &Apache::lonlocal::texthash (
 1283:                      del     => 'Delete?',
 1284:                      rep     => 'Replace:',
 1285:                      upl     => 'Upload:',
 1286:                      default => 'Default',
 1287:                      custom  => 'Custom',
 1288:                                              );
 1289:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1290:         my @currlangs;
 1291:         if (ref($settings) eq 'HASH') {
 1292:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1293:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1294:                     next if ($settings->{'helpurl'}{$key} eq '');
 1295:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1296:                     $type{$key} = 'custom';
 1297:                     unless ($key eq 'nolang') {
 1298:                         push(@currlangs,$key);
 1299:                     }
 1300:                 }
 1301:             } elsif ($settings->{'helpurl'} ne '') {
 1302:                 $type{'nolang'} = 'custom';
 1303:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1304:             }
 1305:         }
 1306:         foreach my $lang ('nolang',sort(@currlangs)) {
 1307:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1308:             $datatable .= '<tr'.$css_class.'>';
 1309:             if ($url{$lang} eq '') {
 1310:                 $url{$lang} = $defaulturl;
 1311:             }
 1312:             if ($type{$lang} eq '') {
 1313:                 $type{$lang} = $defaulttype;
 1314:             }
 1315:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1316:             if ($lang eq 'nolang') {
 1317:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1318:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1319:             } else {
 1320:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1321:                                   $langchoices{$lang},
 1322:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1323:             }
 1324:             $datatable .= '</span></td>'."\n".
 1325:                           '<td class="LC_left_item">';
 1326:             if ($type{$lang} eq 'custom') {
 1327:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1328:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1329:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1330:             } else {
 1331:                 $datatable .= $lt{'upl'};
 1332:             }
 1333:             $datatable .='<br />';
 1334:             if ($switchserver) {
 1335:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1336:             } else {
 1337:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1338:             }
 1339:             $datatable .= '</td></tr>';
 1340:             $itemcount ++;
 1341:         }
 1342:         my @addlangs;
 1343:         foreach my $lang (sort(keys(%langchoices))) {
 1344:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1345:             push(@addlangs,$lang);
 1346:         }
 1347:         if (@addlangs > 0) {
 1348:             my %toadd;
 1349:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1350:             $toadd{''} = &mt('Select');
 1351:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1352:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1353:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1354:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1355:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1356:             if ($switchserver) {
 1357:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1358:             } else {
 1359:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1360:             }
 1361:             $datatable .= '</td></tr>';
 1362:             $itemcount ++;
 1363:         }
 1364:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1365:     } elsif ($caller eq 'headtag') {
 1366:         my %domservers = &Apache::lonnet::get_servers($dom);
 1367:         my $choice = $choices{'headtag'};
 1368:         $css_class = ' class="LC_odd_row"';
 1369:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1370:                       '<td align="left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1371:                       '<th>'.$choices{'current'}.'</th>'.
 1372:                       '<th>'.$choices{'action'}.'</th>'.
 1373:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1374:         my (%currurls,%currexempt);
 1375:         if (ref($settings) eq 'HASH') {
 1376:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1377:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1378:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1379:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1380:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1381:                     }
 1382:                 }
 1383:             }
 1384:         }
 1385:         my %lt = &Apache::lonlocal::texthash(
 1386:                                                del  => 'Delete?',
 1387:                                                rep  => 'Replace:',
 1388:                                                upl  => 'Upload:',
 1389:                                                curr => 'View contents',
 1390:                                                none => 'None',
 1391:         );
 1392:         my $switchserver = &check_switchserver($dom,$confname);
 1393:         foreach my $lonhost (sort(keys(%domservers))) {
 1394:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1395:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1396:             if ($currurls{$lonhost}) {
 1397:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1398:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1399:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1400:                               '">'.$lt{'curr'}.'</a></td>'.
 1401:                               '<td><span class="LC_nobreak"><label>'.
 1402:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1403:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1404:             } else {
 1405:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1406:             }
 1407:             $datatable .='<br />';
 1408:             if ($switchserver) {
 1409:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1410:             } else {
 1411:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1412:             }
 1413:             $datatable .= '</td><td><input type="textbox" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1414:         }
 1415:         $datatable .= '</table></td></tr>';
 1416:     }
 1417:     return $datatable;
 1418: }
 1419: 
 1420: sub login_choices {
 1421:     my %choices =
 1422:         &Apache::lonlocal::texthash (
 1423:             coursecatalog => 'Display Course/Community Catalog link?',
 1424:             adminmail     => "Display Administrator's E-mail Address?",
 1425:             helpdesk      => 'Display "Contact Helpdesk" link',
 1426:             disallowlogin => "Login page requests redirected",
 1427:             hostid        => "Server",
 1428:             server        => "Redirect to:",
 1429:             serverpath    => "Path",
 1430:             custompath    => "Custom", 
 1431:             exempt        => "Exempt IP(s)",
 1432:             directlogin   => "No redirect",
 1433:             newuser       => "Link to create a user account",
 1434:             img           => "Header",
 1435:             logo          => "Main Logo",
 1436:             domlogo       => "Domain Logo",
 1437:             login         => "Log-in Header", 
 1438:             textcol       => "Text color",
 1439:             bgcol         => "Box color",
 1440:             bgs           => "Background colors",
 1441:             links         => "Link colors",
 1442:             font          => "Font color",
 1443:             pgbg          => "Header",
 1444:             mainbg        => "Page",
 1445:             sidebg        => "Login box",
 1446:             link          => "Link",
 1447:             alink         => "Active link",
 1448:             vlink         => "Visited link",
 1449:             headtag       => "Custom markup",
 1450:             action        => "Action",
 1451:             current       => "Current",
 1452:         );
 1453:     return %choices;
 1454: }
 1455: 
 1456: sub print_rolecolors {
 1457:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1458:     my %choices = &color_font_choices();
 1459:     my @bgs = ('pgbg','tabbg','sidebg');
 1460:     my @links = ('link','alink','vlink');
 1461:     my @images = ('img');
 1462:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1463:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1464:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1465:     my (%is_custom,%designs);
 1466:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1467:     if (ref($settings) eq 'HASH') {
 1468:         if (ref($settings->{$role}) eq 'HASH') {
 1469:             if ($settings->{$role}->{'img'} ne '') {
 1470:                 $designs{'img'} = $settings->{$role}->{'img'};
 1471:                 $is_custom{'img'} = 1;
 1472:             }
 1473:             if ($settings->{$role}->{'font'} ne '') {
 1474:                 $designs{'font'} = $settings->{$role}->{'font'};
 1475:                 $is_custom{'font'} = 1;
 1476:             }
 1477:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1478:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1479:                 $is_custom{'fontmenu'} = 1;
 1480:             }
 1481:             foreach my $item (@bgs) {
 1482:                 if ($settings->{$role}->{$item} ne '') {
 1483:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1484:                     $is_custom{$item} = 1;
 1485:                 }
 1486:             }
 1487:             foreach my $item (@links) {
 1488:                 if ($settings->{$role}->{$item} ne '') {
 1489:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1490:                     $is_custom{$item} = 1;
 1491:                 }
 1492:             }
 1493:         }
 1494:     } else {
 1495:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1496:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1497:             $is_custom{'img'} = 1;
 1498:         }
 1499:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1500:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1501:             $is_custom{'fontmenu'} = 1; 
 1502:         }
 1503:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1504:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1505:             $is_custom{'font'} = 1;
 1506:         }
 1507:         foreach my $item (@bgs) {
 1508:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1509:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1510:                 $is_custom{$item} = 1;
 1511:             
 1512:             }
 1513:         }
 1514:         foreach my $item (@links) {
 1515:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1516:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1517:                 $is_custom{$item} = 1;
 1518:             }
 1519:         }
 1520:     }
 1521:     my $itemcount = 1;
 1522:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1523:     $datatable .= '</tr></table></td></tr>';
 1524:     return $datatable;
 1525: }
 1526: 
 1527: sub role_defaults {
 1528:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1529:     my %defaults;
 1530:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1531:         return %defaults;
 1532:     }
 1533:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1534:     if ($role eq 'login') {
 1535:         %defaults = (
 1536:                        font => $defaultdesign{$role.'.font'},
 1537:                     );
 1538:         if (ref($logintext) eq 'ARRAY') {
 1539:             foreach my $item (@{$logintext}) {
 1540:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1541:             }
 1542:         }
 1543:         foreach my $item (@{$images}) {
 1544:             $defaults{'showlogo'}{$item} = 1;
 1545:         }
 1546:     } else {
 1547:         %defaults = (
 1548:                        img => $defaultdesign{$role.'.img'},
 1549:                        font => $defaultdesign{$role.'.font'},
 1550:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1551:                     );
 1552:     }
 1553:     foreach my $item (@{$bgs}) {
 1554:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1555:     }
 1556:     foreach my $item (@{$links}) {
 1557:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1558:     }
 1559:     foreach my $item (@{$images}) {
 1560:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1561:     }
 1562:     return %defaults;
 1563: }
 1564: 
 1565: sub display_color_options {
 1566:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1567:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1568:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1569:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1570:     my $datatable = '<tr'.$css_class.'>'.
 1571:         '<td>'.$choices->{'font'}.'</td>';
 1572:     if (!$is_custom->{'font'}) {
 1573:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1574:     } else {
 1575:         $datatable .= '<td>&nbsp;</td>';
 1576:     }
 1577:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1578: 
 1579:     $datatable .= '<td><span class="LC_nobreak">'.
 1580:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1581:                   ' value="'.$current_color.'" />&nbsp;'.
 1582:                   '&nbsp;</td></tr>';
 1583:     unless ($role eq 'login') { 
 1584:         $datatable .= '<tr'.$css_class.'>'.
 1585:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1586:         if (!$is_custom->{'fontmenu'}) {
 1587:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1588:         } else {
 1589:             $datatable .= '<td>&nbsp;</td>';
 1590:         }
 1591: 	$current_color = $designs->{'fontmenu'} ?
 1592: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1593:         $datatable .= '<td><span class="LC_nobreak">'.
 1594:                       '<input class="colorchooser" type="text" size="10" name="'
 1595: 		      .$role.'_fontmenu"'.
 1596:                       ' value="'.$current_color.'" />&nbsp;'.
 1597:                       '&nbsp;</td></tr>';
 1598:     }
 1599:     my $switchserver = &check_switchserver($dom,$confname);
 1600:     foreach my $img (@{$images}) {
 1601: 	$itemcount ++;
 1602:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1603:         $datatable .= '<tr'.$css_class.'>'.
 1604:                       '<td>'.$choices->{$img};
 1605:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1606:         if ($role eq 'login') {
 1607:             if ($img eq 'login') {
 1608:                 $login_hdr_pick =
 1609:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1610:                 $logincolors =
 1611:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1612:                                        $designs,$defaults);
 1613:             } elsif ($img ne 'domlogo') {
 1614:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1615:             }
 1616:         }
 1617:         $datatable .= '</td>';
 1618:         if ($designs->{$img} ne '') {
 1619:             $imgfile = $designs->{$img};
 1620: 	    $img_import = ($imgfile =~ m{^/adm/});
 1621:         } else {
 1622:             $imgfile = $defaults->{$img};
 1623:         }
 1624:         if ($imgfile) {
 1625:             my ($showfile,$fullsize);
 1626:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1627:                 my $urldir = $1;
 1628:                 my $filename = $2;
 1629:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1630:                 if (@info) {
 1631:                     my $thumbfile = 'tn-'.$filename;
 1632:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1633:                     if (@thumb) {
 1634:                         $showfile = $urldir.'/'.$thumbfile;
 1635:                     } else {
 1636:                         $showfile = $imgfile;
 1637:                     }
 1638:                 } else {
 1639:                     $showfile = '';
 1640:                 }
 1641:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1642:                 $showfile = $imgfile;
 1643:                 my $imgdir = $1;
 1644:                 my $filename = $2;
 1645:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1646:                     $showfile = "/$imgdir/tn-".$filename;
 1647:                 } else {
 1648:                     my $input = $londocroot.$imgfile;
 1649:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1650:                     if (!-e $output) {
 1651:                         my ($width,$height) = &thumb_dimensions();
 1652:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1653:                         if ($fullwidth ne '' && $fullheight ne '') {
 1654:                             if ($fullwidth > $width && $fullheight > $height) { 
 1655:                                 my $size = $width.'x'.$height;
 1656:                                 system("convert -sample $size $input $output");
 1657:                                 $showfile = "/$imgdir/tn-".$filename;
 1658:                             }
 1659:                         }
 1660:                     }
 1661:                 }
 1662:             }
 1663:             if ($showfile) {
 1664:                 if ($showfile =~ m{^/(adm|res)/}) {
 1665:                     if ($showfile =~ m{^/res/}) {
 1666:                         my $local_showfile =
 1667:                             &Apache::lonnet::filelocation('',$showfile);
 1668:                         &Apache::lonnet::repcopy($local_showfile);
 1669:                     }
 1670:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1671:                 }
 1672:                 if ($imgfile) {
 1673:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1674:                         if ($imgfile =~ m{^/res/}) {
 1675:                             my $local_imgfile =
 1676:                                 &Apache::lonnet::filelocation('',$imgfile);
 1677:                             &Apache::lonnet::repcopy($local_imgfile);
 1678:                         }
 1679:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1680:                     } else {
 1681:                         $fullsize = $imgfile;
 1682:                     }
 1683:                 }
 1684:                 $datatable .= '<td>';
 1685:                 if ($img eq 'login') {
 1686:                     $datatable .= $login_hdr_pick;
 1687:                 } 
 1688:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1689:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1690:             } else {
 1691:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1692:                               &mt('Upload:').'<br />';
 1693:             }
 1694:         } else {
 1695:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1696:                           &mt('Upload:').'<br />';
 1697:         }
 1698:         if ($switchserver) {
 1699:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1700:         } else {
 1701:             if ($img ne 'login') { # suppress file selection for Log-in header
 1702:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1703:             }
 1704:         }
 1705:         $datatable .= '</td></tr>';
 1706:     }
 1707:     $itemcount ++;
 1708:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1709:     $datatable .= '<tr'.$css_class.'>'.
 1710:                   '<td>'.$choices->{'bgs'}.'</td>';
 1711:     my $bgs_def;
 1712:     foreach my $item (@{$bgs}) {
 1713:         if (!$is_custom->{$item}) {
 1714:             $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>';
 1715:         }
 1716:     }
 1717:     if ($bgs_def) {
 1718:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1719:     } else {
 1720:         $datatable .= '<td>&nbsp;</td>';
 1721:     }
 1722:     $datatable .= '<td class="LC_right_item">'.
 1723:                   '<table border="0"><tr>';
 1724: 
 1725:     foreach my $item (@{$bgs}) {
 1726:         $datatable .= '<td align="center">'.$choices->{$item};
 1727: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1728:         if ($designs->{'bgs'}{$item}) {
 1729:             $datatable .= '&nbsp;';
 1730:         }
 1731:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1732:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1733:     }
 1734:     $datatable .= '</tr></table></td></tr>';
 1735:     $itemcount ++;
 1736:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1737:     $datatable .= '<tr'.$css_class.'>'.
 1738:                   '<td>'.$choices->{'links'}.'</td>';
 1739:     my $links_def;
 1740:     foreach my $item (@{$links}) {
 1741:         if (!$is_custom->{$item}) {
 1742:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1743:         }
 1744:     }
 1745:     if ($links_def) {
 1746:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1747:     } else {
 1748:         $datatable .= '<td>&nbsp;</td>';
 1749:     }
 1750:     $datatable .= '<td class="LC_right_item">'.
 1751:                   '<table border="0"><tr>';
 1752:     foreach my $item (@{$links}) {
 1753: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1754:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1755:         if ($designs->{'links'}{$item}) {
 1756:             $datatable.='&nbsp;';
 1757:         }
 1758:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1759:                       '" /></td>';
 1760:     }
 1761:     $$rowtotal += $itemcount;
 1762:     return $datatable;
 1763: }
 1764: 
 1765: sub logo_display_options {
 1766:     my ($img,$defaults,$designs) = @_;
 1767:     my $checkedon;
 1768:     if (ref($defaults) eq 'HASH') {
 1769:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1770:             if ($defaults->{'showlogo'}{$img}) {
 1771:                 $checkedon = 'checked="checked" ';     
 1772:             }
 1773:         } 
 1774:     }
 1775:     if (ref($designs) eq 'HASH') {
 1776:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1777:             if (defined($designs->{'showlogo'}{$img})) {
 1778:                 if ($designs->{'showlogo'}{$img} == 0) {
 1779:                     $checkedon = '';
 1780:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1781:                     $checkedon = 'checked="checked" ';
 1782:                 }
 1783:             }
 1784:         }
 1785:     }
 1786:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1787:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1788:            &mt('show').'</label>'."\n";
 1789: }
 1790: 
 1791: sub login_header_options  {
 1792:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1793:     my $output = '';
 1794:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1795:         $output .= &mt('Text default(s):').'<br />';
 1796:         if (!$is_custom->{'textcol'}) {
 1797:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1798:                        '&nbsp;&nbsp;&nbsp;';
 1799:         }
 1800:         if (!$is_custom->{'bgcol'}) {
 1801:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1802:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1803:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1804:         }
 1805:         $output .= '<br />';
 1806:     }
 1807:     $output .='<br />';
 1808:     return $output;
 1809: }
 1810: 
 1811: sub login_text_colors {
 1812:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1813:     my $color_menu = '<table border="0"><tr>';
 1814:     foreach my $item (@{$logintext}) {
 1815:         $color_menu .= '<td align="center">'.$choices->{$item};
 1816:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1817:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1818:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1819:     }
 1820:     $color_menu .= '</tr></table><br />';
 1821:     return $color_menu;
 1822: }
 1823: 
 1824: sub image_changes {
 1825:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1826:     my $output;
 1827:     if ($img eq 'login') {
 1828:             # suppress image for Log-in header
 1829:     } elsif (!$is_custom) {
 1830:         if ($img ne 'domlogo') {
 1831:             $output .= &mt('Default image:').'<br />';
 1832:         } else {
 1833:             $output .= &mt('Default in use:').'<br />';
 1834:         }
 1835:     }
 1836:     if ($img eq 'login') { # suppress image for Log-in header
 1837:         $output .= '<td>'.$logincolors;
 1838:     } else {
 1839:         if ($img_import) {
 1840:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1841:         }
 1842:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1843:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1844:         if ($is_custom) {
 1845:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1846:                        '<input type="checkbox" name="'.
 1847:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1848:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1849:         } else {
 1850:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1851:         }
 1852:     }
 1853:     return $output;
 1854: }
 1855: 
 1856: sub print_quotas {
 1857:     my ($dom,$settings,$rowtotal,$action) = @_;
 1858:     my $context;
 1859:     if ($action eq 'quotas') {
 1860:         $context = 'tools';
 1861:     } else {
 1862:         $context = $action;
 1863:     }
 1864:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1865:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1866:     my $typecount = 0;
 1867:     my ($css_class,%titles);
 1868:     if ($context eq 'requestcourses') {
 1869:         @usertools = ('official','unofficial','community','textbook','placement');
 1870:         @options =('norequest','approval','validate','autolimit');
 1871:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1872:         %titles = &courserequest_titles();
 1873:     } elsif ($context eq 'requestauthor') {
 1874:         @usertools = ('author');
 1875:         @options = ('norequest','approval','automatic');
 1876:         %titles = &authorrequest_titles();
 1877:     } else {
 1878:         @usertools = ('aboutme','blog','webdav','portfolio');
 1879:         %titles = &tool_titles();
 1880:     }
 1881:     if (ref($types) eq 'ARRAY') {
 1882:         foreach my $type (@{$types}) {
 1883:             my ($currdefquota,$currauthorquota);
 1884:             unless (($context eq 'requestcourses') ||
 1885:                     ($context eq 'requestauthor')) {
 1886:                 if (ref($settings) eq 'HASH') {
 1887:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1888:                         $currdefquota = $settings->{defaultquota}->{$type};
 1889:                     } else {
 1890:                         $currdefquota = $settings->{$type};
 1891:                     }
 1892:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1893:                         $currauthorquota = $settings->{authorquota}->{$type};
 1894:                     }
 1895:                 }
 1896:             }
 1897:             if (defined($usertypes->{$type})) {
 1898:                 $typecount ++;
 1899:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1900:                 $datatable .= '<tr'.$css_class.'>'.
 1901:                               '<td>'.$usertypes->{$type}.'</td>'.
 1902:                               '<td class="LC_left_item">';
 1903:                 if ($context eq 'requestcourses') {
 1904:                     $datatable .= '<table><tr>';
 1905:                 }
 1906:                 my %cell;  
 1907:                 foreach my $item (@usertools) {
 1908:                     if ($context eq 'requestcourses') {
 1909:                         my ($curroption,$currlimit);
 1910:                         if (ref($settings) eq 'HASH') {
 1911:                             if (ref($settings->{$item}) eq 'HASH') {
 1912:                                 $curroption = $settings->{$item}->{$type};
 1913:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1914:                                     $currlimit = $1; 
 1915:                                 }
 1916:                             }
 1917:                         }
 1918:                         if (!$curroption) {
 1919:                             $curroption = 'norequest';
 1920:                         }
 1921:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1922:                         foreach my $option (@options) {
 1923:                             my $val = $option;
 1924:                             if ($option eq 'norequest') {
 1925:                                 $val = 0;  
 1926:                             }
 1927:                             if ($option eq 'validate') {
 1928:                                 my $canvalidate = 0;
 1929:                                 if (ref($validations{$item}) eq 'HASH') { 
 1930:                                     if ($validations{$item}{$type}) {
 1931:                                         $canvalidate = 1;
 1932:                                     }
 1933:                                 }
 1934:                                 next if (!$canvalidate);
 1935:                             }
 1936:                             my $checked = '';
 1937:                             if ($option eq $curroption) {
 1938:                                 $checked = ' checked="checked"';
 1939:                             } elsif ($option eq 'autolimit') {
 1940:                                 if ($curroption =~ /^autolimit/) {
 1941:                                     $checked = ' checked="checked"';
 1942:                                 }                       
 1943:                             } 
 1944:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1945:                                   '<input type="radio" name="crsreq_'.$item.
 1946:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1947:                                   $titles{$option}.'</label>';
 1948:                             if ($option eq 'autolimit') {
 1949:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1950:                                                 $item.'_limit_'.$type.'" size="1" '.
 1951:                                                 'value="'.$currlimit.'" />';
 1952:                             }
 1953:                             $cell{$item} .= '</span> ';
 1954:                             if ($option eq 'autolimit') {
 1955:                                 $cell{$item} .= $titles{'unlimited'};
 1956:                             }
 1957:                         }
 1958:                     } elsif ($context eq 'requestauthor') {
 1959:                         my $curroption;
 1960:                         if (ref($settings) eq 'HASH') {
 1961:                             $curroption = $settings->{$type};
 1962:                         }
 1963:                         if (!$curroption) {
 1964:                             $curroption = 'norequest';
 1965:                         }
 1966:                         foreach my $option (@options) {
 1967:                             my $val = $option;
 1968:                             if ($option eq 'norequest') {
 1969:                                 $val = 0;
 1970:                             }
 1971:                             my $checked = '';
 1972:                             if ($option eq $curroption) {
 1973:                                 $checked = ' checked="checked"';
 1974:                             }
 1975:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1976:                                   '<input type="radio" name="authorreq_'.$type.
 1977:                                   '" value="'.$val.'"'.$checked.' />'.
 1978:                                   $titles{$option}.'</label></span>&nbsp; ';
 1979:                         }
 1980:                     } else {
 1981:                         my $checked = 'checked="checked" ';
 1982:                         if (ref($settings) eq 'HASH') {
 1983:                             if (ref($settings->{$item}) eq 'HASH') {
 1984:                                 if ($settings->{$item}->{$type} == 0) {
 1985:                                     $checked = '';
 1986:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1987:                                     $checked =  'checked="checked" ';
 1988:                                 }
 1989:                             }
 1990:                         }
 1991:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1992:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1993:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1994:                                       '</label></span>&nbsp; ';
 1995:                     }
 1996:                 }
 1997:                 if ($context eq 'requestcourses') {
 1998:                     $datatable .= '</tr><tr>';
 1999:                     foreach my $item (@usertools) {
 2000:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 2001:                     }
 2002:                     $datatable .= '</tr></table>';
 2003:                 }
 2004:                 $datatable .= '</td>';
 2005:                 unless (($context eq 'requestcourses') ||
 2006:                         ($context eq 'requestauthor')) {
 2007:                     $datatable .= 
 2008:                               '<td class="LC_right_item">'.
 2009:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2010:                               '<input type="text" name="quota_'.$type.
 2011:                               '" value="'.$currdefquota.
 2012:                               '" size="5" /></span>'.('&nbsp;' x 2).
 2013:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2014:                               '<input type="text" name="authorquota_'.$type.
 2015:                               '" value="'.$currauthorquota.
 2016:                               '" size="5" /></span></td>';
 2017:                 }
 2018:                 $datatable .= '</tr>';
 2019:             }
 2020:         }
 2021:     }
 2022:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2023:         $defaultquota = '20';
 2024:         $authorquota = '500';
 2025:         if (ref($settings) eq 'HASH') {
 2026:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2027:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2028:             } elsif (defined($settings->{'default'})) {
 2029:                 $defaultquota = $settings->{'default'};
 2030:             }
 2031:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 2032:                 $authorquota = $settings->{'authorquota'}->{'default'};
 2033:             }
 2034:         }
 2035:     }
 2036:     $typecount ++;
 2037:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2038:     $datatable .= '<tr'.$css_class.'>'.
 2039:                   '<td>'.$othertitle.'</td>'.
 2040:                   '<td class="LC_left_item">';
 2041:     if ($context eq 'requestcourses') {
 2042:         $datatable .= '<table><tr>';
 2043:     }
 2044:     my %defcell;
 2045:     foreach my $item (@usertools) {
 2046:         if ($context eq 'requestcourses') {
 2047:             my ($curroption,$currlimit);
 2048:             if (ref($settings) eq 'HASH') {
 2049:                 if (ref($settings->{$item}) eq 'HASH') {
 2050:                     $curroption = $settings->{$item}->{'default'};
 2051:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2052:                         $currlimit = $1;
 2053:                     }
 2054:                 }
 2055:             }
 2056:             if (!$curroption) {
 2057:                 $curroption = 'norequest';
 2058:             }
 2059:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2060:             foreach my $option (@options) {
 2061:                 my $val = $option;
 2062:                 if ($option eq 'norequest') {
 2063:                     $val = 0;
 2064:                 }
 2065:                 if ($option eq 'validate') {
 2066:                     my $canvalidate = 0;
 2067:                     if (ref($validations{$item}) eq 'HASH') {
 2068:                         if ($validations{$item}{'default'}) {
 2069:                             $canvalidate = 1;
 2070:                         }
 2071:                     }
 2072:                     next if (!$canvalidate);
 2073:                 }
 2074:                 my $checked = '';
 2075:                 if ($option eq $curroption) {
 2076:                     $checked = ' checked="checked"';
 2077:                 } elsif ($option eq 'autolimit') {
 2078:                     if ($curroption =~ /^autolimit/) {
 2079:                         $checked = ' checked="checked"';
 2080:                     }
 2081:                 }
 2082:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2083:                                   '<input type="radio" name="crsreq_'.$item.
 2084:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2085:                                   $titles{$option}.'</label>';
 2086:                 if ($option eq 'autolimit') {
 2087:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2088:                                        $item.'_limit_default" size="1" '.
 2089:                                        'value="'.$currlimit.'" />';
 2090:                 }
 2091:                 $defcell{$item} .= '</span> ';
 2092:                 if ($option eq 'autolimit') {
 2093:                     $defcell{$item} .= $titles{'unlimited'};
 2094:                 }
 2095:             }
 2096:         } elsif ($context eq 'requestauthor') {
 2097:             my $curroption;
 2098:             if (ref($settings) eq 'HASH') {
 2099:                 $curroption = $settings->{'default'};
 2100:             }
 2101:             if (!$curroption) {
 2102:                 $curroption = 'norequest';
 2103:             }
 2104:             foreach my $option (@options) {
 2105:                 my $val = $option;
 2106:                 if ($option eq 'norequest') {
 2107:                     $val = 0;
 2108:                 }
 2109:                 my $checked = '';
 2110:                 if ($option eq $curroption) {
 2111:                     $checked = ' checked="checked"';
 2112:                 }
 2113:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2114:                               '<input type="radio" name="authorreq_default"'.
 2115:                               ' value="'.$val.'"'.$checked.' />'.
 2116:                               $titles{$option}.'</label></span>&nbsp; ';
 2117:             }
 2118:         } else {
 2119:             my $checked = 'checked="checked" ';
 2120:             if (ref($settings) eq 'HASH') {
 2121:                 if (ref($settings->{$item}) eq 'HASH') {
 2122:                     if ($settings->{$item}->{'default'} == 0) {
 2123:                         $checked = '';
 2124:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2125:                         $checked = 'checked="checked" ';
 2126:                     }
 2127:                 }
 2128:             }
 2129:             $datatable .= '<span class="LC_nobreak"><label>'.
 2130:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2131:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2132:                           '</label></span>&nbsp; ';
 2133:         }
 2134:     }
 2135:     if ($context eq 'requestcourses') {
 2136:         $datatable .= '</tr><tr>';
 2137:         foreach my $item (@usertools) {
 2138:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2139:         }
 2140:         $datatable .= '</tr></table>';
 2141:     }
 2142:     $datatable .= '</td>';
 2143:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2144:         $datatable .= '<td class="LC_right_item">'.
 2145:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2146:                       '<input type="text" name="defaultquota" value="'.
 2147:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2148:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2149:                       '<input type="text" name="authorquota" value="'.
 2150:                       $authorquota.'" size="5" /></span></td>';
 2151:     }
 2152:     $datatable .= '</tr>';
 2153:     $typecount ++;
 2154:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2155:     $datatable .= '<tr'.$css_class.'>'.
 2156:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2157:     if ($context eq 'requestcourses') {
 2158:         $datatable .= &mt('(overrides affiliation, if set)').
 2159:                       '</td>'.
 2160:                       '<td class="LC_left_item">'.
 2161:                       '<table><tr>';
 2162:     } else {
 2163:         $datatable .= &mt('(overrides affiliation, if checked)').
 2164:                       '</td>'.
 2165:                       '<td class="LC_left_item" colspan="2">'.
 2166:                       '<br />';
 2167:     }
 2168:     my %advcell;
 2169:     foreach my $item (@usertools) {
 2170:         if ($context eq 'requestcourses') {
 2171:             my ($curroption,$currlimit);
 2172:             if (ref($settings) eq 'HASH') {
 2173:                 if (ref($settings->{$item}) eq 'HASH') {
 2174:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2175:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2176:                         $currlimit = $1;
 2177:                     }
 2178:                 }
 2179:             }
 2180:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2181:             my $checked = '';
 2182:             if ($curroption eq '') {
 2183:                 $checked = ' checked="checked"';
 2184:             }
 2185:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2186:                                '<input type="radio" name="crsreq_'.$item.
 2187:                                '__LC_adv" value=""'.$checked.' />'.
 2188:                                &mt('No override set').'</label></span>&nbsp; ';
 2189:             foreach my $option (@options) {
 2190:                 my $val = $option;
 2191:                 if ($option eq 'norequest') {
 2192:                     $val = 0;
 2193:                 }
 2194:                 if ($option eq 'validate') {
 2195:                     my $canvalidate = 0;
 2196:                     if (ref($validations{$item}) eq 'HASH') {
 2197:                         if ($validations{$item}{'_LC_adv'}) {
 2198:                             $canvalidate = 1;
 2199:                         }
 2200:                     }
 2201:                     next if (!$canvalidate);
 2202:                 }
 2203:                 my $checked = '';
 2204:                 if ($val eq $curroption) {
 2205:                     $checked = ' checked="checked"';
 2206:                 } elsif ($option eq 'autolimit') {
 2207:                     if ($curroption =~ /^autolimit/) {
 2208:                         $checked = ' checked="checked"';
 2209:                     }
 2210:                 }
 2211:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2212:                                   '<input type="radio" name="crsreq_'.$item.
 2213:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2214:                                   $titles{$option}.'</label>';
 2215:                 if ($option eq 'autolimit') {
 2216:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2217:                                        $item.'_limit__LC_adv" size="1" '.
 2218:                                        'value="'.$currlimit.'" />';
 2219:                 }
 2220:                 $advcell{$item} .= '</span> ';
 2221:                 if ($option eq 'autolimit') {
 2222:                     $advcell{$item} .= $titles{'unlimited'};
 2223:                 }
 2224:             }
 2225:         } elsif ($context eq 'requestauthor') {
 2226:             my $curroption;
 2227:             if (ref($settings) eq 'HASH') {
 2228:                 $curroption = $settings->{'_LC_adv'};
 2229:             }
 2230:             my $checked = '';
 2231:             if ($curroption eq '') {
 2232:                 $checked = ' checked="checked"';
 2233:             }
 2234:             $datatable .= '<span class="LC_nobreak"><label>'.
 2235:                           '<input type="radio" name="authorreq__LC_adv"'.
 2236:                           ' value=""'.$checked.' />'.
 2237:                           &mt('No override set').'</label></span>&nbsp; ';
 2238:             foreach my $option (@options) {
 2239:                 my $val = $option;
 2240:                 if ($option eq 'norequest') {
 2241:                     $val = 0;
 2242:                 }
 2243:                 my $checked = '';
 2244:                 if ($val eq $curroption) {
 2245:                     $checked = ' checked="checked"';
 2246:                 }
 2247:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2248:                               '<input type="radio" name="authorreq__LC_adv"'.
 2249:                               ' value="'.$val.'"'.$checked.' />'.
 2250:                               $titles{$option}.'</label></span>&nbsp; ';
 2251:             }
 2252:         } else {
 2253:             my $checked = 'checked="checked" ';
 2254:             if (ref($settings) eq 'HASH') {
 2255:                 if (ref($settings->{$item}) eq 'HASH') {
 2256:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2257:                         $checked = '';
 2258:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2259:                         $checked = 'checked="checked" ';
 2260:                     }
 2261:                 }
 2262:             }
 2263:             $datatable .= '<span class="LC_nobreak"><label>'.
 2264:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2265:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2266:                           '</label></span>&nbsp; ';
 2267:         }
 2268:     }
 2269:     if ($context eq 'requestcourses') {
 2270:         $datatable .= '</tr><tr>';
 2271:         foreach my $item (@usertools) {
 2272:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2273:         }
 2274:         $datatable .= '</tr></table>';
 2275:     }
 2276:     $datatable .= '</td></tr>';
 2277:     $$rowtotal += $typecount;
 2278:     return $datatable;
 2279: }
 2280: 
 2281: sub print_requestmail {
 2282:     my ($dom,$action,$settings,$rowtotal) = @_;
 2283:     my ($now,$datatable,%currapp);
 2284:     $now = time;
 2285:     if (ref($settings) eq 'HASH') {
 2286:         if (ref($settings->{'notify'}) eq 'HASH') {
 2287:             if ($settings->{'notify'}{'approval'} ne '') {
 2288:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2289:             }
 2290:         }
 2291:     }
 2292:     my $numinrow = 2;
 2293:     my $css_class;
 2294:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 2295:     my $text;
 2296:     if ($action eq 'requestcourses') {
 2297:         $text = &mt('Receive notification of course requests requiring approval');
 2298:     } elsif ($action eq 'requestauthor') {
 2299:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2300:     } else {
 2301:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2302:     }
 2303:     $datatable = '<tr'.$css_class.'>'.
 2304:                  ' <td>'.$text.'</td>'.
 2305:                  ' <td class="LC_left_item">';
 2306:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2307:                                                  $action.'notifyapproval',%currapp);
 2308:     if ($numdc > 0) {
 2309:         $datatable .= $table;
 2310:     } else {
 2311:         $datatable .= &mt('There are no active Domain Coordinators');
 2312:     }
 2313:     $datatable .='</td></tr>';
 2314:     return $datatable;
 2315: }
 2316: 
 2317: sub print_studentcode {
 2318:     my ($settings,$rowtotal) = @_;
 2319:     my $rownum = 0; 
 2320:     my ($output,%current);
 2321:     my @crstypes = ('official','unofficial','community','textbook','placement');
 2322:     if (ref($settings) eq 'HASH') {
 2323:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2324:             foreach my $type (@crstypes) {
 2325:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2326:             }
 2327:         }
 2328:     }
 2329:     $output .= '<tr>'.
 2330:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2331:                '<td class="LC_left_item">';
 2332:     foreach my $type (@crstypes) {
 2333:         my $check = ' ';
 2334:         if ($current{$type}) {
 2335:             $check = ' checked="checked" ';
 2336:         }
 2337:         $output .= '<span class="LC_nobreak"><label>'.
 2338:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2339:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2340:     }
 2341:     $output .= '</td></tr>';
 2342:     $$rowtotal ++;
 2343:     return $output;
 2344: }
 2345: 
 2346: sub print_textbookcourses {
 2347:     my ($dom,$type,$settings,$rowtotal) = @_;
 2348:     my $rownum = 0;
 2349:     my $css_class;
 2350:     my $itemcount = 1;
 2351:     my $maxnum = 0;
 2352:     my $bookshash;
 2353:     if (ref($settings) eq 'HASH') {
 2354:         $bookshash = $settings->{$type};
 2355:     }
 2356:     my %ordered;
 2357:     if (ref($bookshash) eq 'HASH') {
 2358:         foreach my $item (keys(%{$bookshash})) {
 2359:             if (ref($bookshash->{$item}) eq 'HASH') {
 2360:                 my $num = $bookshash->{$item}{'order'};
 2361:                 $ordered{$num} = $item;
 2362:             }
 2363:         }
 2364:     }
 2365:     my $confname = $dom.'-domainconfig';
 2366:     my $switchserver = &check_switchserver($dom,$confname);
 2367:     my $maxnum = scalar(keys(%ordered));
 2368:     my $datatable;
 2369:     if (keys(%ordered)) {
 2370:         my @items = sort { $a <=> $b } keys(%ordered);
 2371:         for (my $i=0; $i<@items; $i++) {
 2372:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2373:             my $key = $ordered{$items[$i]};
 2374:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2375:             my $coursetitle = $coursehash{'description'};
 2376:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2377:             if (ref($bookshash->{$key}) eq 'HASH') {
 2378:                 $subject = $bookshash->{$key}->{'subject'};
 2379:                 $title = $bookshash->{$key}->{'title'};
 2380:                 if ($type eq 'textbooks') {
 2381:                     $publisher = $bookshash->{$key}->{'publisher'};
 2382:                     $author = $bookshash->{$key}->{'author'};
 2383:                     $image = $bookshash->{$key}->{'image'};
 2384:                     if ($image ne '') {
 2385:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2386:                         my $imagethumb = "$path/tn-".$imagefile;
 2387:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2388:                     }
 2389:                 }
 2390:             }
 2391:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2392:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2393:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2394:             for (my $k=0; $k<=$maxnum; $k++) {
 2395:                 my $vpos = $k+1;
 2396:                 my $selstr;
 2397:                 if ($k == $i) {
 2398:                     $selstr = ' selected="selected" ';
 2399:                 }
 2400:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2401:             }
 2402:             $datatable .= '</select>'.('&nbsp;'x2).
 2403:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2404:                 &mt('Delete?').'</label></span></td>'.
 2405:                 '<td colspan="2">'.
 2406:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2407:                 ('&nbsp;'x2).
 2408:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2409:             if ($type eq 'textbooks') {
 2410:                 $datatable .= ('&nbsp;'x2).
 2411:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2412:                               ('&nbsp;'x2).
 2413:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2414:                               ('&nbsp;'x2).
 2415:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2416:                 if ($image) {
 2417:                     $datatable .= $imgsrc.
 2418:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2419:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2420:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2421:                 }
 2422:                 if ($switchserver) {
 2423:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2424:                 } else {
 2425:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2426:                 }
 2427:             }
 2428:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2429:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2430:                           $coursetitle.'</span></td></tr>'."\n";
 2431:             $itemcount ++;
 2432:         }
 2433:     }
 2434:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2435:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2436:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2437:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2438:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2439:     for (my $k=0; $k<$maxnum+1; $k++) {
 2440:         my $vpos = $k+1;
 2441:         my $selstr;
 2442:         if ($k == $maxnum) {
 2443:             $selstr = ' selected="selected" ';
 2444:         }
 2445:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2446:     }
 2447:     $datatable .= '</select>&nbsp;'."\n".
 2448:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2449:                   '<td colspan="2">'.
 2450:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2451:                   ('&nbsp;'x2).
 2452:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2453:                   ('&nbsp;'x2);
 2454:     if ($type eq 'textbooks') {
 2455:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2456:                       ('&nbsp;'x2).
 2457:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2458:                       ('&nbsp;'x2).
 2459:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2460:         if ($switchserver) {
 2461:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2462:         } else {
 2463:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2464:         }
 2465:     }
 2466:     $datatable .= '</span>'."\n".
 2467:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2468:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2469:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2470:                   &Apache::loncommon::selectcourse_link
 2471:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course');
 2472:                   '</span></td>'."\n".
 2473:                   '</tr>'."\n";
 2474:     $itemcount ++;
 2475:     return $datatable;
 2476: }
 2477: 
 2478: sub textbookcourses_javascript {
 2479:     my ($settings) = @_;
 2480:     return unless(ref($settings) eq 'HASH');
 2481:     my (%ordered,%total,%jstext);
 2482:     foreach my $type ('textbooks','templates') {
 2483:         $total{$type} = 0;
 2484:         if (ref($settings->{$type}) eq 'HASH') {
 2485:             foreach my $item (keys(%{$settings->{$type}})) {
 2486:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2487:                     my $num = $settings->{$type}->{$item}{'order'};
 2488:                     $ordered{$type}{$num} = $item;
 2489:                 }
 2490:             }
 2491:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2492:         }
 2493:         my @jsarray = ();
 2494:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2495:             push(@jsarray,$ordered{$type}{$item});
 2496:         }
 2497:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2498:     }
 2499:     return <<"ENDSCRIPT";
 2500: <script type="text/javascript">
 2501: // <![CDATA[
 2502: function reorderBooks(form,item,caller) {
 2503:     var changedVal;
 2504: $jstext{'textbooks'};
 2505: $jstext{'templates'};
 2506:     var newpos;
 2507:     var maxh;
 2508:     if (caller == 'textbooks') {  
 2509:         newpos = 'textbooks_addbook_pos';
 2510:         maxh = 1 + $total{'textbooks'};
 2511:     } else {
 2512:         newpos = 'templates_addbook_pos';
 2513:         maxh = 1 + $total{'templates'};
 2514:     }
 2515:     var current = new Array;
 2516:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2517:     if (item == newpos) {
 2518:         changedVal = newitemVal;
 2519:     } else {
 2520:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2521:         current[newitemVal] = newpos;
 2522:     }
 2523:     if (caller == 'textbooks') {
 2524:         for (var i=0; i<textbooks.length; i++) {
 2525:             var elementName = 'textbooks_'+textbooks[i];
 2526:             if (elementName != item) {
 2527:                 if (form.elements[elementName]) {
 2528:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2529:                     current[currVal] = elementName;
 2530:                 }
 2531:             }
 2532:         }
 2533:     }
 2534:     if (caller == 'templates') {
 2535:         for (var i=0; i<templates.length; i++) {
 2536:             var elementName = 'templates_'+templates[i];
 2537:             if (elementName != item) {
 2538:                 if (form.elements[elementName]) {
 2539:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2540:                     current[currVal] = elementName;
 2541:                 }
 2542:             }
 2543:         }
 2544:     }
 2545:     var oldVal;
 2546:     for (var j=0; j<maxh; j++) {
 2547:         if (current[j] == undefined) {
 2548:             oldVal = j;
 2549:         }
 2550:     }
 2551:     if (oldVal < changedVal) {
 2552:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2553:            var elementName = current[k];
 2554:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2555:         }
 2556:     } else {
 2557:         for (var k=changedVal; k<oldVal; k++) {
 2558:             var elementName = current[k];
 2559:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2560:         }
 2561:     }
 2562:     return;
 2563: }
 2564: 
 2565: // ]]>
 2566: </script>
 2567: 
 2568: ENDSCRIPT
 2569: }
 2570: 
 2571: sub ltitools_javascript {
 2572:     my ($settings) = @_;
 2573:     return unless(ref($settings) eq 'HASH');
 2574:     my (%ordered,$total,%jstext);
 2575:     $total = 0;
 2576:     foreach my $item (keys(%{$settings})) {
 2577:         if (ref($settings->{$item}) eq 'HASH') {
 2578:             my $num = $settings->{$item}{'order'};
 2579:             $ordered{$num} = $item;
 2580:         }
 2581:     }
 2582:     $total = scalar(keys(%{$settings}));
 2583:     my @jsarray = ();
 2584:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2585:         push(@jsarray,$ordered{$item});
 2586:     }
 2587:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 2588:     return <<"ENDSCRIPT";
 2589: <script type="text/javascript">
 2590: // <![CDATA[
 2591: function reorderLTI(form,item) {
 2592:     var changedVal;
 2593: $jstext
 2594:     var newpos = 'ltitools_add_pos';
 2595:     var maxh = 1 + $total;
 2596:     var current = new Array;
 2597:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2598:     if (item == newpos) {
 2599:         changedVal = newitemVal;
 2600:     } else {
 2601:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2602:         current[newitemVal] = newpos;
 2603:     }
 2604:     for (var i=0; i<ltitools.length; i++) {
 2605:         var elementName = 'ltitools_'+ltitools[i];
 2606:         if (elementName != item) {
 2607:             if (form.elements[elementName]) {
 2608:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2609:                 current[currVal] = elementName;
 2610:             }
 2611:         }
 2612:     }
 2613:     var oldVal;
 2614:     for (var j=0; j<maxh; j++) {
 2615:         if (current[j] == undefined) {
 2616:             oldVal = j;
 2617:         }
 2618:     }
 2619:     if (oldVal < changedVal) {
 2620:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2621:            var elementName = current[k];
 2622:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2623:         }
 2624:     } else {
 2625:         for (var k=changedVal; k<oldVal; k++) {
 2626:             var elementName = current[k];
 2627:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2628:         }
 2629:     }
 2630:     return;
 2631: }
 2632: 
 2633: // ]]>
 2634: </script>
 2635: 
 2636: ENDSCRIPT
 2637: }
 2638: 
 2639: sub print_autoenroll {
 2640:     my ($dom,$settings,$rowtotal) = @_;
 2641:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2642:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 2643:     if (ref($settings) eq 'HASH') {
 2644:         if (exists($settings->{'run'})) {
 2645:             if ($settings->{'run'} eq '0') {
 2646:                 $runoff = ' checked="checked" ';
 2647:                 $runon = ' ';
 2648:             } else {
 2649:                 $runon = ' checked="checked" ';
 2650:                 $runoff = ' ';
 2651:             }
 2652:         } else {
 2653:             if ($autorun) {
 2654:                 $runon = ' checked="checked" ';
 2655:                 $runoff = ' ';
 2656:             } else {
 2657:                 $runoff = ' checked="checked" ';
 2658:                 $runon = ' ';
 2659:             }
 2660:         }
 2661:         if (exists($settings->{'co-owners'})) {
 2662:             if ($settings->{'co-owners'} eq '0') {
 2663:                 $coownersoff = ' checked="checked" ';
 2664:                 $coownerson = ' ';
 2665:             } else {
 2666:                 $coownerson = ' checked="checked" ';
 2667:                 $coownersoff = ' ';
 2668:             }
 2669:         } else {
 2670:             $coownersoff = ' checked="checked" ';
 2671:             $coownerson = ' ';
 2672:         }
 2673:         if (exists($settings->{'sender_domain'})) {
 2674:             $defdom = $settings->{'sender_domain'};
 2675:         }
 2676:         if (exists($settings->{'autofailsafe'})) {
 2677:             $failsafe = $settings->{'autofailsafe'};
 2678:         }
 2679:     } else {
 2680:         if ($autorun) {
 2681:             $runon = ' checked="checked" ';
 2682:             $runoff = ' ';
 2683:         } else {
 2684:             $runoff = ' checked="checked" ';
 2685:             $runon = ' ';
 2686:         }
 2687:     }
 2688:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2689:     my $notif_sender;
 2690:     if (ref($settings) eq 'HASH') {
 2691:         $notif_sender = $settings->{'sender_uname'};
 2692:     }
 2693:     my $datatable='<tr class="LC_odd_row">'.
 2694:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2695:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2696:                   '<input type="radio" name="autoenroll_run"'.
 2697:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2698:                   '<label><input type="radio" name="autoenroll_run"'.
 2699:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2700:                   '</tr><tr>'.
 2701:                   '<td>'.&mt('Notification messages - sender').
 2702:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2703:                   &mt('username').':&nbsp;'.
 2704:                   '<input type="text" name="sender_uname" value="'.
 2705:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2706:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2707:                   '<tr class="LC_odd_row">'.
 2708:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2709:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2710:                   '<input type="radio" name="autoassign_coowners"'.
 2711:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2712:                   '<label><input type="radio" name="autoassign_coowners"'.
 2713:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2714:                   '</tr><tr>'.
 2715:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 2716:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2717:                   '<input type="text" name="autoenroll_failsafe"'.
 2718:                   ' value="'.$failsafe.'" size="4" /></td></tr>';
 2719:     $$rowtotal += 4;
 2720:     return $datatable;
 2721: }
 2722: 
 2723: sub print_autoupdate {
 2724:     my ($position,$dom,$settings,$rowtotal) = @_;
 2725:     my $datatable;
 2726:     if ($position eq 'top') {
 2727:         my $updateon = ' ';
 2728:         my $updateoff = ' checked="checked" ';
 2729:         my $classlistson = ' ';
 2730:         my $classlistsoff = ' checked="checked" ';
 2731:         if (ref($settings) eq 'HASH') {
 2732:             if ($settings->{'run'} eq '1') {
 2733:                 $updateon = $updateoff;
 2734:                 $updateoff = ' ';
 2735:             }
 2736:             if ($settings->{'classlists'} eq '1') {
 2737:                 $classlistson = $classlistsoff;
 2738:                 $classlistsoff = ' ';
 2739:             }
 2740:         }
 2741:         my %title = (
 2742:                    run => 'Auto-update active?',
 2743:                    classlists => 'Update information in classlists?',
 2744:                     );
 2745:         $datatable = '<tr class="LC_odd_row">'. 
 2746:                   '<td>'.&mt($title{'run'}).'</td>'.
 2747:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2748:                   '<input type="radio" name="autoupdate_run"'.
 2749:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2750:                   '<label><input type="radio" name="autoupdate_run"'.
 2751:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2752:                   '</tr><tr>'.
 2753:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2754:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2755:                   '<label><input type="radio" name="classlists"'.
 2756:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2757:                   '<label><input type="radio" name="classlists"'.
 2758:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2759:                   '</tr>';
 2760:         $$rowtotal += 2;
 2761:     } elsif ($position eq 'middle') {
 2762:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2763:         my $numinrow = 3;
 2764:         my $locknamesettings;
 2765:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2766:                                      $dom,$numinrow,$othertitle,
 2767:                                     'lockablenames');
 2768:         $$rowtotal ++;
 2769:     } else {
 2770:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2771:         my @fields = ('lastname','firstname','middlename','generation',
 2772:                       'permanentemail','id');
 2773:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2774:         my $numrows = 0;
 2775:         if (ref($types) eq 'ARRAY') {
 2776:             if (@{$types} > 0) {
 2777:                 $datatable = 
 2778:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2779:                                          \@fields,$types,\$numrows);
 2780:                     $$rowtotal += @{$types}; 
 2781:             }
 2782:         }
 2783:         $datatable .= 
 2784:             &usertype_update_row($settings,{'default' => $othertitle},
 2785:                                  \%fieldtitles,\@fields,['default'],
 2786:                                  \$numrows);
 2787:         $$rowtotal ++;     
 2788:     }
 2789:     return $datatable;
 2790: }
 2791: 
 2792: sub print_autocreate {
 2793:     my ($dom,$settings,$rowtotal) = @_;
 2794:     my (%createon,%createoff,%currhash);
 2795:     my @types = ('xml','req');
 2796:     if (ref($settings) eq 'HASH') {
 2797:         foreach my $item (@types) {
 2798:             $createoff{$item} = ' checked="checked" ';
 2799:             $createon{$item} = ' ';
 2800:             if (exists($settings->{$item})) {
 2801:                 if ($settings->{$item}) {
 2802:                     $createon{$item} = ' checked="checked" ';
 2803:                     $createoff{$item} = ' ';
 2804:                 }
 2805:             }
 2806:         }
 2807:         if ($settings->{'xmldc'} ne '') {
 2808:             $currhash{$settings->{'xmldc'}} = 1;
 2809:         }
 2810:     } else {
 2811:         foreach my $item (@types) {
 2812:             $createoff{$item} = ' checked="checked" ';
 2813:             $createon{$item} = ' ';
 2814:         }
 2815:     }
 2816:     $$rowtotal += 2;
 2817:     my $numinrow = 2;
 2818:     my $datatable='<tr class="LC_odd_row">'.
 2819:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2820:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2821:                   '<input type="radio" name="autocreate_xml"'.
 2822:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2823:                   '<label><input type="radio" name="autocreate_xml"'.
 2824:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2825:                   '</td></tr><tr>'.
 2826:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2827:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2828:                   '<input type="radio" name="autocreate_req"'.
 2829:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2830:                   '<label><input type="radio" name="autocreate_req"'.
 2831:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2832:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2833:                                                    'autocreate_xmldc',%currhash);
 2834:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2835:     if ($numdc > 1) {
 2836:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2837:                       '</td><td class="LC_left_item">';
 2838:     } else {
 2839:         $datatable .= &mt('Course creation processed as:').
 2840:                       '</td><td class="LC_right_item">';
 2841:     }
 2842:     $datatable .= $dctable.'</td></tr>';
 2843:     $$rowtotal += $rows;
 2844:     return $datatable;
 2845: }
 2846: 
 2847: sub print_directorysrch {
 2848:     my ($position,$dom,$settings,$rowtotal) = @_;
 2849:     my $datatable;
 2850:     if ($position eq 'top') {
 2851:         my $instsrchon = ' ';
 2852:         my $instsrchoff = ' checked="checked" ';
 2853:         my ($exacton,$containson,$beginson);
 2854:         my $instlocalon = ' ';
 2855:         my $instlocaloff = ' checked="checked" ';
 2856:         if (ref($settings) eq 'HASH') {
 2857:             if ($settings->{'available'} eq '1') {
 2858:                 $instsrchon = $instsrchoff;
 2859:                 $instsrchoff = ' ';
 2860:             }
 2861:             if ($settings->{'localonly'} eq '1') {
 2862:                 $instlocalon = $instlocaloff;
 2863:                 $instlocaloff = ' ';
 2864:             }
 2865:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2866:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 2867:                     if ($type eq 'exact') {
 2868:                         $exacton = ' checked="checked" ';
 2869:                     } elsif ($type eq 'contains') {
 2870:                         $containson = ' checked="checked" ';
 2871:                     } elsif ($type eq 'begins') {
 2872:                         $beginson = ' checked="checked" ';
 2873:                     }
 2874:                 }
 2875:             } else {
 2876:                 if ($settings->{'searchtypes'} eq 'exact') {
 2877:                     $exacton = ' checked="checked" ';
 2878:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 2879:                     $containson = ' checked="checked" ';
 2880:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 2881:                     $exacton = ' checked="checked" ';
 2882:                     $containson = ' checked="checked" ';
 2883:                 }
 2884:             }
 2885:         }
 2886:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2887:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2888: 
 2889:         my $numinrow = 4;
 2890:         my $cansrchrow = 0;
 2891:         $datatable='<tr class="LC_odd_row">'.
 2892:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 2893:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2894:                    '<input type="radio" name="dirsrch_available"'.
 2895:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2896:                    '<label><input type="radio" name="dirsrch_available"'.
 2897:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2898:                    '</tr><tr>'.
 2899:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 2900:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2901:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 2902:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2903:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 2904:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2905:                    '</tr>';
 2906:         $$rowtotal += 2;
 2907:         if (ref($usertypes) eq 'HASH') {
 2908:             if (keys(%{$usertypes}) > 0) {
 2909:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2910:                                              $numinrow,$othertitle,'cansearch');
 2911:                 $cansrchrow = 1;
 2912:             }
 2913:         }
 2914:         if ($cansrchrow) {
 2915:             $$rowtotal ++;
 2916:             $datatable .= '<tr>';
 2917:         } else {
 2918:             $datatable .= '<tr class="LC_odd_row">';
 2919:         }
 2920:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2921:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2922:         foreach my $title (@{$titleorder}) {
 2923:             if (defined($searchtitles->{$title})) {
 2924:                 my $check = ' ';
 2925:                 if (ref($settings) eq 'HASH') {
 2926:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2927:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2928:                             $check = ' checked="checked" ';
 2929:                         }
 2930:                     }
 2931:                 }
 2932:                 $datatable .= '<td class="LC_left_item">'.
 2933:                               '<span class="LC_nobreak"><label>'.
 2934:                               '<input type="checkbox" name="searchby" '.
 2935:                               'value="'.$title.'"'.$check.'/>'.
 2936:                               $searchtitles->{$title}.'</label></span></td>';
 2937:             }
 2938:         }
 2939:         $datatable .= '</tr></table></td></tr>';
 2940:         $$rowtotal ++;
 2941:         if ($cansrchrow) {
 2942:             $datatable .= '<tr class="LC_odd_row">';
 2943:         } else {
 2944:             $datatable .= '<tr>';
 2945:         }
 2946:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2947:                       '<td class="LC_left_item" colspan="2">'.
 2948:                       '<span class="LC_nobreak"><label>'.
 2949:                       '<input type="checkbox" name="searchtypes" '.
 2950:                       $exacton.' value="exact" />'.&mt('Exact match').
 2951:                       '</label>&nbsp;'.
 2952:                       '<label><input type="checkbox" name="searchtypes" '.
 2953:                       $beginson.' value="begins" />'.&mt('Begins with').
 2954:                       '</label>&nbsp;'.
 2955:                       '<label><input type="checkbox" name="searchtypes" '.
 2956:                       $containson.' value="contains" />'.&mt('Contains').
 2957:                       '</label></span></td></tr>';
 2958:         $$rowtotal ++;
 2959:     } else {
 2960:         my $domsrchon = ' checked="checked" ';
 2961:         my $domsrchoff = ' ';
 2962:         my $domlocalon = ' ';
 2963:         my $domlocaloff = ' checked="checked" ';
 2964:         if (ref($settings) eq 'HASH') {
 2965:             if ($settings->{'lclocalonly'} eq '1') {
 2966:                 $domlocalon = $domlocaloff;
 2967:                 $domlocaloff = ' ';
 2968:             }
 2969:             if ($settings->{'lcavailable'} eq '0') {
 2970:                 $domsrchoff = $domsrchon;
 2971:                 $domsrchon = ' ';
 2972:             }
 2973:         }
 2974:         $datatable='<tr class="LC_odd_row">'.
 2975:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 2976:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2977:                       '<input type="radio" name="dirsrch_domavailable"'.
 2978:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2979:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 2980:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2981:                       '</tr><tr>'.
 2982:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 2983:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2984:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 2985:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2986:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 2987:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2988:                       '</tr>';
 2989:         $$rowtotal += 2;
 2990:     }
 2991:     return $datatable;
 2992: }
 2993: 
 2994: sub print_contacts {
 2995:     my ($position,$dom,$settings,$rowtotal) = @_;
 2996:     my $datatable;
 2997:     my @contacts = ('adminemail','supportemail');
 2998:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 2999:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings);
 3000:     if ($position eq 'top') {
 3001:         if (ref($settings) eq 'HASH') {
 3002:             foreach my $item (@contacts) {
 3003:                 if (exists($settings->{$item})) {
 3004:                     $to{$item} = $settings->{$item};
 3005:                 }
 3006:             }
 3007:         }
 3008:     } elsif ($position eq 'middle') {
 3009:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 3010:                      'updatesmail','idconflictsmail');
 3011:         foreach my $type (@mailings) {
 3012:             $otheremails{$type} = '';
 3013:         }
 3014:     } else {
 3015:         @mailings = ('helpdeskmail','otherdomsmail');
 3016:         foreach my $type (@mailings) {
 3017:             $otheremails{$type} = '';
 3018:         }
 3019:         $bccemails{'helpdeskmail'} = '';
 3020:         $bccemails{'otherdomsmail'} = '';
 3021:         $includestr{'helpdeskmail'} = '';
 3022:         $includestr{'otherdomsmail'} = '';
 3023:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 3024:     }
 3025:     if (ref($settings) eq 'HASH') {
 3026:         unless ($position eq 'top') {
 3027:             foreach my $type (@mailings) {
 3028:                 if (exists($settings->{$type})) {
 3029:                     if (ref($settings->{$type}) eq 'HASH') {
 3030:                         foreach my $item (@contacts) {
 3031:                             if ($settings->{$type}{$item}) {
 3032:                                 $checked{$type}{$item} = ' checked="checked" ';
 3033:                             }
 3034:                         }
 3035:                         $otheremails{$type} = $settings->{$type}{'others'};
 3036:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3037:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 3038:                             if ($settings->{$type}{'include'} ne '') {
 3039:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3040:                                 $includestr{$type} = &unescape($includestr{$type});
 3041:                             }
 3042:                         }
 3043:                     }
 3044:                 } elsif ($type eq 'lonstatusmail') {
 3045:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3046:                 }
 3047:             }
 3048:         }
 3049:         if ($position eq 'bottom') {
 3050:             foreach my $type (@mailings) {
 3051:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 3052:                 if ($settings->{$type}{'include'} ne '') {
 3053:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3054:                     $includestr{$type} = &unescape($includestr{$type});
 3055:                 }
 3056:             }
 3057:             if (ref($settings->{'helpform'}) eq 'HASH') {
 3058:                 if (ref($fields) eq 'ARRAY') {
 3059:                     foreach my $field (@{$fields}) {
 3060:                         $currfield{$field} = $settings->{'helpform'}{$field};
 3061:                     }
 3062:                 }
 3063:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 3064:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 3065:                 } else {
 3066:                     $maxsize = '1.0';
 3067:                 }
 3068:             } else {
 3069:                 if (ref($fields) eq 'ARRAY') {
 3070:                     foreach my $field (@{$fields}) {
 3071:                         $currfield{$field} = 'yes';
 3072:                     }
 3073:                 }
 3074:                 $maxsize = '1.0';
 3075:             }
 3076:         }
 3077:     } else {
 3078:         if ($position eq 'top') {
 3079:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 3080:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 3081:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 3082:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 3083:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3084:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 3085:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 3086:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 3087:         } elsif ($position eq 'bottom') {
 3088:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 3089:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 3090:             if (ref($fields) eq 'ARRAY') {
 3091:                 foreach my $field (@{$fields}) {
 3092:                     $currfield{$field} = 'yes';
 3093:                 }
 3094:             }
 3095:             $maxsize = '1.0';
 3096:         }
 3097:     }
 3098:     my ($titles,$short_titles) = &contact_titles();
 3099:     my $rownum = 0;
 3100:     my $css_class;
 3101:     if ($position eq 'top') {
 3102:         foreach my $item (@contacts) {
 3103:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3104:             $datatable .= '<tr'.$css_class.'>'. 
 3105:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 3106:                           '</span></td><td class="LC_right_item">'.
 3107:                           '<input type="text" name="'.$item.'" value="'.
 3108:                           $to{$item}.'" /></td></tr>';
 3109:             $rownum ++;
 3110:         }
 3111:     } else {
 3112:         foreach my $type (@mailings) {
 3113:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3114:             $datatable .= '<tr'.$css_class.'>'.
 3115:                           '<td><span class="LC_nobreak">'.
 3116:                           $titles->{$type}.': </span></td>'.
 3117:                           '<td class="LC_left_item">';
 3118:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3119:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 3120:             }
 3121:             $datatable .= '<span class="LC_nobreak">';
 3122:             foreach my $item (@contacts) {
 3123:                 $datatable .= '<label>'.
 3124:                               '<input type="checkbox" name="'.$type.'"'.
 3125:                               $checked{$type}{$item}.
 3126:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 3127:                               '</label>&nbsp;';
 3128:             }
 3129:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3130:                           '<input type="text" name="'.$type.'_others" '.
 3131:                           'value="'.$otheremails{$type}.'"  />';
 3132:             my %locchecked;
 3133:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3134:                 foreach my $loc ('s','b') {
 3135:                     if ($includeloc{$type} eq $loc) {
 3136:                         $locchecked{$loc} = ' checked="checked"';
 3137:                         last;
 3138:                     }
 3139:                 }
 3140:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3141:                               '<input type="text" name="'.$type.'_bcc" '.
 3142:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 3143:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3144:                               &mt('Text automatically added to e-mail:').' '.
 3145:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br >'.
 3146:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3147:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3148:                               ('&nbsp;'x2).
 3149:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3150:                               '</span></fieldset>';
 3151:             }
 3152:             $datatable .= '</td></tr>'."\n";
 3153:             $rownum ++;
 3154:         }
 3155:     }
 3156:     if ($position eq 'middle') {
 3157:         my %choices;
 3158:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 3159:                                        &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3160:                                        &mt('LON-CAPA core group - MSU'),600,500));
 3161:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3162:                                         &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3163:                                         &mt('LON-CAPA core group - MSU'),600,500));
 3164:         my @toggles = ('reporterrors','reportupdates');
 3165:         my %defaultchecked = ('reporterrors'  => 'on',
 3166:                               'reportupdates' => 'on');
 3167:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3168:                                                    \%choices,$rownum);
 3169:         $datatable .= $reports;
 3170:     } elsif ($position eq 'bottom') {
 3171:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3172:         $datatable .= '<tr'.$css_class.'>'.
 3173:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 3174:                       &mt('(e-mail, subject, and description always shown)').
 3175:                       '</td><td class="LC_left_item">';
 3176:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 3177:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 3178:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 3179:             foreach my $field (@{$fields}) {
 3180:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 3181:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 3182:                     $datatable .= ' '.&mt('(logged-in users)');
 3183:                 }
 3184:                 $datatable .='</td><td>';
 3185:                 my $clickaction;
 3186:                 if ($field eq 'screenshot') {
 3187:                     $clickaction = ' onclick="screenshotSize(this);"';
 3188:                 }
 3189:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 3190:                     foreach my $option (@{$possoptions->{$field}}) {
 3191:                         my $checked;
 3192:                         if ($currfield{$field} eq $option) {
 3193:                             $checked = ' checked="checked"';
 3194:                         }
 3195:                         $datatable .= '<span class="LC_nobreak"><label>'.
 3196:                                       '<input type="radio" name="helpform_'.$field.'" '.
 3197:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 3198:                                       '</label></span>'.('&nbsp;'x2);
 3199:                     }
 3200:                 }
 3201:                 if ($field eq 'screenshot') {
 3202:                     my $display;
 3203:                     if ($currfield{$field} eq 'no') {
 3204:                         $display = ' style="display:none"';
 3205:                     }
 3206:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.' />'.
 3207:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3208:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3209:                 }
 3210:                 $datatable .= '</td></tr>';
 3211:             }
 3212:             $datatable .= '</table>';
 3213:         }
 3214:         $datatable .= '</td></tr>'."\n";
 3215:         $rownum ++;
 3216:     }
 3217:     $$rowtotal += $rownum;
 3218:     return $datatable;
 3219: }
 3220: 
 3221: sub contacts_javascript {
 3222:     return <<"ENDSCRIPT";
 3223: 
 3224: <script type="text/javascript">
 3225: // <![CDATA[
 3226: 
 3227: function screenshotSize(field) {
 3228:     if (document.getElementById('help_screenshotsize')) {
 3229:         if (field.value == 'no') {
 3230:             document.getElementById('help_screenshotsize').style.display="none";
 3231:         } else {
 3232:             document.getElementById('help_screenshotsize').style.display="";
 3233:         }
 3234:     }
 3235:     return;
 3236: }
 3237: 
 3238: // ]]>
 3239: </script>
 3240: 
 3241: ENDSCRIPT
 3242: }
 3243: 
 3244: sub print_helpsettings {
 3245:     my ($position,$dom,$settings,$rowtotal) = @_;
 3246:     my $confname = $dom.'-domainconfig';
 3247:     my $formname = 'display';
 3248:     my ($datatable,$itemcount);
 3249:     if ($position eq 'top') {
 3250:         $itemcount = 1;
 3251:         my (%choices,%defaultchecked,@toggles);
 3252:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 3253:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 3254:                                      &mt('LON-CAPA bug tracker'),600,500));
 3255:         %defaultchecked = ('submitbugs' => 'on');
 3256:         @toggles = ('submitbugs');
 3257:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3258:                                                      \%choices,$itemcount);
 3259:         $$rowtotal ++;
 3260:     } else {
 3261:         my $css_class;
 3262:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 3263:         my (%customroles,%ordered,%current);
 3264:         if (ref($settings->{'adhoc'}) eq 'HASH') {
 3265:             %current = %{$settings->{'adhoc'}};
 3266:         }
 3267:         my $count = 0;
 3268:         foreach my $key (sort(keys(%existing))) {
 3269:             if ($key=~/^rolesdef\_(\w+)$/) {
 3270:                 my $rolename = $1;
 3271:                 my (%privs,$order);
 3272:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3273:                 $customroles{$rolename} = \%privs;
 3274:                 if (ref($current{$rolename}) eq 'HASH') {
 3275:                     $order = $current{$rolename}{'order'};
 3276:                 }
 3277:                 if ($order eq '') {
 3278:                     $order = $count;
 3279:                 }
 3280:                 $ordered{$order} = $rolename;
 3281:                 $count++;
 3282:             }
 3283:         }
 3284:         my $maxnum = scalar(keys(%ordered));
 3285:         my @roles_by_num = ();
 3286:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3287:             push(@roles_by_num,$item);
 3288:         }
 3289:         my $context = 'domprefs';
 3290:         my $crstype = 'Course';
 3291:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3292:         my @accesstypes = ('all','dh','da','none');
 3293:         my ($numstatustypes,@jsarray);
 3294:         if (ref($types) eq 'ARRAY') {
 3295:             if (@{$types} > 0) {
 3296:                 $numstatustypes = scalar(@{$types});
 3297:                 push(@accesstypes,'status');
 3298:                 @jsarray = ('bystatus');
 3299:             }
 3300:         }
 3301:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 3302:         if (keys(%domhelpdesk)) {
 3303:             push(@accesstypes,('inc','exc'));
 3304:             push(@jsarray,('notinc','notexc'));
 3305:         }
 3306:         my $hiddenstr = join("','",@jsarray);
 3307:         $datatable .= &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname);
 3308:         my $context = 'domprefs';
 3309:         my $crstype = 'Course';
 3310:         my $prefix = 'helproles_';
 3311:         my $add_class = 'LC_hidden';
 3312:         foreach my $num (@roles_by_num) {
 3313:             my $role = $ordered{$num};
 3314:             my ($desc,$access,@statuses);
 3315:             if (ref($current{$role}) eq 'HASH') {
 3316:                 $desc = $current{$role}{'desc'};
 3317:                 $access = $current{$role}{'access'};
 3318:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 3319:                     @statuses = @{$current{$role}{'insttypes'}};
 3320:                 }
 3321:             }
 3322:             if ($desc eq '') {
 3323:                 $desc = $role;
 3324:             }
 3325:             my $identifier = 'custhelp'.$num;
 3326:             my %full=();
 3327:             my %levels= (
 3328:                          course => {},
 3329:                          domain => {},
 3330:                          system => {},
 3331:                         );
 3332:             my %levelscurrent=(
 3333:                                course => {},
 3334:                                domain => {},
 3335:                                system => {},
 3336:                               );
 3337:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 3338:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3339:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3340:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 3341:             $datatable .= '<tr '.$css_class.'><td valign="top"><b>'.$role.'</b><br />'.
 3342:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 3343:             for (my $k=0; $k<=$maxnum; $k++) {
 3344:                 my $vpos = $k+1;
 3345:                 my $selstr;
 3346:                 if ($k == $num) {
 3347:                     $selstr = ' selected="selected" ';
 3348:                 }
 3349:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3350:             }
 3351:             $datatable .= '</select>'.('&nbsp;'x2).
 3352:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 3353:                           '</td>'.
 3354:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3355:                           &mt('Name shown to users:').
 3356:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 3357:                           '</fieldset>'.
 3358:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 3359:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 3360:                           '<fieldset>'.
 3361:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 3362:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 3363:                                                                    \%levelscurrent,$identifier,
 3364:                                                                    'LC_hidden',$prefix.$num.'_privs').
 3365:                           '</fieldset></td>';
 3366:             $itemcount ++;
 3367:         }
 3368:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3369:         my $newcust = 'custhelp'.$count;
 3370:         my (%privs,%levelscurrent);
 3371:         my %full=();
 3372:         my %levels= (
 3373:                      course => {},
 3374:                      domain => {},
 3375:                      system => {},
 3376:                     );
 3377:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 3378:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3379:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 3380:         $datatable .= '<tr '.$css_class.'><td valign="top"><span class="LC_nobreak"><label>'.
 3381:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 3382:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 3383:         for (my $k=0; $k<$maxnum+1; $k++) {
 3384:             my $vpos = $k+1;
 3385:             my $selstr;
 3386:             if ($k == $maxnum) {
 3387:                 $selstr = ' selected="selected" ';
 3388:             }
 3389:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3390:         }
 3391:         $datatable .= '</select>&nbsp;'."\n".
 3392:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 3393:                       '</label></span></td>'.
 3394:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3395:                       '<span class="LC_nobreak">'.
 3396:                       &mt('Internal name:').
 3397:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 3398:                       '</span>'.('&nbsp;'x4).
 3399:                       '<span class="LC_nobreak">'.
 3400:                       &mt('Name shown to users:').
 3401:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 3402:                       '</span></fieldset>'.
 3403:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 3404:                                              $usertypes,$types,\%domhelpdesk).
 3405:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 3406:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 3407:                                                                 \@templateroles,$newcust).
 3408:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 3409:                                                                \%levelscurrent,$newcust).
 3410:                       '</fieldset></td></tr>';
 3411:         $count ++;
 3412:         $$rowtotal += $count;
 3413:     }
 3414:     return $datatable;
 3415: }
 3416: 
 3417: sub adhocbutton {
 3418:     my ($prefix,$num,$field,$visibility) = @_;
 3419:     my %lt = &Apache::lonlocal::texthash(
 3420:                                           show => 'Show details',
 3421:                                           hide => 'Hide details',
 3422:                                         );
 3423:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 3424:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 3425:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 3426:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 3427: }
 3428: 
 3429: sub helpsettings_javascript {
 3430:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 3431:     return unless(ref($roles_by_num) eq 'ARRAY');
 3432:     my %html_js_lt = &Apache::lonlocal::texthash(
 3433:                                           show => 'Show details',
 3434:                                           hide => 'Hide details',
 3435:                                         );
 3436:     &html_escape(\%html_js_lt);
 3437:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 3438:     return <<"ENDSCRIPT";
 3439: <script type="text/javascript">
 3440: // <![CDATA[
 3441: 
 3442: function reorderHelpRoles(form,item) {
 3443:     var changedVal;
 3444: $jstext
 3445:     var newpos = 'helproles_${total}_pos';
 3446:     var maxh = 1 + $total;
 3447:     var current = new Array();
 3448:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3449:     if (item == newpos) {
 3450:         changedVal = newitemVal;
 3451:     } else {
 3452:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3453:         current[newitemVal] = newpos;
 3454:     }
 3455:     for (var i=0; i<helproles.length; i++) {
 3456:         var elementName = 'helproles_'+helproles[i]+'_pos';
 3457:         if (elementName != item) {
 3458:             if (form.elements[elementName]) {
 3459:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3460:                 current[currVal] = elementName;
 3461:             }
 3462:         }
 3463:     }
 3464:     var oldVal;
 3465:     for (var j=0; j<maxh; j++) {
 3466:         if (current[j] == undefined) {
 3467:             oldVal = j;
 3468:         }
 3469:     }
 3470:     if (oldVal < changedVal) {
 3471:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3472:            var elementName = current[k];
 3473:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3474:         }
 3475:     } else {
 3476:         for (var k=changedVal; k<oldVal; k++) {
 3477:             var elementName = current[k];
 3478:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3479:         }
 3480:     }
 3481:     return;
 3482: }
 3483: 
 3484: function helpdeskAccess(num) {
 3485:     var curraccess = null;
 3486:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 3487:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 3488:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 3489:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 3490:             }
 3491:         }
 3492:     }
 3493:     var shown = Array();
 3494:     var hidden = Array();
 3495:     if (curraccess == 'none') {
 3496:         hidden = Array('$hiddenstr');
 3497:     } else {
 3498:         if (curraccess == 'status') {
 3499:             shown = Array('bystatus');
 3500:             hidden = Array('notinc','notexc');
 3501:         } else {
 3502:             if (curraccess == 'exc') {
 3503:                 shown = Array('notexc');
 3504:                 hidden = Array('notinc','bystatus');
 3505:             }
 3506:             if (curraccess == 'inc') {
 3507:                 shown = Array('notinc');
 3508:                 hidden = Array('notexc','bystatus');
 3509:             }
 3510:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 3511:                 hidden = Array('notinc','notexc','bystatus');
 3512:             }
 3513:         }
 3514:     }
 3515:     if (hidden.length > 0) {
 3516:         for (var i=0; i<hidden.length; i++) {
 3517:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 3518:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 3519:             }
 3520:         }
 3521:     }
 3522:     if (shown.length > 0) {
 3523:         for (var i=0; i<shown.length; i++) {
 3524:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 3525:                 if (shown[i] == 'privs') {
 3526:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 3527:                 } else {
 3528:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 3529:                 }
 3530:             }
 3531:         }
 3532:     }
 3533:     return;
 3534: }
 3535: 
 3536: function toggleHelpdeskItem(num,field) {
 3537:     if (document.getElementById('helproles_'+num+'_'+field)) {
 3538:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 3539:             document.getElementById('helproles_'+num+'_'+field).className =
 3540:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 3541:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3542:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 3543:             }
 3544:         } else {
 3545:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 3546:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3547:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 3548:             }
 3549:         }
 3550:     }
 3551:     return;
 3552: }
 3553: 
 3554: // ]]>
 3555: </script>
 3556: 
 3557: ENDSCRIPT
 3558: }
 3559: 
 3560: sub helpdeskroles_access {
 3561:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 3562:         $usertypes,$types,$domhelpdesk) = @_;
 3563:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 3564:     my %lt = &Apache::lonlocal::texthash(
 3565:                     'rou'    => 'Role usage',
 3566:                     'whi'    => 'Which helpdesk personnel may use this role?',
 3567:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 3568:                     'dh'     => 'All with domain helpdesk role',
 3569:                     'da'     => 'All with domain helpdesk assistant role',
 3570:                     'none'   => 'None',
 3571:                     'status' => 'Determined based on institutional status',
 3572:                     'inc'    => 'Include all, but exclude specific personnel',
 3573:                     'exc'    => 'Exclude all, but include specific personnel',
 3574:                   );
 3575:     my %usecheck = (
 3576:                      all => ' checked="checked"',
 3577:                    );
 3578:     my %displaydiv = (
 3579:                       status => 'none',
 3580:                       inc    => 'none',
 3581:                       exc    => 'none',
 3582:                       priv   => 'block',
 3583:                      );
 3584:     my $output;
 3585:     if (ref($current) eq 'HASH') {
 3586:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 3587:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 3588:                 $usecheck{$current->{access}} = $usecheck{'all'};
 3589:                 delete($usecheck{'all'});
 3590:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 3591:                     my $access = $1;
 3592:                     $displaydiv{$access} = 'inline';
 3593:                 } elsif ($current->{access} eq 'none') {
 3594:                     $displaydiv{'priv'} = 'none';
 3595:                 }
 3596:             }
 3597:         }
 3598:     }
 3599:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 3600:               '<p>'.$lt{'whi'}.'</p>';
 3601:     foreach my $access (@{$accesstypes}) {
 3602:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 3603:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 3604:                    $lt{$access}.'</label>';
 3605:         if ($access eq 'status') {
 3606:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 3607:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 3608:                                                                  $othertitle,$usertypes,$types).
 3609:                        '</div>';
 3610:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 3611:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 3612:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3613:                        '</div>';
 3614:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 3615:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 3616:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3617:                        '</div>';
 3618:         }
 3619:         $output .= '</p>';
 3620:     }
 3621:     $output .= '</fieldset>';
 3622:     return $output;
 3623: }
 3624: 
 3625: sub radiobutton_prefs {
 3626:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 3627:         $additional,$align) = @_;
 3628:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 3629:                    (ref($choices) eq 'HASH'));
 3630: 
 3631:     my (%checkedon,%checkedoff,$datatable,$css_class);
 3632: 
 3633:     foreach my $item (@{$toggles}) {
 3634:         if ($defaultchecked->{$item} eq 'on') {
 3635:             $checkedon{$item} = ' checked="checked" ';
 3636:             $checkedoff{$item} = ' ';
 3637:         } elsif ($defaultchecked->{$item} eq 'off') {
 3638:             $checkedoff{$item} = ' checked="checked" ';
 3639:             $checkedon{$item} = ' ';
 3640:         }
 3641:     }
 3642:     if (ref($settings) eq 'HASH') {
 3643:         foreach my $item (@{$toggles}) {
 3644:             if ($settings->{$item} eq '1') {
 3645:                 $checkedon{$item} =  ' checked="checked" ';
 3646:                 $checkedoff{$item} = ' ';
 3647:             } elsif ($settings->{$item} eq '0') {
 3648:                 $checkedoff{$item} =  ' checked="checked" ';
 3649:                 $checkedon{$item} = ' ';
 3650:             }
 3651:         }
 3652:     }
 3653:     if ($onclick) {
 3654:         $onclick = ' onclick="'.$onclick.'"';
 3655:     }
 3656:     foreach my $item (@{$toggles}) {
 3657:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3658:         $datatable .=
 3659:             '<tr'.$css_class.'><td valign="top">'.
 3660:             '<span class="LC_nobreak">'.$choices->{$item}.
 3661:             '</span></td>';
 3662:         if ($align eq 'left') {
 3663:             $datatable .= '<td class="LC_left_item">';
 3664:         } else {
 3665:             $datatable .= '<td class="LC_right_item">';
 3666:         }
 3667:         $datatable .=
 3668:             '<span class="LC_nobreak">'.
 3669:             '<label><input type="radio" name="'.
 3670:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 3671:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 3672:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3673:             '</span>'.$additional.
 3674:             '</td>'.
 3675:             '</tr>';
 3676:         $itemcount ++;
 3677:     }
 3678:     return ($datatable,$itemcount);
 3679: }
 3680: 
 3681: sub print_ltitools {
 3682:     my ($dom,$settings,$rowtotal) = @_;
 3683:     my $rownum = 0;
 3684:     my $css_class;
 3685:     my $itemcount = 1;
 3686:     my $maxnum = 0;
 3687:     my %ordered;
 3688:     if (ref($settings) eq 'HASH') {
 3689:         foreach my $item (keys(%{$settings})) {
 3690:             if (ref($settings->{$item}) eq 'HASH') {
 3691:                 my $num = $settings->{$item}{'order'};
 3692:                 $ordered{$num} = $item;
 3693:             }
 3694:         }
 3695:     }
 3696:     my $confname = $dom.'-domainconfig';
 3697:     my $switchserver = &check_switchserver($dom,$confname);
 3698:     my $maxnum = scalar(keys(%ordered));
 3699:     my $datatable = &ltitools_javascript($settings);
 3700:     my %lt = &ltitools_names();
 3701:     my @courseroles = ('cc','in','ta','ep','st');
 3702:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 3703:     my @fields = ('fullname','firstname','lastname','email','user','roles');
 3704:     if (keys(%ordered)) {
 3705:         my @items = sort { $a <=> $b } keys(%ordered);
 3706:         for (my $i=0; $i<@items; $i++) {
 3707:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3708:             my $item = $ordered{$items[$i]};
 3709:             my ($title,$key,$secret,$url,$imgsrc,$version);
 3710:             if (ref($settings->{$item}) eq 'HASH') {
 3711:                 $title = $settings->{$item}->{'title'};
 3712:                 $url = $settings->{$item}->{'url'};
 3713:                 $key = $settings->{$item}->{'key'};
 3714:                 $secret = $settings->{$item}->{'secret'};
 3715:                 my $image = $settings->{$item}->{'image'};
 3716:                 if ($image ne '') {
 3717:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 3718:                 }
 3719:             }
 3720:             my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_".$item."'".');"';
 3721:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3722:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 3723:             for (my $k=0; $k<=$maxnum; $k++) {
 3724:                 my $vpos = $k+1;
 3725:                 my $selstr;
 3726:                 if ($k == $i) {
 3727:                     $selstr = ' selected="selected" ';
 3728:                 }
 3729:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3730:             }
 3731:             $datatable .= '</select>'.('&nbsp;'x2).
 3732:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 3733:                 &mt('Delete?').'</label></span></td>'.
 3734:                 '<td colspan="2">'.
 3735:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 3736:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 3737:                 ('&nbsp;'x2).
 3738:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 3739:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 3740:                 ('&nbsp;'x2).
 3741:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 3742:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 3743:                 '<br /><br />'.
 3744:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_url_'.$i.'"'.
 3745:                 ' value="'.$url.'" /></span>'.
 3746:                 ('&nbsp;'x2).
 3747:                 '<span class="LC_nobreak">'.$lt{'key'}.
 3748:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 3749:                 ('&nbsp;'x2).
 3750:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 3751:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 3752:                 '<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>'.
 3753:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 3754:                 '</fieldset>'.
 3755:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 3756:                 '<span class="LC_nobreak">'.&mt('Display target:');
 3757:             my %currdisp;
 3758:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 3759:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 3760:                     $currdisp{'window'} = ' checked="checked"';
 3761:                 } else {
 3762:                     $currdisp{'iframe'} = ' checked="checked"';
 3763:                 }
 3764:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 3765:                     $currdisp{'width'} = $1;
 3766:                 }
 3767:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 3768:                      $currdisp{'height'} = $1;
 3769:                 }
 3770:                 $currdisp{'linktext'} = $settings->{$item}->{'display'}->{'linktext'};
 3771:                 $currdisp{'explanation'} = $settings->{$item}->{'display'}->{'explanation'};
 3772:             } else {
 3773:                 $currdisp{'iframe'} = ' checked="checked"';
 3774:             }
 3775:             foreach my $disp ('iframe','window') {
 3776:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 3777:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 3778:             }
 3779:             $datatable .= ('&nbsp;'x4);
 3780:             foreach my $dimen ('width','height') {
 3781:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 3782:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 3783:                               ('&nbsp;'x2);
 3784:             }
 3785:             $datatable .= '<br />'.
 3786:                           '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 3787:                           '<input type="text" name="ltitools_linktext_'.$i.'" size="25" value="'.$currdisp{'linktext'}.'" /></label></div>'.
 3788:                           '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 3789:                           '<textarea name="ltitools_explanation_'.$i.'" rows="5" cols="40">'.$currdisp{'explanation'}.
 3790:                           '</textarea></div><div style=""></div><br />';
 3791:             $datatable .= '<br />';
 3792:             foreach my $extra ('passback','roster') {
 3793:                 my $checkedon = '';
 3794:                 my $checkedoff = ' checked="checked"';
 3795:                 if ($settings->{$item}->{$extra}) {
 3796:                     $checkedon = $checkedoff;
 3797:                     $checkedoff = '';
 3798:                 }
 3799:                 $datatable .= $lt{$extra}.'&nbsp;'.
 3800:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.' />'.
 3801:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 3802:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.' />'.
 3803:                               &mt('No').'</label>'.('&nbsp;'x4);
 3804:             }
 3805:             $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 3806:             if ($imgsrc) {
 3807:                 $datatable .= $imgsrc.
 3808:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 3809:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 3810:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 3811:             } else {
 3812:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 3813:             }
 3814:             if ($switchserver) {
 3815:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3816:             } else {
 3817:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 3818:             }
 3819:             $datatable .= '</span></fieldset>';
 3820:             my (%checkedfields,%rolemaps);
 3821:             if (ref($settings->{$item}) eq 'HASH') {
 3822:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 3823:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 3824:                 }
 3825:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 3826:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 3827:                     $checkedfields{'roles'} = 1;
 3828:                 }
 3829:             }
 3830:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 3831:                           '<span class="LC_nobreak">';
 3832:             foreach my $field (@fields) {
 3833:                 my $checked;
 3834:                 if ($checkedfields{$field}) {
 3835:                     $checked = ' checked="checked"';
 3836:                 }
 3837:                 $datatable .= '<label>'.
 3838:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$checked.' />'.
 3839:                               $lt{$field}.'</label>'.('&nbsp;' x2);
 3840:             }
 3841:             $datatable .= '</span></fieldset>'.
 3842:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 3843:             foreach my $role (@courseroles) {
 3844:                 my ($selected,$selectnone);
 3845:                 if (!$rolemaps{$role}) {
 3846:                     $selectnone = ' selected="selected"';
 3847:                 }
 3848:                 $datatable .= '<td align="center">'. 
 3849:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 3850:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 3851:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 3852:                 foreach my $ltirole (@ltiroles) {
 3853:                     unless ($selectnone) {
 3854:                         if ($rolemaps{$role} eq $ltirole) {
 3855:                             $selected = ' selected="selected"';
 3856:                         } else {
 3857:                             $selected = '';
 3858:                         }
 3859:                     }
 3860:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 3861:                 }
 3862:                 $datatable .= '</select></td>';
 3863:             }
 3864:             $datatable .= '</tr></table></fieldset>';
 3865:             my %courseconfig;
 3866:             if (ref($settings->{$item}) eq 'HASH') {
 3867:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 3868:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 3869:                 }
 3870:             }
 3871:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 3872:             foreach my $item ('label','title','target','linktext','explanation') {
 3873:                 my $checked;
 3874:                 if ($courseconfig{$item}) {
 3875:                     $checked = ' checked="checked"';
 3876:                 }
 3877:                 $datatable .= '<label>'.
 3878:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 3879:                        $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 3880:             }
 3881:             $datatable .= '</span></fieldset>'.
 3882:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 3883:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 3884:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 3885:                 my %custom = %{$settings->{$item}->{'custom'}};
 3886:                 if (keys(%custom) > 0) {
 3887:                     foreach my $key (sort(keys(%custom))) {
 3888:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 3889:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 3890:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 3891:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 3892:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 3893:                     }
 3894:                 }
 3895:             }
 3896:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 3897:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 3898:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 3899:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 3900:             $datatable .= '</table></fieldset></td></tr>'."\n";
 3901:             $itemcount ++;
 3902:         }
 3903:     }
 3904:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3905:     my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_add_pos'".');"';
 3906:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 3907:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 3908:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 3909:     for (my $k=0; $k<$maxnum+1; $k++) {
 3910:         my $vpos = $k+1;
 3911:         my $selstr;
 3912:         if ($k == $maxnum) {
 3913:             $selstr = ' selected="selected" ';
 3914:         }
 3915:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3916:     }
 3917:     $datatable .= '</select>&nbsp;'."\n".
 3918:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</td>'."\n".
 3919:                   '<td colspan="2">'.
 3920:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 3921:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_add_title" value="" /></span> '."\n".
 3922:                   ('&nbsp;'x2).
 3923:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 3924:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 3925:                   ('&nbsp;'x2).
 3926:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 3927:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 3928:                   '<br />'.
 3929:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_add_url" value="" /></span> '."\n".
 3930:                   ('&nbsp;'x2).
 3931:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 3932:                   ('&nbsp;'x2).
 3933:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 3934:                   '<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".
 3935:                   '</fieldset>'.
 3936:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 3937:                   '<span class="LC_nobreak">'.&mt('Display target:');
 3938:     my %defaultdisp;
 3939:     $defaultdisp{'iframe'} = ' checked="checked"';
 3940:     foreach my $disp ('iframe','window') {
 3941:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 3942:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 3943:     }
 3944:     $datatable .= ('&nbsp;'x4);
 3945:     foreach my $dimen ('width','height') {
 3946:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 3947:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 3948:                       ('&nbsp;'x2);
 3949:     }
 3950:     $datatable .= '<br />'.
 3951:                   '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 3952:                   '<input type="text" name="ltitools_add_linktext" size="5" /></label></div>'.
 3953:                   '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 3954:                   '<textarea name=ltitools_add_explanation" rows="5" cols="40"></textarea>'.
 3955:                   '</div><div style=""></div><br />';
 3956:     foreach my $extra ('passback','roster') {
 3957:         $datatable .= $lt{$extra}.'&nbsp;'.
 3958:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="1" />'.
 3959:                       &mt('Yes').'</label>'.('&nbsp;'x2).
 3960:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="0" checked="checked" />'.
 3961:                       &mt('No').'</label>'.('&nbsp;'x4);
 3962:     }
 3963:     $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 3964:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 3965:     if ($switchserver) {
 3966:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3967:     } else {
 3968:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 3969:     }
 3970:     $datatable .= '</span></fieldset>'.
 3971:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 3972:                   '<span class="LC_nobreak">';
 3973:     foreach my $field (@fields) {
 3974:         $datatable .= '<label>'.
 3975:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'" />'.
 3976:                       $lt{$field}.'</label>'.('&nbsp;' x2);
 3977:     }
 3978:     $datatable .= '</span></fieldset>'.
 3979:                   '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 3980:     foreach my $role (@courseroles) {
 3981:         my ($checked,$checkednone);
 3982:         $datatable .= '<td align="center">'.
 3983:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 3984:                       '<select name="ltitools_add_roles_'.$role.'">'.
 3985:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 3986:         foreach my $ltirole (@ltiroles) {
 3987:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 3988:         }
 3989:         $datatable .= '</select></td>';
 3990:     }
 3991:     $datatable .= '</tr></table></fieldset>'.
 3992:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 3993:     foreach my $item ('label','title','target','linktext','explanation') {
 3994:         $datatable .= '<label>'.
 3995:                       '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 3996:                       $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 3997:     }
 3998:     $datatable .= '</span></fieldset>'.
 3999:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4000:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 4001:                   '<tr><td><span class="LC_nobreak">'.
 4002:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 4003:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 4004:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 4005:                   '</table></fieldset></td></tr>'."\n".
 4006:                   '</td>'."\n".
 4007:                   '</tr>'."\n";
 4008:     $itemcount ++;
 4009:     return $datatable;
 4010: }
 4011: 
 4012: sub ltitools_names {
 4013:     my %lt = &Apache::lonlocal::texthash(
 4014:                                           'title'          => 'Title',
 4015:                                           'version'        => 'Version',
 4016:                                           'msgtype'        => 'Message Type',
 4017:                                           'url'            => 'URL',
 4018:                                           'key'            => 'Key',
 4019:                                           'secret'         => 'Secret',
 4020:                                           'icon'           => 'Icon',   
 4021:                                           'user'           => 'Username:domain',
 4022:                                           'fullname'       => 'Full Name',
 4023:                                           'firstname'      => 'First Name',
 4024:                                           'lastname'       => 'Last Name',
 4025:                                           'email'          => 'E-mail',
 4026:                                           'roles'          => 'Role',
 4027:                                           'window'         => 'Window/Tab',
 4028:                                           'iframe'         => 'iFrame',
 4029:                                           'height'         => 'Height',
 4030:                                           'width'          => 'Width',
 4031:                                           'linktext'       => 'Default Link Text',
 4032:                                           'explanation'    => 'Default Explanation',
 4033:                                           'passback'       => 'Tool can return grades:',
 4034:                                           'roster'         => 'Tool can retrieve roster:',
 4035:                                           'crstarget'      => 'Display target',
 4036:                                           'crslabel'       => 'Course label',
 4037:                                           'crstitle'       => 'Course title', 
 4038:                                           'crslinktext'    => 'Link Text',
 4039:                                           'crsexplanation' => 'Explanation',
 4040:                                         );
 4041:     return %lt;
 4042: }
 4043: 
 4044: sub print_coursedefaults {
 4045:     my ($position,$dom,$settings,$rowtotal) = @_;
 4046:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 4047:     my $itemcount = 1;
 4048:     my %choices =  &Apache::lonlocal::texthash (
 4049:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 4050:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 4051:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 4052:         coursecredits        => 'Credits can be specified for courses',
 4053:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 4054:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 4055:         postsubmit           => 'Disable submit button/keypress following student submission',
 4056:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 4057:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 4058:     );
 4059:     my %staticdefaults = (
 4060:                            anonsurvey_threshold => 10,
 4061:                            uploadquota          => 500,
 4062:                            postsubmit           => 60,
 4063:                            mysqltables          => 172800,
 4064:                          );
 4065:     if ($position eq 'top') {
 4066:         %defaultchecked = (
 4067:                             'canuse_pdfforms' => 'off',
 4068:                             'uselcmath'       => 'on',
 4069:                             'usejsme'         => 'on',
 4070:                             'canclone'        => 'none',
 4071:                           );
 4072:         @toggles = ('canuse_pdfforms','uselcmath','usejsme');
 4073:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4074:                                                      \%choices,$itemcount);
 4075:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4076:         $datatable .=
 4077:             '<tr'.$css_class.'><td valign="top">'.
 4078:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 4079:             '</span></td><td class="LC_left_item">';
 4080:         my $currcanclone = 'none';
 4081:         my $onclick;
 4082:         my @cloneoptions = ('none','domain');
 4083:         my %clonetitles = (
 4084:                              none     => 'No additional course requesters',
 4085:                              domain   => "Any course requester in course's domain",
 4086:                              instcode => 'Course requests for official courses ...',
 4087:                           );
 4088:         my (%codedefaults,@code_order,@posscodes);
 4089:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 4090:                                                     \@code_order) eq 'ok') {
 4091:             if (@code_order > 0) {
 4092:                 push(@cloneoptions,'instcode');
 4093:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 4094:             }
 4095:         }
 4096:         if (ref($settings) eq 'HASH') {
 4097:             if ($settings->{'canclone'}) {
 4098:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 4099:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 4100:                         if (@code_order > 0) {
 4101:                             $currcanclone = 'instcode';
 4102:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 4103:                         }
 4104:                     }
 4105:                 } elsif ($settings->{'canclone'} eq 'domain') {
 4106:                     $currcanclone = $settings->{'canclone'};
 4107:                 }
 4108:             }
 4109:         }
 4110:         foreach my $option (@cloneoptions) {
 4111:             my ($checked,$additional);
 4112:             if ($currcanclone eq $option) {
 4113:                 $checked = ' checked="checked"';
 4114:             }
 4115:             if ($option eq 'instcode') {
 4116:                 if (@code_order) {
 4117:                     my $show = 'none';
 4118:                     if ($checked) {
 4119:                         $show = 'block';
 4120:                     }
 4121:                     $additional = '<div id="cloneinstcode" style="display:'.$show.'" />'.
 4122:                                   &mt('Institutional codes for new and cloned course have identical:').
 4123:                                   '<br />';
 4124:                     foreach my $item (@code_order) {
 4125:                         my $codechk;
 4126:                         if ($checked) {
 4127:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 4128:                                 $codechk = ' checked="checked"';
 4129:                             }
 4130:                         }
 4131:                         $additional .= '<label>'.
 4132:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 4133:                                        $item.'</label>';
 4134:                     }
 4135:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 4136:                 }
 4137:             }
 4138:             $datatable .=
 4139:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 4140:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 4141:                 '</label>&nbsp;'.$additional.'</span><br />';
 4142:         }
 4143:         $datatable .= '</td>'.
 4144:                       '</tr>';
 4145:         $itemcount ++;
 4146:     } else {
 4147:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4148:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 4149:         my $currusecredits = 0;
 4150:         my $postsubmitclient = 1;
 4151:         my @types = ('official','unofficial','community','textbook','placement');
 4152:         if (ref($settings) eq 'HASH') {
 4153:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 4154:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 4155:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 4156:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 4157:                 }
 4158:             }
 4159:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 4160:                 foreach my $type (@types) {
 4161:                     next if ($type eq 'community');
 4162:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 4163:                     if ($defcredits{$type} ne '') {
 4164:                         $currusecredits = 1;
 4165:                     }
 4166:                 }
 4167:             }
 4168:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 4169:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 4170:                     $postsubmitclient = 0;
 4171:                     foreach my $type (@types) {
 4172:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4173:                     }
 4174:                 } else {
 4175:                     foreach my $type (@types) {
 4176:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 4177:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 4178:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 4179:                             } else {
 4180:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4181:                             }
 4182:                         } else {
 4183:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4184:                         }
 4185:                     }
 4186:                 }
 4187:             } else {
 4188:                 foreach my $type (@types) {
 4189:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4190:                 }
 4191:             }
 4192:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 4193:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 4194:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 4195:                 }
 4196:             } else {
 4197:                 foreach my $type (@types) {
 4198:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 4199:                 }
 4200:             }
 4201:         } else {
 4202:             foreach my $type (@types) {
 4203:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4204:             }
 4205:         }
 4206:         if (!$currdefresponder) {
 4207:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 4208:         } elsif ($currdefresponder < 1) {
 4209:             $currdefresponder = 1;
 4210:         }
 4211:         foreach my $type (@types) {
 4212:             if ($curruploadquota{$type} eq '') {
 4213:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 4214:             }
 4215:         }
 4216:         $datatable .=
 4217:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4218:                 $choices{'anonsurvey_threshold'}.
 4219:                 '</span></td>'.
 4220:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 4221:                 '<input type="text" name="anonsurvey_threshold"'.
 4222:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 4223:                 '</td></tr>'."\n";
 4224:         $itemcount ++;
 4225:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4226:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4227:                       $choices{'uploadquota'}.
 4228:                       '</span></td>'.
 4229:                       '<td align="right" class="LC_right_item">'.
 4230:                       '<table><tr>';
 4231:         foreach my $type (@types) {
 4232:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 4233:                            '<input type="text" name="uploadquota_'.$type.'"'.
 4234:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 4235:         }
 4236:         $datatable .= '</tr></table></td></tr>'."\n";
 4237:         $itemcount ++;
 4238:         my $onclick = "toggleDisplay(this.form,'credits');";
 4239:         my $display = 'none';
 4240:         if ($currusecredits) {
 4241:             $display = 'block';
 4242:         }
 4243:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 4244:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 4245:         foreach my $type (@types) {
 4246:             next if ($type eq 'community');
 4247:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 4248:                            '<input type="text" name="'.$type.'_credits"'.
 4249:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 4250:         }
 4251:         $additional .= '</tr></table></div>'."\n";
 4252:         %defaultchecked = ('coursecredits' => 'off');
 4253:         @toggles = ('coursecredits');
 4254:         my $current = {
 4255:                         'coursecredits' => $currusecredits,
 4256:                       };
 4257:         (my $table,$itemcount) =
 4258:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4259:                                \%choices,$itemcount,$onclick,$additional,'left');
 4260:         $datatable .= $table;
 4261:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 4262:         my $display = 'none';
 4263:         if ($postsubmitclient) {
 4264:             $display = 'block';
 4265:         }
 4266:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 4267:                       &mt('Number of seconds submit is disabled').'<br />'.
 4268:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 4269:                       '<table><tr>';
 4270:         foreach my $type (@types) {
 4271:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 4272:                            '<input type="text" name="'.$type.'_timeout" value="'.
 4273:                            $deftimeout{$type}.'" size="5" /></td>';
 4274:         }
 4275:         $additional .= '</tr></table></div>'."\n";
 4276:         %defaultchecked = ('postsubmit' => 'on');
 4277:         @toggles = ('postsubmit');
 4278:         $current = {
 4279:                        'postsubmit' => $postsubmitclient,
 4280:                    };
 4281:         ($table,$itemcount) =
 4282:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4283:                                \%choices,$itemcount,$onclick,$additional,'left');
 4284:         $datatable .= $table;
 4285:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4286:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4287:                       $choices{'mysqltables'}.
 4288:                       '</span></td>'.
 4289:                       '<td align="right" class="LC_right_item">'.
 4290:                       '<table><tr>';
 4291:         foreach my $type (@types) {
 4292:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 4293:                            '<input type="text" name="mysqltables_'.$type.'"'.
 4294:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 4295:         }
 4296:         $datatable .= '</tr></table></td></tr>'."\n";
 4297:         $itemcount ++;
 4298: 
 4299:     }
 4300:     $$rowtotal += $itemcount;
 4301:     return $datatable;
 4302: }
 4303: 
 4304: sub print_selfenrollment {
 4305:     my ($position,$dom,$settings,$rowtotal) = @_;
 4306:     my ($css_class,$datatable);
 4307:     my $itemcount = 1;
 4308:     my @types = ('official','unofficial','community','textbook','placement');
 4309:     if (($position eq 'top') || ($position eq 'middle')) {
 4310:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 4311:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 4312:         my @rows;
 4313:         my $key;
 4314:         if ($position eq 'top') {
 4315:             $key = 'admin'; 
 4316:             if (ref($rowsref) eq 'ARRAY') {
 4317:                 @rows = @{$rowsref};
 4318:             }
 4319:         } elsif ($position eq 'middle') {
 4320:             $key = 'default';
 4321:             @rows = ('types','registered','approval','limit');
 4322:         }
 4323:         foreach my $row (@rows) {
 4324:             if (defined($titlesref->{$row})) {
 4325:                 $itemcount ++;
 4326:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4327:                 $datatable .= '<tr'.$css_class.'>'.
 4328:                               '<td>'.$titlesref->{$row}.'</td>'.
 4329:                               '<td class="LC_left_item">'.
 4330:                               '<table><tr>';
 4331:                 my (%current,%currentcap);
 4332:                 if (ref($settings) eq 'HASH') {
 4333:                     if (ref($settings->{$key}) eq 'HASH') {
 4334:                         foreach my $type (@types) {
 4335:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4336:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 4337:                             }
 4338:                             if (($row eq 'limit') && ($key eq 'default')) {
 4339:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4340:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 4341:                                 }
 4342:                             }
 4343:                         }
 4344:                     }
 4345:                 }
 4346:                 my %roles = (
 4347:                              '0' => &Apache::lonnet::plaintext('dc'),
 4348:                             ); 
 4349:             
 4350:                 foreach my $type (@types) {
 4351:                     unless (($row eq 'registered') && ($key eq 'default')) {
 4352:                         $datatable .= '<th>'.&mt($type).'</th>';
 4353:                     }
 4354:                 }
 4355:                 unless (($row eq 'registered') && ($key eq 'default')) {
 4356:                     $datatable .= '</tr><tr>';
 4357:                 }
 4358:                 foreach my $type (@types) {
 4359:                     if ($type eq 'community') {
 4360:                         $roles{'1'} = &mt('Community personnel');
 4361:                     } else {
 4362:                         $roles{'1'} = &mt('Course personnel');
 4363:                     }
 4364:                     $datatable .= '<td style="vertical-align: top">';
 4365:                     if ($position eq 'top') {
 4366:                         my %checked;
 4367:                         if ($current{$type} eq '0') {
 4368:                             $checked{'0'} = ' checked="checked"';
 4369:                         } else {
 4370:                             $checked{'1'} = ' checked="checked"';
 4371:                         }
 4372:                         foreach my $role ('1','0') {
 4373:                             $datatable .= '<span class="LC_nobreak"><label>'.
 4374:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 4375:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 4376:                                           $roles{$role}.'</label></span> ';
 4377:                         }
 4378:                     } else {
 4379:                         if ($row eq 'types') {
 4380:                             my %checked;
 4381:                             if ($current{$type} =~ /^(all|dom)$/) {
 4382:                                 $checked{$1} = ' checked="checked"';
 4383:                             } else {
 4384:                                 $checked{''} = ' checked="checked"';
 4385:                             }
 4386:                             foreach my $val ('','dom','all') {
 4387:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4388:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4389:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4390:                             }
 4391:                         } elsif ($row eq 'registered') {
 4392:                             my %checked;
 4393:                             if ($current{$type} eq '1') {
 4394:                                 $checked{'1'} = ' checked="checked"';
 4395:                             } else {
 4396:                                 $checked{'0'} = ' checked="checked"';
 4397:                             }
 4398:                             foreach my $val ('0','1') {
 4399:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4400:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4401:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4402:                             }
 4403:                         } elsif ($row eq 'approval') {
 4404:                             my %checked;
 4405:                             if ($current{$type} =~ /^([12])$/) {
 4406:                                 $checked{$1} = ' checked="checked"';
 4407:                             } else {
 4408:                                 $checked{'0'} = ' checked="checked"';
 4409:                             }
 4410:                             for my $val (0..2) {
 4411:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4412:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4413:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4414:                             }
 4415:                         } elsif ($row eq 'limit') {
 4416:                             my %checked;
 4417:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 4418:                                 $checked{$1} = ' checked="checked"';
 4419:                             } else {
 4420:                                 $checked{'none'} = ' checked="checked"';
 4421:                             }
 4422:                             my $cap;
 4423:                             if ($currentcap{$type} =~ /^\d+$/) {
 4424:                                 $cap = $currentcap{$type};
 4425:                             }
 4426:                             foreach my $val ('none','allstudents','selfenrolled') {
 4427:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4428:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4429:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4430:                             }
 4431:                             $datatable .= '<br />'.
 4432:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 4433:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 4434:                                           '</span>'; 
 4435:                         }
 4436:                     }
 4437:                     $datatable .= '</td>';
 4438:                 }
 4439:                 $datatable .= '</tr>';
 4440:             }
 4441:             $datatable .= '</table></td></tr>';
 4442:         }
 4443:     } elsif ($position eq 'bottom') {
 4444:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 4445:     }
 4446:     $$rowtotal += $itemcount;
 4447:     return $datatable;
 4448: }
 4449: 
 4450: sub print_validation_rows {
 4451:     my ($caller,$dom,$settings,$rowtotal) = @_;
 4452:     my ($itemsref,$namesref,$fieldsref);
 4453:     if ($caller eq 'selfenroll') { 
 4454:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 4455:     } elsif ($caller eq 'requestcourses') {
 4456:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 4457:     }
 4458:     my %currvalidation;
 4459:     if (ref($settings) eq 'HASH') {
 4460:         if (ref($settings->{'validation'}) eq 'HASH') {
 4461:             %currvalidation = %{$settings->{'validation'}};
 4462:         }
 4463:     }
 4464:     my $datatable;
 4465:     my $itemcount = 0;
 4466:     foreach my $item (@{$itemsref}) {
 4467:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4468:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4469:                       $namesref->{$item}.
 4470:                       '</span></td>'.
 4471:                       '<td class="LC_left_item">';
 4472:         if (($item eq 'url') || ($item eq 'button')) {
 4473:             $datatable .= '<span class="LC_nobreak">'.
 4474:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 4475:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 4476:         } elsif ($item eq 'fields') {
 4477:             my @currfields;
 4478:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 4479:                 @currfields = @{$currvalidation{$item}};
 4480:             }
 4481:             foreach my $field (@{$fieldsref}) {
 4482:                 my $check = '';
 4483:                 if (grep(/^\Q$field\E$/,@currfields)) {
 4484:                     $check = ' checked="checked"';
 4485:                 }
 4486:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4487:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 4488:                               ' value="'.$field.'"'.$check.' />'.$field.
 4489:                               '</label></span> ';
 4490:             }
 4491:         } elsif ($item eq 'markup') {
 4492:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 4493:                            $currvalidation{$item}.
 4494:                               '</textarea>';
 4495:         }
 4496:         $datatable .= '</td></tr>'."\n";
 4497:         if (ref($rowtotal)) {
 4498:             $itemcount ++;
 4499:         }
 4500:     }
 4501:     if ($caller eq 'requestcourses') {
 4502:         my %currhash;
 4503:         if (ref($settings) eq 'HASH') {
 4504:             if (ref($settings->{'validation'}) eq 'HASH') {
 4505:                 if ($settings->{'validation'}{'dc'} ne '') {
 4506:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 4507:                 }
 4508:             }
 4509:         }
 4510:         my $numinrow = 2;
 4511:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4512:                                                        'validationdc',%currhash);
 4513:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4514:         $datatable .= '</td></tr><tr'.$css_class.'><td>';
 4515:         if ($numdc > 1) {
 4516:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 4517:         } else {
 4518:             $datatable .=  &mt('Course creation processed as: ');
 4519:         }
 4520:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 4521:         $itemcount ++;
 4522:     }
 4523:     if (ref($rowtotal)) {
 4524:         $$rowtotal += $itemcount;
 4525:     }
 4526:     return $datatable;
 4527: }
 4528: 
 4529: sub print_usersessions {
 4530:     my ($position,$dom,$settings,$rowtotal) = @_;
 4531:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 4532:     my (%by_ip,%by_location,@intdoms,@instdoms);
 4533:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 4534: 
 4535:     my @alldoms = &Apache::lonnet::all_domains();
 4536:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 4537:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4538:     my %altids = &id_for_thisdom(%servers);
 4539:     if ($position eq 'top') {
 4540:         if (keys(%serverhomes) > 1) {
 4541:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 4542:             my $curroffloadnow;
 4543:             if (ref($settings) eq 'HASH') {
 4544:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 4545:                     $curroffloadnow = $settings->{'offloadnow'};
 4546:                 }
 4547:             }
 4548:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 4549:         } else {
 4550:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 4551:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 4552:                           '</td></tr>';
 4553:         }
 4554:     } else {
 4555:         my %titles = &usersession_titles();
 4556:         my ($prefix,@types);
 4557:         if ($position eq 'bottom') {
 4558:             $prefix = 'remote';
 4559:             @types = ('version','excludedomain','includedomain');
 4560:         } else {
 4561:             $prefix = 'hosted';
 4562:             @types = ('excludedomain','includedomain');
 4563:         }
 4564:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 4565:     }
 4566:     $$rowtotal += $itemcount;
 4567:     return $datatable;
 4568: }
 4569: 
 4570: sub rules_by_location {
 4571:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 4572:     my ($datatable,$itemcount,$css_class);
 4573:     if (keys(%{$by_location}) == 0) {
 4574:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4575:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 4576:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 4577:                      '</td></tr>';
 4578:         $itemcount = 1;
 4579:     } else {
 4580:         $itemcount = 0;
 4581:         my $numinrow = 5;
 4582:         my (%current,%checkedon,%checkedoff);
 4583:         my @locations = sort(keys(%{$by_location}));
 4584:         foreach my $type (@{$types}) {
 4585:             $checkedon{$type} = '';
 4586:             $checkedoff{$type} = ' checked="checked"';
 4587:         }
 4588:         if (ref($settings) eq 'HASH') {
 4589:             if (ref($settings->{$prefix}) eq 'HASH') {
 4590:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 4591:                     $current{$key} = $settings->{$prefix}{$key};
 4592:                     if ($key eq 'version') {
 4593:                         if ($current{$key} ne '') {
 4594:                             $checkedon{$key} = ' checked="checked"';
 4595:                             $checkedoff{$key} = '';
 4596:                         }
 4597:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 4598:                         $checkedon{$key} = ' checked="checked"';
 4599:                         $checkedoff{$key} = '';
 4600:                     }
 4601:                 }
 4602:             }
 4603:         }
 4604:         foreach my $type (@{$types}) {
 4605:             next if ($type ne 'version' && !@locations);
 4606:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4607:             $datatable .= '<tr'.$css_class.'>
 4608:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 4609:                            <span class="LC_nobreak">&nbsp;
 4610:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 4611:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 4612:             if ($type eq 'version') {
 4613:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 4614:                 my $selector = '<select name="'.$prefix.'_version">';
 4615:                 foreach my $version (@lcversions) {
 4616:                     my $selected = '';
 4617:                     if ($current{'version'} eq $version) {
 4618:                         $selected = ' selected="selected"';
 4619:                     }
 4620:                     $selector .= ' <option value="'.$version.'"'.
 4621:                                  $selected.'>'.$version.'</option>';
 4622:                 }
 4623:                 $selector .= '</select> ';
 4624:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 4625:             } else {
 4626:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 4627:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 4628:                              ' />'.('&nbsp;'x2).
 4629:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 4630:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 4631:                              "\n".
 4632:                              '</div><div><table>';
 4633:                 my $rem;
 4634:                 for (my $i=0; $i<@locations; $i++) {
 4635:                     my ($showloc,$value,$checkedtype);
 4636:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 4637:                         my $ip = $by_location->{$locations[$i]}->[0];
 4638:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4639:                             $value = join(':',@{$by_ip->{$ip}});
 4640:                             $showloc = join(', ',@{$by_ip->{$ip}});
 4641:                             if (ref($current{$type}) eq 'ARRAY') {
 4642:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 4643:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 4644:                                         $checkedtype = ' checked="checked"';
 4645:                                         last;
 4646:                                     }
 4647:                                 }
 4648:                             }
 4649:                         }
 4650:                     }
 4651:                     $rem = $i%($numinrow);
 4652:                     if ($rem == 0) {
 4653:                         if ($i > 0) {
 4654:                             $datatable .= '</tr>';
 4655:                         }
 4656:                         $datatable .= '<tr>';
 4657:                     }
 4658:                     $datatable .= '<td class="LC_left_item">'.
 4659:                                   '<span class="LC_nobreak"><label>'.
 4660:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 4661:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 4662:                                   '</label></span></td>';
 4663:                 }
 4664:                 $rem = @locations%($numinrow);
 4665:                 my $colsleft = $numinrow - $rem;
 4666:                 if ($colsleft > 1 ) {
 4667:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4668:                                   '&nbsp;</td>';
 4669:                 } elsif ($colsleft == 1) {
 4670:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4671:                 }
 4672:                 $datatable .= '</tr></table>';
 4673:             }
 4674:             $datatable .= '</td></tr>';
 4675:             $itemcount ++;
 4676:         }
 4677:     }
 4678:     return ($datatable,$itemcount);
 4679: }
 4680: 
 4681: sub print_ssl {
 4682:     my ($position,$dom,$settings,$rowtotal) = @_;
 4683:     my ($css_class,$datatable);
 4684:     my $itemcount = 1;
 4685:     if ($position eq 'top') {
 4686:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 4687:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 4688:         my $same_institution;
 4689:         if ($intdom ne '') {
 4690:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 4691:             if (ref($internet_names) eq 'ARRAY') {
 4692:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 4693:                     $same_institution = 1;
 4694:                 }
 4695:             }
 4696:         }
 4697:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4698:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 4699:         if ($same_institution) {
 4700:             my %domservers = &Apache::lonnet::get_servers($dom);
 4701:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 4702:         } else {
 4703:             $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.");
 4704:         }
 4705:         $datatable .= '</td></tr>';
 4706:         $itemcount ++;
 4707:     } else {
 4708:         my %titles = &ssl_titles();
 4709:         my (%by_ip,%by_location,@intdoms,@instdoms);
 4710:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 4711:         my @alldoms = &Apache::lonnet::all_domains();
 4712:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 4713:         my @domservers = &Apache::lonnet::get_servers($dom);
 4714:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4715:         my %altids = &id_for_thisdom(%servers);
 4716:         if (($position eq 'connto') || ($position eq 'connfrom')) {
 4717:             my $legacy;
 4718:             unless (ref($settings) eq 'HASH') {
 4719:                 my $name;
 4720:                 if ($position eq 'connto') {
 4721:                     $name = 'loncAllowInsecure';
 4722:                 } else {
 4723:                     $name = 'londAllowInsecure';
 4724:                 }
 4725:                 my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
 4726:                 my @ids=&Apache::lonnet::current_machine_ids();
 4727:                 if (($primarylibserv ne '') && (!grep(/^\Q$primarylibserv\E$/,@ids))) {
 4728:                     my %what = (
 4729:                                    $name => 1,
 4730:                                );
 4731:                     my ($result,$returnhash) =
 4732:                         &Apache::lonnet::get_remote_globals($primarylibserv,\%what);
 4733:                     if ($result eq 'ok') {
 4734:                         if (ref($returnhash) eq 'HASH') {
 4735:                             $legacy = $returnhash->{$name};
 4736:                         }
 4737:                     }
 4738:                 } else {
 4739:                     $legacy = $Apache::lonnet::perlvar{$name};
 4740:                 }
 4741:             }
 4742:             foreach my $type ('dom','intdom','other') {
 4743:                 my %checked;
 4744:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4745:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 4746:                               '<td class="LC_right_item">';
 4747:                 my $skip; 
 4748:                 if ($type eq 'dom') {
 4749:                     unless (keys(%servers) > 1) {
 4750:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 4751:                         $skip = 1;
 4752:                     }
 4753:                 }
 4754:                 if ($type eq 'intdom') {
 4755:                     unless (@instdoms > 1) {
 4756:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 4757:                         $skip = 1;
 4758:                     } 
 4759:                 } elsif ($type eq 'other') {
 4760:                     if (keys(%by_location) == 0) {
 4761:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 4762:                         $skip = 1;
 4763:                     }
 4764:                 }
 4765:                 unless ($skip) {
 4766:                     $checked{'yes'} = ' checked="checked"'; 
 4767:                     if (ref($settings) eq 'HASH') {
 4768:                         if (ref($settings->{$position}) eq 'HASH') {
 4769:                             if ($settings->{$position}->{$type} =~ /^(no|req)$/) {
 4770:                                 $checked{$1} = $checked{'yes'};
 4771:                                 delete($checked{'yes'}); 
 4772:                             }
 4773:                         }
 4774:                     } else {
 4775:                         if ($legacy == 0) {
 4776:                             $checked{'req'} = $checked{'yes'};
 4777:                             delete($checked{'yes'});    
 4778:                         }
 4779:                     }
 4780:                     foreach my $option ('no','yes','req') {
 4781:                         $datatable .= '<span class="LC_nobreak"><label>'.
 4782:                                       '<input type="radio" name="'.$position.'_'.$type.'" '.
 4783:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 4784:                                       '</label></span>'.('&nbsp;'x2);
 4785:                     }
 4786:                 }
 4787:                 $datatable .= '</td></tr>';
 4788:                 $itemcount ++; 
 4789:             }
 4790:         } else {
 4791:             my $prefix = 'replication';
 4792:             my @types = ('certreq','nocertreq');
 4793:             if (keys(%by_location) == 0) {
 4794:                 $datatable .= '<tr'.$css_class.'><td>'.
 4795:                               &mt('Nothing to set here, as there are no other institutions').
 4796:                               '</td></tr>';
 4797:                 $itemcount ++;
 4798:             } else {
 4799:                 ($datatable,$itemcount) = 
 4800:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 4801:             }
 4802:         }
 4803:     }
 4804:     $$rowtotal += $itemcount;
 4805:     return $datatable;
 4806: }
 4807: 
 4808: sub ssl_titles {
 4809:     return &Apache::lonlocal::texthash (
 4810:                dom           => 'LON-CAPA servers/VMs from same domain',
 4811:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 4812:                other         => 'External LON-CAPA servers/VMs',
 4813:                connto        => 'Connections to other servers',
 4814:                connfrom      => 'Connections from other servers',
 4815:                replication   => 'Replicating content to other institutions',
 4816:                certreq       => 'Client certificate required, but specific domains exempt',
 4817:                nocertreq     => 'No client certificate required, except for specific domains',
 4818:                no            => 'SSL not used',
 4819:                yes           => 'SSL Optional (used if available)',
 4820:                req           => 'SSL Required',
 4821:     );
 4822: }
 4823: 
 4824: sub print_trust {
 4825:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 4826:     my ($css_class,$datatable,%checked,%choices);
 4827:     my (%by_ip,%by_location,@intdoms,@instdoms);
 4828:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 4829:     my $itemcount = 1;
 4830:     my %titles = &trust_titles();
 4831:     my @types = ('exc','inc');
 4832:     if ($prefix eq 'top') {
 4833:         $prefix = 'content';
 4834:     } elsif ($prefix eq 'bottom') {
 4835:         $prefix = 'msg';
 4836:     }
 4837:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 4838:     $$rowtotal += $itemcount;
 4839:     return $datatable;
 4840: }
 4841: 
 4842: sub trust_titles {
 4843:     return &Apache::lonlocal::texthash(
 4844:                content  => "Access to this domain's content by others",
 4845:                shared   => "Access to other domain's content by this domain",
 4846:                enroll   => "Enrollment in this domain's courses by others", 
 4847:                othcoau  => "Co-author roles in this domain for others",
 4848:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 4849:                domroles => "Domain roles in this domain assignable to others",
 4850:                catalog  => "Course Catalog for this domain displayed elsewhere",
 4851:                reqcrs   => "Requests for creation of courses in this domain by others",
 4852:                msg      => "Users in other domains can send messages to this domain",
 4853:                exc      => "Allow all, but exclude specific domains",
 4854:                inc      => "Deny all, but include specific domains",
 4855:            );
 4856: } 
 4857: 
 4858: sub build_location_hashes {
 4859:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 4860:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 4861:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 4862:     my %iphost = &Apache::lonnet::get_iphost();
 4863:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 4864:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 4865:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 4866:         foreach my $id (@{$iphost{$primary_ip}}) {
 4867:             my $intdom = &Apache::lonnet::internet_dom($id);
 4868:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 4869:                 push(@{$intdoms},$intdom);
 4870:             }
 4871:         }
 4872:     }
 4873:     foreach my $ip (keys(%iphost)) {
 4874:         if (ref($iphost{$ip}) eq 'ARRAY') {
 4875:             foreach my $id (@{$iphost{$ip}}) {
 4876:                 my $location = &Apache::lonnet::internet_dom($id);
 4877:                 if ($location) {
 4878:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 4879:                         my $dom = &Apache::lonnet::host_domain($id);
 4880:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 4881:                             push(@{$instdoms},$dom);
 4882:                         }
 4883:                         next;
 4884:                     }
 4885:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4886:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 4887:                             push(@{$by_ip->{$ip}},$location);
 4888:                         }
 4889:                     } else {
 4890:                         $by_ip->{$ip} = [$location];
 4891:                     }
 4892:                 }
 4893:             }
 4894:         }
 4895:     }
 4896:     foreach my $ip (sort(keys(%{$by_ip}))) {
 4897:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4898:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 4899:             my $first = $by_ip->{$ip}->[0];
 4900:             if (ref($by_location->{$first}) eq 'ARRAY') {
 4901:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 4902:                     push(@{$by_location->{$first}},$ip);
 4903:                 }
 4904:             } else {
 4905:                 $by_location->{$first} = [$ip];
 4906:             }
 4907:         }
 4908:     }
 4909:     return;
 4910: }
 4911: 
 4912: sub current_offloads_to {
 4913:     my ($dom,$settings,$servers) = @_;
 4914:     my (%spareid,%otherdomconfigs);
 4915:     if (ref($servers) eq 'HASH') {
 4916:         foreach my $lonhost (sort(keys(%{$servers}))) {
 4917:             my $gotspares;
 4918:             if (ref($settings) eq 'HASH') {
 4919:                 if (ref($settings->{'spares'}) eq 'HASH') {
 4920:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 4921:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 4922:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 4923:                         $gotspares = 1;
 4924:                     }
 4925:                 }
 4926:             }
 4927:             unless ($gotspares) {
 4928:                 my $gotspares;
 4929:                 my $serverhomeID =
 4930:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 4931:                 my $serverhomedom =
 4932:                     &Apache::lonnet::host_domain($serverhomeID);
 4933:                 if ($serverhomedom ne $dom) {
 4934:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 4935:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4936:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4937:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4938:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4939:                                 $gotspares = 1;
 4940:                             }
 4941:                         }
 4942:                     } else {
 4943:                         $otherdomconfigs{$serverhomedom} =
 4944:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 4945:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 4946:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4947:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4948:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 4949:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4950:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4951:                                         $gotspares = 1;
 4952:                                     }
 4953:                                 }
 4954:                             }
 4955:                         }
 4956:                     }
 4957:                 }
 4958:             }
 4959:             unless ($gotspares) {
 4960:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4961:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4962:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4963:                } else {
 4964:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 4965:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 4966:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4967:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4968:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4969:                     } else {
 4970:                         my %what = (
 4971:                              spareid => 1,
 4972:                         );
 4973:                         my ($result,$returnhash) = 
 4974:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 4975:                         if ($result eq 'ok') { 
 4976:                             if (ref($returnhash) eq 'HASH') {
 4977:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 4978:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 4979:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 4980:                                 }
 4981:                             }
 4982:                         }
 4983:                     }
 4984:                 }
 4985:             }
 4986:         }
 4987:     }
 4988:     return %spareid;
 4989: }
 4990: 
 4991: sub spares_row {
 4992:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 4993:     my $css_class;
 4994:     my $numinrow = 4;
 4995:     my $itemcount = 1;
 4996:     my $datatable;
 4997:     my %typetitles = &sparestype_titles();
 4998:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 4999:         foreach my $server (sort(keys(%{$servers}))) {
 5000:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 5001:             my ($othercontrol,$serverdom);
 5002:             if ($serverhome ne $server) {
 5003:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 5004:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 5005:             } else {
 5006:                 $serverdom = &Apache::lonnet::host_domain($server);
 5007:                 if ($serverdom ne $dom) {
 5008:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 5009:                 }
 5010:             }
 5011:             next unless (ref($spareid->{$server}) eq 'HASH');
 5012:             my $checkednow;
 5013:             if (ref($curroffloadnow) eq 'HASH') {
 5014:                 if ($curroffloadnow->{$server}) {
 5015:                     $checkednow = ' checked="checked"';
 5016:                 }
 5017:             }
 5018:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5019:             $datatable .= '<tr'.$css_class.'>
 5020:                            <td rowspan="2">
 5021:                             <span class="LC_nobreak">'.
 5022:                           &mt('[_1] when busy, offloads to:'
 5023:                               ,'<b>'.$server.'</b>').'</span><br />'.
 5024:                           '<span class="LC_nobreak">'."\n".
 5025:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 5026:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 5027:                           "\n";
 5028:             my (%current,%canselect);
 5029:             my @choices = 
 5030:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 5031:             foreach my $type ('primary','default') {
 5032:                 if (ref($spareid->{$server}) eq 'HASH') {
 5033:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 5034:                         my @spares = @{$spareid->{$server}{$type}};
 5035:                         if (@spares > 0) {
 5036:                             if ($othercontrol) {
 5037:                                 $current{$type} = join(', ',@spares);
 5038:                             } else {
 5039:                                 $current{$type} .= '<table>';
 5040:                                 my $numspares = scalar(@spares);
 5041:                                 for (my $i=0;  $i<@spares; $i++) {
 5042:                                     my $rem = $i%($numinrow);
 5043:                                     if ($rem == 0) {
 5044:                                         if ($i > 0) {
 5045:                                             $current{$type} .= '</tr>';
 5046:                                         }
 5047:                                         $current{$type} .= '<tr>';
 5048:                                     }
 5049:                                     $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;'.
 5050:                                                        $spareid->{$server}{$type}[$i].
 5051:                                                        '</label></td>'."\n";
 5052:                                 }
 5053:                                 my $rem = @spares%($numinrow);
 5054:                                 my $colsleft = $numinrow - $rem;
 5055:                                 if ($colsleft > 1 ) {
 5056:                                     $current{$type} .= '<td colspan="'.$colsleft.
 5057:                                                        '" class="LC_left_item">'.
 5058:                                                        '&nbsp;</td>';
 5059:                                 } elsif ($colsleft == 1) {
 5060:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 5061:                                 }
 5062:                                 $current{$type} .= '</tr></table>';
 5063:                             }
 5064:                         }
 5065:                     }
 5066:                     if ($current{$type} eq '') {
 5067:                         $current{$type} = &mt('None specified');
 5068:                     }
 5069:                     if ($othercontrol) {
 5070:                         if ($type eq 'primary') {
 5071:                             $canselect{$type} = $othercontrol;
 5072:                         }
 5073:                     } else {
 5074:                         $canselect{$type} = 
 5075:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 5076:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 5077:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 5078:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 5079:                         if (@choices > 0) {
 5080:                             foreach my $lonhost (@choices) {
 5081:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 5082:                             }
 5083:                         }
 5084:                         $canselect{$type} .= '</select>'."\n";
 5085:                     }
 5086:                 } else {
 5087:                     $current{$type} = &mt('Could not be determined');
 5088:                     if ($type eq 'primary') {
 5089:                         $canselect{$type} =  $othercontrol;
 5090:                     }
 5091:                 }
 5092:                 if ($type eq 'default') {
 5093:                     $datatable .= '<tr'.$css_class.'>';
 5094:                 }
 5095:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 5096:                               '<td>'.$current{$type}.'</td>'."\n".
 5097:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 5098:             }
 5099:             $itemcount ++;
 5100:         }
 5101:     }
 5102:     $$rowtotal += $itemcount;
 5103:     return $datatable;
 5104: }
 5105: 
 5106: sub possible_newspares {
 5107:     my ($server,$currspares,$serverhomes,$altids) = @_;
 5108:     my $serverhostname = &Apache::lonnet::hostname($server);
 5109:     my %excluded;
 5110:     if ($serverhostname ne '') {
 5111:         %excluded = (
 5112:                        $serverhostname => 1,
 5113:                     );
 5114:     }
 5115:     if (ref($currspares) eq 'HASH') {
 5116:         foreach my $type (keys(%{$currspares})) {
 5117:             if (ref($currspares->{$type}) eq 'ARRAY') {
 5118:                 if (@{$currspares->{$type}} > 0) {
 5119:                     foreach my $curr (@{$currspares->{$type}}) {
 5120:                         my $hostname = &Apache::lonnet::hostname($curr);
 5121:                         $excluded{$hostname} = 1;
 5122:                     }
 5123:                 }
 5124:             }
 5125:         }
 5126:     }
 5127:     my @choices;
 5128:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 5129:         if (keys(%{$serverhomes}) > 1) {
 5130:             foreach my $name (sort(keys(%{$serverhomes}))) {
 5131:                 unless ($excluded{$name}) {
 5132:                     if (exists($altids->{$serverhomes->{$name}})) {
 5133:                         push(@choices,$altids->{$serverhomes->{$name}});
 5134:                     } else {
 5135:                         push(@choices,$serverhomes->{$name});
 5136:                     }
 5137:                 }
 5138:             }
 5139:         }
 5140:     }
 5141:     return sort(@choices);
 5142: }
 5143: 
 5144: sub print_loadbalancing {
 5145:     my ($dom,$settings,$rowtotal) = @_;
 5146:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 5147:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 5148:     my $numinrow = 1;
 5149:     my $datatable;
 5150:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5151:     my (%currbalancer,%currtargets,%currrules,%existing);
 5152:     if (ref($settings) eq 'HASH') {
 5153:         %existing = %{$settings};
 5154:     }
 5155:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 5156:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 5157:                                   \%currtargets,\%currrules);
 5158:     } else {
 5159:         return;
 5160:     }
 5161:     my ($othertitle,$usertypes,$types) =
 5162:         &Apache::loncommon::sorted_inst_types($dom);
 5163:     my $rownum = 8;
 5164:     if (ref($types) eq 'ARRAY') {
 5165:         $rownum += scalar(@{$types});
 5166:     }
 5167:     my @css_class = ('LC_odd_row','LC_even_row');
 5168:     my $balnum = 0;
 5169:     my $islast;
 5170:     my (@toshow,$disabledtext);
 5171:     if (keys(%currbalancer) > 0) {
 5172:         @toshow = sort(keys(%currbalancer));
 5173:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 5174:             push(@toshow,'');
 5175:         }
 5176:     } else {
 5177:         @toshow = ('');
 5178:         $disabledtext = &mt('No existing load balancer');
 5179:     }
 5180:     foreach my $lonhost (@toshow) {
 5181:         if ($balnum == scalar(@toshow)-1) {
 5182:             $islast = 1;
 5183:         } else {
 5184:             $islast = 0;
 5185:         }
 5186:         my $cssidx = $balnum%2;
 5187:         my $targets_div_style = 'display: none';
 5188:         my $disabled_div_style = 'display: block';
 5189:         my $homedom_div_style = 'display: none';
 5190:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 5191:                       '<td rowspan="'.$rownum.'" valign="top">'.
 5192:                       '<p>';
 5193:         if ($lonhost eq '') {
 5194:             $datatable .= '<span class="LC_nobreak">';
 5195:             if (keys(%currbalancer) > 0) {
 5196:                 $datatable .= &mt('Add balancer:');
 5197:             } else {
 5198:                 $datatable .= &mt('Enable balancer:');
 5199:             }
 5200:             $datatable .= '&nbsp;'.
 5201:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 5202:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 5203:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 5204:                           '<option value="" selected="selected">'.&mt('None').
 5205:                           '</option>'."\n";
 5206:             foreach my $server (sort(keys(%servers))) {
 5207:                 next if ($currbalancer{$server});
 5208:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 5209:             }
 5210:             $datatable .=
 5211:                 '</select>'."\n".
 5212:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 5213:         } else {
 5214:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 5215:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 5216:                            &mt('Stop balancing').'</label>'.
 5217:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 5218:             $targets_div_style = 'display: block';
 5219:             $disabled_div_style = 'display: none';
 5220:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 5221:                 $homedom_div_style = 'display: block';
 5222:             }
 5223:         }
 5224:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 5225:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 5226:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 5227:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 5228:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 5229:         my @sparestypes = ('primary','default');
 5230:         my %typetitles = &sparestype_titles();
 5231:         my %hostherechecked = (
 5232:                                   no => ' checked="checked"',
 5233:                               );
 5234:         foreach my $sparetype (@sparestypes) {
 5235:             my $targettable;
 5236:             for (my $i=0; $i<$numspares; $i++) {
 5237:                 my $checked;
 5238:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 5239:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5240:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5241:                             $checked = ' checked="checked"';
 5242:                         }
 5243:                     }
 5244:                 }
 5245:                 my ($chkboxval,$disabled);
 5246:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 5247:                     $chkboxval = $spares[$i];
 5248:                 }
 5249:                 if (exists($currbalancer{$spares[$i]})) {
 5250:                     $disabled = ' disabled="disabled"';
 5251:                 }
 5252:                 $targettable .=
 5253:                     '<td><span class="LC_nobreak"><label>'.
 5254:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 5255:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 5256:                     '</span></label></span></td>';
 5257:                 my $rem = $i%($numinrow);
 5258:                 if ($rem == 0) {
 5259:                     if (($i > 0) && ($i < $numspares-1)) {
 5260:                         $targettable .= '</tr>';
 5261:                     }
 5262:                     if ($i < $numspares-1) {
 5263:                         $targettable .= '<tr>';
 5264:                     }
 5265:                 }
 5266:             }
 5267:             if ($targettable ne '') {
 5268:                 my $rem = $numspares%($numinrow);
 5269:                 my $colsleft = $numinrow - $rem;
 5270:                 if ($colsleft > 1 ) {
 5271:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5272:                                     '&nbsp;</td>';
 5273:                 } elsif ($colsleft == 1) {
 5274:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 5275:                 }
 5276:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 5277:                                '<table><tr>'.$targettable.'</tr></table><br />';
 5278:             }
 5279:             $hostherechecked{$sparetype} = '';
 5280:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 5281:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5282:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5283:                         $hostherechecked{$sparetype} = ' checked="checked"';
 5284:                         $hostherechecked{'no'} = '';
 5285:                     }
 5286:                 }
 5287:             }
 5288:         }
 5289:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 5290:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 5291:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 5292:         foreach my $sparetype (@sparestypes) {
 5293:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 5294:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 5295:                           '</i></label><br />';
 5296:         }
 5297:         $datatable .= '</div></td></tr>'.
 5298:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 5299:                                            $othertitle,$usertypes,$types,\%servers,
 5300:                                            \%currbalancer,$lonhost,
 5301:                                            $targets_div_style,$homedom_div_style,
 5302:                                            $css_class[$cssidx],$balnum,$islast);
 5303:         $$rowtotal += $rownum;
 5304:         $balnum ++;
 5305:     }
 5306:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 5307:     return $datatable;
 5308: }
 5309: 
 5310: sub get_loadbalancers_config {
 5311:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 5312:     return unless ((ref($servers) eq 'HASH') &&
 5313:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 5314:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 5315:     if (keys(%{$existing}) > 0) {
 5316:         my $oldlonhost;
 5317:         foreach my $key (sort(keys(%{$existing}))) {
 5318:             if ($key eq 'lonhost') {
 5319:                 $oldlonhost = $existing->{'lonhost'};
 5320:                 $currbalancer->{$oldlonhost} = 1;
 5321:             } elsif ($key eq 'targets') {
 5322:                 if ($oldlonhost) {
 5323:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 5324:                 }
 5325:             } elsif ($key eq 'rules') {
 5326:                 if ($oldlonhost) {
 5327:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 5328:                 }
 5329:             } elsif (ref($existing->{$key}) eq 'HASH') {
 5330:                 $currbalancer->{$key} = 1;
 5331:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 5332:                 $currrules->{$key} = $existing->{$key}{'rules'};
 5333:             }
 5334:         }
 5335:     } else {
 5336:         my ($balancerref,$targetsref) =
 5337:                 &Apache::lonnet::get_lonbalancer_config($servers);
 5338:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 5339:             foreach my $server (sort(keys(%{$balancerref}))) {
 5340:                 $currbalancer->{$server} = 1;
 5341:                 $currtargets->{$server} = $targetsref->{$server};
 5342:             }
 5343:         }
 5344:     }
 5345:     return;
 5346: }
 5347: 
 5348: sub loadbalancing_rules {
 5349:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 5350:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 5351:         $css_class,$balnum,$islast) = @_;
 5352:     my $output;
 5353:     my $num = 0;
 5354:     my ($alltypes,$othertypes,$titles) =
 5355:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 5356:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 5357:         foreach my $type (@{$alltypes}) {
 5358:             $num ++;
 5359:             my $current;
 5360:             if (ref($currrules) eq 'HASH') {
 5361:                 $current = $currrules->{$type};
 5362:             }
 5363:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5364:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 5365:                     $current = '';
 5366:                 }
 5367:             }
 5368:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 5369:                                              $servers,$currbalancer,$lonhost,$dom,
 5370:                                              $targets_div_style,$homedom_div_style,
 5371:                                              $css_class,$balnum,$num,$islast);
 5372:         }
 5373:     }
 5374:     return $output;
 5375: }
 5376: 
 5377: sub loadbalancing_titles {
 5378:     my ($dom,$intdom,$usertypes,$types) = @_;
 5379:     my %othertypes = (
 5380:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 5381:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 5382:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 5383:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 5384:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 5385:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 5386:                      );
 5387:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 5388:     if (ref($types) eq 'ARRAY') {
 5389:         unshift(@alltypes,@{$types},'default');
 5390:     }
 5391:     my %titles;
 5392:     foreach my $type (@alltypes) {
 5393:         if ($type =~ /^_LC_/) {
 5394:             $titles{$type} = $othertypes{$type};
 5395:         } elsif ($type eq 'default') {
 5396:             $titles{$type} = &mt('All users from [_1]',$dom);
 5397:             if (ref($types) eq 'ARRAY') {
 5398:                 if (@{$types} > 0) {
 5399:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 5400:                 }
 5401:             }
 5402:         } elsif (ref($usertypes) eq 'HASH') {
 5403:             $titles{$type} = $usertypes->{$type};
 5404:         }
 5405:     }
 5406:     return (\@alltypes,\%othertypes,\%titles);
 5407: }
 5408: 
 5409: sub loadbalance_rule_row {
 5410:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 5411:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 5412:     my @rulenames;
 5413:     my %ruletitles = &offloadtype_text();
 5414:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 5415:         @rulenames = ('balancer','offloadedto','specific');
 5416:     } else {
 5417:         @rulenames = ('default','homeserver');
 5418:         if ($type eq '_LC_external') {
 5419:             push(@rulenames,'externalbalancer');
 5420:         } else {
 5421:             push(@rulenames,'specific');
 5422:         }
 5423:         push(@rulenames,'none');
 5424:     }
 5425:     my $style = $targets_div_style;
 5426:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5427:         $style = $homedom_div_style;
 5428:     }
 5429:     my $space;
 5430:     if ($islast && $num == 1) {
 5431:         $space = '<div display="inline-block">&nbsp;</div>';
 5432:     }
 5433:     my $output =
 5434:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 5435:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 5436:         '<td valaign="top">'.$space.
 5437:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 5438:     for (my $i=0; $i<@rulenames; $i++) {
 5439:         my $rule = $rulenames[$i];
 5440:         my ($checked,$extra);
 5441:         if ($rulenames[$i] eq 'default') {
 5442:             $rule = '';
 5443:         }
 5444:         if ($rulenames[$i] eq 'specific') {
 5445:             if (ref($servers) eq 'HASH') {
 5446:                 my $default;
 5447:                 if (($current ne '') && (exists($servers->{$current}))) {
 5448:                     $checked = ' checked="checked"';
 5449:                 }
 5450:                 unless ($checked) {
 5451:                     $default = ' selected="selected"';
 5452:                 }
 5453:                 $extra =
 5454:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5455:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5456:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 5457:                     '<option value=""'.$default.'></option>'."\n";
 5458:                 foreach my $server (sort(keys(%{$servers}))) {
 5459:                     if (ref($currbalancer) eq 'HASH') {
 5460:                         next if (exists($currbalancer->{$server}));
 5461:                     }
 5462:                     my $selected;
 5463:                     if ($server eq $current) {
 5464:                         $selected = ' selected="selected"';
 5465:                     }
 5466:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 5467:                 }
 5468:                 $extra .= '</select>';
 5469:             }
 5470:         } elsif ($rule eq $current) {
 5471:             $checked = ' checked="checked"';
 5472:         }
 5473:         $output .= '<span class="LC_nobreak"><label>'.
 5474:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 5475:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 5476:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 5477:                    ')"'.$checked.' />&nbsp;';
 5478:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 5479:             $output .= $ruletitles{'particular'};
 5480:         } else {
 5481:             $output .= $ruletitles{$rulenames[$i]};
 5482:         }
 5483:         $output .= '</label>'.$extra.'</span><br />'."\n";
 5484:     }
 5485:     $output .= '</div></td></tr>'."\n";
 5486:     return $output;
 5487: }
 5488: 
 5489: sub offloadtype_text {
 5490:     my %ruletitles = &Apache::lonlocal::texthash (
 5491:            'default'          => 'Offloads to default destinations',
 5492:            'homeserver'       => "Offloads to user's home server",
 5493:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 5494:            'specific'         => 'Offloads to specific server',
 5495:            'none'             => 'No offload',
 5496:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 5497:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 5498:            'particular'       => 'Session hosted (after re-auth) on server:',
 5499:     );
 5500:     return %ruletitles;
 5501: }
 5502: 
 5503: sub sparestype_titles {
 5504:     my %typestitles = &Apache::lonlocal::texthash (
 5505:                           'primary' => 'primary',
 5506:                           'default' => 'default',
 5507:                       );
 5508:     return %typestitles;
 5509: }
 5510: 
 5511: sub contact_titles {
 5512:     my %titles = &Apache::lonlocal::texthash (
 5513:                    'supportemail'    => 'Support E-mail address',
 5514:                    'adminemail'      => 'Default Server Admin E-mail address',
 5515:                    'errormail'       => 'Error reports to be e-mailed to',
 5516:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 5517:                    'helpdeskmail'    => "Helpdesk requests for this domain's users",
 5518:                    'otherdomsmail'   => 'Helpdesk requests for other (unconfigured) domains',
 5519:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 5520:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 5521:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 5522:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 5523:                  );
 5524:     my %short_titles = &Apache::lonlocal::texthash (
 5525:                            adminemail   => 'Admin E-mail address',
 5526:                            supportemail => 'Support E-mail',
 5527:                        );   
 5528:     return (\%titles,\%short_titles);
 5529: }
 5530: 
 5531: sub helpform_fields {
 5532:     my %titles =  &Apache::lonlocal::texthash (
 5533:                        'username'   => 'Name',
 5534:                        'user'       => 'Username/domain',
 5535:                        'phone'      => 'Phone',
 5536:                        'cc'         => 'Cc e-mail',
 5537:                        'course'     => 'Course Details',
 5538:                        'section'    => 'Sections',
 5539:                        'screenshot' => 'File upload',
 5540:     );
 5541:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 5542:     my %possoptions = (
 5543:                         username     => ['yes','no','req'],
 5544:                         phone        => ['yes','no','req'],
 5545:                         user         => ['yes','no'],
 5546:                         cc           => ['yes','no'],
 5547:                         course       => ['yes','no'],
 5548:                         section      => ['yes','no'],
 5549:                         screenshot   => ['yes','no'],
 5550:                       );
 5551:     my %fieldoptions = &Apache::lonlocal::texthash (
 5552:                          'yes'  => 'Optional',
 5553:                          'req'  => 'Required',
 5554:                          'no'   => "Not shown",
 5555:     );
 5556:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 5557: }
 5558: 
 5559: sub tool_titles {
 5560:     my %titles = &Apache::lonlocal::texthash (
 5561:                      aboutme    => 'Personal web page',
 5562:                      blog       => 'Blog',
 5563:                      webdav     => 'WebDAV',
 5564:                      portfolio  => 'Portfolio',
 5565:                      official   => 'Official courses (with institutional codes)',
 5566:                      unofficial => 'Unofficial courses',
 5567:                      community  => 'Communities',
 5568:                      textbook   => 'Textbook courses',
 5569:                      placement  => 'Placement tests',
 5570:                  );
 5571:     return %titles;
 5572: }
 5573: 
 5574: sub courserequest_titles {
 5575:     my %titles = &Apache::lonlocal::texthash (
 5576:                                    official   => 'Official',
 5577:                                    unofficial => 'Unofficial',
 5578:                                    community  => 'Communities',
 5579:                                    textbook   => 'Textbook',
 5580:                                    placement  => 'Placement tests',
 5581:                                    norequest  => 'Not allowed',
 5582:                                    approval   => 'Approval by Dom. Coord.',
 5583:                                    validate   => 'With validation',
 5584:                                    autolimit  => 'Numerical limit',
 5585:                                    unlimited  => '(blank for unlimited)',
 5586:                  );
 5587:     return %titles;
 5588: }
 5589: 
 5590: sub authorrequest_titles {
 5591:     my %titles = &Apache::lonlocal::texthash (
 5592:                                    norequest  => 'Not allowed',
 5593:                                    approval   => 'Approval by Dom. Coord.',
 5594:                                    automatic  => 'Automatic approval',
 5595:                  );
 5596:     return %titles;
 5597: }
 5598: 
 5599: sub courserequest_conditions {
 5600:     my %conditions = &Apache::lonlocal::texthash (
 5601:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 5602:        validate   => '(Processing of request subject to institutional validation).',
 5603:                  );
 5604:     return %conditions;
 5605: }
 5606: 
 5607: 
 5608: sub print_usercreation {
 5609:     my ($position,$dom,$settings,$rowtotal) = @_;
 5610:     my $numinrow = 4;
 5611:     my $datatable;
 5612:     if ($position eq 'top') {
 5613:         $$rowtotal ++;
 5614:         my $rowcount = 0;
 5615:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 5616:         if (ref($rules) eq 'HASH') {
 5617:             if (keys(%{$rules}) > 0) {
 5618:                 $datatable .= &user_formats_row('username',$settings,$rules,
 5619:                                                 $ruleorder,$numinrow,$rowcount);
 5620:                 $$rowtotal ++;
 5621:                 $rowcount ++;
 5622:             }
 5623:         }
 5624:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 5625:         if (ref($idrules) eq 'HASH') {
 5626:             if (keys(%{$idrules}) > 0) {
 5627:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 5628:                                                 $idruleorder,$numinrow,$rowcount);
 5629:                 $$rowtotal ++;
 5630:                 $rowcount ++;
 5631:             }
 5632:         }
 5633:         if ($rowcount == 0) {
 5634:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 5635:             $$rowtotal ++;
 5636:             $rowcount ++;
 5637:         }
 5638:     } elsif ($position eq 'middle') {
 5639:         my @creators = ('author','course','requestcrs');
 5640:         my ($rules,$ruleorder) =
 5641:             &Apache::lonnet::inst_userrules($dom,'username');
 5642:         my %lt = &usercreation_types();
 5643:         my %checked;
 5644:         if (ref($settings) eq 'HASH') {
 5645:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 5646:                 foreach my $item (@creators) {
 5647:                     $checked{$item} = $settings->{'cancreate'}{$item};
 5648:                 }
 5649:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 5650:                 foreach my $item (@creators) {
 5651:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 5652:                         $checked{$item} = 'none';
 5653:                     }
 5654:                 }
 5655:             }
 5656:         }
 5657:         my $rownum = 0;
 5658:         foreach my $item (@creators) {
 5659:             $rownum ++;
 5660:             if ($checked{$item} eq '') {
 5661:                 $checked{$item} = 'any';
 5662:             }
 5663:             my $css_class;
 5664:             if ($rownum%2) {
 5665:                 $css_class = '';
 5666:             } else {
 5667:                 $css_class = ' class="LC_odd_row" ';
 5668:             }
 5669:             $datatable .= '<tr'.$css_class.'>'.
 5670:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 5671:                          '</span></td><td align="right">';
 5672:             my @options = ('any');
 5673:             if (ref($rules) eq 'HASH') {
 5674:                 if (keys(%{$rules}) > 0) {
 5675:                     push(@options,('official','unofficial'));
 5676:                 }
 5677:             }
 5678:             push(@options,'none');
 5679:             foreach my $option (@options) {
 5680:                 my $type = 'radio';
 5681:                 my $check = ' ';
 5682:                 if ($checked{$item} eq $option) {
 5683:                     $check = ' checked="checked" ';
 5684:                 } 
 5685:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5686:                               '<input type="'.$type.'" name="can_createuser_'.
 5687:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 5688:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 5689:             }
 5690:             $datatable .= '</td></tr>';
 5691:         }
 5692:     } else {
 5693:         my @contexts = ('author','course','domain');
 5694:         my @authtypes = ('int','krb4','krb5','loc');
 5695:         my %checked;
 5696:         if (ref($settings) eq 'HASH') {
 5697:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 5698:                 foreach my $item (@contexts) {
 5699:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 5700:                         foreach my $auth (@authtypes) {
 5701:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 5702:                                 $checked{$item}{$auth} = ' checked="checked" ';
 5703:                             }
 5704:                         }
 5705:                     }
 5706:                 }
 5707:             }
 5708:         } else {
 5709:             foreach my $item (@contexts) {
 5710:                 foreach my $auth (@authtypes) {
 5711:                     $checked{$item}{$auth} = ' checked="checked" ';
 5712:                 }
 5713:             }
 5714:         }
 5715:         my %title = &context_names();
 5716:         my %authname = &authtype_names();
 5717:         my $rownum = 0;
 5718:         my $css_class; 
 5719:         foreach my $item (@contexts) {
 5720:             if ($rownum%2) {
 5721:                 $css_class = '';
 5722:             } else {
 5723:                 $css_class = ' class="LC_odd_row" ';
 5724:             }
 5725:             $datatable .=   '<tr'.$css_class.'>'.
 5726:                             '<td>'.$title{$item}.
 5727:                             '</td><td class="LC_left_item">'.
 5728:                             '<span class="LC_nobreak">';
 5729:             foreach my $auth (@authtypes) {
 5730:                 $datatable .= '<label>'. 
 5731:                               '<input type="checkbox" name="'.$item.'_auth" '.
 5732:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 5733:                               $authname{$auth}.'</label>&nbsp;';
 5734:             }
 5735:             $datatable .= '</span></td></tr>';
 5736:             $rownum ++;
 5737:         }
 5738:         $$rowtotal += $rownum;
 5739:     }
 5740:     return $datatable;
 5741: }
 5742: 
 5743: sub print_selfcreation {
 5744:     my ($position,$dom,$settings,$rowtotal) = @_;
 5745:     my (@selfcreate,$createsettings,$processing,$datatable);
 5746:     if (ref($settings) eq 'HASH') {
 5747:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 5748:             $createsettings = $settings->{'cancreate'};
 5749:             if (ref($createsettings) eq 'HASH') {
 5750:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 5751:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 5752:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 5753:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 5754:                         @selfcreate = ('email','login','sso');
 5755:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 5756:                         @selfcreate = ($createsettings->{'selfcreate'});
 5757:                     }
 5758:                 }
 5759:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 5760:                     $processing = $createsettings->{'selfcreateprocessing'};
 5761:                 }
 5762:             }
 5763:         }
 5764:     }
 5765:     my %radiohash;
 5766:     my $numinrow = 4;
 5767:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 5768:     if ($position eq 'top') {
 5769:         my %choices = &Apache::lonlocal::texthash (
 5770:                                                       cancreate_login      => 'Institutional Login',
 5771:                                                       cancreate_sso        => 'Institutional Single Sign On',
 5772:                                                   );
 5773:         my @toggles = sort(keys(%choices));
 5774:         my %defaultchecked = (
 5775:                                'cancreate_login' => 'off',
 5776:                                'cancreate_sso'   => 'off',
 5777:                              );
 5778:         my ($onclick,$itemcount);
 5779:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5780:                                                      \%choices,$itemcount,$onclick);
 5781:         $$rowtotal += $itemcount;
 5782:         
 5783:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5784: 
 5785:         if (ref($usertypes) eq 'HASH') {
 5786:             if (keys(%{$usertypes}) > 0) {
 5787:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 5788:                                              $dom,$numinrow,$othertitle,
 5789:                                              'statustocreate',$$rowtotal);
 5790:                 $$rowtotal ++;
 5791:             }
 5792:         }
 5793:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 5794:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5795:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 5796:         my $rem;
 5797:         my $numperrow = 2;
 5798:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 5799:         $datatable .= '<tr'.$css_class.'>'.
 5800:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 5801:                      '<td class="LC_left_item">'."\n".
 5802:                      '<table><tr><td>'."\n";
 5803:         for (my $i=0; $i<@fields; $i++) {
 5804:             $rem = $i%($numperrow);
 5805:             if ($rem == 0) {
 5806:                 if ($i > 0) {
 5807:                     $datatable .= '</tr>';
 5808:                 }
 5809:                 $datatable .= '<tr>';
 5810:             }
 5811:             my $currval;
 5812:             if (ref($createsettings) eq 'HASH') {
 5813:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 5814:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 5815:                 }
 5816:             }
 5817:             $datatable .= '<td class="LC_left_item">'.
 5818:                           '<span class="LC_nobreak">'.
 5819:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 5820:                           'value="'.$currval.'" size="10" />&nbsp;'.
 5821:                           $fieldtitles{$fields[$i]}.'</span></td>';
 5822:         }
 5823:         my $colsleft = $numperrow - $rem;
 5824:         if ($colsleft > 1 ) {
 5825:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5826:                          '&nbsp;</td>';
 5827:         } elsif ($colsleft == 1) {
 5828:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5829:         }
 5830:         $datatable .= '</tr></table></td></tr>';
 5831:         $$rowtotal ++;
 5832:     } elsif ($position eq 'middle') {
 5833:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 5834:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5835:         $usertypes->{'default'} = $othertitle;
 5836:         if (ref($types) eq 'ARRAY') {
 5837:             push(@{$types},'default');
 5838:             $usertypes->{'default'} = $othertitle;
 5839:             foreach my $status (@{$types}) {
 5840:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 5841:                                                        $numinrow,$$rowtotal,$usertypes);
 5842:                 $$rowtotal ++;
 5843:             }
 5844:         }
 5845:     } else {
 5846:         my %choices = &Apache::lonlocal::texthash (
 5847:                                                       cancreate_email => 'E-mail address as username',
 5848:                                                   );
 5849:         my @toggles = sort(keys(%choices));
 5850:         my %defaultchecked = (
 5851:                                'cancreate_email' => 'off',
 5852:                              );
 5853:         my $itemcount = 0;
 5854:         my $display = 'none';
 5855:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 5856:             $display = 'block';
 5857:         }
 5858:         my $onclick = "toggleDisplay(this.form,'emailoptions');";
 5859:         my $additional = '<div id="emailoptions" style="display: '.$display.'">';
 5860:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5861:         my $usertypes = {};
 5862:         my $order = [];
 5863:         if ((ref($domdefaults{'inststatustypes'}) eq 'HASH') && (ref($domdefaults{'inststatusguest'}) eq 'ARRAY')) {
 5864:             $usertypes = $domdefaults{'inststatustypes'};
 5865:             $order = $domdefaults{'inststatusguest'};
 5866:         }
 5867:         if (ref($order) eq 'ARRAY') {
 5868:             push(@{$order},'default');
 5869:             if (@{$order} > 1) {
 5870:                 $usertypes->{'default'} = &mt('Other users');
 5871:                 $additional .= '<table><tr>';
 5872:                 foreach my $status (@{$order}) {
 5873:                     $additional .= '<th>'.$usertypes->{$status}.'</th>';
 5874:                 }
 5875:                 $additional .= '</tr><tr>';
 5876:                 foreach my $status (@{$order}) {
 5877:                     $additional .= '<td>'.&email_as_username($rowtotal,$processing,$status).'</td>';
 5878:                 }
 5879:                 $additional .= '</tr></table>';
 5880:             } else {
 5881:                 $usertypes->{'default'} = &mt('All users');
 5882:                 $additional .= &email_as_username($rowtotal,$processing);
 5883:             }
 5884:         }
 5885:         $additional .= '</div>'."\n";
 5886: 
 5887:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5888:                                                      \%choices,$$rowtotal,$onclick,$additional);
 5889:         $$rowtotal ++;
 5890:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 5891:         $$rowtotal ++;
 5892:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 5893:         $numinrow = 1;
 5894:         if (ref($order) eq 'ARRAY') {
 5895:             foreach my $status (@{$order}) {
 5896:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 5897:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 5898:                 $$rowtotal ++;
 5899:             }
 5900:         }
 5901:         my ($emailrules,$emailruleorder) =
 5902:             &Apache::lonnet::inst_userrules($dom,'email');
 5903:         if (ref($emailrules) eq 'HASH') {
 5904:             if (keys(%{$emailrules}) > 0) {
 5905:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 5906:                                                 $emailruleorder,$numinrow,$$rowtotal);
 5907:                 $$rowtotal ++;
 5908:             }
 5909:         }
 5910:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 5911:     }
 5912:     return $datatable;
 5913: }
 5914: 
 5915: sub email_as_username {
 5916:     my ($rowtotal,$processing,$type) = @_;
 5917:     my %choices =
 5918:         &Apache::lonlocal::texthash (
 5919:                                       automatic => 'Automatic approval',
 5920:                                       approval  => 'Queued for approval',
 5921:                                     );
 5922:     my $output;
 5923:     foreach my $option ('automatic','approval') {
 5924:         my $checked;
 5925:         if (ref($processing) eq 'HASH') {
 5926:             if ($type eq '') {   
 5927:                 if (!exists($processing->{'default'})) {
 5928:                     if ($option eq 'automatic') {
 5929:                         $checked = ' checked="checked"';
 5930:                     }
 5931:                 } else {
 5932:                     if ($processing->{'default'} eq $option) {
 5933:                         $checked = ' checked="checked"';
 5934:                     }
 5935:                 }
 5936:             } else {
 5937:                 if (!exists($processing->{$type})) {
 5938:                     if ($option eq 'automatic') {
 5939:                         $checked = ' checked="checked"';
 5940:                     }
 5941:                 } else {
 5942:                     if ($processing->{$type} eq $option) {
 5943:                         $checked = ' checked="checked"';
 5944:                     }
 5945:                 }
 5946:             }
 5947:         } elsif ($option eq 'automatic') {
 5948:             $checked = ' checked="checked"'; 
 5949:         }
 5950:         my $name = 'cancreate_emailprocess';
 5951:         if (($type ne '') && ($type ne 'default')) {
 5952:             $name .= '_'.$type;
 5953:         }
 5954:         $output .= '<span class="LC_nobreak"><label>'.
 5955:                    '<input type="radio" name="'.$name.'"'.
 5956:                    $checked.' value="'.$option.'" />'.
 5957:                    $choices{$option}.'</label></span>';
 5958:         if ($type eq '') {
 5959:             $output .= '&nbsp;';
 5960:         } else {
 5961:             $output .= '<br />';
 5962:         }
 5963:     }
 5964:     $$rowtotal ++;
 5965:     return $output;
 5966: }
 5967: 
 5968: sub captcha_choice {
 5969:     my ($context,$settings,$itemcount) = @_;
 5970:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 5971:         $vertext,$currver);
 5972:     my %lt = &captcha_phrases();
 5973:     $keyentry = 'hidden';
 5974:     if ($context eq 'cancreate') {
 5975:         $rowname = &mt('CAPTCHA validation');
 5976:     } elsif ($context eq 'login') {
 5977:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 5978:     }
 5979:     if (ref($settings) eq 'HASH') {
 5980:         if ($settings->{'captcha'}) {
 5981:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 5982:         } else {
 5983:             $checked{'original'} = ' checked="checked"';
 5984:         }
 5985:         if ($settings->{'captcha'} eq 'recaptcha') {
 5986:             $pubtext = $lt{'pub'};
 5987:             $privtext = $lt{'priv'};
 5988:             $keyentry = 'text';
 5989:             $vertext = $lt{'ver'};
 5990:             $currver = $settings->{'recaptchaversion'};
 5991:             if ($currver ne '2') {
 5992:                 $currver = 1;
 5993:             }
 5994:         }
 5995:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 5996:             $currpub = $settings->{'recaptchakeys'}{'public'};
 5997:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 5998:         }
 5999:     } else {
 6000:         $checked{'original'} = ' checked="checked"';
 6001:     }
 6002:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6003:     my $output = '<tr'.$css_class.'>'.
 6004:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 6005:                  '<table><tr><td>'."\n";
 6006:     foreach my $option ('original','recaptcha','notused') {
 6007:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 6008:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 6009:                    $lt{$option}.'</label></span>';
 6010:         unless ($option eq 'notused') {
 6011:             $output .= ('&nbsp;'x2)."\n";
 6012:         }
 6013:     }
 6014: #
 6015: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 6016: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 6017: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 6018: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 6019: #
 6020:     $output .= '</td></tr>'."\n".
 6021:                '<tr><td>'."\n".
 6022:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 6023:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 6024:                $currpub.'" size="40" /></span><br />'."\n".
 6025:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 6026:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 6027:                $currpriv.'" size="40" /></span><br />'.
 6028:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 6029:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 6030:                $currver.'" size="3" /></span><br />'.
 6031:                '</td></tr></table>'."\n".
 6032:                '</td></tr>';
 6033:     return $output;
 6034: }
 6035: 
 6036: sub user_formats_row {
 6037:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 6038:     my $output;
 6039:     my %text = (
 6040:                    'username' => 'new usernames',
 6041:                    'id'       => 'IDs',
 6042:                    'email'    => 'self-created accounts (e-mail)',
 6043:                );
 6044:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 6045:     $output = '<tr '.$css_class.'>'.
 6046:               '<td><span class="LC_nobreak">';
 6047:     if ($type eq 'email') {
 6048:         $output .= &mt("Formats disallowed for $text{$type}: ");
 6049:     } else {
 6050:         $output .= &mt("Format rules to check for $text{$type}: ");
 6051:     }
 6052:     $output .= '</span></td>'.
 6053:                '<td class="LC_left_item" colspan="2"><table>';
 6054:     my $rem;
 6055:     if (ref($ruleorder) eq 'ARRAY') {
 6056:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 6057:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 6058:                 my $rem = $i%($numinrow);
 6059:                 if ($rem == 0) {
 6060:                     if ($i > 0) {
 6061:                         $output .= '</tr>';
 6062:                     }
 6063:                     $output .= '<tr>';
 6064:                 }
 6065:                 my $check = ' ';
 6066:                 if (ref($settings) eq 'HASH') {
 6067:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 6068:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 6069:                             $check = ' checked="checked" ';
 6070:                         }
 6071:                     }
 6072:                 }
 6073:                 $output .= '<td class="LC_left_item">'.
 6074:                            '<span class="LC_nobreak"><label>'.
 6075:                            '<input type="checkbox" name="'.$type.'_rule" '.
 6076:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 6077:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 6078:             }
 6079:         }
 6080:         $rem = @{$ruleorder}%($numinrow);
 6081:     }
 6082:     my $colsleft = $numinrow - $rem;
 6083:     if ($colsleft > 1 ) {
 6084:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6085:                    '&nbsp;</td>';
 6086:     } elsif ($colsleft == 1) {
 6087:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 6088:     }
 6089:     $output .= '</tr></table></td></tr>';
 6090:     return $output;
 6091: }
 6092: 
 6093: sub usercreation_types {
 6094:     my %lt = &Apache::lonlocal::texthash (
 6095:                     author     => 'When adding a co-author',
 6096:                     course     => 'When adding a user to a course',
 6097:                     requestcrs => 'When requesting a course',
 6098:                     any        => 'Any',
 6099:                     official   => 'Institutional only ',
 6100:                     unofficial => 'Non-institutional only',
 6101:                     none       => 'None',
 6102:     );
 6103:     return %lt;
 6104: }
 6105: 
 6106: sub selfcreation_types {
 6107:     my %lt = &Apache::lonlocal::texthash (
 6108:                     selfcreate => 'User creates own account',
 6109:                     any        => 'Any',
 6110:                     official   => 'Institutional only ',
 6111:                     unofficial => 'Non-institutional only',
 6112:                     email      => 'E-mail address',
 6113:                     login      => 'Institutional Login',
 6114:                     sso        => 'SSO',
 6115:              );
 6116: }
 6117: 
 6118: sub authtype_names {
 6119:     my %lt = &Apache::lonlocal::texthash(
 6120:                       int    => 'Internal',
 6121:                       krb4   => 'Kerberos 4',
 6122:                       krb5   => 'Kerberos 5',
 6123:                       loc    => 'Local',
 6124:                   );
 6125:     return %lt;
 6126: }
 6127: 
 6128: sub context_names {
 6129:     my %context_title = &Apache::lonlocal::texthash(
 6130:        author => 'Creating users when an Author',
 6131:        course => 'Creating users when in a course',
 6132:        domain => 'Creating users when a Domain Coordinator',
 6133:     );
 6134:     return %context_title;
 6135: }
 6136: 
 6137: sub print_usermodification {
 6138:     my ($position,$dom,$settings,$rowtotal) = @_;
 6139:     my $numinrow = 4;
 6140:     my ($context,$datatable,$rowcount);
 6141:     if ($position eq 'top') {
 6142:         $rowcount = 0;
 6143:         $context = 'author'; 
 6144:         foreach my $role ('ca','aa') {
 6145:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6146:                                                    $numinrow,$rowcount);
 6147:             $$rowtotal ++;
 6148:             $rowcount ++;
 6149:         }
 6150:     } elsif ($position eq 'bottom') {
 6151:         $context = 'course';
 6152:         $rowcount = 0;
 6153:         foreach my $role ('st','ep','ta','in','cr') {
 6154:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6155:                                                    $numinrow,$rowcount);
 6156:             $$rowtotal ++;
 6157:             $rowcount ++;
 6158:         }
 6159:     }
 6160:     return $datatable;
 6161: }
 6162: 
 6163: sub print_defaults {
 6164:     my ($position,$dom,$settings,$rowtotal) = @_;
 6165:     my $rownum = 0;
 6166:     my ($datatable,$css_class,$titles);
 6167:     unless ($position eq 'bottom') {
 6168:         $titles = &defaults_titles($dom);
 6169:     }
 6170:     if ($position eq 'top') {
 6171:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 6172:                      'datelocale_def','portal_def');
 6173:         my %defaults;
 6174:         if (ref($settings) eq 'HASH') {
 6175:             %defaults = %{$settings};
 6176:         } else {
 6177:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6178:             foreach my $item (@items) {
 6179:                 $defaults{$item} = $domdefaults{$item};
 6180:             }
 6181:         }
 6182:         foreach my $item (@items) {
 6183:             if ($rownum%2) {
 6184:                 $css_class = '';
 6185:             } else {
 6186:                 $css_class = ' class="LC_odd_row" ';
 6187:             }
 6188:             $datatable .= '<tr'.$css_class.'>'.
 6189:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 6190:                           '</span></td><td class="LC_right_item" colspan="3">';
 6191:             if ($item eq 'auth_def') {
 6192:                 my @authtypes = ('internal','krb4','krb5','localauth');
 6193:                 my %shortauth = (
 6194:                                  internal => 'int',
 6195:                                  krb4 => 'krb4',
 6196:                                  krb5 => 'krb5',
 6197:                                  localauth  => 'loc'
 6198:                                 );
 6199:                 my %authnames = &authtype_names();
 6200:                 foreach my $auth (@authtypes) {
 6201:                     my $checked = ' ';
 6202:                     if ($defaults{$item} eq $auth) {
 6203:                         $checked = ' checked="checked" ';
 6204:                     }
 6205:                     $datatable .= '<label><input type="radio" name="'.$item.
 6206:                                   '" value="'.$auth.'"'.$checked.'/>'.
 6207:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 6208:                 }
 6209:             } elsif ($item eq 'timezone_def') {
 6210:                 my $includeempty = 1;
 6211:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 6212:             } elsif ($item eq 'datelocale_def') {
 6213:                 my $includeempty = 1;
 6214:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 6215:             } elsif ($item eq 'lang_def') {
 6216:                 my $includeempty = 1;
 6217:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 6218:             } else {
 6219:                 my $size;
 6220:                 if ($item eq 'portal_def') {
 6221:                     $size = ' size="25"';
 6222:                 }
 6223:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6224:                               $defaults{$item}.'"'.$size.' />';
 6225:             }
 6226:             $datatable .= '</td></tr>';
 6227:             $rownum ++;
 6228:         }
 6229:     } elsif ($position eq 'middle') {
 6230:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 6231:         my %defaults;
 6232:         if (ref($settings) eq 'HASH') {
 6233:             %defaults = %{$settings};
 6234:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 6235:                 $defaults{'intauth_cost'} = 10;
 6236:             }
 6237:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 6238:                 $defaults{'intauth_check'} = 0;
 6239:             }
 6240:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 6241:                 $defaults{'intauth_switch'} = 0;
 6242:             }
 6243:         } else {
 6244:             %defaults = (
 6245:                           'intauth_cost'   => 10,
 6246:                           'intauth_check'  => 0,
 6247:                           'intauth_switch' => 0,
 6248:                         );
 6249:         }
 6250:         foreach my $item (@items) {
 6251:             if ($rownum%2) {
 6252:                 $css_class = '';
 6253:             } else {
 6254:                 $css_class = ' class="LC_odd_row" ';
 6255:             }
 6256:             $datatable .= '<tr'.$css_class.'>'.
 6257:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 6258:                           '</span></td><td class="LC_left_item" colspan="3">';
 6259:             if ($item eq 'intauth_switch') {
 6260:                 my @options = (0,1,2);
 6261:                 my %optiondesc = &Apache::lonlocal::texthash (
 6262:                                    0 => 'No',
 6263:                                    1 => 'Yes',
 6264:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 6265:                                  );
 6266:                 $datatable .= '<table width="100%">';
 6267:                 foreach my $option (@options) {
 6268:                     my $checked = ' ';
 6269:                     if ($defaults{$item} eq $option) {
 6270:                         $checked = ' checked="checked"';
 6271:                     }
 6272:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6273:                                   '<label><input type="radio" name="'.$item.
 6274:                                   '" value="'.$option.'"'.$checked.' />'.
 6275:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6276:                 }
 6277:                 $datatable .= '</table>';
 6278:             } elsif ($item eq 'intauth_check') {
 6279:                 my @options = (0,1,2);
 6280:                 my %optiondesc = &Apache::lonlocal::texthash (
 6281:                                    0 => 'No',
 6282:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 6283:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 6284:                                  );
 6285:                 $datatable .= '<table wisth="100%">';
 6286:                 foreach my $option (@options) {
 6287:                     my $checked = ' ';
 6288:                     my $onclick;
 6289:                     if ($defaults{$item} eq $option) {
 6290:                         $checked = ' checked="checked"';
 6291:                     }
 6292:                     if ($option == 2) {
 6293:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 6294:                     }
 6295:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6296:                                   '<label><input type="radio" name="'.$item.
 6297:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 6298:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6299:                 }
 6300:                 $datatable .= '</table>';
 6301:             } else {
 6302:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6303:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />'; 
 6304:             }
 6305:             $datatable .= '</td></tr>';
 6306:             $rownum ++;
 6307:         }
 6308:     } else {
 6309:         my %defaults;
 6310:         if (ref($settings) eq 'HASH') {
 6311:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH') &&
 6312:                 (ref($settings->{'inststatusguest'}) eq 'ARRAY')) {
 6313:                 my $maxnum = @{$settings->{'inststatusorder'}};
 6314:                 for (my $i=0; $i<$maxnum; $i++) {
 6315:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 6316:                     my $item = $settings->{'inststatusorder'}->[$i];
 6317:                     my $title = $settings->{'inststatustypes'}->{$item};
 6318:                     my $guestok;
 6319:                     if (grep(/^\Q$item\E$/,@{$settings->{'inststatusguest'}})) {
 6320:                         $guestok = 1;
 6321:                     }
 6322:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 6323:                     $datatable .= '<tr'.$css_class.'>'.
 6324:                                   '<td><span class="LC_nobreak">'.
 6325:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 6326:                     for (my $k=0; $k<=$maxnum; $k++) {
 6327:                         my $vpos = $k+1;
 6328:                         my $selstr;
 6329:                         if ($k == $i) {
 6330:                             $selstr = ' selected="selected" ';
 6331:                         }
 6332:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6333:                     }
 6334:                     my ($checkedon,$checkedoff);
 6335:                     $checkedoff = ' checked="checked"';
 6336:                     if ($guestok) {
 6337:                         $checkedon = $checkedoff;
 6338:                         $checkedoff = ''; 
 6339:                     }
 6340:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 6341:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 6342:                                   &mt('delete').'</span></td>'.
 6343:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 6344:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 6345:                                   '</span></td>'.
 6346:                                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6347:                                   '<label><input type="radio" value="1" name="inststatus_guest_'.$item.'"'.$checkedon.' />'.
 6348:                                   &mt('Yes').'</label>'.('&nbsp;'x2).
 6349:                                   '<label><input type="radio" value="0" name="inststatus_guest_'.$item.'"'.$checkedoff.' />'.
 6350:                                   &mt('No').'</label></span></td></tr>';
 6351:                 }
 6352:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 6353:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 6354:                 $datatable .= '<tr '.$css_class.'>'.
 6355:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 6356:                 for (my $k=0; $k<=$maxnum; $k++) {
 6357:                     my $vpos = $k+1;
 6358:                     my $selstr;
 6359:                     if ($k == $maxnum) {
 6360:                         $selstr = ' selected="selected" ';
 6361:                     }
 6362:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6363:                 }
 6364:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 6365:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 6366:                               '&nbsp;'.&mt('(new)').
 6367:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 6368:                               &mt('Name displayed:').
 6369:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 6370:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 6371:                               '<label><input type="radio" value="1" name="addinststatus_guest" />'.
 6372:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 6373:                               '<label><input type="radio" value="0" name="addinststatus_guest" />'.
 6374:                               &mt('No').'</label></span></td></tr>';
 6375:                               '</tr>'."\n";
 6376:                 $rownum ++;
 6377:             }
 6378:         }
 6379:     }
 6380:     $$rowtotal += $rownum;
 6381:     return $datatable;
 6382: }
 6383: 
 6384: sub get_languages_hash {
 6385:     my %langchoices;
 6386:     foreach my $id (&Apache::loncommon::languageids()) {
 6387:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 6388:         if ($code ne '') {
 6389:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 6390:         }
 6391:     }
 6392:     return %langchoices;
 6393: }
 6394: 
 6395: sub defaults_titles {
 6396:     my ($dom) = @_;
 6397:     my %titles = &Apache::lonlocal::texthash (
 6398:                    'auth_def'      => 'Default authentication type',
 6399:                    'auth_arg_def'  => 'Default authentication argument',
 6400:                    'lang_def'      => 'Default language',
 6401:                    'timezone_def'  => 'Default timezone',
 6402:                    'datelocale_def' => 'Default locale for dates',
 6403:                    'portal_def'     => 'Portal/Default URL',
 6404:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 6405:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 6406:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 6407:                  );
 6408:     if ($dom) {
 6409:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 6410:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 6411:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 6412:         $protocol = 'http' if ($protocol ne 'https');
 6413:         if ($uint_dom) {
 6414:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 6415:                                          $uint_dom);
 6416:         }
 6417:     }
 6418:     return (\%titles);
 6419: }
 6420: 
 6421: sub print_scantronformat {
 6422:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 6423:     my $itemcount = 1;
 6424:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 6425:         %confhash);
 6426:     my $switchserver = &check_switchserver($dom,$confname);
 6427:     my %lt = &Apache::lonlocal::texthash (
 6428:                 default => 'Default bubblesheet format file error',
 6429:                 custom  => 'Custom bubblesheet format file error',
 6430:              );
 6431:     my %scantronfiles = (
 6432:         default => 'default.tab',
 6433:         custom => 'custom.tab',
 6434:     );
 6435:     foreach my $key (keys(%scantronfiles)) {
 6436:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 6437:                               .$scantronfiles{$key};
 6438:     }
 6439:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 6440:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 6441:         if (!$switchserver) {
 6442:             my $servadm = $r->dir_config('lonAdmEMail');
 6443:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 6444:             if ($configuserok eq 'ok') {
 6445:                 if ($author_ok eq 'ok') {
 6446:                     my %legacyfile = (
 6447:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 6448:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 6449:                     );
 6450:                     my %md5chk;
 6451:                     foreach my $type (keys(%legacyfile)) {
 6452:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 6453:                         chomp($md5chk{$type});
 6454:                     }
 6455:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 6456:                         foreach my $type (keys(%legacyfile)) {
 6457:                             ($scantronurls{$type},my $error) = 
 6458:                                 &legacy_scantronformat($r,$dom,$confname,
 6459:                                                  $type,$legacyfile{$type},
 6460:                                                  $scantronurls{$type},
 6461:                                                  $scantronfiles{$type});
 6462:                             if ($error ne '') {
 6463:                                 $error{$type} = $error;
 6464:                             }
 6465:                         }
 6466:                         if (keys(%error) == 0) {
 6467:                             $is_custom = 1;
 6468:                             $confhash{'scantron'}{'scantronformat'} = 
 6469:                                 $scantronurls{'custom'};
 6470:                             my $putresult = 
 6471:                                 &Apache::lonnet::put_dom('configuration',
 6472:                                                          \%confhash,$dom);
 6473:                             if ($putresult ne 'ok') {
 6474:                                 $error{'custom'} = 
 6475:                                     '<span class="LC_error">'.
 6476:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6477:                             }
 6478:                         }
 6479:                     } else {
 6480:                         ($scantronurls{'default'},my $error) =
 6481:                             &legacy_scantronformat($r,$dom,$confname,
 6482:                                           'default',$legacyfile{'default'},
 6483:                                           $scantronurls{'default'},
 6484:                                           $scantronfiles{'default'});
 6485:                         if ($error eq '') {
 6486:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 6487:                             my $putresult =
 6488:                                 &Apache::lonnet::put_dom('configuration',
 6489:                                                          \%confhash,$dom);
 6490:                             if ($putresult ne 'ok') {
 6491:                                 $error{'default'} =
 6492:                                     '<span class="LC_error">'.
 6493:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6494:                             }
 6495:                         } else {
 6496:                             $error{'default'} = $error;
 6497:                         }
 6498:                     }
 6499:                 }
 6500:             }
 6501:         } else {
 6502:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 6503:         }
 6504:     }
 6505:     if (ref($settings) eq 'HASH') {
 6506:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 6507:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 6508:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 6509:                 $scantronurl = '';
 6510:             } else {
 6511:                 $scantronurl = $settings->{'scantronformat'};
 6512:             }
 6513:             $is_custom = 1;
 6514:         } else {
 6515:             $scantronurl = $scantronurls{'default'};
 6516:         }
 6517:     } else {
 6518:         if ($is_custom) {
 6519:             $scantronurl = $scantronurls{'custom'};
 6520:         } else {
 6521:             $scantronurl = $scantronurls{'default'};
 6522:         }
 6523:     }
 6524:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6525:     $datatable .= '<tr'.$css_class.'>';
 6526:     if (!$is_custom) {
 6527:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 6528:                       '<span class="LC_nobreak">';
 6529:         if ($scantronurl) {
 6530:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 6531:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 6532:         } else {
 6533:             $datatable = &mt('File unavailable for display');
 6534:         }
 6535:         $datatable .= '</span></td>';
 6536:         if (keys(%error) == 0) { 
 6537:             $datatable .= '<td valign="bottom">';
 6538:             if (!$switchserver) {
 6539:                 $datatable .= &mt('Upload:').'<br />';
 6540:             }
 6541:         } else {
 6542:             my $errorstr;
 6543:             foreach my $key (sort(keys(%error))) {
 6544:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6545:             }
 6546:             $datatable .= '<td>'.$errorstr;
 6547:         }
 6548:     } else {
 6549:         if (keys(%error) > 0) {
 6550:             my $errorstr;
 6551:             foreach my $key (sort(keys(%error))) {
 6552:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6553:             } 
 6554:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 6555:         } elsif ($scantronurl) {
 6556:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 6557:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 6558:             $datatable .= '<td><span class="LC_nobreak">'.
 6559:                           $link.
 6560:                           '<label><input type="checkbox" name="scantronformat_del"'.
 6561:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 6562:                           '<td><span class="LC_nobreak">&nbsp;'.
 6563:                           &mt('Replace:').'</span><br />';
 6564:         }
 6565:     }
 6566:     if (keys(%error) == 0) {
 6567:         if ($switchserver) {
 6568:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 6569:         } else {
 6570:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 6571:                          '<input type="file" name="scantronformat" /></span>';
 6572:         }
 6573:     }
 6574:     $datatable .= '</td></tr>';
 6575:     $$rowtotal ++;
 6576:     return $datatable;
 6577: }
 6578: 
 6579: sub legacy_scantronformat {
 6580:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 6581:     my ($url,$error);
 6582:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 6583:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 6584:         (my $result,$url) =
 6585:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 6586:                          '','',$newfile);
 6587:         if ($result ne 'ok') {
 6588:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 6589:         }
 6590:     }
 6591:     return ($url,$error);
 6592: }
 6593: 
 6594: sub print_coursecategories {
 6595:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 6596:     my $datatable;
 6597:     if ($position eq 'top') {
 6598:         my (%checked);
 6599:         my @catitems = ('unauth','auth');
 6600:         my @cattypes = ('std','domonly','codesrch','none');
 6601:         $checked{'unauth'} = 'std';
 6602:         $checked{'auth'} = 'std';
 6603:         if (ref($settings) eq 'HASH') {
 6604:             foreach my $type (@cattypes) {
 6605:                 if ($type eq $settings->{'unauth'}) {
 6606:                     $checked{'unauth'} = $type;
 6607:                 }
 6608:                 if ($type eq $settings->{'auth'}) {
 6609:                     $checked{'auth'} = $type;
 6610:                 }
 6611:             }
 6612:         }
 6613:         my %lt = &Apache::lonlocal::texthash (
 6614:                                                unauth   => 'Catalog type for unauthenticated users',
 6615:                                                auth     => 'Catalog type for authenticated users',
 6616:                                                none     => 'No catalog',
 6617:                                                std      => 'Standard catalog',
 6618:                                                domonly  => 'Domain-only catalog',
 6619:                                                codesrch => "Code search form",
 6620:                                              );
 6621:        my $itemcount = 0;
 6622:        foreach my $item (@catitems) {
 6623:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 6624:            $datatable .= '<tr '.$css_class.'>'.
 6625:                          '<td>'.$lt{$item}.'</td>'.
 6626:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 6627:            foreach my $type (@cattypes) {
 6628:                my $ischecked;
 6629:                if ($checked{$item} eq $type) {
 6630:                    $ischecked=' checked="checked"';
 6631:                }
 6632:                $datatable .= '<label>'.
 6633:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 6634:                              ' />'.$lt{$type}.'</label>&nbsp;';
 6635:            }
 6636:            $datatable .= '</td></tr>';
 6637:            $itemcount ++;
 6638:         }
 6639:         $$rowtotal += $itemcount;
 6640:     } elsif ($position eq 'middle') {
 6641:         my $toggle_cats_crs = ' ';
 6642:         my $toggle_cats_dom = ' checked="checked" ';
 6643:         my $can_cat_crs = ' ';
 6644:         my $can_cat_dom = ' checked="checked" ';
 6645:         my $toggle_catscomm_comm = ' ';
 6646:         my $toggle_catscomm_dom = ' checked="checked" ';
 6647:         my $can_catcomm_comm = ' ';
 6648:         my $can_catcomm_dom = ' checked="checked" ';
 6649:         my $toggle_catsplace_place = ' ';
 6650:         my $toggle_catsplace_dom = ' checked="checked" ';
 6651:         my $can_catplace_place = ' ';
 6652:         my $can_catplace_dom = ' checked="checked" ';
 6653: 
 6654:         if (ref($settings) eq 'HASH') {
 6655:             if ($settings->{'togglecats'} eq 'crs') {
 6656:                 $toggle_cats_crs = $toggle_cats_dom;
 6657:                 $toggle_cats_dom = ' ';
 6658:             }
 6659:             if ($settings->{'categorize'} eq 'crs') {
 6660:                 $can_cat_crs = $can_cat_dom;
 6661:                 $can_cat_dom = ' ';
 6662:             }
 6663:             if ($settings->{'togglecatscomm'} eq 'comm') {
 6664:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 6665:                 $toggle_catscomm_dom = ' ';
 6666:             }
 6667:             if ($settings->{'categorizecomm'} eq 'comm') {
 6668:                 $can_catcomm_comm = $can_catcomm_dom;
 6669:                 $can_catcomm_dom = ' ';
 6670:             }
 6671:             if ($settings->{'togglecatsplace'} eq 'place') {
 6672:                 $toggle_catsplace_place = $toggle_catsplace_dom;
 6673:                 $toggle_catsplace_dom = ' ';
 6674:             }
 6675:             if ($settings->{'categorizeplace'} eq 'place') {
 6676:                 $can_catplace_place = $can_catplace_dom;
 6677:                 $can_catplace_dom = ' ';
 6678:             }
 6679:         }
 6680:         my %title = &Apache::lonlocal::texthash (
 6681:                      togglecats      => 'Show/Hide a course in catalog',
 6682:                      togglecatscomm  => 'Show/Hide a community in catalog',
 6683:                      togglecatsplace => 'Show/Hide a placement test in catalog',
 6684:                      categorize      => 'Assign a category to a course',
 6685:                      categorizecomm  => 'Assign a category to a community',
 6686:                      categorizeplace => 'Assign a category to a placement test',
 6687:                     );
 6688:         my %level = &Apache::lonlocal::texthash (
 6689:                      dom   => 'Set in Domain',
 6690:                      crs   => 'Set in Course',
 6691:                      comm  => 'Set in Community',
 6692:                      place => 'Set in Placement Test',
 6693:                     );
 6694:         $datatable = '<tr class="LC_odd_row">'.
 6695:                   '<td>'.$title{'togglecats'}.'</td>'.
 6696:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6697:                   '<input type="radio" name="togglecats"'.
 6698:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6699:                   '<label><input type="radio" name="togglecats"'.
 6700:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 6701:                   '</tr><tr>'.
 6702:                   '<td>'.$title{'categorize'}.'</td>'.
 6703:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6704:                   '<label><input type="radio" name="categorize"'.
 6705:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6706:                   '<label><input type="radio" name="categorize"'.
 6707:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 6708:                   '</tr><tr class="LC_odd_row">'.
 6709:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 6710:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6711:                   '<input type="radio" name="togglecatscomm"'.
 6712:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6713:                   '<label><input type="radio" name="togglecatscomm"'.
 6714:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 6715:                   '</tr><tr>'.
 6716:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 6717:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6718:                   '<label><input type="radio" name="categorizecomm"'.
 6719:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6720:                   '<label><input type="radio" name="categorizecomm"'.
 6721:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 6722:                   '</tr><tr>'.
 6723:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
 6724:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6725:                   '<input type="radio" name="togglecatsplace"'.
 6726:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6727:                   '<label><input type="radio" name="togglecatscomm"'.
 6728:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
 6729:                   '</tr><tr>'.
 6730:                   '<td>'.$title{'categorizeplace'}.'</td>'.
 6731:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6732:                   '<label><input type="radio" name="categorizeplace"'.
 6733:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6734:                   '<label><input type="radio" name="categorizeplace"'.
 6735:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
 6736:                   '</tr>';
 6737:         $$rowtotal += 6;
 6738:     } else {
 6739:         my $css_class;
 6740:         my $itemcount = 1;
 6741:         my $cathash; 
 6742:         if (ref($settings) eq 'HASH') {
 6743:             $cathash = $settings->{'cats'};
 6744:         }
 6745:         if (ref($cathash) eq 'HASH') {
 6746:             my (@cats,@trails,%allitems,%idx,@jsarray);
 6747:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 6748:                                                    \%allitems,\%idx,\@jsarray);
 6749:             my $maxdepth = scalar(@cats);
 6750:             my $colattrib = '';
 6751:             if ($maxdepth > 2) {
 6752:                 $colattrib = ' colspan="2" ';
 6753:             }
 6754:             my @path;
 6755:             if (@cats > 0) {
 6756:                 if (ref($cats[0]) eq 'ARRAY') {
 6757:                     my $numtop = @{$cats[0]};
 6758:                     my $maxnum = $numtop;
 6759:                     my %default_names = (
 6760:                           instcode    => &mt('Official courses'),
 6761:                           communities => &mt('Communities'),
 6762:                           placement   => &mt('Placement Tests'),
 6763:                     );
 6764: 
 6765:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 6766:                         ($cathash->{'instcode::0'} eq '') ||
 6767:                         (!grep(/^communities$/,@{$cats[0]})) || 
 6768:                         ($cathash->{'communities::0'} eq '') ||
 6769:                         (!grep(/^placement$/,@{$cats[0]})) ||
 6770:                         ($cathash->{'placement::0'} eq '')) {
 6771:                         $maxnum ++;
 6772:                     }
 6773:                     my $lastidx;
 6774:                     for (my $i=0; $i<$numtop; $i++) {
 6775:                         my $parent = $cats[0][$i];
 6776:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6777:                         my $item = &escape($parent).'::0';
 6778:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 6779:                         $lastidx = $idx{$item};
 6780:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6781:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 6782:                         for (my $k=0; $k<=$maxnum; $k++) {
 6783:                             my $vpos = $k+1;
 6784:                             my $selstr;
 6785:                             if ($k == $i) {
 6786:                                 $selstr = ' selected="selected" ';
 6787:                             }
 6788:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6789:                         }
 6790:                         $datatable .= '</select></span></td><td>';
 6791:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
 6792:                             $datatable .=  '<span class="LC_nobreak">'
 6793:                                            .$default_names{$parent}.'</span>';
 6794:                             if ($parent eq 'instcode') {
 6795:                                 $datatable .= '<br /><span class="LC_nobreak">('
 6796:                                               .&mt('with institutional codes')
 6797:                                               .')</span></td><td'.$colattrib.'>';
 6798:                             } else {
 6799:                                 $datatable .= '<table><tr><td>';
 6800:                             }
 6801:                             $datatable .= '<span class="LC_nobreak">'
 6802:                                           .'<label><input type="radio" name="'
 6803:                                           .$parent.'" value="1" checked="checked" />'
 6804:                                           .&mt('Display').'</label>';
 6805:                             if ($parent eq 'instcode') {
 6806:                                 $datatable .= '&nbsp;';
 6807:                             } else {
 6808:                                 $datatable .= '</span></td></tr><tr><td>'
 6809:                                               .'<span class="LC_nobreak">';
 6810:                             }
 6811:                             $datatable .= '<label><input type="radio" name="'
 6812:                                           .$parent.'" value="0" />'
 6813:                                           .&mt('Do not display').'</label></span>';
 6814:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
 6815:                                 $datatable .= '</td></tr></table>';
 6816:                             }
 6817:                             $datatable .= '</td>';
 6818:                         } else {
 6819:                             $datatable .= $parent
 6820:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 6821:                                           .'<input type="checkbox" name="deletecategory" '
 6822:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 6823:                         }
 6824:                         my $depth = 1;
 6825:                         push(@path,$parent);
 6826:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 6827:                         pop(@path);
 6828:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 6829:                         $itemcount ++;
 6830:                     }
 6831:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6832:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 6833:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 6834:                     for (my $k=0; $k<=$maxnum; $k++) {
 6835:                         my $vpos = $k+1;
 6836:                         my $selstr;
 6837:                         if ($k == $numtop) {
 6838:                             $selstr = ' selected="selected" ';
 6839:                         }
 6840:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6841:                     }
 6842:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 6843:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 6844:                                   .'</tr>'."\n";
 6845:                     $itemcount ++;
 6846:                     foreach my $default ('instcode','communities','placement') {
 6847:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 6848:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6849:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 6850:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 6851:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 6852:                             for (my $k=0; $k<=$maxnum; $k++) {
 6853:                                 my $vpos = $k+1;
 6854:                                 my $selstr;
 6855:                                 if ($k == $maxnum) {
 6856:                                     $selstr = ' selected="selected" ';
 6857:                                 }
 6858:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6859:                             }
 6860:                             $datatable .= '</select></span></td>'.
 6861:                                           '<td><span class="LC_nobreak">'.
 6862:                                           $default_names{$default}.'</span>';
 6863:                             if ($default eq 'instcode') {
 6864:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 6865:                                               .&mt('with institutional codes').')</span>';
 6866:                             }
 6867:                             $datatable .= '</td>'
 6868:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 6869:                                           .&mt('Display').'</label>&nbsp;'
 6870:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 6871:                                           .&mt('Do not display').'</label></span></td></tr>';
 6872:                         }
 6873:                     }
 6874:                 }
 6875:             } else {
 6876:                 $datatable .= &initialize_categories($itemcount);
 6877:             }
 6878:         } else {
 6879:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 6880:                           .&initialize_categories($itemcount);
 6881:         }
 6882:         $$rowtotal += $itemcount;
 6883:     }
 6884:     return $datatable;
 6885: }
 6886: 
 6887: sub print_serverstatuses {
 6888:     my ($dom,$settings,$rowtotal) = @_;
 6889:     my $datatable;
 6890:     my @pages = &serverstatus_pages();
 6891:     my (%namedaccess,%machineaccess);
 6892:     foreach my $type (@pages) {
 6893:         $namedaccess{$type} = '';
 6894:         $machineaccess{$type}= '';
 6895:     }
 6896:     if (ref($settings) eq 'HASH') {
 6897:         foreach my $type (@pages) {
 6898:             if (exists($settings->{$type})) {
 6899:                 if (ref($settings->{$type}) eq 'HASH') {
 6900:                     foreach my $key (keys(%{$settings->{$type}})) {
 6901:                         if ($key eq 'namedusers') {
 6902:                             $namedaccess{$type} = $settings->{$type}->{$key};
 6903:                         } elsif ($key eq 'machines') {
 6904:                             $machineaccess{$type} = $settings->{$type}->{$key};
 6905:                         }
 6906:                     }
 6907:                 }
 6908:             }
 6909:         }
 6910:     }
 6911:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 6912:     my $rownum = 0;
 6913:     my $css_class;
 6914:     foreach my $type (@pages) {
 6915:         $rownum ++;
 6916:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 6917:         $datatable .= '<tr'.$css_class.'>'.
 6918:                       '<td><span class="LC_nobreak">'.
 6919:                       $titles->{$type}.'</span></td>'.
 6920:                       '<td class="LC_left_item">'.
 6921:                       '<input type="text" name="'.$type.'_namedusers" '.
 6922:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 6923:                       '<td class="LC_right_item">'.
 6924:                       '<span class="LC_nobreak">'.
 6925:                       '<input type="text" name="'.$type.'_machines" '.
 6926:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 6927:                       '</td></tr>'."\n";
 6928:     }
 6929:     $$rowtotal += $rownum;
 6930:     return $datatable;
 6931: }
 6932: 
 6933: sub serverstatus_pages {
 6934:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 6935:             'checksums','clusterstatus','certstatus','metadata_keywords',
 6936:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
 6937:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
 6938: }
 6939: 
 6940: sub defaults_javascript {
 6941:     my ($settings) = @_;
 6942:     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.');
 6943:     my $intauthcost = &mt('Warning: bcrypt encryption cost for internal authentication must be an integer.');
 6944:     &js_escape(\$intauthcheck);
 6945:     &js_escape(\$intauthcost);
 6946:     my $intauthjs = <<"ENDSCRIPT";
 6947: 
 6948: function warnIntAuth(field) {
 6949:     if (field.name == 'intauth_check') {
 6950:         if (field.value == '2') {
 6951:             alert('$intauthcheck');
 6952:         }
 6953:     }
 6954:     if (field.name == 'intauth_cost') {
 6955:         field.value.replace(/\s/g,'');
 6956:         if (field.value != '') {
 6957:             var regexdigit=/^\\d+\$/;
 6958:             if (!regexdigit.test(field.value)) {
 6959:                 alert('$intauthcost');
 6960:             }
 6961:         }
 6962:     }
 6963:     return;
 6964: }
 6965: 
 6966: ENDSCRIPT
 6967: 
 6968:     if (ref($settings) ne 'HASH') {
 6969:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 6970:     }
 6971:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 6972:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 6973:         if ($maxnum eq '') {
 6974:             $maxnum = 0;
 6975:         }
 6976:         $maxnum ++;
 6977:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 6978:         return <<"ENDSCRIPT";
 6979: <script type="text/javascript">
 6980: // <![CDATA[
 6981: function reorderTypes(form,caller) {
 6982:     var changedVal;
 6983: $jstext 
 6984:     var newpos = 'addinststatus_pos';
 6985:     var current = new Array;
 6986:     var maxh = $maxnum;
 6987:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 6988:     var oldVal;
 6989:     if (caller == newpos) {
 6990:         changedVal = newitemVal;
 6991:     } else {
 6992:         var curritem = 'inststatus_pos_'+caller;
 6993:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 6994:         current[newitemVal] = newpos;
 6995:     }
 6996:     for (var i=0; i<inststatuses.length; i++) {
 6997:         if (inststatuses[i] != caller) {
 6998:             var elementName = 'inststatus_pos_'+inststatuses[i];
 6999:             if (form.elements[elementName]) {
 7000:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 7001:                 current[currVal] = elementName;
 7002:             }
 7003:         }
 7004:     }
 7005:     for (var j=0; j<maxh; j++) {
 7006:         if (current[j] == undefined) {
 7007:             oldVal = j;
 7008:         }
 7009:     }
 7010:     if (oldVal < changedVal) {
 7011:         for (var k=oldVal+1; k<=changedVal ; k++) {
 7012:            var elementName = current[k];
 7013:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 7014:         }
 7015:     } else {
 7016:         for (var k=changedVal; k<oldVal; k++) {
 7017:             var elementName = current[k];
 7018:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 7019:         }
 7020:     }
 7021:     return;
 7022: }
 7023: 
 7024: $intauthjs
 7025: 
 7026: // ]]>
 7027: </script>
 7028: 
 7029: ENDSCRIPT
 7030:     } else {
 7031:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 7032:     }
 7033: }
 7034: 
 7035: sub coursecategories_javascript {
 7036:     my ($settings) = @_;
 7037:     my ($output,$jstext,$cathash);
 7038:     if (ref($settings) eq 'HASH') {
 7039:         $cathash = $settings->{'cats'};
 7040:     }
 7041:     if (ref($cathash) eq 'HASH') {
 7042:         my (@cats,@jsarray,%idx);
 7043:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 7044:         if (@jsarray > 0) {
 7045:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 7046:             for (my $i=0; $i<@jsarray; $i++) {
 7047:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 7048:                     my $catstr = join('","',@{$jsarray[$i]});
 7049:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 7050:                 }
 7051:             }
 7052:         }
 7053:     } else {
 7054:         $jstext  = '    var categories = Array(1);'."\n".
 7055:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 7056:     }
 7057:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 7058:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 7059:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
 7060:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 7061:     &js_escape(\$instcode_reserved);
 7062:     &js_escape(\$communities_reserved);
 7063:     &js_escape(\$placement_reserved);
 7064:     &js_escape(\$choose_again);
 7065:     $output = <<"ENDSCRIPT";
 7066: <script type="text/javascript">
 7067: // <![CDATA[
 7068: function reorderCats(form,parent,item,idx) {
 7069:     var changedVal;
 7070: $jstext
 7071:     var newpos = 'addcategory_pos';
 7072:     if (parent == '') {
 7073:         var has_instcode = 0;
 7074:         var maxtop = categories[idx].length;
 7075:         for (var j=0; j<maxtop; j++) {
 7076:             if (categories[idx][j] == 'instcode::0') {
 7077:                 has_instcode == 1;
 7078:             }
 7079:         }
 7080:         if (has_instcode == 0) {
 7081:             categories[idx][maxtop] = 'instcode_pos';
 7082:         }
 7083:     } else {
 7084:         newpos += '_'+parent;
 7085:     }
 7086:     var maxh = 1 + categories[idx].length;
 7087:     var current = new Array;
 7088:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 7089:     if (item == newpos) {
 7090:         changedVal = newitemVal;
 7091:     } else {
 7092:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 7093:         current[newitemVal] = newpos;
 7094:     }
 7095:     for (var i=0; i<categories[idx].length; i++) {
 7096:         var elementName = categories[idx][i];
 7097:         if (elementName != item) {
 7098:             if (form.elements[elementName]) {
 7099:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 7100:                 current[currVal] = elementName;
 7101:             }
 7102:         }
 7103:     }
 7104:     var oldVal;
 7105:     for (var j=0; j<maxh; j++) {
 7106:         if (current[j] == undefined) {
 7107:             oldVal = j;
 7108:         }
 7109:     }
 7110:     if (oldVal < changedVal) {
 7111:         for (var k=oldVal+1; k<=changedVal ; k++) {
 7112:            var elementName = current[k];
 7113:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 7114:         }
 7115:     } else {
 7116:         for (var k=changedVal; k<oldVal; k++) {
 7117:             var elementName = current[k];
 7118:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 7119:         }
 7120:     }
 7121:     return;
 7122: }
 7123: 
 7124: function categoryCheck(form) {
 7125:     if (form.elements['addcategory_name'].value == 'instcode') {
 7126:         alert('$instcode_reserved\\n$choose_again');
 7127:         return false;
 7128:     }
 7129:     if (form.elements['addcategory_name'].value == 'communities') {
 7130:         alert('$communities_reserved\\n$choose_again');
 7131:         return false;
 7132:     }
 7133:     if (form.elements['addcategory_name'].value == 'placement') {
 7134:         alert('$placement_reserved\\n$choose_again');
 7135:         return false;
 7136:     }
 7137:     return true;
 7138: }
 7139: 
 7140: // ]]>
 7141: </script>
 7142: 
 7143: ENDSCRIPT
 7144:     return $output;
 7145: }
 7146: 
 7147: sub initialize_categories {
 7148:     my ($itemcount) = @_;
 7149:     my ($datatable,$css_class,$chgstr);
 7150:     my %default_names = (
 7151:                       instcode    => 'Official courses (with institutional codes)',
 7152:                       communities => 'Communities',
 7153:                       placement   => 'Placement Tests',
 7154:                         );
 7155:     my $select0 = ' selected="selected"';
 7156:     my $select1 = '';
 7157:     foreach my $default ('instcode','communities','placement') {
 7158:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7159:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 7160:         if (($default eq 'communities') || ($default eq 'placement')) {
 7161:             $select1 = $select0;
 7162:             $select0 = '';
 7163:         }
 7164:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7165:                      .'<select name="'.$default.'_pos">'
 7166:                      .'<option value="0"'.$select0.'>1</option>'
 7167:                      .'<option value="1"'.$select1.'>2</option>'
 7168:                      .'<option value="2">3</option></select>&nbsp;'
 7169:                      .$default_names{$default}
 7170:                      .'</span></td><td><span class="LC_nobreak">'
 7171:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 7172:                      .&mt('Display').'</label>&nbsp;<label>'
 7173:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 7174:                  .'</label></span></td></tr>';
 7175:         $itemcount ++;
 7176:     }
 7177:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7178:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 7179:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7180:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 7181:                   .'<option value="0">1</option>'
 7182:                   .'<option value="1">2</option>'
 7183:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 7184:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 7185:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 7186:     return $datatable;
 7187: }
 7188: 
 7189: sub build_category_rows {
 7190:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 7191:     my ($text,$name,$item,$chgstr);
 7192:     if (ref($cats) eq 'ARRAY') {
 7193:         my $maxdepth = scalar(@{$cats});
 7194:         if (ref($cats->[$depth]) eq 'HASH') {
 7195:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 7196:                 my $numchildren = @{$cats->[$depth]{$parent}};
 7197:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7198:                 $text .= '<td><table class="LC_data_table">';
 7199:                 my ($idxnum,$parent_name,$parent_item);
 7200:                 my $higher = $depth - 1;
 7201:                 if ($higher == 0) {
 7202:                     $parent_name = &escape($parent).'::'.$higher;
 7203:                 } else {
 7204:                     if (ref($path) eq 'ARRAY') {
 7205:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7206:                     }
 7207:                 }
 7208:                 $parent_item = 'addcategory_pos_'.$parent_name;
 7209:                 for (my $j=0; $j<=$numchildren; $j++) {
 7210:                     if ($j < $numchildren) {
 7211:                         $name = $cats->[$depth]{$parent}[$j];
 7212:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 7213:                         $idxnum = $idx->{$item};
 7214:                     } else {
 7215:                         $name = $parent_name;
 7216:                         $item = $parent_item;
 7217:                     }
 7218:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 7219:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 7220:                     for (my $i=0; $i<=$numchildren; $i++) {
 7221:                         my $vpos = $i+1;
 7222:                         my $selstr;
 7223:                         if ($j == $i) {
 7224:                             $selstr = ' selected="selected" ';
 7225:                         }
 7226:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 7227:                     }
 7228:                     $text .= '</select>&nbsp;';
 7229:                     if ($j < $numchildren) {
 7230:                         my $deeper = $depth+1;
 7231:                         $text .= $name.'&nbsp;'
 7232:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 7233:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 7234:                         if(ref($path) eq 'ARRAY') {
 7235:                             push(@{$path},$name);
 7236:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 7237:                             pop(@{$path});
 7238:                         }
 7239:                     } else {
 7240:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 7241:                         if ($j == $numchildren) {
 7242:                             $text .= $name;
 7243:                         } else {
 7244:                             $text .= $item;
 7245:                         }
 7246:                         $text .= '" value="" />';
 7247:                     }
 7248:                     $text .= '</td></tr>';
 7249:                 }
 7250:                 $text .= '</table></td>';
 7251:             } else {
 7252:                 my $higher = $depth-1;
 7253:                 if ($higher == 0) {
 7254:                     $name = &escape($parent).'::'.$higher;
 7255:                 } else {
 7256:                     if (ref($path) eq 'ARRAY') {
 7257:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7258:                     }
 7259:                 }
 7260:                 my $colspan;
 7261:                 if ($parent ne 'instcode') {
 7262:                     $colspan = $maxdepth - $depth - 1;
 7263:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 7264:                 }
 7265:             }
 7266:         }
 7267:     }
 7268:     return $text;
 7269: }
 7270: 
 7271: sub modifiable_userdata_row {
 7272:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 7273:     my ($role,$rolename,$statustype);
 7274:     $role = $item;
 7275:     if ($context eq 'cancreate') {
 7276:         if ($item =~ /^emailusername_(.+)$/) {
 7277:             $statustype = $1;
 7278:             $role = 'emailusername';
 7279:             if (ref($usertypes) eq 'HASH') {
 7280:                 if ($usertypes->{$statustype}) {
 7281:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 7282:                 } else {
 7283:                     $rolename = &mt('Data provided by user');
 7284:                 }
 7285:             }
 7286:         }
 7287:     } elsif ($context eq 'selfcreate') {
 7288:         if (ref($usertypes) eq 'HASH') {
 7289:             $rolename = $usertypes->{$role};
 7290:         } else {
 7291:             $rolename = $role;
 7292:         }
 7293:     } else {
 7294:         if ($role eq 'cr') {
 7295:             $rolename = &mt('Custom role');
 7296:         } else {
 7297:             $rolename = &Apache::lonnet::plaintext($role);
 7298:         }
 7299:     }
 7300:     my (@fields,%fieldtitles);
 7301:     if (ref($fieldsref) eq 'ARRAY') {
 7302:         @fields = @{$fieldsref};
 7303:     } else {
 7304:         @fields = ('lastname','firstname','middlename','generation',
 7305:                    'permanentemail','id');
 7306:     }
 7307:     if ((ref($titlesref) eq 'HASH')) {
 7308:         %fieldtitles = %{$titlesref};
 7309:     } else {
 7310:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7311:     }
 7312:     my $output;
 7313:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 7314:     $output = '<tr '.$css_class.'>'.
 7315:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 7316:               '<td class="LC_left_item" colspan="2"><table>';
 7317:     my $rem;
 7318:     my %checks;
 7319:     if (ref($settings) eq 'HASH') {
 7320:         if (ref($settings->{$context}) eq 'HASH') {
 7321:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 7322:                 my $hashref = $settings->{$context}->{$role};
 7323:                 if ($role eq 'emailusername') {
 7324:                     if ($statustype) {
 7325:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 7326:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 7327:                             if (ref($hashref) eq 'HASH') { 
 7328:                                 foreach my $field (@fields) {
 7329:                                     if ($hashref->{$field}) {
 7330:                                         $checks{$field} = $hashref->{$field};
 7331:                                     }
 7332:                                 }
 7333:                             }
 7334:                         }
 7335:                     }
 7336:                 } else {
 7337:                     if (ref($hashref) eq 'HASH') {
 7338:                         foreach my $field (@fields) {
 7339:                             if ($hashref->{$field}) {
 7340:                                 $checks{$field} = ' checked="checked" ';
 7341:                             }
 7342:                         }
 7343:                     }
 7344:                 }
 7345:             }
 7346:         }
 7347:     }
 7348:      
 7349:     for (my $i=0; $i<@fields; $i++) {
 7350:         my $rem = $i%($numinrow);
 7351:         if ($rem == 0) {
 7352:             if ($i > 0) {
 7353:                 $output .= '</tr>';
 7354:             }
 7355:             $output .= '<tr>';
 7356:         }
 7357:         my $check = ' ';
 7358:         unless ($role eq 'emailusername') {
 7359:             if (exists($checks{$fields[$i]})) {
 7360:                 $check = $checks{$fields[$i]}
 7361:             } else {
 7362:                 if ($role eq 'st') {
 7363:                     if (ref($settings) ne 'HASH') {
 7364:                         $check = ' checked="checked" '; 
 7365:                     }
 7366:                 }
 7367:             }
 7368:         }
 7369:         $output .= '<td class="LC_left_item">'.
 7370:                    '<span class="LC_nobreak">';
 7371:         if ($role eq 'emailusername') {
 7372:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 7373:                 $checks{$fields[$i]} = 'omit';
 7374:             }
 7375:             foreach my $option ('required','optional','omit') {
 7376:                 my $checked='';
 7377:                 if ($checks{$fields[$i]} eq $option) {
 7378:                     $checked='checked="checked" ';
 7379:                 }
 7380:                 $output .= '<label>'.
 7381:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 7382:                            &mt($option).'</label>'.('&nbsp;' x2);
 7383:             }
 7384:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 7385:         } else {
 7386:             $output .= '<label>'.
 7387:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 7388:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 7389:                        '</label>';
 7390:         }
 7391:         $output .= '</span></td>';
 7392:         $rem = @fields%($numinrow);
 7393:     }
 7394:     my $colsleft = $numinrow - $rem;
 7395:     if ($colsleft > 1 ) {
 7396:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7397:                    '&nbsp;</td>';
 7398:     } elsif ($colsleft == 1) {
 7399:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 7400:     }
 7401:     $output .= '</tr></table></td></tr>';
 7402:     return $output;
 7403: }
 7404: 
 7405: sub insttypes_row {
 7406:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 7407:     my %lt = &Apache::lonlocal::texthash (
 7408:                       cansearch => 'Users allowed to search',
 7409:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 7410:                       lockablenames => 'User preference to lock name',
 7411:              );
 7412:     my $showdom;
 7413:     if ($context eq 'cansearch') {
 7414:         $showdom = ' ('.$dom.')';
 7415:     }
 7416:     my $class = 'LC_left_item';
 7417:     if ($context eq 'statustocreate') {
 7418:         $class = 'LC_right_item';
 7419:     }
 7420:     my $css_class = ' class="LC_odd_row"';
 7421:     if ($rownum ne '') { 
 7422:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 7423:     }
 7424:     my $output = '<tr'.$css_class.'>'.
 7425:                  '<td>'.$lt{$context}.$showdom.
 7426:                  '</td><td class="'.$class.'" colspan="2"><table>';
 7427:     my $rem;
 7428:     if (ref($types) eq 'ARRAY') {
 7429:         for (my $i=0; $i<@{$types}; $i++) {
 7430:             if (defined($usertypes->{$types->[$i]})) {
 7431:                 my $rem = $i%($numinrow);
 7432:                 if ($rem == 0) {
 7433:                     if ($i > 0) {
 7434:                         $output .= '</tr>';
 7435:                     }
 7436:                     $output .= '<tr>';
 7437:                 }
 7438:                 my $check = ' ';
 7439:                 if (ref($settings) eq 'HASH') {
 7440:                     if (ref($settings->{$context}) eq 'ARRAY') {
 7441:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 7442:                             $check = ' checked="checked" ';
 7443:                         }
 7444:                     } elsif ($context eq 'statustocreate') {
 7445:                         $check = ' checked="checked" ';
 7446:                     }
 7447:                 }
 7448:                 $output .= '<td class="LC_left_item">'.
 7449:                            '<span class="LC_nobreak"><label>'.
 7450:                            '<input type="checkbox" name="'.$context.'" '.
 7451:                            'value="'.$types->[$i].'"'.$check.'/>'.
 7452:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 7453:             }
 7454:         }
 7455:         $rem = @{$types}%($numinrow);
 7456:     }
 7457:     my $colsleft = $numinrow - $rem;
 7458:     if (($rem == 0) && (@{$types} > 0)) {
 7459:         $output .= '<tr>';
 7460:     }
 7461:     if ($colsleft > 1) {
 7462:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 7463:     } else {
 7464:         $output .= '<td class="LC_left_item">';
 7465:     }
 7466:     my $defcheck = ' ';
 7467:     if (ref($settings) eq 'HASH') {  
 7468:         if (ref($settings->{$context}) eq 'ARRAY') {
 7469:             if (grep(/^default$/,@{$settings->{$context}})) {
 7470:                 $defcheck = ' checked="checked" ';
 7471:             }
 7472:         } elsif ($context eq 'statustocreate') {
 7473:             $defcheck = ' checked="checked" ';
 7474:         }
 7475:     }
 7476:     $output .= '<span class="LC_nobreak"><label>'.
 7477:                '<input type="checkbox" name="'.$context.'" '.
 7478:                'value="default"'.$defcheck.'/>'.
 7479:                $othertitle.'</label></span></td>'.
 7480:                '</tr></table></td></tr>';
 7481:     return $output;
 7482: }
 7483: 
 7484: sub sorted_searchtitles {
 7485:     my %searchtitles = &Apache::lonlocal::texthash(
 7486:                          'uname' => 'username',
 7487:                          'lastname' => 'last name',
 7488:                          'lastfirst' => 'last name, first name',
 7489:                      );
 7490:     my @titleorder = ('uname','lastname','lastfirst');
 7491:     return (\%searchtitles,\@titleorder);
 7492: }
 7493: 
 7494: sub sorted_searchtypes {
 7495:     my %srchtypes_desc = (
 7496:                            exact    => 'is exact match',
 7497:                            contains => 'contains ..',
 7498:                            begins   => 'begins with ..',
 7499:                          );
 7500:     my @srchtypeorder = ('exact','begins','contains');
 7501:     return (\%srchtypes_desc,\@srchtypeorder);
 7502: }
 7503: 
 7504: sub usertype_update_row {
 7505:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 7506:     my $datatable;
 7507:     my $numinrow = 4;
 7508:     foreach my $type (@{$types}) {
 7509:         if (defined($usertypes->{$type})) {
 7510:             $$rownums ++;
 7511:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 7512:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 7513:                           '</td><td class="LC_left_item"><table>';
 7514:             for (my $i=0; $i<@{$fields}; $i++) {
 7515:                 my $rem = $i%($numinrow);
 7516:                 if ($rem == 0) {
 7517:                     if ($i > 0) {
 7518:                         $datatable .= '</tr>';
 7519:                     }
 7520:                     $datatable .= '<tr>';
 7521:                 }
 7522:                 my $check = ' ';
 7523:                 if (ref($settings) eq 'HASH') {
 7524:                     if (ref($settings->{'fields'}) eq 'HASH') {
 7525:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 7526:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 7527:                                 $check = ' checked="checked" ';
 7528:                             }
 7529:                         }
 7530:                     }
 7531:                 }
 7532: 
 7533:                 if ($i == @{$fields}-1) {
 7534:                     my $colsleft = $numinrow - $rem;
 7535:                     if ($colsleft > 1) {
 7536:                         $datatable .= '<td colspan="'.$colsleft.'">';
 7537:                     } else {
 7538:                         $datatable .= '<td>';
 7539:                     }
 7540:                 } else {
 7541:                     $datatable .= '<td>';
 7542:                 }
 7543:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7544:                               '<input type="checkbox" name="updateable_'.$type.
 7545:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 7546:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 7547:             }
 7548:             $datatable .= '</tr></table></td></tr>';
 7549:         }
 7550:     }
 7551:     return $datatable;
 7552: }
 7553: 
 7554: sub modify_login {
 7555:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 7556:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 7557:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 7558:     %title = ( coursecatalog => 'Display course catalog',
 7559:                adminmail => 'Display administrator E-mail address',
 7560:                helpdesk  => 'Display "Contact Helpdesk" link',
 7561:                newuser => 'Link for visitors to create a user account',
 7562:                loginheader => 'Log-in box header');
 7563:     @offon = ('off','on');
 7564:     if (ref($domconfig{login}) eq 'HASH') {
 7565:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 7566:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 7567:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 7568:             }
 7569:         }
 7570:     }
 7571:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 7572:                                            \%domconfig,\%loginhash);
 7573:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7574:     foreach my $item (@toggles) {
 7575:         $loginhash{login}{$item} = $env{'form.'.$item};
 7576:     }
 7577:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 7578:     if (ref($colchanges{'login'}) eq 'HASH') {  
 7579:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 7580:                                          \%loginhash);
 7581:     }
 7582: 
 7583:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7584:     my %domservers = &Apache::lonnet::get_servers($dom);
 7585:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 7586:     if (keys(%servers) > 1) {
 7587:         foreach my $lonhost (keys(%servers)) {
 7588:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 7589:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 7590:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 7591:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 7592:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 7593:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7594:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7595:                         $changes{'loginvia'}{$lonhost} = 1;
 7596:                     } else {
 7597:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 7598:                         $changes{'loginvia'}{$lonhost} = 1;
 7599:                     }
 7600:                 } else {
 7601:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7602:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7603:                         $changes{'loginvia'}{$lonhost} = 1;
 7604:                     }
 7605:                 }
 7606:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 7607:                     foreach my $item (@loginvia_attribs) {
 7608:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 7609:                     }
 7610:                 } else {
 7611:                     foreach my $item (@loginvia_attribs) {
 7612:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7613:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7614:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 7615:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7616:                                 $new = '/';
 7617:                             }
 7618:                         }
 7619:                         if (($item eq 'custompath') && 
 7620:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 7621:                             $new = '';
 7622:                         }
 7623:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 7624:                             $changes{'loginvia'}{$lonhost} = 1;
 7625:                         }
 7626:                         if ($item eq 'exempt') {
 7627:                             $new = &check_exempt_addresses($new);
 7628:                         }
 7629:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 7630:                     }
 7631:                 }
 7632:             } else {
 7633:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7634:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7635:                     $changes{'loginvia'}{$lonhost} = 1;
 7636:                     foreach my $item (@loginvia_attribs) {
 7637:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7638:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7639:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7640:                                 $new = '/';
 7641:                             }
 7642:                         }
 7643:                         if (($item eq 'custompath') && 
 7644:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 7645:                             $new = '';
 7646:                         }
 7647:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 7648:                     }
 7649:                 }
 7650:             }
 7651:         }
 7652:     }
 7653: 
 7654:     my $servadm = $r->dir_config('lonAdmEMail');
 7655:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 7656:     if (ref($domconfig{'login'}) eq 'HASH') {
 7657:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 7658:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 7659:                 if ($lang eq 'nolang') {
 7660:                     push(@currlangs,$lang);
 7661:                 } elsif (defined($langchoices{$lang})) {
 7662:                     push(@currlangs,$lang);
 7663:                 } else {
 7664:                     next;
 7665:                 }
 7666:             }
 7667:         }
 7668:     }
 7669:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 7670:     if (@currlangs > 0) {
 7671:         foreach my $lang (@currlangs) {
 7672:             if (grep(/^\Q$lang\E$/,@delurls)) {
 7673:                 $changes{'helpurl'}{$lang} = 1;
 7674:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 7675:                 $changes{'helpurl'}{$lang} = 1;
 7676:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 7677:                 push(@newlangs,$lang);
 7678:             } else {
 7679:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 7680:             }
 7681:         }
 7682:     }
 7683:     unless (grep(/^nolang$/,@currlangs)) {
 7684:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 7685:             $changes{'helpurl'}{'nolang'} = 1;
 7686:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 7687:             push(@newlangs,'nolang');
 7688:         }
 7689:     }
 7690:     if ($env{'form.loginhelpurl_add_lang'}) {
 7691:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 7692:             ($env{'form.loginhelpurl_add_file.filename'})) {
 7693:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 7694:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 7695:         }
 7696:     }
 7697:     if ((@newlangs > 0) || ($addedfile)) {
 7698:         my $error;
 7699:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7700:         if ($configuserok eq 'ok') {
 7701:             if ($switchserver) {
 7702:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 7703:             } elsif ($author_ok eq 'ok') {
 7704:                 my @allnew = @newlangs;
 7705:                 if ($addedfile ne '') {
 7706:                     push(@allnew,$addedfile);
 7707:                 }
 7708:                 foreach my $lang (@allnew) {
 7709:                     my $formelem = 'loginhelpurl_'.$lang;
 7710:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 7711:                         $formelem = 'loginhelpurl_add_file';
 7712:                     }
 7713:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 7714:                                                                "help/$lang",'','',$newfile{$lang});
 7715:                     if ($result eq 'ok') {
 7716:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 7717:                         $changes{'helpurl'}{$lang} = 1;
 7718:                     } else {
 7719:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 7720:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 7721:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 7722:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 7723:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 7724:                         }
 7725:                     }
 7726:                 }
 7727:             } else {
 7728:                 $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);
 7729:             }
 7730:         } else {
 7731:             $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);
 7732:         }
 7733:         if ($error) {
 7734:             &Apache::lonnet::logthis($error);
 7735:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7736:         }
 7737:     }
 7738: 
 7739:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 7740:     if (ref($domconfig{'login'}) eq 'HASH') {
 7741:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 7742:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 7743:                 if ($domservers{$lonhost}) {
 7744:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 7745:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 7746:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 7747:                     }
 7748:                 }
 7749:             }
 7750:         }
 7751:     }
 7752:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 7753:     foreach my $lonhost (sort(keys(%domservers))) {
 7754:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 7755:             $changes{'headtag'}{$lonhost} = 1;
 7756:         } else {
 7757:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 7758:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 7759:             }
 7760:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 7761:                 push(@newhosts,$lonhost);
 7762:             } elsif ($currheadtagurls{$lonhost}) {
 7763:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 7764:                 if ($currexempt{$lonhost}) {
 7765:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 7766:                         $changes{'headtag'}{$lonhost} = 1;
 7767:                     }
 7768:                 } elsif ($possexempt{$lonhost}) {
 7769:                     $changes{'headtag'}{$lonhost} = 1;
 7770:                 }
 7771:                 if ($possexempt{$lonhost}) {
 7772:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 7773:                 }
 7774:             }
 7775:         }
 7776:     }
 7777:     if (@newhosts) {
 7778:         my $error;
 7779:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7780:         if ($configuserok eq 'ok') {
 7781:             if ($switchserver) {
 7782:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 7783:             } elsif ($author_ok eq 'ok') {
 7784:                 foreach my $lonhost (@newhosts) {
 7785:                     my $formelem = 'loginheadtag_'.$lonhost;
 7786:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 7787:                                                                           "login/headtag/$lonhost",'','',
 7788:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 7789:                     if ($result eq 'ok') {
 7790:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 7791:                         $changes{'headtag'}{$lonhost} = 1;
 7792:                         if ($possexempt{$lonhost}) {
 7793:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 7794:                         }
 7795:                     } else {
 7796:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 7797:                                            $newheadtagurls{$lonhost},$result);
 7798:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 7799:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 7800:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 7801:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 7802:                         }
 7803:                     }
 7804:                 }
 7805:             } else {
 7806:                 $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);
 7807:             }
 7808:         } else {
 7809:             $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);
 7810:         }
 7811:         if ($error) {
 7812:             &Apache::lonnet::logthis($error);
 7813:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7814:         }
 7815:     }
 7816:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 7817: 
 7818:     my $defaulthelpfile = '/adm/loginproblems.html';
 7819:     my $defaulttext = &mt('Default in use');
 7820: 
 7821:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 7822:                                              $dom);
 7823:     if ($putresult eq 'ok') {
 7824:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7825:         my %defaultchecked = (
 7826:                     'coursecatalog' => 'on',
 7827:                     'helpdesk'      => 'on',
 7828:                     'adminmail'     => 'off',
 7829:                     'newuser'       => 'off',
 7830:         );
 7831:         if (ref($domconfig{'login'}) eq 'HASH') {
 7832:             foreach my $item (@toggles) {
 7833:                 if ($defaultchecked{$item} eq 'on') { 
 7834:                     if (($domconfig{'login'}{$item} eq '0') &&
 7835:                         ($env{'form.'.$item} eq '1')) {
 7836:                         $changes{$item} = 1;
 7837:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 7838:                               $domconfig{'login'}{$item} eq '1') &&
 7839:                              ($env{'form.'.$item} eq '0')) {
 7840:                         $changes{$item} = 1;
 7841:                     }
 7842:                 } elsif ($defaultchecked{$item} eq 'off') {
 7843:                     if (($domconfig{'login'}{$item} eq '1') &&
 7844:                         ($env{'form.'.$item} eq '0')) {
 7845:                         $changes{$item} = 1;
 7846:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 7847:                               $domconfig{'login'}{$item} eq '0') &&
 7848:                              ($env{'form.'.$item} eq '1')) {
 7849:                         $changes{$item} = 1;
 7850:                     }
 7851:                 }
 7852:             }
 7853:         }
 7854:         if (keys(%changes) > 0 || $colchgtext) {
 7855:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7856:             if (ref($lastactref) eq 'HASH') {
 7857:                 $lastactref->{'domainconfig'} = 1;
 7858:             }
 7859:             $resulttext = &mt('Changes made:').'<ul>';
 7860:             foreach my $item (sort(keys(%changes))) {
 7861:                 if ($item eq 'loginvia') {
 7862:                     if (ref($changes{$item}) eq 'HASH') {
 7863:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 7864:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 7865:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 7866:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 7867:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 7868:                                     $protocol = 'http' if ($protocol ne 'https');
 7869:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 7870: 
 7871:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 7872:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 7873:                                     } else {
 7874:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 7875:                                     }
 7876:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 7877:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 7878:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 7879:                                     }
 7880:                                     $resulttext .= '</li>';
 7881:                                 } else {
 7882:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 7883:                                 }
 7884:                             } else {
 7885:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 7886:                             }
 7887:                         }
 7888:                         $resulttext .= '</ul></li>';
 7889:                     }
 7890:                 } elsif ($item eq 'helpurl') {
 7891:                     if (ref($changes{$item}) eq 'HASH') {
 7892:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 7893:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 7894:                                 my ($chg,$link);
 7895:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 7896:                                 if ($lang eq 'nolang') {
 7897:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 7898:                                 } else {
 7899:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 7900:                                 }
 7901:                                 $resulttext .= '<li>'.$chg.'</li>';
 7902:                             } else {
 7903:                                 my $chg;
 7904:                                 if ($lang eq 'nolang') {
 7905:                                     $chg = &mt('custom log-in help file for no preferred language');
 7906:                                 } else {
 7907:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 7908:                                 }
 7909:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 7910:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 7911:                                                       '?inhibitmenu=yes',$chg,600,500).
 7912:                                                '</li>';
 7913:                             }
 7914:                         }
 7915:                     }
 7916:                 } elsif ($item eq 'headtag') {
 7917:                     if (ref($changes{$item}) eq 'HASH') {
 7918:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 7919:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 7920:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 7921:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 7922:                                 $resulttext .= '<li><a href="'.
 7923:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 7924:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 7925:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 7926:                                 if ($possexempt{$lonhost}) {
 7927:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 7928:                                 } else {
 7929:                                     $resulttext .= &mt('included for any client IP');
 7930:                                 }
 7931:                                 $resulttext .= '</li>';
 7932:                             }
 7933:                         }
 7934:                     }
 7935:                 } elsif ($item eq 'captcha') {
 7936:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7937:                         my $chgtxt;
 7938:                         if ($loginhash{'login'}{$item} eq 'notused') {
 7939:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 7940:                         } else {
 7941:                             my %captchas = &captcha_phrases();
 7942:                             if ($captchas{$loginhash{'login'}{$item}}) {
 7943:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 7944:                             } else {
 7945:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 7946:                             }
 7947:                         }
 7948:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 7949:                     }
 7950:                 } elsif ($item eq 'recaptchakeys') {
 7951:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7952:                         my ($privkey,$pubkey);
 7953:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 7954:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 7955:                             $privkey = $loginhash{'login'}{$item}{'private'};
 7956:                         }
 7957:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 7958:                         if (!$pubkey) {
 7959:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 7960:                         } else {
 7961:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 7962:                         }
 7963:                         if (!$privkey) {
 7964:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 7965:                         } else {
 7966:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 7967:                         }
 7968:                         $chgtxt .= '</ul>';
 7969:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 7970:                     }
 7971:                 } elsif ($item eq 'recaptchaversion') {
 7972:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7973:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 7974:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 7975:                                            '</li>';
 7976:                         }
 7977:                     }
 7978:                 } else {
 7979:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 7980:                 }
 7981:             }
 7982:             $resulttext .= $colchgtext.'</ul>';
 7983:         } else {
 7984:             $resulttext = &mt('No changes made to log-in page settings');
 7985:         }
 7986:     } else {
 7987:         $resulttext = '<span class="LC_error">'.
 7988: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7989:     }
 7990:     if ($errors) {
 7991:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 7992:                        $errors.'</ul>';
 7993:     }
 7994:     return $resulttext;
 7995: }
 7996: 
 7997: sub check_exempt_addresses {
 7998:     my ($iplist) = @_;
 7999:     $iplist =~ s/^\s+//;
 8000:     $iplist =~ s/\s+$//;
 8001:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 8002:     my (@okips,$new);
 8003:     foreach my $ip (@poss_ips) {
 8004:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 8005:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 8006:                 push(@okips,$ip);
 8007:             }
 8008:         }
 8009:     }
 8010:     if (@okips > 0) {
 8011:         $new = join(',',@okips);
 8012:     } else {
 8013:         $new = '';
 8014:     }
 8015:     return $new;
 8016: }
 8017: 
 8018: sub color_font_choices {
 8019:     my %choices =
 8020:         &Apache::lonlocal::texthash (
 8021:             img => "Header",
 8022:             bgs => "Background colors",
 8023:             links => "Link colors",
 8024:             images => "Images",
 8025:             font => "Font color",
 8026:             fontmenu => "Font menu",
 8027:             pgbg => "Page",
 8028:             tabbg => "Header",
 8029:             sidebg => "Border",
 8030:             link => "Link",
 8031:             alink => "Active link",
 8032:             vlink => "Visited link",
 8033:         );
 8034:     return %choices;
 8035: }
 8036: 
 8037: sub modify_rolecolors {
 8038:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 8039:     my ($resulttext,%rolehash);
 8040:     $rolehash{'rolecolors'} = {};
 8041:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 8042:         if ($domconfig{'rolecolors'} eq '') {
 8043:             $domconfig{'rolecolors'} = {};
 8044:         }
 8045:     }
 8046:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 8047:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 8048:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 8049:                                              $dom);
 8050:     if ($putresult eq 'ok') {
 8051:         if (keys(%changes) > 0) {
 8052:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8053:             if (ref($lastactref) eq 'HASH') {
 8054:                 $lastactref->{'domainconfig'} = 1;
 8055:             }
 8056:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 8057:                                              $rolehash{'rolecolors'});
 8058:         } else {
 8059:             $resulttext = &mt('No changes made to default color schemes');
 8060:         }
 8061:     } else {
 8062:         $resulttext = '<span class="LC_error">'.
 8063: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8064:     }
 8065:     if ($errors) {
 8066:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 8067:                        $errors.'</ul>';
 8068:     }
 8069:     return $resulttext;
 8070: }
 8071: 
 8072: sub modify_colors {
 8073:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 8074:     my (%changes,%choices);
 8075:     my @bgs;
 8076:     my @links = ('link','alink','vlink');
 8077:     my @logintext;
 8078:     my @images;
 8079:     my $servadm = $r->dir_config('lonAdmEMail');
 8080:     my $errors;
 8081:     my %defaults;
 8082:     foreach my $role (@{$roles}) {
 8083:         if ($role eq 'login') {
 8084:             %choices = &login_choices();
 8085:             @logintext = ('textcol','bgcol');
 8086:         } else {
 8087:             %choices = &color_font_choices();
 8088:         }
 8089:         if ($role eq 'login') {
 8090:             @images = ('img','logo','domlogo','login');
 8091:             @bgs = ('pgbg','mainbg','sidebg');
 8092:         } else {
 8093:             @images = ('img');
 8094:             @bgs = ('pgbg','tabbg','sidebg');
 8095:         }
 8096:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 8097:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 8098:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 8099:         }
 8100:         if ($role eq 'login') {
 8101:             foreach my $item (@logintext) {
 8102:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8103:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8104:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8105:                 }
 8106:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 8107:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8108:                 }
 8109:             }
 8110:         } else {
 8111:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 8112:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 8113:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 8114:             }
 8115:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 8116:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 8117:             }
 8118:         }
 8119:         foreach my $item (@bgs) {
 8120:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8121:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8122:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8123:             }
 8124:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 8125:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8126:             }
 8127:         }
 8128:         foreach my $item (@links) {
 8129:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8130:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8131:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8132:             }
 8133:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 8134:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8135:             }
 8136:         }
 8137:         my ($configuserok,$author_ok,$switchserver) = 
 8138:             &config_check($dom,$confname,$servadm);
 8139:         my ($width,$height) = &thumb_dimensions();
 8140:         if (ref($domconfig->{$role}) ne 'HASH') {
 8141:             $domconfig->{$role} = {};
 8142:         }
 8143:         foreach my $img (@images) {
 8144:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 8145:                 if (defined($env{'form.login_showlogo_'.$img})) {
 8146:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 8147:                 } else { 
 8148:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 8149:                 }
 8150:             } 
 8151: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 8152: 		 && !defined($domconfig->{$role}{$img})
 8153: 		 && !$env{'form.'.$role.'_del_'.$img}
 8154: 		 && $env{'form.'.$role.'_import_'.$img}) {
 8155: 		# import the old configured image from the .tab setting
 8156: 		# if they haven't provided a new one 
 8157: 		$domconfig->{$role}{$img} = 
 8158: 		    $env{'form.'.$role.'_import_'.$img};
 8159: 	    }
 8160:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 8161:                 my $error;
 8162:                 if ($configuserok eq 'ok') {
 8163:                     if ($switchserver) {
 8164:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 8165:                     } else {
 8166:                         if ($author_ok eq 'ok') {
 8167:                             my ($result,$logourl) = 
 8168:                                 &publishlogo($r,'upload',$role.'_'.$img,
 8169:                                            $dom,$confname,$img,$width,$height);
 8170:                             if ($result eq 'ok') {
 8171:                                 $confhash->{$role}{$img} = $logourl;
 8172:                                 $changes{$role}{'images'}{$img} = 1;
 8173:                             } else {
 8174:                                 $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);
 8175:                             }
 8176:                         } else {
 8177:                             $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);
 8178:                         }
 8179:                     }
 8180:                 } else {
 8181:                     $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);
 8182:                 }
 8183:                 if ($error) {
 8184:                     &Apache::lonnet::logthis($error);
 8185:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8186:                 }
 8187:             } elsif ($domconfig->{$role}{$img} ne '') {
 8188:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 8189:                     my $error;
 8190:                     if ($configuserok eq 'ok') {
 8191: # is confname an author?
 8192:                         if ($switchserver eq '') {
 8193:                             if ($author_ok eq 'ok') {
 8194:                                 my ($result,$logourl) = 
 8195:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 8196:                                             $dom,$confname,$img,$width,$height);
 8197:                                 if ($result eq 'ok') {
 8198:                                     $confhash->{$role}{$img} = $logourl;
 8199: 				    $changes{$role}{'images'}{$img} = 1;
 8200:                                 }
 8201:                             }
 8202:                         }
 8203:                     }
 8204:                 }
 8205:             }
 8206:         }
 8207:         if (ref($domconfig) eq 'HASH') {
 8208:             if (ref($domconfig->{$role}) eq 'HASH') {
 8209:                 foreach my $img (@images) {
 8210:                     if ($domconfig->{$role}{$img} ne '') {
 8211:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8212:                             $confhash->{$role}{$img} = '';
 8213:                             $changes{$role}{'images'}{$img} = 1;
 8214:                         } else {
 8215:                             if ($confhash->{$role}{$img} eq '') {
 8216:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 8217:                             }
 8218:                         }
 8219:                     } else {
 8220:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8221:                             $confhash->{$role}{$img} = '';
 8222:                             $changes{$role}{'images'}{$img} = 1;
 8223:                         } 
 8224:                     }
 8225:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 8226:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 8227:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 8228:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 8229:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 8230:                             }
 8231:                         } else {
 8232:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8233:                                 $changes{$role}{'showlogo'}{$img} = 1;
 8234:                             }
 8235:                         }
 8236:                     }
 8237:                 }
 8238:                 if ($domconfig->{$role}{'font'} ne '') {
 8239:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 8240:                         $changes{$role}{'font'} = 1;
 8241:                     }
 8242:                 } else {
 8243:                     if ($confhash->{$role}{'font'}) {
 8244:                         $changes{$role}{'font'} = 1;
 8245:                     }
 8246:                 }
 8247:                 if ($role ne 'login') {
 8248:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 8249:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 8250:                             $changes{$role}{'fontmenu'} = 1;
 8251:                         }
 8252:                     } else {
 8253:                         if ($confhash->{$role}{'fontmenu'}) {
 8254:                             $changes{$role}{'fontmenu'} = 1;
 8255:                         }
 8256:                     }
 8257:                 }
 8258:                 foreach my $item (@bgs) {
 8259:                     if ($domconfig->{$role}{$item} ne '') {
 8260:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8261:                             $changes{$role}{'bgs'}{$item} = 1;
 8262:                         } 
 8263:                     } else {
 8264:                         if ($confhash->{$role}{$item}) {
 8265:                             $changes{$role}{'bgs'}{$item} = 1;
 8266:                         }
 8267:                     }
 8268:                 }
 8269:                 foreach my $item (@links) {
 8270:                     if ($domconfig->{$role}{$item} ne '') {
 8271:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8272:                             $changes{$role}{'links'}{$item} = 1;
 8273:                         }
 8274:                     } else {
 8275:                         if ($confhash->{$role}{$item}) {
 8276:                             $changes{$role}{'links'}{$item} = 1;
 8277:                         }
 8278:                     }
 8279:                 }
 8280:                 foreach my $item (@logintext) {
 8281:                     if ($domconfig->{$role}{$item} ne '') {
 8282:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8283:                             $changes{$role}{'logintext'}{$item} = 1;
 8284:                         }
 8285:                     } else {
 8286:                         if ($confhash->{$role}{$item}) {
 8287:                             $changes{$role}{'logintext'}{$item} = 1;
 8288:                         }
 8289:                     }
 8290:                 }
 8291:             } else {
 8292:                 &default_change_checker($role,\@images,\@links,\@bgs,
 8293:                                         \@logintext,$confhash,\%changes); 
 8294:             }
 8295:         } else {
 8296:             &default_change_checker($role,\@images,\@links,\@bgs,
 8297:                                     \@logintext,$confhash,\%changes); 
 8298:         }
 8299:     }
 8300:     return ($errors,%changes);
 8301: }
 8302: 
 8303: sub config_check {
 8304:     my ($dom,$confname,$servadm) = @_;
 8305:     my ($configuserok,$author_ok,$switchserver,%currroles);
 8306:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 8307:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 8308:                                                    $confname,$servadm);
 8309:     if ($configuserok eq 'ok') {
 8310:         $switchserver = &check_switchserver($dom,$confname);
 8311:         if ($switchserver eq '') {
 8312:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 8313:         }
 8314:     }
 8315:     return ($configuserok,$author_ok,$switchserver);
 8316: }
 8317: 
 8318: sub default_change_checker {
 8319:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 8320:     foreach my $item (@{$links}) {
 8321:         if ($confhash->{$role}{$item}) {
 8322:             $changes->{$role}{'links'}{$item} = 1;
 8323:         }
 8324:     }
 8325:     foreach my $item (@{$bgs}) {
 8326:         if ($confhash->{$role}{$item}) {
 8327:             $changes->{$role}{'bgs'}{$item} = 1;
 8328:         }
 8329:     }
 8330:     foreach my $item (@{$logintext}) {
 8331:         if ($confhash->{$role}{$item}) {
 8332:             $changes->{$role}{'logintext'}{$item} = 1;
 8333:         }
 8334:     }
 8335:     foreach my $img (@{$images}) {
 8336:         if ($env{'form.'.$role.'_del_'.$img}) {
 8337:             $confhash->{$role}{$img} = '';
 8338:             $changes->{$role}{'images'}{$img} = 1;
 8339:         }
 8340:         if ($role eq 'login') {
 8341:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8342:                 $changes->{$role}{'showlogo'}{$img} = 1;
 8343:             }
 8344:         }
 8345:     }
 8346:     if ($confhash->{$role}{'font'}) {
 8347:         $changes->{$role}{'font'} = 1;
 8348:     }
 8349: }
 8350: 
 8351: sub display_colorchgs {
 8352:     my ($dom,$changes,$roles,$confhash) = @_;
 8353:     my (%choices,$resulttext);
 8354:     if (!grep(/^login$/,@{$roles})) {
 8355:         $resulttext = &mt('Changes made:').'<br />';
 8356:     }
 8357:     foreach my $role (@{$roles}) {
 8358:         if ($role eq 'login') {
 8359:             %choices = &login_choices();
 8360:         } else {
 8361:             %choices = &color_font_choices();
 8362:         }
 8363:         if (ref($changes->{$role}) eq 'HASH') {
 8364:             if ($role ne 'login') {
 8365:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 8366:             }
 8367:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 8368:                 if ($role ne 'login') {
 8369:                     $resulttext .= '<ul>';
 8370:                 }
 8371:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 8372:                     if ($role ne 'login') {
 8373:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 8374:                     }
 8375:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 8376:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 8377:                             if ($confhash->{$role}{$key}{$item}) {
 8378:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 8379:                             } else {
 8380:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 8381:                             }
 8382:                         } elsif ($confhash->{$role}{$item} eq '') {
 8383:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 8384:                         } else {
 8385:                             my $newitem = $confhash->{$role}{$item};
 8386:                             if ($key eq 'images') {
 8387:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 8388:                             }
 8389:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 8390:                         }
 8391:                     }
 8392:                     if ($role ne 'login') {
 8393:                         $resulttext .= '</ul></li>';
 8394:                     }
 8395:                 } else {
 8396:                     if ($confhash->{$role}{$key} eq '') {
 8397:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 8398:                     } else {
 8399:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 8400:                     }
 8401:                 }
 8402:                 if ($role ne 'login') {
 8403:                     $resulttext .= '</ul>';
 8404:                 }
 8405:             }
 8406:         }
 8407:     }
 8408:     return $resulttext;
 8409: }
 8410: 
 8411: sub thumb_dimensions {
 8412:     return ('200','50');
 8413: }
 8414: 
 8415: sub check_dimensions {
 8416:     my ($inputfile) = @_;
 8417:     my ($fullwidth,$fullheight);
 8418:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 8419:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 8420:             my $imageinfo = <PIPE>;
 8421:             if (!close(PIPE)) {
 8422:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 8423:             }
 8424:             chomp($imageinfo);
 8425:             my ($fullsize) = 
 8426:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 8427:             if ($fullsize) {
 8428:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 8429:             }
 8430:         }
 8431:     }
 8432:     return ($fullwidth,$fullheight);
 8433: }
 8434: 
 8435: sub check_configuser {
 8436:     my ($uhome,$dom,$confname,$servadm) = @_;
 8437:     my ($configuserok,%currroles);
 8438:     if ($uhome eq 'no_host') {
 8439:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 8440:         my $configpass = &LONCAPA::Enrollment::create_password();
 8441:         $configuserok = 
 8442:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 8443:                              $configpass,'','','','','',undef,$servadm);
 8444:     } else {
 8445:         $configuserok = 'ok';
 8446:         %currroles = 
 8447:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 8448:     }
 8449:     return ($configuserok,%currroles);
 8450: }
 8451: 
 8452: sub check_authorstatus {
 8453:     my ($dom,$confname,%currroles) = @_;
 8454:     my $author_ok;
 8455:     if (!$currroles{':'.$dom.':au'}) {
 8456:         my $start = time;
 8457:         my $end = 0;
 8458:         $author_ok = 
 8459:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 8460:                                         'au',$end,$start,'','','domconfig');
 8461:     } else {
 8462:         $author_ok = 'ok';
 8463:     }
 8464:     return $author_ok;
 8465: }
 8466: 
 8467: sub publishlogo {
 8468:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 8469:     my ($output,$fname,$logourl,$madethumb);
 8470:     if ($action eq 'upload') {
 8471:         $fname=$env{'form.'.$formname.'.filename'};
 8472:         chop($env{'form.'.$formname});
 8473:     } else {
 8474:         ($fname) = ($formname =~ /([^\/]+)$/);
 8475:     }
 8476:     if ($savefileas ne '') {
 8477:         $fname = $savefileas;
 8478:     }
 8479:     $fname=&Apache::lonnet::clean_filename($fname);
 8480: # See if there is anything left
 8481:     unless ($fname) { return ('error: no uploaded file'); }
 8482:     $fname="$subdir/$fname";
 8483:     my $docroot=$r->dir_config('lonDocRoot');
 8484:     my $filepath="$docroot/priv";
 8485:     my $relpath = "$dom/$confname";
 8486:     my ($fnamepath,$file,$fetchthumb);
 8487:     $file=$fname;
 8488:     if ($fname=~m|/|) {
 8489:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 8490:     }
 8491:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 8492:     my $count;
 8493:     for ($count=5;$count<=$#parts;$count++) {
 8494:         $filepath.="/$parts[$count]";
 8495:         if ((-e $filepath)!=1) {
 8496:             mkdir($filepath,02770);
 8497:         }
 8498:     }
 8499:     # Check for bad extension and disallow upload
 8500:     if ($file=~/\.(\w+)$/ &&
 8501:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 8502:         $output = 
 8503:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 8504:     } elsif ($file=~/\.(\w+)$/ &&
 8505:         !defined(&Apache::loncommon::fileembstyle($1))) {
 8506:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 8507:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 8508:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 8509:     } elsif (-d "$filepath/$file") {
 8510:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 8511:     } else {
 8512:         my $source = $filepath.'/'.$file;
 8513:         my $logfile;
 8514:         if (!open($logfile,">>$source".'.log')) {
 8515:             return (&mt('No write permission to Authoring Space'));
 8516:         }
 8517:         print $logfile
 8518: "\n================= Publish ".localtime()." ================\n".
 8519: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 8520: # Save the file
 8521:         if (!open(FH,'>'.$source)) {
 8522:             &Apache::lonnet::logthis('Failed to create '.$source);
 8523:             return (&mt('Failed to create file'));
 8524:         }
 8525:         if ($action eq 'upload') {
 8526:             if (!print FH ($env{'form.'.$formname})) {
 8527:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 8528:                 return (&mt('Failed to write file'));
 8529:             }
 8530:         } else {
 8531:             my $original = &Apache::lonnet::filelocation('',$formname);
 8532:             if(!copy($original,$source)) {
 8533:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 8534:                 return (&mt('Failed to write file'));
 8535:             }
 8536:         }
 8537:         close(FH);
 8538:         chmod(0660, $source); # Permissions to rw-rw---.
 8539: 
 8540:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 8541:         my $copyfile=$targetdir.'/'.$file;
 8542: 
 8543:         my @parts=split(/\//,$targetdir);
 8544:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 8545:         for (my $count=5;$count<=$#parts;$count++) {
 8546:             $path.="/$parts[$count]";
 8547:             if (!-e $path) {
 8548:                 print $logfile "\nCreating directory ".$path;
 8549:                 mkdir($path,02770);
 8550:             }
 8551:         }
 8552:         my $versionresult;
 8553:         if (-e $copyfile) {
 8554:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 8555:         } else {
 8556:             $versionresult = 'ok';
 8557:         }
 8558:         if ($versionresult eq 'ok') {
 8559:             if (copy($source,$copyfile)) {
 8560:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 8561:                 $output = 'ok';
 8562:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 8563:                 push(@{$modified_urls},[$copyfile,$source]);
 8564:                 my $metaoutput = 
 8565:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 8566:                 unless ($registered_cleanup) {
 8567:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8568:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8569:                     $registered_cleanup=1;
 8570:                 }
 8571:             } else {
 8572:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 8573:                 $output = &mt('Failed to copy file to RES space').", $!";
 8574:             }
 8575:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 8576:                 my $inputfile = $filepath.'/'.$file;
 8577:                 my $outfile = $filepath.'/'.'tn-'.$file;
 8578:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 8579:                 if ($fullwidth ne '' && $fullheight ne '') { 
 8580:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 8581:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 8582:                         system("convert -sample $thumbsize $inputfile $outfile");
 8583:                         chmod(0660, $filepath.'/tn-'.$file);
 8584:                         if (-e $outfile) {
 8585:                             my $copyfile=$targetdir.'/tn-'.$file;
 8586:                             if (copy($outfile,$copyfile)) {
 8587:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 8588:                                 my $thumb_metaoutput = 
 8589:                                     &write_metadata($dom,$confname,$formname,
 8590:                                                     $targetdir,'tn-'.$file,$logfile);
 8591:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 8592:                                 unless ($registered_cleanup) {
 8593:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8594:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8595:                                     $registered_cleanup=1;
 8596:                                 }
 8597:                                 $madethumb = 1;
 8598:                             } else {
 8599:                                 print $logfile "\nUnable to write ".$copyfile.
 8600:                                                ':'.$!."\n";
 8601:                             }
 8602:                         }
 8603:                     }
 8604:                 }
 8605:             }
 8606:         } else {
 8607:             $output = $versionresult;
 8608:         }
 8609:     }
 8610:     return ($output,$logourl,$madethumb);
 8611: }
 8612: 
 8613: sub logo_versioning {
 8614:     my ($targetdir,$file,$logfile) = @_;
 8615:     my $target = $targetdir.'/'.$file;
 8616:     my ($maxversion,$fn,$extn,$output);
 8617:     $maxversion = 0;
 8618:     if ($file =~ /^(.+)\.(\w+)$/) {
 8619:         $fn=$1;
 8620:         $extn=$2;
 8621:     }
 8622:     opendir(DIR,$targetdir);
 8623:     while (my $filename=readdir(DIR)) {
 8624:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 8625:             $maxversion=($1>$maxversion)?$1:$maxversion;
 8626:         }
 8627:     }
 8628:     $maxversion++;
 8629:     print $logfile "\nCreating old version ".$maxversion."\n";
 8630:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 8631:     if (copy($target,$copyfile)) {
 8632:         print $logfile "Copied old target to ".$copyfile."\n";
 8633:         $copyfile=$copyfile.'.meta';
 8634:         if (copy($target.'.meta',$copyfile)) {
 8635:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 8636:             $output = 'ok';
 8637:         } else {
 8638:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 8639:             $output = &mt('Failed to copy old meta').", $!, ";
 8640:         }
 8641:     } else {
 8642:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 8643:         $output = &mt('Failed to copy old target').", $!, ";
 8644:     }
 8645:     return $output;
 8646: }
 8647: 
 8648: sub write_metadata {
 8649:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 8650:     my (%metadatafields,%metadatakeys,$output);
 8651:     $metadatafields{'title'}=$formname;
 8652:     $metadatafields{'creationdate'}=time;
 8653:     $metadatafields{'lastrevisiondate'}=time;
 8654:     $metadatafields{'copyright'}='public';
 8655:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 8656:                                          $env{'user.domain'};
 8657:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 8658:     $metadatafields{'domain'}=$dom;
 8659:     {
 8660:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 8661:         my $mfh;
 8662:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 8663:             foreach (sort(keys(%metadatafields))) {
 8664:                 unless ($_=~/\./) {
 8665:                     my $unikey=$_;
 8666:                     $unikey=~/^([A-Za-z]+)/;
 8667:                     my $tag=$1;
 8668:                     $tag=~tr/A-Z/a-z/;
 8669:                     print $mfh "\n\<$tag";
 8670:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 8671:                         my $value=$metadatafields{$unikey.'.'.$_};
 8672:                         $value=~s/\"/\'\'/g;
 8673:                         print $mfh ' '.$_.'="'.$value.'"';
 8674:                     }
 8675:                     print $mfh '>'.
 8676:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 8677:                             .'</'.$tag.'>';
 8678:                 }
 8679:             }
 8680:             $output = 'ok';
 8681:             print $logfile "\nWrote metadata";
 8682:             close($mfh);
 8683:         } else {
 8684:             print $logfile "\nFailed to open metadata file";
 8685:             $output = &mt('Could not write metadata');
 8686:         }
 8687:     }
 8688:     return $output;
 8689: }
 8690: 
 8691: sub notifysubscribed {
 8692:     foreach my $targetsource (@{$modified_urls}){
 8693:         next unless (ref($targetsource) eq 'ARRAY');
 8694:         my ($target,$source)=@{$targetsource};
 8695:         if ($source ne '') {
 8696:             if (open(my $logfh,'>>'.$source.'.log')) {
 8697:                 print $logfh "\nCleanup phase: Notifications\n";
 8698:                 my @subscribed=&subscribed_hosts($target);
 8699:                 foreach my $subhost (@subscribed) {
 8700:                     print $logfh "\nNotifying host ".$subhost.':';
 8701:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 8702:                     print $logfh $reply;
 8703:                 }
 8704:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 8705:                 foreach my $subhost (@subscribedmeta) {
 8706:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 8707:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 8708:                                                         $subhost);
 8709:                     print $logfh $reply;
 8710:                 }
 8711:                 print $logfh "\n============ Done ============\n";
 8712:                 close($logfh);
 8713:             }
 8714:         }
 8715:     }
 8716:     return OK;
 8717: }
 8718: 
 8719: sub subscribed_hosts {
 8720:     my ($target) = @_;
 8721:     my @subscribed;
 8722:     if (open(my $fh,"<$target.subscription")) {
 8723:         while (my $subline=<$fh>) {
 8724:             if ($subline =~ /^($match_lonid):/) {
 8725:                 my $host = $1;
 8726:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 8727:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 8728:                         push(@subscribed,$host);
 8729:                     }
 8730:                 }
 8731:             }
 8732:         }
 8733:     }
 8734:     return @subscribed;
 8735: }
 8736: 
 8737: sub check_switchserver {
 8738:     my ($dom,$confname) = @_;
 8739:     my ($allowed,$switchserver);
 8740:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 8741:     if ($home eq 'no_host') {
 8742:         $home = &Apache::lonnet::domain($dom,'primary');
 8743:     }
 8744:     my @ids=&Apache::lonnet::current_machine_ids();
 8745:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 8746:     if (!$allowed) {
 8747: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 8748:     }
 8749:     return $switchserver;
 8750: }
 8751: 
 8752: sub modify_quotas {
 8753:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 8754:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 8755:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 8756:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 8757:         $validationfieldsref);
 8758:     if ($action eq 'quotas') {
 8759:         $context = 'tools'; 
 8760:     } else {
 8761:         $context = $action;
 8762:     }
 8763:     if ($context eq 'requestcourses') {
 8764:         @usertools = ('official','unofficial','community','textbook','placement');
 8765:         @options =('norequest','approval','validate','autolimit');
 8766:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 8767:         %titles = &courserequest_titles();
 8768:         $toolregexp = join('|',@usertools);
 8769:         %conditions = &courserequest_conditions();
 8770:         $confname = $dom.'-domainconfig';
 8771:         my $servadm = $r->dir_config('lonAdmEMail');
 8772:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8773:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 8774:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 8775:     } elsif ($context eq 'requestauthor') {
 8776:         @usertools = ('author');
 8777:         %titles = &authorrequest_titles();
 8778:     } else {
 8779:         @usertools = ('aboutme','blog','webdav','portfolio');
 8780:         %titles = &tool_titles();
 8781:     }
 8782:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8783:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8784:     foreach my $key (keys(%env)) {
 8785:         if ($context eq 'requestcourses') {
 8786:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 8787:                 my $item = $1;
 8788:                 my $type = $2;
 8789:                 if ($type =~ /^limit_(.+)/) {
 8790:                     $limithash{$item}{$1} = $env{$key};
 8791:                 } else {
 8792:                     $confhash{$item}{$type} = $env{$key};
 8793:                 }
 8794:             }
 8795:         } elsif ($context eq 'requestauthor') {
 8796:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 8797:                 $confhash{$1} = $env{$key};
 8798:             }
 8799:         } else {
 8800:             if ($key =~ /^form\.quota_(.+)$/) {
 8801:                 $confhash{'defaultquota'}{$1} = $env{$key};
 8802:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 8803:                 $confhash{'authorquota'}{$1} = $env{$key};
 8804:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 8805:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 8806:             }
 8807:         }
 8808:     }
 8809:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 8810:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 8811:         @approvalnotify = sort(@approvalnotify);
 8812:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 8813:         my @crstypes = ('official','unofficial','community','textbook','placement');
 8814:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 8815:         foreach my $type (@hasuniquecode) {
 8816:             if (grep(/^\Q$type\E$/,@crstypes)) {
 8817:                 $confhash{'uniquecode'}{$type} = 1;
 8818:             }
 8819:         }
 8820:         my (%newbook,%allpos);
 8821:         if ($context eq 'requestcourses') {
 8822:             foreach my $type ('textbooks','templates') {
 8823:                 @{$allpos{$type}} = (); 
 8824:                 my $invalid;
 8825:                 if ($type eq 'textbooks') {
 8826:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 8827:                 } else {
 8828:                     $invalid = &mt('Invalid LON-CAPA course for template');
 8829:                 }
 8830:                 if ($env{'form.'.$type.'_addbook'}) {
 8831:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 8832:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 8833:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 8834:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 8835:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 8836:                         } else {
 8837:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 8838:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 8839:                             $position =~ s/\D+//g;
 8840:                             if ($position ne '') {
 8841:                                 $allpos{$type}[$position] = $newbook{$type};
 8842:                             }
 8843:                         }
 8844:                     } else {
 8845:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 8846:                     }
 8847:                 }
 8848:             } 
 8849:         }
 8850:         if (ref($domconfig{$action}) eq 'HASH') {
 8851:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 8852:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 8853:                     $changes{'notify'}{'approval'} = 1;
 8854:                 }
 8855:             } else {
 8856:                 if ($confhash{'notify'}{'approval'}) {
 8857:                     $changes{'notify'}{'approval'} = 1;
 8858:                 }
 8859:             }
 8860:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 8861:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 8862:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 8863:                         unless ($confhash{'uniquecode'}{$crstype}) {
 8864:                             $changes{'uniquecode'} = 1;
 8865:                         }
 8866:                     }
 8867:                     unless ($changes{'uniquecode'}) {
 8868:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 8869:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 8870:                                 $changes{'uniquecode'} = 1;
 8871:                             }
 8872:                         }
 8873:                     }
 8874:                } else {
 8875:                    $changes{'uniquecode'} = 1;
 8876:                }
 8877:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 8878:                 $changes{'uniquecode'} = 1;
 8879:             }
 8880:             if ($context eq 'requestcourses') {
 8881:                 foreach my $type ('textbooks','templates') {
 8882:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 8883:                         my %deletions;
 8884:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 8885:                         if (@todelete) {
 8886:                             map { $deletions{$_} = 1; } @todelete;
 8887:                         }
 8888:                         my %imgdeletions;
 8889:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 8890:                         if (@todeleteimages) {
 8891:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 8892:                         }
 8893:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 8894:                         for (my $i=0; $i<=$maxnum; $i++) {
 8895:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 8896:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 8897:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 8898:                                 if ($deletions{$key}) {
 8899:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 8900:                                         #FIXME need to obsolete item in RES space
 8901:                                     }
 8902:                                     next;
 8903:                                 } else {
 8904:                                     my $newpos = $env{'form.'.$itemid};
 8905:                                     $newpos =~ s/\D+//g;
 8906:                                     foreach my $item ('subject','title','publisher','author') {
 8907:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8908:                                                  ($type eq 'templates'));
 8909:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 8910:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 8911:                                             $changes{$type}{$key} = 1;
 8912:                                         }
 8913:                                     }
 8914:                                     $allpos{$type}[$newpos] = $key;
 8915:                                 }
 8916:                                 if ($imgdeletions{$key}) {
 8917:                                     $changes{$type}{$key} = 1;
 8918:                                     #FIXME need to obsolete item in RES space
 8919:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 8920:                                     my ($cdom,$cnum) = split(/_/,$key);
 8921:                                     my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 8922:                                                                                   $cdom,$cnum,$type,$configuserok,
 8923:                                                                                   $switchserver,$author_ok);
 8924:                                     if ($imgurl) {
 8925:                                         $confhash{$type}{$key}{'image'} = $imgurl;
 8926:                                         $changes{$type}{$key} = 1; 
 8927:                                     }
 8928:                                     if ($error) {
 8929:                                         &Apache::lonnet::logthis($error);
 8930:                                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8931:                                     } 
 8932:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 8933:                                     $confhash{$type}{$key}{'image'} = 
 8934:                                         $domconfig{$action}{$type}{$key}{'image'};
 8935:                                 }
 8936:                             }
 8937:                         }
 8938:                     }
 8939:                 }
 8940:             }
 8941:         } else {
 8942:             if ($confhash{'notify'}{'approval'}) {
 8943:                 $changes{'notify'}{'approval'} = 1;
 8944:             }
 8945:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 8946:                 $changes{'uniquecode'} = 1;
 8947:             }
 8948:         }
 8949:         if ($context eq 'requestcourses') {
 8950:             foreach my $type ('textbooks','templates') {
 8951:                 if ($newbook{$type}) {
 8952:                     $changes{$type}{$newbook{$type}} = 1;
 8953:                     foreach my $item ('subject','title','publisher','author') {
 8954:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8955:                                  ($type eq 'template'));
 8956:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 8957:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 8958:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 8959:                         }
 8960:                     }
 8961:                     if ($type eq 'textbooks') {
 8962:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 8963:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 8964:                             my ($imageurl,$error) =
 8965:                                 &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 8966:                                                         $configuserok,$switchserver,$author_ok);
 8967:                             if ($imageurl) {
 8968:                                 $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 8969:                             }
 8970:                             if ($error) {
 8971:                                 &Apache::lonnet::logthis($error);
 8972:                                 $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8973:                             }
 8974:                         }
 8975:                     }
 8976:                 }
 8977:                 if (@{$allpos{$type}} > 0) {
 8978:                     my $idx = 0;
 8979:                     foreach my $item (@{$allpos{$type}}) {
 8980:                         if ($item ne '') {
 8981:                             $confhash{$type}{$item}{'order'} = $idx;
 8982:                             if (ref($domconfig{$action}) eq 'HASH') {
 8983:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 8984:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 8985:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 8986:                                             $changes{$type}{$item} = 1;
 8987:                                         }
 8988:                                     }
 8989:                                 }
 8990:                             }
 8991:                             $idx ++;
 8992:                         }
 8993:                     }
 8994:                 }
 8995:             }
 8996:             if (ref($validationitemsref) eq 'ARRAY') {
 8997:                 foreach my $item (@{$validationitemsref}) {
 8998:                     if ($item eq 'fields') {
 8999:                         my @changed;
 9000:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 9001:                         if (@{$confhash{'validation'}{$item}} > 0) {
 9002:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 9003:                         }
 9004:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9005:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9006:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 9007:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 9008:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
 9009:                                 } else {
 9010:                                     @changed = @{$confhash{'validation'}{$item}};
 9011:                                 }
 9012:                             } else {
 9013:                                 @changed = @{$confhash{'validation'}{$item}};
 9014:                             }
 9015:                         } else {
 9016:                             @changed = @{$confhash{'validation'}{$item}};
 9017:                         }
 9018:                         if (@changed) {
 9019:                             if ($confhash{'validation'}{$item}) {
 9020:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 9021:                             } else {
 9022:                                 $changes{'validation'}{$item} = &mt('None');
 9023:                             }
 9024:                         }
 9025:                     } else {
 9026:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 9027:                         if ($item eq 'markup') {
 9028:                             if ($env{'form.requestcourses_validation_'.$item}) {
 9029:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 9030:                             }
 9031:                         }
 9032:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9033:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9034:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 9035:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9036:                                 }
 9037:                             } else {
 9038:                                 if ($confhash{'validation'}{$item} ne '') {
 9039:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9040:                                 }
 9041:                             }
 9042:                         } else {
 9043:                             if ($confhash{'validation'}{$item} ne '') {
 9044:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9045:                             }
 9046:                         }
 9047:                     }
 9048:                 }
 9049:             }
 9050:             if ($env{'form.validationdc'}) {
 9051:                 my $newval = $env{'form.validationdc'};
 9052:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 9053:                 if (exists($domcoords{$newval})) {
 9054:                     $confhash{'validation'}{'dc'} = $newval;
 9055:                 }
 9056:             }
 9057:             if (ref($confhash{'validation'}) eq 'HASH') {
 9058:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9059:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9060:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9061:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9062:                                 if ($confhash{'validation'}{'dc'} eq '') {
 9063:                                     $changes{'validation'}{'dc'} = &mt('None');
 9064:                                 } else {
 9065:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9066:                                 }
 9067:                             }
 9068:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
 9069:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9070:                         }
 9071:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 9072:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9073:                     }
 9074:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 9075:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9076:                 }
 9077:             } else {
 9078:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9079:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9080:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9081:                             $changes{'validation'}{'dc'} = &mt('None');
 9082:                         }
 9083:                     }
 9084:                 }
 9085:             }
 9086:         }
 9087:     } else {
 9088:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 9089:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 9090:     }
 9091:     foreach my $item (@usertools) {
 9092:         foreach my $type (@{$types},'default','_LC_adv') {
 9093:             my $unset; 
 9094:             if ($context eq 'requestcourses') {
 9095:                 $unset = '0';
 9096:                 if ($type eq '_LC_adv') {
 9097:                     $unset = '';
 9098:                 }
 9099:                 if ($confhash{$item}{$type} eq 'autolimit') {
 9100:                     $confhash{$item}{$type} .= '=';
 9101:                     unless ($limithash{$item}{$type} =~ /\D/) {
 9102:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 9103:                     }
 9104:                 }
 9105:             } elsif ($context eq 'requestauthor') {
 9106:                 $unset = '0';
 9107:                 if ($type eq '_LC_adv') {
 9108:                     $unset = '';
 9109:                 }
 9110:             } else {
 9111:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 9112:                     $confhash{$item}{$type} = 1;
 9113:                 } else {
 9114:                     $confhash{$item}{$type} = 0;
 9115:                 }
 9116:             }
 9117:             if (ref($domconfig{$action}) eq 'HASH') {
 9118:                 if ($action eq 'requestauthor') {
 9119:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 9120:                         $changes{$type} = 1;
 9121:                     }
 9122:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 9123:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 9124:                         $changes{$item}{$type} = 1;
 9125:                     }
 9126:                 } else {
 9127:                     if ($context eq 'requestcourses') {
 9128:                         if ($confhash{$item}{$type} ne $unset) {
 9129:                             $changes{$item}{$type} = 1;
 9130:                         }
 9131:                     } else {
 9132:                         if (!$confhash{$item}{$type}) {
 9133:                             $changes{$item}{$type} = 1;
 9134:                         }
 9135:                     }
 9136:                 }
 9137:             } else {
 9138:                 if ($context eq 'requestcourses') {
 9139:                     if ($confhash{$item}{$type} ne $unset) {
 9140:                         $changes{$item}{$type} = 1;
 9141:                     }
 9142:                 } elsif ($context eq 'requestauthor') {
 9143:                     if ($confhash{$type} ne $unset) {
 9144:                         $changes{$type} = 1;
 9145:                     }
 9146:                 } else {
 9147:                     if (!$confhash{$item}{$type}) {
 9148:                         $changes{$item}{$type} = 1;
 9149:                     }
 9150:                 }
 9151:             }
 9152:         }
 9153:     }
 9154:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 9155:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 9156:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 9157:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 9158:                     if (exists($confhash{'defaultquota'}{$key})) {
 9159:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 9160:                             $changes{'defaultquota'}{$key} = 1;
 9161:                         }
 9162:                     } else {
 9163:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 9164:                     }
 9165:                 }
 9166:             } else {
 9167:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 9168:                     if (exists($confhash{'defaultquota'}{$key})) {
 9169:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 9170:                             $changes{'defaultquota'}{$key} = 1;
 9171:                         }
 9172:                     } else {
 9173:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 9174:                     }
 9175:                 }
 9176:             }
 9177:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9178:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 9179:                     if (exists($confhash{'authorquota'}{$key})) {
 9180:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 9181:                             $changes{'authorquota'}{$key} = 1;
 9182:                         }
 9183:                     } else {
 9184:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 9185:                     }
 9186:                 }
 9187:             }
 9188:         }
 9189:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 9190:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 9191:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9192:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 9193:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 9194:                             $changes{'defaultquota'}{$key} = 1;
 9195:                         }
 9196:                     } else {
 9197:                         if (!exists($domconfig{'quotas'}{$key})) {
 9198:                             $changes{'defaultquota'}{$key} = 1;
 9199:                         }
 9200:                     }
 9201:                 } else {
 9202:                     $changes{'defaultquota'}{$key} = 1;
 9203:                 }
 9204:             }
 9205:         }
 9206:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 9207:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 9208:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9209:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9210:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 9211:                             $changes{'authorquota'}{$key} = 1;
 9212:                         }
 9213:                     } else {
 9214:                         $changes{'authorquota'}{$key} = 1;
 9215:                     }
 9216:                 } else {
 9217:                     $changes{'authorquota'}{$key} = 1;
 9218:                 }
 9219:             }
 9220:         }
 9221:     }
 9222: 
 9223:     if ($context eq 'requestauthor') {
 9224:         $domdefaults{'requestauthor'} = \%confhash;
 9225:     } else {
 9226:         foreach my $key (keys(%confhash)) {
 9227:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 9228:                 $domdefaults{$key} = $confhash{$key};
 9229:             }
 9230:         }
 9231:     }
 9232: 
 9233:     my %quotahash = (
 9234:                       $action => { %confhash }
 9235:                     );
 9236:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 9237:                                              $dom);
 9238:     if ($putresult eq 'ok') {
 9239:         if (keys(%changes) > 0) {
 9240:             my $cachetime = 24*60*60;
 9241:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9242:             if (ref($lastactref) eq 'HASH') {
 9243:                 $lastactref->{'domdefaults'} = 1;
 9244:             }
 9245:             $resulttext = &mt('Changes made:').'<ul>';
 9246:             unless (($context eq 'requestcourses') ||
 9247:                     ($context eq 'requestauthor')) {
 9248:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 9249:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 9250:                     foreach my $type (@{$types},'default') {
 9251:                         if (defined($changes{'defaultquota'}{$type})) {
 9252:                             my $typetitle = $usertypes->{$type};
 9253:                             if ($type eq 'default') {
 9254:                                 $typetitle = $othertitle;
 9255:                             }
 9256:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 9257:                         }
 9258:                     }
 9259:                     $resulttext .= '</ul></li>';
 9260:                 }
 9261:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 9262:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 9263:                     foreach my $type (@{$types},'default') {
 9264:                         if (defined($changes{'authorquota'}{$type})) {
 9265:                             my $typetitle = $usertypes->{$type};
 9266:                             if ($type eq 'default') {
 9267:                                 $typetitle = $othertitle;
 9268:                             }
 9269:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 9270:                         }
 9271:                     }
 9272:                     $resulttext .= '</ul></li>';
 9273:                 }
 9274:             }
 9275:             my %newenv;
 9276:             foreach my $item (@usertools) {
 9277:                 my (%haschgs,%inconf);
 9278:                 if ($context eq 'requestauthor') {
 9279:                     %haschgs = %changes;
 9280:                     %inconf = %confhash;
 9281:                 } else {
 9282:                     if (ref($changes{$item}) eq 'HASH') {
 9283:                         %haschgs = %{$changes{$item}};
 9284:                     }
 9285:                     if (ref($confhash{$item}) eq 'HASH') {
 9286:                         %inconf = %{$confhash{$item}};
 9287:                     }
 9288:                 }
 9289:                 if (keys(%haschgs) > 0) {
 9290:                     my $newacc = 
 9291:                         &Apache::lonnet::usertools_access($env{'user.name'},
 9292:                                                           $env{'user.domain'},
 9293:                                                           $item,'reload',$context);
 9294:                     if (($context eq 'requestcourses') ||
 9295:                         ($context eq 'requestauthor')) {
 9296:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 9297:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 9298:                         }
 9299:                     } else {
 9300:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 9301:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 9302:                         }
 9303:                     }
 9304:                     unless ($context eq 'requestauthor') {
 9305:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 9306:                     }
 9307:                     foreach my $type (@{$types},'default','_LC_adv') {
 9308:                         if ($haschgs{$type}) {
 9309:                             my $typetitle = $usertypes->{$type};
 9310:                             if ($type eq 'default') {
 9311:                                 $typetitle = $othertitle;
 9312:                             } elsif ($type eq '_LC_adv') {
 9313:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 9314:                             }
 9315:                             if ($inconf{$type}) {
 9316:                                 if ($context eq 'requestcourses') {
 9317:                                     my $cond;
 9318:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 9319:                                         if ($1 eq '') {
 9320:                                             $cond = &mt('(Automatic processing of any request).');
 9321:                                         } else {
 9322:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 9323:                                         }
 9324:                                     } else { 
 9325:                                         $cond = $conditions{$inconf{$type}};
 9326:                                     }
 9327:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 9328:                                 } elsif ($context eq 'requestauthor') {
 9329:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 9330:                                                              $titles{$inconf{$type}},$typetitle);
 9331: 
 9332:                                 } else {
 9333:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 9334:                                 }
 9335:                             } else {
 9336:                                 if ($type eq '_LC_adv') {
 9337:                                     if ($inconf{$type} eq '0') {
 9338:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9339:                                     } else { 
 9340:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 9341:                                     }
 9342:                                 } else {
 9343:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9344:                                 }
 9345:                             }
 9346:                         }
 9347:                     }
 9348:                     unless ($context eq 'requestauthor') {
 9349:                         $resulttext .= '</ul></li>';
 9350:                     }
 9351:                 }
 9352:             }
 9353:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 9354:                 if (ref($changes{'notify'}) eq 'HASH') {
 9355:                     if ($changes{'notify'}{'approval'}) {
 9356:                         if (ref($confhash{'notify'}) eq 'HASH') {
 9357:                             if ($confhash{'notify'}{'approval'}) {
 9358:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 9359:                             } else {
 9360:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 9361:                             }
 9362:                         }
 9363:                     }
 9364:                 }
 9365:             }
 9366:             if ($action eq 'requestcourses') {
 9367:                 my @offon = ('off','on');
 9368:                 if ($changes{'uniquecode'}) {
 9369:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 9370:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 9371:                         $resulttext .= '<li>'.
 9372:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 9373:                                        '</li>';
 9374:                     } else {
 9375:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 9376:                                        '</li>';
 9377:                     }
 9378:                 }
 9379:                 foreach my $type ('textbooks','templates') {
 9380:                     if (ref($changes{$type}) eq 'HASH') {
 9381:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 9382:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 9383:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 9384:                             my $coursetitle = $coursehash{'description'};
 9385:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 9386:                             $resulttext .= '<li>';
 9387:                             foreach my $item ('subject','title','publisher','author') {
 9388:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9389:                                          ($type eq 'templates'));
 9390:                                 my $name = $item.':';
 9391:                                 $name =~ s/^(\w)/\U$1/;
 9392:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 9393:                             }
 9394:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 9395:                             if ($type eq 'textbooks') {
 9396:                                 if ($confhash{$type}{$key}{'image'}) {
 9397:                                     $resulttext .= ' '.&mt('Image: [_1]',
 9398:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 9399:                                                    ' alt="Textbook cover" />').'<br />';
 9400:                                 }
 9401:                             }
 9402:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 9403:                         }
 9404:                         $resulttext .= '</ul></li>';
 9405:                     }
 9406:                 }
 9407:                 if (ref($changes{'validation'}) eq 'HASH') {
 9408:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 9409:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 9410:                         foreach my $item (@{$validationitemsref}) {
 9411:                             if (exists($changes{'validation'}{$item})) {
 9412:                                 if ($item eq 'markup') {
 9413:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9414:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 9415:                                 } else {
 9416:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9417:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 9418:                                 }
 9419:                             }
 9420:                         }
 9421:                         if (exists($changes{'validation'}{'dc'})) {
 9422:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 9423:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 9424:                         }
 9425:                     }
 9426:                 }
 9427:             }
 9428:             $resulttext .= '</ul>';
 9429:             if (keys(%newenv)) {
 9430:                 &Apache::lonnet::appenv(\%newenv);
 9431:             }
 9432:         } else {
 9433:             if ($context eq 'requestcourses') {
 9434:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 9435:             } elsif ($context eq 'requestauthor') {
 9436:                 $resulttext = &mt('No changes made to rights to request author space.');
 9437:             } else {
 9438:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 9439:             }
 9440:         }
 9441:     } else {
 9442:         $resulttext = '<span class="LC_error">'.
 9443: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9444:     }
 9445:     if ($errors) {
 9446:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 9447:                        '<ul>'.$errors.'</ul></p>';
 9448:     }
 9449:     return $resulttext;
 9450: }
 9451: 
 9452: sub process_textbook_image {
 9453:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 9454:     my $filename = $env{'form.'.$caller.'.filename'};
 9455:     my ($error,$url);
 9456:     my ($width,$height) = (50,50);
 9457:     if ($configuserok eq 'ok') {
 9458:         if ($switchserver) {
 9459:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 9460:                          $switchserver);
 9461:         } elsif ($author_ok eq 'ok') {
 9462:             my ($result,$imageurl) =
 9463:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 9464:                              "$type/$dom/$cnum/cover",$width,$height);
 9465:             if ($result eq 'ok') {
 9466:                 $url = $imageurl;
 9467:             } else {
 9468:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 9469:             }
 9470:         } else {
 9471:             $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);
 9472:         }
 9473:     } else {
 9474:         $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);
 9475:     }
 9476:     return ($url,$error);
 9477: }
 9478: 
 9479: sub modify_ltitools {
 9480:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 9481:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9482:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
 9483:     my $confname = $dom.'-domainconfig';
 9484:     my $servadm = $r->dir_config('lonAdmEMail');
 9485:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9486:     my (%posslti,%possfield);
 9487:     my @courseroles = ('cc','in','ta','ep','st');
 9488:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 9489:     map { $posslti{$_} = 1; } @ltiroles;
 9490:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
 9491:     map { $possfield{$_} = 1; } @allfields;
 9492:     my %lt = &ltitools_names(); 
 9493:     if ($env{'form.ltitools_add'}) {
 9494:         my $title = $env{'form.ltitools_add_title'};
 9495:         $title =~ s/(`)/'/g;
 9496:         ($newid,my $error) = &get_ltitools_id($dom,$title);
 9497:         if ($newid) {
 9498:             my $position = $env{'form.ltitools_add_pos'};
 9499:             $position =~ s/\D+//g;
 9500:             if ($position ne '') {
 9501:                 $allpos[$position] = $newid;
 9502:             }
 9503:             $changes{$newid} = 1;
 9504:             foreach my $item ('title','url','key','secret') {
 9505:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
 9506:                 if ($env{'form.ltitools_add_'.$item}) {
 9507:                     if (($item eq 'key') || ($item eq 'secret')) {
 9508:                         $encconfig{$newid}{$item} = $env{'form.ltitools_add_'.$item};
 9509:                     } else {
 9510:                         $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
 9511:                     }
 9512:                 }
 9513:             }
 9514:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
 9515:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
 9516:             }
 9517:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
 9518:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
 9519:             }
 9520:             foreach my $item ('width','height','linktext','explanation') {
 9521:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
 9522:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
 9523:                 if (($item eq 'width') || ($item eq 'height')) {
 9524:                     if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
 9525:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
 9526:                     }
 9527:                 } else {
 9528:                     if ($env{'form.ltitools_add_'.$item} ne '') {
 9529:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item}; 
 9530:                     }
 9531:                 }
 9532:             }
 9533:             if ($env{'form.ltitools_add_target'} eq 'window') {
 9534:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
 9535:             } else {
 9536:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
 9537:             }
 9538:             foreach my $item ('passback','roster') {
 9539:                 if ($env{'form.ltitools_add_'.$item}) {
 9540:                     $confhash{$newid}{$item} = 1;
 9541:                 }
 9542:             }
 9543:             if ($env{'form.ltitools_add_image.filename'} ne '') {
 9544:                 my ($imageurl,$error) =
 9545:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$dom,
 9546:                                             $configuserok,$switchserver,$author_ok);
 9547:                 if ($imageurl) {
 9548:                     $confhash{$newid}{'image'} = $imageurl;
 9549:                 }
 9550:                 if ($error) {
 9551:                     &Apache::lonnet::logthis($error);
 9552:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9553:                 }
 9554:             }
 9555:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
 9556:             foreach my $field (@fields) {
 9557:                 if ($possfield{$field}) {
 9558:                     if ($field eq 'roles') {
 9559:                         foreach my $role (@courseroles) {
 9560:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
 9561:                             if (($choice ne '') && ($posslti{$choice})) {
 9562:                                 $confhash{$newid}{'roles'}{$role} = $choice;
 9563:                                 if ($role eq 'cc') {
 9564:                                     $confhash{$newid}{'roles'}{'co'} = $choice; 
 9565:                                 }
 9566:                             }
 9567:                         }
 9568:                     } else {
 9569:                         $confhash{$newid}{'fields'}{$field} = 1;
 9570:                     }
 9571:                 }
 9572:             }
 9573:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
 9574:             foreach my $item (@courseconfig) {
 9575:                 $confhash{$newid}{'crsconf'}{$item} = 1;
 9576:             }
 9577:             if ($env{'form.ltitools_add_custom'}) {
 9578:                 my $name = $env{'form.ltitools_add_custom_name'};
 9579:                 my $value = $env{'form.ltitools_add_custom_value'};
 9580:                 $value =~ s/(`)/'/g;
 9581:                 $name =~ s/(`)/'/g;
 9582:                 $confhash{$newid}{'custom'}{$name} = $value;
 9583:             }
 9584:         } else {
 9585:             my $error = &mt('Failed to acquire unique ID for new external tool');   
 9586:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9587:         }
 9588:     }
 9589:     if (ref($domconfig{$action}) eq 'HASH') {
 9590:         my %deletions;
 9591:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
 9592:         if (@todelete) {
 9593:             map { $deletions{$_} = 1; } @todelete;
 9594:         }
 9595:         my %customadds;
 9596:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
 9597:         if (@newcustom) {
 9598:             map { $customadds{$_} = 1; } @newcustom;
 9599:         } 
 9600:         my %imgdeletions;
 9601:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
 9602:         if (@todeleteimages) {
 9603:             map { $imgdeletions{$_} = 1; } @todeleteimages;
 9604:         }
 9605:         my $maxnum = $env{'form.ltitools_maxnum'};
 9606:         for (my $i=0; $i<=$maxnum; $i++) {
 9607:             my $itemid = $env{'form.ltitools_id_'.$i};
 9608:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
 9609:                 if ($deletions{$itemid}) {
 9610:                     if ($domconfig{$action}{$itemid}{'image'}) {
 9611:                         #FIXME need to obsolete item in RES space
 9612:                     }
 9613:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
 9614:                     next;
 9615:                 } else {
 9616:                     my $newpos = $env{'form.ltitools_'.$itemid};
 9617:                     $newpos =~ s/\D+//g;
 9618:                     foreach my $item ('title','url') {
 9619:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
 9620:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
 9621:                             $changes{$itemid} = 1;
 9622:                         }
 9623:                     }
 9624:                     foreach my $item ('key','secret') {
 9625:                         $encconfig{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
 9626:                         if ($domconfig{$action}{$itemid}{$item} ne $encconfig{$itemid}{$item}) {
 9627:                             $changes{$itemid} = 1;
 9628:                         }
 9629:                     }
 9630:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
 9631:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
 9632:                     }
 9633:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
 9634:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
 9635:                     }
 9636:                     foreach my $size ('width','height') {
 9637:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
 9638:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
 9639:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
 9640:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
 9641:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9642:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
 9643:                                     $changes{$itemid} = 1;
 9644:                                 }
 9645:                             } else {
 9646:                                 $changes{$itemid} = 1;
 9647:                             }
 9648:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9649:                             if ($domconfig{$action}{$itemid}{'display'}{$size} ne '') {
 9650:                                 $changes{$itemid} = 1;
 9651:                             }
 9652:                         }
 9653:                     }
 9654:                     foreach my $item ('linktext','explanation') {
 9655:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/^\s+//;
 9656:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/\s+$//;
 9657:                         if ($env{'form.ltitools_'.$item.'_'.$i} ne '') {
 9658:                             $confhash{$itemid}{'display'}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
 9659:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9660:                                 if ($domconfig{$action}{$itemid}{'display'}{$item} ne $confhash{$itemid}{'display'}{$item}) {
 9661:                                     $changes{$itemid} = 1;
 9662:                                 }
 9663:                             } else {
 9664:                                 $changes{$itemid} = 1;
 9665:                             }
 9666:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9667:                             if ($domconfig{$action}{$itemid}{'display'}{$item} ne '') {
 9668:                                 $changes{$itemid} = 1;
 9669:                             }
 9670:                         }
 9671:                     }
 9672:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
 9673:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
 9674:                     } else {
 9675:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
 9676:                     }
 9677:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9678:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
 9679:                             $changes{$itemid} = 1;
 9680:                         }
 9681:                     } else {
 9682:                         $changes{$itemid} = 1;
 9683:                     }
 9684:                     foreach my $extra ('passback','roster') {
 9685:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
 9686:                             $confhash{$itemid}{$extra} = 1;
 9687:                         }
 9688:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
 9689:                             $changes{$itemid} = 1;
 9690:                         }
 9691:                     }
 9692:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
 9693:                     foreach my $item ('label','title','target','linktext','explanation') {
 9694:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
 9695:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
 9696:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
 9697:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
 9698:                                     $changes{$itemid} = 1;
 9699:                                 }
 9700:                             } else {
 9701:                                 $changes{$itemid} = 1;
 9702:                             }
 9703:                         }
 9704:                     }
 9705:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
 9706:                     foreach my $field (@fields) {
 9707:                         if ($possfield{$field}) {
 9708:                             if ($field eq 'roles') {
 9709:                                 foreach my $role (@courseroles) {
 9710:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
 9711:                                     if (($choice ne '') && ($posslti{$choice})) {
 9712:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
 9713:                                         if ($role eq 'cc') {
 9714:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
 9715:                                         }
 9716:                                     }
 9717:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
 9718:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
 9719:                                             $changes{$itemid} = 1;
 9720:                                         }
 9721:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
 9722:                                         $changes{$itemid} = 1;
 9723:                                     }
 9724:                                 }
 9725:                             } else {
 9726:                                 $confhash{$itemid}{'fields'}{$field} = 1;
 9727:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
 9728:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
 9729:                                         $changes{$itemid} = 1;
 9730:                                     }
 9731:                                 } else {
 9732:                                     $changes{$itemid} = 1;
 9733:                                 }
 9734:                             }
 9735:                         }
 9736:                     }
 9737:                     $allpos[$newpos] = $itemid;
 9738:                 }
 9739:                 if ($imgdeletions{$itemid}) {
 9740:                     $changes{$itemid} = 1;
 9741:                     #FIXME need to obsolete item in RES space
 9742:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
 9743:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
 9744:                                                                  $itemid,$configuserok,$switchserver,
 9745:                                                                  $author_ok);
 9746:                     if ($imgurl) {
 9747:                         $confhash{$itemid}{'image'} = $imgurl;
 9748:                         $changes{$itemid} = 1;
 9749:                     }
 9750:                     if ($error) {
 9751:                         &Apache::lonnet::logthis($error);
 9752:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9753:                     }
 9754:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
 9755:                     $confhash{$itemid}{'image'} =
 9756:                        $domconfig{$action}{$itemid}{'image'};
 9757:                 }
 9758:                 if ($customadds{$i}) {
 9759:                     my $name = $env{'form.ltitools_custom_name_'.$i};
 9760:                     $name =~ s/(`)/'/g;
 9761:                     $name =~ s/^\s+//;
 9762:                     $name =~ s/\s+$//;
 9763:                     my $value = $env{'form.ltitools_custom_value_'.$i};
 9764:                     $value =~ s/(`)/'/g;
 9765:                     $value =~ s/^\s+//;
 9766:                     $value =~ s/\s+$//;
 9767:                     if ($name ne '') {
 9768:                         $confhash{$itemid}{'custom'}{$name} = $value;
 9769:                         $changes{$itemid} = 1;
 9770:                     }
 9771:                 }
 9772:                 my %customdels;
 9773:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i); 
 9774:                 if (@customdeletions) {
 9775:                     $changes{$itemid} = 1;
 9776:                 }
 9777:                 map { $customdels{$_} = 1; } @customdeletions;
 9778:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
 9779:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
 9780:                         unless ($customdels{$key}) {
 9781:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
 9782:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i}; 
 9783:                             }
 9784:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
 9785:                                 $changes{$itemid} = 1;
 9786:                             }
 9787:                         }
 9788:                     }
 9789:                 }
 9790:                 unless ($changes{$itemid}) {
 9791:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
 9792:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
 9793:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
 9794:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
 9795:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
 9796:                                         $changes{$itemid} = 1;
 9797:                                         last;
 9798:                                     }
 9799:                                 }
 9800:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
 9801:                                 $changes{$itemid} = 1;
 9802:                             }
 9803:                         }
 9804:                         last if ($changes{$itemid});
 9805:                     }
 9806:                 }
 9807:             }
 9808:         }
 9809:     }
 9810:     if (@allpos > 0) {
 9811:         my $idx = 0;
 9812:         foreach my $itemid (@allpos) {
 9813:             if ($itemid ne '') {
 9814:                 $confhash{$itemid}{'order'} = $idx;
 9815:                 if (ref($domconfig{$action}) eq 'HASH') {
 9816:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
 9817:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
 9818:                             $changes{$itemid} = 1;
 9819:                         }
 9820:                     }
 9821:                 }
 9822:                 $idx ++;
 9823:             }
 9824:         }
 9825:     }
 9826:     my %ltitoolshash = (
 9827:                           $action => { %confhash }
 9828:                        );
 9829:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
 9830:                                              $dom);
 9831:     if ($putresult eq 'ok') {
 9832:         my %ltienchash = (
 9833:                              $action => { %encconfig }
 9834:                          );
 9835:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
 9836:         if (keys(%changes) > 0) {
 9837:             my $cachetime = 24*60*60;
 9838:             my %ltiall = %confhash;
 9839:             foreach my $id (keys(%ltiall)) {
 9840:                 if (ref($encconfig{$id}) eq 'HASH') {
 9841:                     foreach my $item ('key','secret') {
 9842:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
 9843:                     }
 9844:                 }
 9845:             }
 9846:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%ltiall,$cachetime);
 9847:             if (ref($lastactref) eq 'HASH') {
 9848:                 $lastactref->{'ltitools'} = 1;
 9849:             }
 9850:             $resulttext = &mt('Changes made:').'<ul>';
 9851:             my %bynum;
 9852:             foreach my $itemid (sort(keys(%changes))) {
 9853:                 my $position = $confhash{$itemid}{'order'};
 9854:                 $bynum{$position} = $itemid;
 9855:             }
 9856:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
 9857:                 my $itemid = $bynum{$pos}; 
 9858:                 if (ref($confhash{$itemid}) ne 'HASH') {
 9859:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
 9860:                 } else {
 9861:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
 9862:                     if ($confhash{$itemid}{'image'}) {
 9863:                         $resulttext .= '&nbsp;'.
 9864:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
 9865:                                        ' alt="'.&mt('Tool Provider icon').'" />';
 9866:                     }
 9867:                     $resulttext .= '</li><ul>';
 9868:                     my $position = $pos + 1;
 9869:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
 9870:                     foreach my $item ('version','msgtype','url') {
 9871:                         if ($confhash{$itemid}{$item} ne '') {
 9872:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
 9873:                         }
 9874:                     }
 9875:                     if ($encconfig{$itemid}{'key'} ne '') {
 9876:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
 9877:                     }
 9878:                     if ($encconfig{$itemid}{'secret'} ne '') {
 9879:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
 9880:                         my $num = length($encconfig{$itemid}{'secret'});
 9881:                         $resulttext .= ('*'x$num).'</li>';
 9882:                     }
 9883:                     $resulttext .= '<li>'.&mt('Configurable in course:');
 9884:                     my @possconfig = ('label','title','target','linktext','explanation');
 9885:                     my $numconfig = 0; 
 9886:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') { 
 9887:                         foreach my $item (@possconfig) {
 9888:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
 9889:                                 $numconfig ++;
 9890:                                 $resulttext .= ' "'.$lt{'crs'.$item}.'"';
 9891:                             }
 9892:                         }
 9893:                     }
 9894:                     if (!$numconfig) {
 9895:                         $resulttext .= &mt('None');
 9896:                     }
 9897:                     $resulttext .= '</li>';
 9898:                     foreach my $item ('passback','roster') {
 9899:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
 9900:                         if ($confhash{$itemid}{$item}) {
 9901:                             $resulttext .= &mt('Yes');
 9902:                         } else {
 9903:                             $resulttext .= &mt('No');
 9904:                         }
 9905:                         $resulttext .= '</li>';
 9906:                     }
 9907:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
 9908:                         my $displaylist;
 9909:                         if ($confhash{$itemid}{'display'}{'target'}) {
 9910:                             $displaylist = &mt('Display target').':&nbsp;'.
 9911:                                            $confhash{$itemid}{'display'}{'target'}.',';
 9912:                         }
 9913:                         foreach my $size ('width','height') { 
 9914:                             if ($confhash{$itemid}{'display'}{$size}) {
 9915:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
 9916:                                                 $confhash{$itemid}{'display'}{$size}.',';
 9917:                             }
 9918:                         }
 9919:                         if ($displaylist) {
 9920:                             $displaylist =~ s/,$//;
 9921:                             $resulttext .= '<li>'.$displaylist.'</li>';
 9922:                         }
 9923:                         foreach my $item ('linktext','explanation') {
 9924:                             if ($confhash{$itemid}{'display'}{$item}) {
 9925:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{'display'}{$item}.'</li>';
 9926:                             }
 9927:                         }
 9928:                     }
 9929:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
 9930:                         my $fieldlist;
 9931:                         foreach my $field (@allfields) {
 9932:                             if ($confhash{$itemid}{'fields'}{$field}) {
 9933:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
 9934:                             }
 9935:                         }
 9936:                         if ($fieldlist) {
 9937:                             $fieldlist =~ s/,$//;
 9938:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
 9939:                         }
 9940:                     }
 9941:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
 9942:                         my $rolemaps;
 9943:                         foreach my $role (@courseroles) {
 9944:                             if ($confhash{$itemid}{'roles'}{$role}) {
 9945:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
 9946:                                              $confhash{$itemid}{'roles'}{$role}.',';
 9947:                             }
 9948:                         }
 9949:                         if ($rolemaps) {
 9950:                             $rolemaps =~ s/,$//; 
 9951:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
 9952:                         }
 9953:                     }
 9954:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
 9955:                         my $customlist;
 9956:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
 9957:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
 9958:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
 9959:                             } 
 9960:                         }
 9961:                         if ($customlist) {
 9962:                             $resulttext .= '<li>'.&mt('Custom items').':'.$customlist.'</li>';
 9963:                         }
 9964:                     } 
 9965:                     $resulttext .= '</ul></li>';
 9966:                 }
 9967:             }
 9968:             $resulttext .= '</ul>';
 9969:         } else {
 9970:             $resulttext = &mt('No changes made.');
 9971:         }
 9972:     } else {
 9973:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
 9974:     }
 9975:     if ($errors) {
 9976:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9977:                        $errors.'</ul>';
 9978:     }
 9979:     return $resulttext;
 9980: }
 9981: 
 9982: sub process_ltitools_image {
 9983:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
 9984:     my $filename = $env{'form.'.$caller.'.filename'};
 9985:     my ($error,$url);
 9986:     my ($width,$height) = (21,21);
 9987:     if ($configuserok eq 'ok') {
 9988:         if ($switchserver) {
 9989:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
 9990:                          $switchserver);
 9991:         } elsif ($author_ok eq 'ok') {
 9992:             my ($result,$imageurl,$madethumb) =
 9993:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 9994:                              "ltitools/$itemid/icon",$width,$height);
 9995:             if ($result eq 'ok') {
 9996:                 if ($madethumb) {
 9997:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
 9998:                     my $imagethumb = "$path/tn-".$imagefile;
 9999:                     $url = $imagethumb;
10000:                 } else {
10001:                     $url = $imageurl;
10002:                 }
10003:             } else {
10004:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
10005:             }
10006:         } else {
10007:             $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);
10008:         }
10009:     } else {
10010:         $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);
10011:     }
10012:     return ($url,$error);
10013: }
10014: 
10015: sub get_ltitools_id {
10016:     my ($cdom,$title) = @_;
10017:     # get lock on ltitools db
10018:     my $lockhash = {
10019:                       lock => $env{'user.name'}.
10020:                               ':'.$env{'user.domain'},
10021:                    };
10022:     my $tries = 0;
10023:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
10024:     my ($id,$error);
10025:  
10026:     while (($gotlock ne 'ok') && ($tries<10)) {
10027:         $tries ++;
10028:         sleep (0.1);
10029:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
10030:     }
10031:     if ($gotlock eq 'ok') {
10032:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
10033:         if ($currids{'lock'}) {
10034:             delete($currids{'lock'});
10035:             if (keys(%currids)) {
10036:                 my @curr = sort { $a <=> $b } keys(%currids);
10037:                 if ($curr[-1] =~ /^\d+$/) {
10038:                     $id = 1 + $curr[-1];
10039:                 }
10040:             } else {
10041:                 $id = 1;
10042:             }
10043:             if ($id) {
10044:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
10045:                     $error = 'nostore';
10046:                 }
10047:             } else {
10048:                 $error = 'nonumber';
10049:             }
10050:         }
10051:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
10052:     } else {
10053:         $error = 'nolock';
10054:     }
10055:     return ($id,$error);
10056: }
10057: 
10058: sub modify_autoenroll {
10059:     my ($dom,$lastactref,%domconfig) = @_;
10060:     my ($resulttext,%changes);
10061:     my %currautoenroll;
10062:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
10063:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
10064:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
10065:         }
10066:     }
10067:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
10068:     my %title = ( run => 'Auto-enrollment active',
10069:                   sender => 'Sender for notification messages',
10070:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
10071:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
10072:     my @offon = ('off','on');
10073:     my $sender_uname = $env{'form.sender_uname'};
10074:     my $sender_domain = $env{'form.sender_domain'};
10075:     if ($sender_domain eq '') {
10076:         $sender_uname = '';
10077:     } elsif ($sender_uname eq '') {
10078:         $sender_domain = '';
10079:     }
10080:     my $coowners = $env{'form.autoassign_coowners'};
10081:     my $failsafe = $env{'form.autoenroll_failsafe'};
10082:     $failsafe =~ s{^\s+|\s+$}{}g;
10083:     if ($failsafe =~ /\D/) {
10084:         undef($failsafe);
10085:     }
10086:     my %autoenrollhash =  (
10087:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
10088:                                        'sender_uname' => $sender_uname,
10089:                                        'sender_domain' => $sender_domain,
10090:                                        'co-owners' => $coowners,
10091:                                        'autofailsafe' => $failsafe,
10092:                                 }
10093:                      );
10094:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
10095:                                              $dom);
10096:     if ($putresult eq 'ok') {
10097:         if (exists($currautoenroll{'run'})) {
10098:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
10099:                  $changes{'run'} = 1;
10100:              }
10101:         } elsif ($autorun) {
10102:             if ($env{'form.autoenroll_run'} ne '1') {
10103:                  $changes{'run'} = 1;
10104:             }
10105:         }
10106:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
10107:             $changes{'sender'} = 1;
10108:         }
10109:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
10110:             $changes{'sender'} = 1;
10111:         }
10112:         if ($currautoenroll{'co-owners'} ne '') {
10113:             if ($currautoenroll{'co-owners'} ne $coowners) {
10114:                 $changes{'coowners'} = 1;
10115:             }
10116:         } elsif ($coowners) {
10117:             $changes{'coowners'} = 1;
10118:         }
10119:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
10120:             $changes{'autofailsafe'} = 1;
10121:         }
10122:         if (keys(%changes) > 0) {
10123:             $resulttext = &mt('Changes made:').'<ul>';
10124:             if ($changes{'run'}) {
10125:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
10126:             }
10127:             if ($changes{'sender'}) {
10128:                 if ($sender_uname eq '' || $sender_domain eq '') {
10129:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
10130:                 } else {
10131:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
10132:                 }
10133:             }
10134:             if ($changes{'coowners'}) {
10135:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
10136:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
10137:                 if (ref($lastactref) eq 'HASH') {
10138:                     $lastactref->{'domainconfig'} = 1;
10139:                 }
10140:             }
10141:             if ($changes{'autofailsafe'}) {
10142:                 if ($failsafe ne '') {
10143:                     $resulttext .= '<li>'.&mt("$title{'failsafe'} set to [_1]",$failsafe).'</li>';
10144:                 } else {
10145:                     $resulttext .= '<li>'.&mt("$title{'failsafe'} deleted");
10146:                 }
10147:                 &Apache::lonnet::get_domain_defaults($dom,1);
10148:                 if (ref($lastactref) eq 'HASH') {
10149:                     $lastactref->{'domdefaults'} = 1;
10150:                 }
10151:             }
10152:             $resulttext .= '</ul>';
10153:         } else {
10154:             $resulttext = &mt('No changes made to auto-enrollment settings');
10155:         }
10156:     } else {
10157:         $resulttext = '<span class="LC_error">'.
10158: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10159:     }
10160:     return $resulttext;
10161: }
10162: 
10163: sub modify_autoupdate {
10164:     my ($dom,%domconfig) = @_;
10165:     my ($resulttext,%currautoupdate,%fields,%changes);
10166:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
10167:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
10168:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
10169:         }
10170:     }
10171:     my @offon = ('off','on');
10172:     my %title = &Apache::lonlocal::texthash (
10173:                    run => 'Auto-update:',
10174:                    classlists => 'Updates to user information in classlists?'
10175:                 );
10176:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10177:     my %fieldtitles = &Apache::lonlocal::texthash (
10178:                         id => 'Student/Employee ID',
10179:                         permanentemail => 'E-mail address',
10180:                         lastname => 'Last Name',
10181:                         firstname => 'First Name',
10182:                         middlename => 'Middle Name',
10183:                         generation => 'Generation',
10184:                       );
10185:     $othertitle = &mt('All users');
10186:     if (keys(%{$usertypes}) >  0) {
10187:         $othertitle = &mt('Other users');
10188:     }
10189:     foreach my $key (keys(%env)) {
10190:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
10191:             my ($usertype,$item) = ($1,$2);
10192:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
10193:                 if ($usertype eq 'default') {   
10194:                     push(@{$fields{$1}},$2);
10195:                 } elsif (ref($types) eq 'ARRAY') {
10196:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
10197:                         push(@{$fields{$1}},$2);
10198:                     }
10199:                 }
10200:             }
10201:         }
10202:     }
10203:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
10204:     @lockablenames = sort(@lockablenames);
10205:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
10206:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
10207:         if (@changed) {
10208:             $changes{'lockablenames'} = 1;
10209:         }
10210:     } else {
10211:         if (@lockablenames) {
10212:             $changes{'lockablenames'} = 1;
10213:         }
10214:     }
10215:     my %updatehash = (
10216:                       autoupdate => { run => $env{'form.autoupdate_run'},
10217:                                       classlists => $env{'form.classlists'},
10218:                                       fields => {%fields},
10219:                                       lockablenames => \@lockablenames,
10220:                                     }
10221:                      );
10222:     foreach my $key (keys(%currautoupdate)) {
10223:         if (($key eq 'run') || ($key eq 'classlists')) {
10224:             if (exists($updatehash{autoupdate}{$key})) {
10225:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
10226:                     $changes{$key} = 1;
10227:                 }
10228:             }
10229:         } elsif ($key eq 'fields') {
10230:             if (ref($currautoupdate{$key}) eq 'HASH') {
10231:                 foreach my $item (@{$types},'default') {
10232:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
10233:                         my $change = 0;
10234:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
10235:                             if (!exists($fields{$item})) {
10236:                                 $change = 1;
10237:                                 last;
10238:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
10239:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
10240:                                     $change = 1;
10241:                                     last;
10242:                                 }
10243:                             }
10244:                         }
10245:                         if ($change) {
10246:                             push(@{$changes{$key}},$item);
10247:                         }
10248:                     } 
10249:                 }
10250:             }
10251:         } elsif ($key eq 'lockablenames') {
10252:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
10253:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
10254:                 if (@changed) {
10255:                     $changes{'lockablenames'} = 1;
10256:                 }
10257:             } else {
10258:                 if (@lockablenames) {
10259:                     $changes{'lockablenames'} = 1;
10260:                 }
10261:             }
10262:         }
10263:     }
10264:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
10265:         if (@lockablenames) {
10266:             $changes{'lockablenames'} = 1;
10267:         }
10268:     }
10269:     foreach my $item (@{$types},'default') {
10270:         if (defined($fields{$item})) {
10271:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
10272:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
10273:                     my $change = 0;
10274:                     if (ref($fields{$item}) eq 'ARRAY') {
10275:                         foreach my $type (@{$fields{$item}}) {
10276:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
10277:                                 $change = 1;
10278:                                 last;
10279:                             }
10280:                         }
10281:                     }
10282:                     if ($change) {
10283:                         push(@{$changes{'fields'}},$item);
10284:                     }
10285:                 } else {
10286:                     push(@{$changes{'fields'}},$item);
10287:                 }
10288:             } else {
10289:                 push(@{$changes{'fields'}},$item);
10290:             }
10291:         }
10292:     }
10293:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
10294:                                              $dom);
10295:     if ($putresult eq 'ok') {
10296:         if (keys(%changes) > 0) {
10297:             $resulttext = &mt('Changes made:').'<ul>';
10298:             foreach my $key (sort(keys(%changes))) {
10299:                 if ($key eq 'lockablenames') {
10300:                     $resulttext .= '<li>';
10301:                     if (@lockablenames) {
10302:                         $usertypes->{'default'} = $othertitle;
10303:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
10304:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
10305:                     } else {
10306:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
10307:                     }
10308:                     $resulttext .= '</li>';
10309:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
10310:                     foreach my $item (@{$changes{$key}}) {
10311:                         my @newvalues;
10312:                         foreach my $type (@{$fields{$item}}) {
10313:                             push(@newvalues,$fieldtitles{$type});
10314:                         }
10315:                         my $newvaluestr;
10316:                         if (@newvalues > 0) {
10317:                             $newvaluestr = join(', ',@newvalues);
10318:                         } else {
10319:                             $newvaluestr = &mt('none');
10320:                         }
10321:                         if ($item eq 'default') {
10322:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
10323:                         } else {
10324:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
10325:                         }
10326:                     }
10327:                 } else {
10328:                     my $newvalue;
10329:                     if ($key eq 'run') {
10330:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
10331:                     } else {
10332:                         $newvalue = $offon[$env{'form.'.$key}];
10333:                     }
10334:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
10335:                 }
10336:             }
10337:             $resulttext .= '</ul>';
10338:         } else {
10339:             $resulttext = &mt('No changes made to autoupdates');
10340:         }
10341:     } else {
10342:         $resulttext = '<span class="LC_error">'.
10343: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10344:     }
10345:     return $resulttext;
10346: }
10347: 
10348: sub modify_autocreate {
10349:     my ($dom,%domconfig) = @_;
10350:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
10351:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
10352:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
10353:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
10354:         }
10355:     }
10356:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
10357:                  req => 'Auto-creation of validated requests for official courses',
10358:                  xmldc => 'Identity of course creator of courses from XML files',
10359:                );
10360:     my @types = ('xml','req');
10361:     foreach my $item (@types) {
10362:         $newvals{$item} = $env{'form.autocreate_'.$item};
10363:         $newvals{$item} =~ s/\D//g;
10364:         $newvals{$item} = 0 if ($newvals{$item} eq '');
10365:     }
10366:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
10367:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
10368:     unless (exists($domcoords{$newvals{'xmldc'}})) {
10369:         $newvals{'xmldc'} = '';
10370:     } 
10371:     %autocreatehash =  (
10372:                         autocreate => { xml => $newvals{'xml'},
10373:                                         req => $newvals{'req'},
10374:                                       }
10375:                        );
10376:     if ($newvals{'xmldc'} ne '') {
10377:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
10378:     }
10379:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
10380:                                              $dom);
10381:     if ($putresult eq 'ok') {
10382:         my @items = @types;
10383:         if ($newvals{'xml'}) {
10384:             push(@items,'xmldc');
10385:         }
10386:         foreach my $item (@items) {
10387:             if (exists($currautocreate{$item})) {
10388:                 if ($currautocreate{$item} ne $newvals{$item}) {
10389:                     $changes{$item} = 1;
10390:                 }
10391:             } elsif ($newvals{$item}) {
10392:                 $changes{$item} = 1;
10393:             }
10394:         }
10395:         if (keys(%changes) > 0) {
10396:             my @offon = ('off','on'); 
10397:             $resulttext = &mt('Changes made:').'<ul>';
10398:             foreach my $item (@types) {
10399:                 if ($changes{$item}) {
10400:                     my $newtxt = $offon[$newvals{$item}];
10401:                     $resulttext .= '<li>'.
10402:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
10403:                                        '<b>','</b>').
10404:                                    '</li>';
10405:                 }
10406:             }
10407:             if ($changes{'xmldc'}) {
10408:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
10409:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
10410:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
10411:             }
10412:             $resulttext .= '</ul>';
10413:         } else {
10414:             $resulttext = &mt('No changes made to auto-creation settings');
10415:         }
10416:     } else {
10417:         $resulttext = '<span class="LC_error">'.
10418:             &mt('An error occurred: [_1]',$putresult).'</span>';
10419:     }
10420:     return $resulttext;
10421: }
10422: 
10423: sub modify_directorysrch {
10424:     my ($dom,$lastactref,%domconfig) = @_;
10425:     my ($resulttext,%changes);
10426:     my %currdirsrch;
10427:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
10428:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
10429:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
10430:         }
10431:     }
10432:     my %title = ( available => 'Institutional directory search available',
10433:                   localonly => 'Other domains can search institution',
10434:                   lcavailable => 'LON-CAPA directory search available',
10435:                   lclocalonly => 'Other domains can search LON-CAPA domain',
10436:                   searchby => 'Search types',
10437:                   searchtypes => 'Search latitude');
10438:     my @offon = ('off','on');
10439:     my @otherdoms = ('Yes','No');
10440: 
10441:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
10442:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
10443:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
10444: 
10445:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10446:     if (keys(%{$usertypes}) == 0) {
10447:         @cansearch = ('default');
10448:     } else {
10449:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
10450:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
10451:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
10452:                     push(@{$changes{'cansearch'}},$type);
10453:                 }
10454:             }
10455:             foreach my $type (@cansearch) {
10456:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
10457:                     push(@{$changes{'cansearch'}},$type);
10458:                 }
10459:             }
10460:         } else {
10461:             push(@{$changes{'cansearch'}},@cansearch);
10462:         }
10463:     }
10464: 
10465:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
10466:         foreach my $by (@{$currdirsrch{'searchby'}}) {
10467:             if (!grep(/^\Q$by\E$/,@searchby)) {
10468:                 push(@{$changes{'searchby'}},$by);
10469:             }
10470:         }
10471:         foreach my $by (@searchby) {
10472:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
10473:                 push(@{$changes{'searchby'}},$by);
10474:             }
10475:         }
10476:     } else {
10477:         push(@{$changes{'searchby'}},@searchby);
10478:     }
10479: 
10480:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
10481:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
10482:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
10483:                 push(@{$changes{'searchtypes'}},$type);
10484:             }
10485:         }
10486:         foreach my $type (@searchtypes) {
10487:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
10488:                 push(@{$changes{'searchtypes'}},$type);
10489:             }
10490:         }
10491:     } else {
10492:         if (exists($currdirsrch{'searchtypes'})) {
10493:             foreach my $type (@searchtypes) {  
10494:                 if ($type ne $currdirsrch{'searchtypes'}) { 
10495:                     push(@{$changes{'searchtypes'}},$type);
10496:                 }
10497:             }
10498:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
10499:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
10500:             }   
10501:         } else {
10502:             push(@{$changes{'searchtypes'}},@searchtypes); 
10503:         }
10504:     }
10505: 
10506:     my %dirsrch_hash =  (
10507:             directorysrch => { available => $env{'form.dirsrch_available'},
10508:                                cansearch => \@cansearch,
10509:                                localonly => $env{'form.dirsrch_instlocalonly'},
10510:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
10511:                                lcavailable => $env{'form.dirsrch_domavailable'},
10512:                                searchby => \@searchby,
10513:                                searchtypes => \@searchtypes,
10514:                              }
10515:             );
10516:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
10517:                                              $dom);
10518:     if ($putresult eq 'ok') {
10519:         if (exists($currdirsrch{'available'})) {
10520:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
10521:                  $changes{'available'} = 1;
10522:              }
10523:         } else {
10524:             if ($env{'form.dirsrch_available'} eq '1') {
10525:                 $changes{'available'} = 1;
10526:             }
10527:         }
10528:         if (exists($currdirsrch{'lcavailable'})) {
10529:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
10530:                 $changes{'lcavailable'} = 1;
10531:             }
10532:         } else {
10533:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
10534:                 $changes{'lcavailable'} = 1;
10535:             }
10536:         }
10537:         if (exists($currdirsrch{'localonly'})) {
10538:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
10539:                 $changes{'localonly'} = 1;
10540:             }
10541:         } else {
10542:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
10543:                 $changes{'localonly'} = 1;
10544:             }
10545:         }
10546:         if (exists($currdirsrch{'lclocalonly'})) {
10547:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
10548:                 $changes{'lclocalonly'} = 1;
10549:             }
10550:         } else {
10551:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
10552:                 $changes{'lclocalonly'} = 1;
10553:             }
10554:         }
10555:         if (keys(%changes) > 0) {
10556:             $resulttext = &mt('Changes made:').'<ul>';
10557:             if ($changes{'available'}) {
10558:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
10559:             }
10560:             if ($changes{'lcavailable'}) {
10561:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
10562:             }
10563:             if ($changes{'localonly'}) {
10564:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
10565:             }
10566:             if ($changes{'lclocalonly'}) {
10567:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
10568:             }
10569:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
10570:                 my $chgtext;
10571:                 if (ref($usertypes) eq 'HASH') {
10572:                     if (keys(%{$usertypes}) > 0) {
10573:                         foreach my $type (@{$types}) {
10574:                             if (grep(/^\Q$type\E$/,@cansearch)) {
10575:                                 $chgtext .= $usertypes->{$type}.'; ';
10576:                             }
10577:                         }
10578:                         if (grep(/^default$/,@cansearch)) {
10579:                             $chgtext .= $othertitle;
10580:                         } else {
10581:                             $chgtext =~ s/\; $//;
10582:                         }
10583:                         $resulttext .=
10584:                             '<li>'.
10585:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
10586:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
10587:                             '</li>';
10588:                     }
10589:                 }
10590:             }
10591:             if (ref($changes{'searchby'}) eq 'ARRAY') {
10592:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
10593:                 my $chgtext;
10594:                 foreach my $type (@{$titleorder}) {
10595:                     if (grep(/^\Q$type\E$/,@searchby)) {
10596:                         if (defined($searchtitles->{$type})) {
10597:                             $chgtext .= $searchtitles->{$type}.'; ';
10598:                         }
10599:                     }
10600:                 }
10601:                 $chgtext =~ s/\; $//;
10602:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
10603:             }
10604:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
10605:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
10606:                 my $chgtext;
10607:                 foreach my $type (@{$srchtypeorder}) {
10608:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
10609:                         if (defined($srchtypes_desc->{$type})) {
10610:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
10611:                         }
10612:                     }
10613:                 }
10614:                 $chgtext =~ s/\; $//;
10615:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
10616:             }
10617:             $resulttext .= '</ul>';
10618:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
10619:             if (ref($lastactref) eq 'HASH') {
10620:                 $lastactref->{'directorysrch'} = 1;
10621:             }
10622:         } else {
10623:             $resulttext = &mt('No changes made to directory search settings');
10624:         }
10625:     } else {
10626:         $resulttext = '<span class="LC_error">'.
10627:                       &mt('An error occurred: [_1]',$putresult).'</span>';
10628:     }
10629:     return $resulttext;
10630: }
10631: 
10632: sub modify_contacts {
10633:     my ($dom,$lastactref,%domconfig) = @_;
10634:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
10635:     if (ref($domconfig{'contacts'}) eq 'HASH') {
10636:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
10637:             $currsetting{$key} = $domconfig{'contacts'}{$key};
10638:         }
10639:     }
10640:     my (%others,%to,%bcc,%includestr,%includeloc);
10641:     my @contacts = ('supportemail','adminemail');
10642:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
10643:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail');
10644:     my @toggles = ('reporterrors','reportupdates');
10645:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
10646:     foreach my $type (@mailings) {
10647:         @{$newsetting{$type}} = 
10648:             &Apache::loncommon::get_env_multiple('form.'.$type);
10649:         foreach my $item (@contacts) {
10650:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
10651:                 $contacts_hash{contacts}{$type}{$item} = 1;
10652:             } else {
10653:                 $contacts_hash{contacts}{$type}{$item} = 0;
10654:             }
10655:         }
10656:         $others{$type} = $env{'form.'.$type.'_others'};
10657:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
10658:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10659:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
10660:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
10661:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
10662:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
10663:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
10664:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
10665:             }
10666:         }
10667:     }
10668:     foreach my $item (@contacts) {
10669:         $to{$item} = $env{'form.'.$item};
10670:         $contacts_hash{'contacts'}{$item} = $to{$item};
10671:     }
10672:     foreach my $item (@toggles) {
10673:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
10674:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
10675:         }
10676:     }
10677:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
10678:         foreach my $field (@{$fields}) {
10679:             if (ref($possoptions->{$field}) eq 'ARRAY') {
10680:                 my $value = $env{'form.helpform_'.$field};
10681:                 $value =~ s/^\s+|\s+$//g;
10682:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
10683:                     $contacts_hash{contacts}{'helpform'}{$field} = $value;
10684:                     if ($field eq 'screenshot') {
10685:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
10686:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
10687:                             $contacts_hash{contacts}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
10688:                         }
10689:                     }
10690:                 }
10691:             }
10692:         }
10693:     }
10694:     if (keys(%currsetting) > 0) {
10695:         foreach my $item (@contacts) {
10696:             if ($to{$item} ne $currsetting{$item}) {
10697:                 $changes{$item} = 1;
10698:             }
10699:         }
10700:         foreach my $type (@mailings) {
10701:             foreach my $item (@contacts) {
10702:                 if (ref($currsetting{$type}) eq 'HASH') {
10703:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
10704:                         push(@{$changes{$type}},$item);
10705:                     }
10706:                 } else {
10707:                     push(@{$changes{$type}},@{$newsetting{$type}});
10708:                 }
10709:             }
10710:             if ($others{$type} ne $currsetting{$type}{'others'}) {
10711:                 push(@{$changes{$type}},'others');
10712:             }
10713:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10714:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
10715:                     push(@{$changes{$type}},'bcc'); 
10716:                 }
10717:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
10718:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
10719:                     push(@{$changes{$type}},'include');
10720:                 }
10721:             }
10722:         }
10723:         if (ref($fields) eq 'ARRAY') {
10724:             if (ref($currsetting{'helpform'}) eq 'HASH') {
10725:                 foreach my $field (@{$fields}) {
10726:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
10727:                         push(@{$changes{'helpform'}},$field);
10728:                     }
10729:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10730:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
10731:                             push(@{$changes{'helpform'}},'maxsize');
10732:                         }
10733:                     }
10734:                 }
10735:             } else {
10736:                 foreach my $field (@{$fields}) {
10737:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
10738:                         push(@{$changes{'helpform'}},$field);
10739:                     }
10740:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10741:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
10742:                             push(@{$changes{'helpform'}},'maxsize');
10743:                         }
10744:                     }
10745:                 }
10746:             }
10747:         }
10748:     } else {
10749:         my %default;
10750:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
10751:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
10752:         $default{'errormail'} = 'adminemail';
10753:         $default{'packagesmail'} = 'adminemail';
10754:         $default{'helpdeskmail'} = 'supportemail';
10755:         $default{'otherdomsmail'} = 'supportemail';
10756:         $default{'lonstatusmail'} = 'adminemail';
10757:         $default{'requestsmail'} = 'adminemail';
10758:         $default{'updatesmail'} = 'adminemail';
10759:         foreach my $item (@contacts) {
10760:            if ($to{$item} ne $default{$item}) {
10761:                $changes{$item} = 1;
10762:            }
10763:         }
10764:         foreach my $type (@mailings) {
10765:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
10766:                 push(@{$changes{$type}},@{$newsetting{$type}});
10767:             }
10768:             if ($others{$type} ne '') {
10769:                 push(@{$changes{$type}},'others');
10770:             }
10771:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10772:                 if ($bcc{$type} ne '') {
10773:                     push(@{$changes{$type}},'bcc');
10774:                 }
10775:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
10776:                     push(@{$changes{$type}},'include');
10777:                 }
10778:             }
10779:         }
10780:         if (ref($fields) eq 'ARRAY') {
10781:             foreach my $field (@{$fields}) {
10782:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
10783:                     push(@{$changes{'helpform'}},$field);
10784:                 }
10785:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10786:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
10787:                         push(@{$changes{'helpform'}},'maxsize');
10788:                     }
10789:                 }
10790:             }
10791:         }
10792:     }
10793:     foreach my $item (@toggles) {
10794:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
10795:             $changes{$item} = 1;
10796:         } elsif ((!$env{'form.'.$item}) &&
10797:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
10798:             $changes{$item} = 1;
10799:         }
10800:     }
10801:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
10802:                                              $dom);
10803:     if ($putresult eq 'ok') {
10804:         if (keys(%changes) > 0) {
10805:             &Apache::loncommon::devalidate_domconfig_cache($dom);
10806:             if (ref($lastactref) eq 'HASH') {
10807:                 $lastactref->{'domainconfig'} = 1;
10808:             }
10809:             my ($titles,$short_titles)  = &contact_titles();
10810:             $resulttext = &mt('Changes made:').'<ul>';
10811:             foreach my $item (@contacts) {
10812:                 if ($changes{$item}) {
10813:                     $resulttext .= '<li>'.$titles->{$item}.
10814:                                     &mt(' set to: ').
10815:                                     '<span class="LC_cusr_emph">'.
10816:                                     $to{$item}.'</span></li>';
10817:                 }
10818:             }
10819:             foreach my $type (@mailings) {
10820:                 if (ref($changes{$type}) eq 'ARRAY') {
10821:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10822:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
10823:                     } else {
10824:                         $resulttext .= '<li>'.$titles->{$type}.': ';
10825:                     }
10826:                     my @text;
10827:                     foreach my $item (@{$newsetting{$type}}) {
10828:                         push(@text,$short_titles->{$item});
10829:                     }
10830:                     if ($others{$type} ne '') {
10831:                         push(@text,$others{$type});
10832:                     }
10833:                     if (@text) {
10834:                         $resulttext .= '<span class="LC_cusr_emph">'.
10835:                                        join(', ',@text).'</span>';
10836:                     }
10837:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10838:                         if ($bcc{$type} ne '') {
10839:                             my $bcctext;
10840:                             if (@text) {
10841:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
10842:                             } else {
10843:                                 $bcctext = '(Bcc)';
10844:                             }
10845:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
10846:                         } elsif (!@text) {
10847:                             $resulttext .= &mt('No one');
10848:                         }   
10849:                         if ($includestr{$type} ne '') {
10850:                             if ($includeloc{$type} eq 'b') {
10851:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
10852:                             } elsif ($includeloc{$type} eq 's') {
10853:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
10854:                             }
10855:                         }
10856:                     } elsif (!@text) {
10857:                         $resulttext .= &mt('No recipients');
10858:                     }
10859:                     $resulttext .= '</li>';
10860:                 }
10861:             }
10862:             my @offon = ('off','on');
10863:             if ($changes{'reporterrors'}) {
10864:                 $resulttext .= '<li>'.
10865:                                &mt('E-mail error reports to [_1] set to "'.
10866:                                    $offon[$env{'form.reporterrors'}].'".',
10867:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
10868:                                        &mt('LON-CAPA core group - MSU'),600,500)).
10869:                                '</li>';
10870:             }
10871:             if ($changes{'reportupdates'}) {
10872:                 $resulttext .= '<li>'.
10873:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
10874:                                     $offon[$env{'form.reportupdates'}].'".',
10875:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
10876:                                         &mt('LON-CAPA core group - MSU'),600,500)).
10877:                                 '</li>';
10878:             }
10879:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
10880:                 my (@optional,@required,@unused,$maxsizechg);
10881:                 foreach my $field (@{$changes{'helpform'}}) {
10882:                     if ($field eq 'maxsize') {
10883:                         $maxsizechg = 1;
10884:                         next;
10885:                     }
10886:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
10887:                         push(@optional,$field);
10888:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
10889:                         push(@unused,$field);
10890:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
10891:                         push(@required,$field);
10892:                     }
10893:                 }
10894:                 if (@optional) {
10895:                     $resulttext .= '<li>'.
10896:                                    &mt('Help form fields changed to "Optional": [_1].',
10897:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
10898:                                    '</li>';
10899:                 }
10900:                 if (@required) {
10901:                     $resulttext .= '<li>'.
10902:                                    &mt('Help form fields changed to "Required": [_1].',
10903:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
10904:                                    '</li>';
10905:                 }
10906:                 if (@unused) {
10907:                     $resulttext .= '<li>'.
10908:                                    &mt('Help form fields changed to "Not shown": [_1].',
10909:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
10910:                                    '</li>';
10911:                 }
10912:                 if ($maxsizechg) {
10913:                     $resulttext .= '<li>'.
10914:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
10915:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
10916:                                    '</li>';
10917: 
10918:                 }
10919:             }
10920:             $resulttext .= '</ul>';
10921:         } else {
10922:             $resulttext = &mt('No changes made to contacts and form settings');
10923:         }
10924:     } else {
10925:         $resulttext = '<span class="LC_error">'.
10926:             &mt('An error occurred: [_1].',$putresult).'</span>';
10927:     }
10928:     return $resulttext;
10929: }
10930: 
10931: sub modify_usercreation {
10932:     my ($dom,%domconfig) = @_;
10933:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
10934:     my $warningmsg;
10935:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
10936:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
10937:             if ($key eq 'cancreate') {
10938:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
10939:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
10940:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
10941:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
10942:                             ($item eq 'recaptchaversion')) {
10943:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10944:                         } else {
10945:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10946:                         }
10947:                     }
10948:                 }
10949:             } elsif ($key eq 'email_rule') {
10950:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
10951:             } else {
10952:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
10953:             }
10954:         }
10955:     }
10956:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
10957:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
10958:     my @contexts = ('author','course','requestcrs');
10959:     foreach my $item(@contexts) {
10960:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
10961:     }
10962:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
10963:         foreach my $item (@contexts) {
10964:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
10965:                 push(@{$changes{'cancreate'}},$item);
10966:             }
10967:         }
10968:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
10969:         foreach my $item (@contexts) {
10970:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
10971:                 if ($cancreate{$item} ne 'any') {
10972:                     push(@{$changes{'cancreate'}},$item);
10973:                 }
10974:             } else {
10975:                 if ($cancreate{$item} ne 'none') {
10976:                     push(@{$changes{'cancreate'}},$item);
10977:                 }
10978:             }
10979:         }
10980:     } else {
10981:         foreach my $item (@contexts)  {
10982:             push(@{$changes{'cancreate'}},$item);
10983:         }
10984:     }
10985: 
10986:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
10987:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
10988:             if (!grep(/^\Q$type\E$/,@username_rule)) {
10989:                 push(@{$changes{'username_rule'}},$type);
10990:             }
10991:         }
10992:         foreach my $type (@username_rule) {
10993:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
10994:                 push(@{$changes{'username_rule'}},$type);
10995:             }
10996:         }
10997:     } else {
10998:         push(@{$changes{'username_rule'}},@username_rule);
10999:     }
11000: 
11001:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
11002:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
11003:             if (!grep(/^\Q$type\E$/,@id_rule)) {
11004:                 push(@{$changes{'id_rule'}},$type);
11005:             }
11006:         }
11007:         foreach my $type (@id_rule) {
11008:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
11009:                 push(@{$changes{'id_rule'}},$type);
11010:             }
11011:         }
11012:     } else {
11013:         push(@{$changes{'id_rule'}},@id_rule);
11014:     }
11015: 
11016:     my @authen_contexts = ('author','course','domain');
11017:     my @authtypes = ('int','krb4','krb5','loc');
11018:     my %authhash;
11019:     foreach my $item (@authen_contexts) {
11020:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
11021:         foreach my $auth (@authtypes) {
11022:             if (grep(/^\Q$auth\E$/,@authallowed)) {
11023:                 $authhash{$item}{$auth} = 1;
11024:             } else {
11025:                 $authhash{$item}{$auth} = 0;
11026:             }
11027:         }
11028:     }
11029:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
11030:         foreach my $item (@authen_contexts) {
11031:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
11032:                 foreach my $auth (@authtypes) {
11033:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
11034:                         push(@{$changes{'authtypes'}},$item);
11035:                         last;
11036:                     }
11037:                 }
11038:             }
11039:         }
11040:     } else {
11041:         foreach my $item (@authen_contexts) {
11042:             push(@{$changes{'authtypes'}},$item);
11043:         }
11044:     }
11045: 
11046:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
11047:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
11048:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
11049:     $save_usercreate{'id_rule'} = \@id_rule;
11050:     $save_usercreate{'username_rule'} = \@username_rule,
11051:     $save_usercreate{'authtypes'} = \%authhash;
11052: 
11053:     my %usercreation_hash =  (
11054:         usercreation     => \%save_usercreate,
11055:     );
11056: 
11057:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
11058:                                              $dom);
11059: 
11060:     if ($putresult eq 'ok') {
11061:         if (keys(%changes) > 0) {
11062:             $resulttext = &mt('Changes made:').'<ul>';
11063:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
11064:                 my %lt = &usercreation_types();
11065:                 foreach my $type (@{$changes{'cancreate'}}) {
11066:                     my $chgtext = $lt{$type}.', ';
11067:                     if ($cancreate{$type} eq 'none') {
11068:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
11069:                     } elsif ($cancreate{$type} eq 'any') {
11070:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
11071:                     } elsif ($cancreate{$type} eq 'official') {
11072:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
11073:                     } elsif ($cancreate{$type} eq 'unofficial') {
11074:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
11075:                     }
11076:                     $resulttext .= '<li>'.$chgtext.'</li>';
11077:                 }
11078:             }
11079:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
11080:                 my ($rules,$ruleorder) = 
11081:                     &Apache::lonnet::inst_userrules($dom,'username');
11082:                 my $chgtext = '<ul>';
11083:                 foreach my $type (@username_rule) {
11084:                     if (ref($rules->{$type}) eq 'HASH') {
11085:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
11086:                     }
11087:                 }
11088:                 $chgtext .= '</ul>';
11089:                 if (@username_rule > 0) {
11090:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
11091:                 } else {
11092:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
11093:                 }
11094:             }
11095:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
11096:                 my ($idrules,$idruleorder) = 
11097:                     &Apache::lonnet::inst_userrules($dom,'id');
11098:                 my $chgtext = '<ul>';
11099:                 foreach my $type (@id_rule) {
11100:                     if (ref($idrules->{$type}) eq 'HASH') {
11101:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
11102:                     }
11103:                 }
11104:                 $chgtext .= '</ul>';
11105:                 if (@id_rule > 0) {
11106:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
11107:                 } else {
11108:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
11109:                 }
11110:             }
11111:             my %authname = &authtype_names();
11112:             my %context_title = &context_names();
11113:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
11114:                 my $chgtext = '<ul>';
11115:                 foreach my $type (@{$changes{'authtypes'}}) {
11116:                     my @allowed;
11117:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
11118:                     foreach my $auth (@authtypes) {
11119:                         if ($authhash{$type}{$auth}) {
11120:                             push(@allowed,$authname{$auth});
11121:                         }
11122:                     }
11123:                     if (@allowed > 0) {
11124:                         $chgtext .= join(', ',@allowed).'</li>';
11125:                     } else {
11126:                         $chgtext .= &mt('none').'</li>';
11127:                     }
11128:                 }
11129:                 $chgtext .= '</ul>';
11130:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
11131:                 $resulttext .= '</li>';
11132:             }
11133:             $resulttext .= '</ul>';
11134:         } else {
11135:             $resulttext = &mt('No changes made to user creation settings');
11136:         }
11137:     } else {
11138:         $resulttext = '<span class="LC_error">'.
11139:             &mt('An error occurred: [_1]',$putresult).'</span>';
11140:     }
11141:     if ($warningmsg ne '') {
11142:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
11143:     }
11144:     return $resulttext;
11145: }
11146: 
11147: sub modify_selfcreation {
11148:     my ($dom,%domconfig) = @_;
11149:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
11150:     my (%save_usercreate,%save_usermodify);
11151:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11152:     if (ref($types) eq 'ARRAY') {
11153:         $usertypes->{'default'} = $othertitle;
11154:         push(@{$types},'default');
11155:     }
11156: #
11157: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
11158: #
11159:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
11160:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
11161:             if ($key eq 'cancreate') {
11162:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
11163:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
11164:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
11165:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
11166:                             ($item eq 'recaptchaversion') ||
11167:                             ($item eq 'emailusername') || ($item eq 'notify') ||
11168:                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
11169:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11170:                         } else {
11171:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11172:                         }
11173:                     }
11174:                 }
11175:             } elsif ($key eq 'email_rule') {
11176:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
11177:             } else {
11178:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
11179:             }
11180:         }
11181:     }
11182: #
11183: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
11184: #
11185:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
11186:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
11187:             if ($key eq 'selfcreate') {
11188:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
11189:             } else {
11190:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
11191:             }
11192:         }
11193:     }
11194: 
11195:     my @contexts = ('selfcreate');
11196:     @{$cancreate{'selfcreate'}} = ();
11197:     %{$cancreate{'emailusername'}} = ();
11198:     @{$cancreate{'statustocreate'}} = ();
11199:     %{$cancreate{'selfcreateprocessing'}} = ();
11200:     %{$cancreate{'shibenv'}} = ();
11201:     my %selfcreatetypes = (
11202:                              sso   => 'users authenticated by institutional single sign on',
11203:                              login => 'users authenticated by institutional log-in',
11204:                              email => 'users who provide a valid e-mail address for use as username',
11205:                           );
11206: #
11207: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
11208: # is permitted.
11209: #
11210: 
11211:     my @statuses;
11212:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
11213:         if (ref($domconfig{'inststatus'}{'inststatusguest'}) eq 'ARRAY') {
11214:             @statuses = @{$domconfig{'inststatus'}{'inststatusguest'}};
11215:         }
11216:     }
11217:     push(@statuses,'default');
11218: 
11219:     foreach my $item ('login','sso','email') {
11220:         if ($item eq 'email') {
11221:             if ($env{'form.cancreate_email'}) {
11222:                 push(@{$cancreate{'selfcreate'}},'email');
11223:                 push(@contexts,'selfcreateprocessing');
11224:                 foreach my $type (@statuses) {
11225:                     if ($type eq 'default') {
11226:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess'};
11227:                     } else { 
11228:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
11229:                     }
11230:                 }
11231:             }
11232:         } else {
11233:             if ($env{'form.cancreate_'.$item}) {
11234:                 push(@{$cancreate{'selfcreate'}},$item);
11235:             }
11236:         }
11237:     }
11238:     my (@email_rule,%userinfo,%savecaptcha);
11239:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
11240: #
11241: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
11242: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
11243: #
11244: 
11245:     if ($env{'form.cancreate_email'}) {
11246:         push(@contexts,'emailusername');
11247:         if (ref($types) eq 'ARRAY') {
11248:             foreach my $type (@{$types}) {
11249:                 if (ref($infofields) eq 'ARRAY') {
11250:                     foreach my $field (@{$infofields}) {
11251:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
11252:                             $cancreate{'emailusername'}{$type}{$field} = $1;
11253:                         }
11254:                     }
11255:                 }
11256:             }
11257:         }
11258: #
11259: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
11260: # queued requests for self-creation of account using e-mail address as username
11261: #
11262: 
11263:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
11264:         @approvalnotify = sort(@approvalnotify);
11265:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
11266:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11267:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
11268:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
11269:                     push(@{$changes{'cancreate'}},'notify');
11270:                 }
11271:             } else {
11272:                 if ($cancreate{'notify'}{'approval'}) {
11273:                     push(@{$changes{'cancreate'}},'notify');
11274:                 }
11275:             }
11276:         } elsif ($cancreate{'notify'}{'approval'}) {
11277:             push(@{$changes{'cancreate'}},'notify');
11278:         }
11279: 
11280: #
11281: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
11282: #
11283:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
11284:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
11285:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
11286:             if (@{$curr_usercreation{'email_rule'}} > 0) {
11287:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
11288:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
11289:                         push(@{$changes{'email_rule'}},$type);
11290:                     }
11291:                 }
11292:             }
11293:             if (@email_rule > 0) {
11294:                 foreach my $type (@email_rule) {
11295:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
11296:                         push(@{$changes{'email_rule'}},$type);
11297:                     }
11298:                 }
11299:             }
11300:         } elsif (@email_rule > 0) {
11301:             push(@{$changes{'email_rule'}},@email_rule);
11302:         }
11303:     }
11304: #  
11305: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
11306: # institutional log-in.
11307: #
11308:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
11309:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11310:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
11311:                ($domdefaults{'auth_def'} eq 'localauth'))) {
11312:             $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.').' '.
11313:                           &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.');
11314:         }
11315:     }
11316:     my @fields = ('lastname','firstname','middlename','generation',
11317:                   'permanentemail','id');
11318:     my @shibfields = (@fields,'inststatus');
11319:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11320: #
11321: # Where usernames may created for institutional log-in and/or institutional single sign on:
11322: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
11323: # may self-create accounts 
11324: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
11325: # which the user may supply, if institutional data is unavailable.
11326: #
11327:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
11328:         if (ref($types) eq 'ARRAY') {
11329:             if (@{$types} > 1) {
11330:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
11331:                 push(@contexts,'statustocreate');
11332:             } else {
11333:                 undef($cancreate{'statustocreate'});
11334:             } 
11335:             foreach my $type (@{$types}) {
11336:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
11337:                 foreach my $field (@fields) {
11338:                     if (grep(/^\Q$field\E$/,@modifiable)) {
11339:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
11340:                     } else {
11341:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
11342:                     }
11343:                 }
11344:             }
11345:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
11346:                 foreach my $type (@{$types}) {
11347:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
11348:                         foreach my $field (@fields) {
11349:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
11350:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
11351:                                 push(@{$changes{'selfcreate'}},$type);
11352:                                 last;
11353:                             }
11354:                         }
11355:                     }
11356:                 }
11357:             } else {
11358:                 foreach my $type (@{$types}) {
11359:                     push(@{$changes{'selfcreate'}},$type);
11360:                 }
11361:             }
11362:         }
11363:         foreach my $field (@shibfields) {
11364:             if ($env{'form.shibenv_'.$field} ne '') {
11365:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
11366:             }
11367:         }
11368:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11369:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
11370:                 foreach my $field (@shibfields) {
11371:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
11372:                         push(@{$changes{'cancreate'}},'shibenv');
11373:                     }
11374:                 }
11375:             } else {
11376:                 foreach my $field (@shibfields) {
11377:                     if ($env{'form.shibenv_'.$field}) {
11378:                         push(@{$changes{'cancreate'}},'shibenv');
11379:                         last;
11380:                     }
11381:                 }
11382:             }
11383:         }
11384:     }
11385:     foreach my $item (@contexts) {
11386:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
11387:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
11388:                 if (ref($cancreate{$item}) eq 'ARRAY') {
11389:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
11390:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11391:                             push(@{$changes{'cancreate'}},$item);
11392:                         }
11393:                     }
11394:                 }
11395:             }
11396:             if (ref($cancreate{$item}) eq 'ARRAY') {
11397:                 foreach my $type (@{$cancreate{$item}}) {
11398:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
11399:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11400:                             push(@{$changes{'cancreate'}},$item);
11401:                         }
11402:                     }
11403:                 }
11404:             }
11405:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
11406:             if (ref($cancreate{$item}) eq 'HASH') {
11407:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
11408:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
11409:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
11410:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
11411:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11412:                                     push(@{$changes{'cancreate'}},$item);
11413:                                 }
11414:                             }
11415:                         }
11416:                     } elsif ($item eq 'selfcreateprocessing') {
11417:                         if ($cancreate{$item}{$curr} ne $curr_usercreation{'cancreate'}{$item}{$curr}) {
11418:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11419:                                 push(@{$changes{'cancreate'}},$item);
11420:                             }
11421:                         }
11422:                     } else {
11423:                         if (!$cancreate{$item}{$curr}) {
11424:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11425:                                 push(@{$changes{'cancreate'}},$item);
11426:                             }
11427:                         }
11428:                     }
11429:                 }
11430:                 foreach my $field (keys(%{$cancreate{$item}})) {
11431:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
11432:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
11433:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
11434:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
11435:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11436:                                         push(@{$changes{'cancreate'}},$item);
11437:                                     }
11438:                                 }
11439:                             } else {
11440:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11441:                                     push(@{$changes{'cancreate'}},$item);
11442:                                 }
11443:                             }
11444:                         }
11445:                     } elsif ($item eq 'selfcreateprocessing') {
11446:                         if ($cancreate{$item}{$field} ne $curr_usercreation{'cancreate'}{$item}{$field}) {
11447:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11448:                                 push(@{$changes{'cancreate'}},$item);
11449:                             }
11450:                         }
11451:                     } else {
11452:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
11453:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11454:                                 push(@{$changes{'cancreate'}},$item);
11455:                             }
11456:                         }
11457:                     }
11458:                 }
11459:             }
11460:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
11461:             if (ref($cancreate{$item}) eq 'ARRAY') {
11462:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
11463:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11464:                         push(@{$changes{'cancreate'}},$item);
11465:                     }
11466:                 }
11467:             } elsif (ref($cancreate{$item}) eq 'HASH') {
11468:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
11469:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11470:                         push(@{$changes{'cancreate'}},$item);
11471:                     }
11472:                 }
11473:             }
11474:         } elsif ($item eq 'emailusername') {
11475:             if (ref($cancreate{$item}) eq 'HASH') {
11476:                 foreach my $type (keys(%{$cancreate{$item}})) {
11477:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
11478:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
11479:                             if ($cancreate{$item}{$type}{$field}) {
11480:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11481:                                     push(@{$changes{'cancreate'}},$item);
11482:                                 }
11483:                                 last;
11484:                             }
11485:                         }
11486:                     }
11487:                 }
11488:             }
11489:         }
11490:     }
11491: #
11492: # Populate %save_usercreate hash with updates to self-creation configuration.
11493: #
11494:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
11495:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
11496:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
11497:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
11498:     if (ref($cancreate{'notify'}) eq 'HASH') {
11499:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
11500:     }
11501:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
11502:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
11503:     }
11504:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
11505:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
11506:     }
11507:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
11508:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
11509:     }
11510:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
11511:     $save_usercreate{'emailrule'} = \@email_rule;
11512: 
11513:     my %userconfig_hash = (
11514:             usercreation     => \%save_usercreate,
11515:             usermodification => \%save_usermodify,
11516:     );
11517:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
11518:                                              $dom);
11519: #
11520: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
11521: #
11522:     if ($putresult eq 'ok') {
11523:         if (keys(%changes) > 0) {
11524:             $resulttext = &mt('Changes made:').'<ul>';
11525:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
11526:                 my %lt = &selfcreation_types();
11527:                 foreach my $type (@{$changes{'cancreate'}}) {
11528:                     my $chgtext;
11529:                     if ($type eq 'selfcreate') {
11530:                         if (@{$cancreate{$type}} == 0) {
11531:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
11532:                         } else {
11533:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
11534:                                         '<ul>';
11535:                             foreach my $case (@{$cancreate{$type}}) {
11536:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
11537:                             }
11538:                             $chgtext .= '</ul>';
11539:                             if (ref($cancreate{$type}) eq 'ARRAY') {
11540:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
11541:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
11542:                                         if (@{$cancreate{'statustocreate'}} == 0) {
11543:                                             $chgtext .= '<br />'.
11544:                                                         '<span class="LC_warning">'.
11545:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
11546:                                                         '</span>';
11547:                                         }
11548:                                     }
11549:                                 }
11550:                             }
11551:                         }
11552:                     } elsif ($type eq 'shibenv') {
11553:                         if (keys(%{$cancreate{$type}}) == 0) {
11554:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information'); 
11555:                         } else {
11556:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
11557:                                         '<ul>';
11558:                             foreach my $field (@shibfields) {
11559:                                 next if ($cancreate{$type}{$field} eq '');
11560:                                 if ($field eq 'inststatus') {
11561:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
11562:                                 } else {
11563:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
11564:                                 }
11565:                             }
11566:                             $chgtext .= '</ul>';
11567:                         }  
11568:                     } elsif ($type eq 'statustocreate') {
11569:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
11570:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
11571:                             if (@{$cancreate{'selfcreate'}} > 0) {
11572:                                 if (@{$cancreate{'statustocreate'}} == 0) {
11573:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
11574:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
11575:                                         $chgtext .= '<br />'.
11576:                                                     '<span class="LC_warning">'.
11577:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
11578:                                                     '</span>';
11579:                                     }
11580:                                 } elsif (ref($usertypes) eq 'HASH') {
11581:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
11582:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
11583:                                     } else {
11584:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
11585:                                     }
11586:                                     $chgtext .= '<ul>';
11587:                                     foreach my $case (@{$cancreate{$type}}) {
11588:                                         if ($case eq 'default') {
11589:                                             $chgtext .= '<li>'.$othertitle.'</li>';
11590:                                         } else {
11591:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
11592:                                         }
11593:                                     }
11594:                                     $chgtext .= '</ul>';
11595:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
11596:                                         $chgtext .= '<br /><span class="LC_warning">'.
11597:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
11598:                                                     '</span>';
11599:                                     }
11600:                                 }
11601:                             } else {
11602:                                 if (@{$cancreate{$type}} == 0) {
11603:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
11604:                                 } else {
11605:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
11606:                                 }
11607:                             }
11608:                         }
11609:                     } elsif ($type eq 'selfcreateprocessing') {
11610:                         my %choices = &Apache::lonlocal::texthash (
11611:                                                                     automatic => 'Automatic approval',
11612:                                                                     approval  => 'Queued for approval',
11613:                                                                   );
11614:                         if (@statuses > 1) {
11615:                             $chgtext .= &mt('Processing of requests to create account with e-mail address as username set as follows:'). 
11616:                                         '<ul>';
11617:                            foreach my $type (@statuses) {
11618:                                if ($type eq 'default') {
11619:                                    $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
11620:                                } else {
11621:                                    $chgtext .= '<li>'.$usertypes->{$type}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
11622:                                }
11623:                            }
11624:                            $chgtext .= '</ul>';
11625:                         } else {
11626:                            $chgtext .= &mt('Processing of requests to create account with e-mail address as username set to: "[_1]"',
11627:                                          $choices{$cancreate{'selfcreateprocessing'}{'default'}});
11628:                         }
11629:                     } elsif ($type eq 'captcha') {
11630:                         if ($savecaptcha{$type} eq 'notused') {
11631:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
11632:                         } else {
11633:                             my %captchas = &captcha_phrases();
11634:                             if ($captchas{$savecaptcha{$type}}) {
11635:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
11636:                             } else {
11637:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
11638:                             }
11639:                         }
11640:                     } elsif ($type eq 'recaptchakeys') {
11641:                         my ($privkey,$pubkey);
11642:                         if (ref($savecaptcha{$type}) eq 'HASH') {
11643:                             $pubkey = $savecaptcha{$type}{'public'};
11644:                             $privkey = $savecaptcha{$type}{'private'};
11645:                         }
11646:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
11647:                         if (!$pubkey) {
11648:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
11649:                         } else {
11650:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
11651:                         }
11652:                         if (!$privkey) {
11653:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
11654:                         } else {
11655:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
11656:                         }
11657:                         $chgtext .= '</ul>';
11658:                     } elsif ($type eq 'recaptchaversion') {
11659:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
11660:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
11661:                         }
11662:                     } elsif ($type eq 'emailusername') {
11663:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
11664:                             if (ref($types) eq 'ARRAY') {
11665:                                 foreach my $type (@{$types}) {
11666:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
11667:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
11668:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',"'$usertypes->{$type}'").
11669:                                                     '<ul>';
11670:                                             foreach my $field (@{$infofields}) {
11671:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
11672:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
11673:                                                 }
11674:                                             }
11675:                                             $chgtext .= '</ul>';
11676:                                         } else {
11677:                                             $chgtext .= &mt('When self creating account with e-mail as username, no information besides e-mail address will be provided by [_1].',"'$usertypes->{$type}'").'<br />';
11678:                                         }
11679:                                     } else {
11680:                                         $chgtext .= &mt('When self creating account with e-mail as username, no information besides e-mail address will be provided by [_1].',"'$usertypes->{$type}'").'<br />';
11681:                                     }
11682:                                 }
11683:                             }
11684:                         }
11685:                     } elsif ($type eq 'notify') {
11686:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
11687:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
11688:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
11689:                                 if ($cancreate{'notify'}{'approval'}) {
11690:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
11691:                                 }
11692:                             }
11693:                         }
11694:                     }
11695:                     if ($chgtext) {
11696:                         $resulttext .= '<li>'.$chgtext.'</li>';
11697:                     }
11698:                 }
11699:             }
11700:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
11701:                 my ($emailrules,$emailruleorder) =
11702:                     &Apache::lonnet::inst_userrules($dom,'email');
11703:                 my $chgtext = '<ul>';
11704:                 foreach my $type (@email_rule) {
11705:                     if (ref($emailrules->{$type}) eq 'HASH') {
11706:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
11707:                     }
11708:                 }
11709:                 $chgtext .= '</ul>';
11710:                 if (@email_rule > 0) {
11711:                     $resulttext .= '<li>'.
11712:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
11713:                                        $chgtext.
11714:                                    '</li>';
11715:                 } else {
11716:                     $resulttext .= '<li>'.
11717:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
11718:                                    '</li>';
11719:                 }
11720:             }
11721:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
11722:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
11723:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11724:                 foreach my $type (@{$changes{'selfcreate'}}) {
11725:                     my $typename = $type;
11726:                     if (ref($usertypes) eq 'HASH') {
11727:                         if ($usertypes->{$type} ne '') {
11728:                             $typename = $usertypes->{$type};
11729:                         }
11730:                     }
11731:                     my @modifiable;
11732:                     $resulttext .= '<li>'.
11733:                                     &mt('Self-creation of account by users with status: [_1]',
11734:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
11735:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
11736:                     foreach my $field (@fields) {
11737:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
11738:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
11739:                         }
11740:                     }
11741:                     if (@modifiable > 0) {
11742:                         $resulttext .= join(', ',@modifiable);
11743:                     } else {
11744:                         $resulttext .= &mt('none');
11745:                     }
11746:                     $resulttext .= '</li>';
11747:                 }
11748:                 $resulttext .= '</ul></li>';
11749:             }
11750:             $resulttext .= '</ul>';
11751:         } else {
11752:             $resulttext = &mt('No changes made to self-creation settings');
11753:         }
11754:     } else {
11755:         $resulttext = '<span class="LC_error">'.
11756:             &mt('An error occurred: [_1]',$putresult).'</span>';
11757:     }
11758:     if ($warningmsg ne '') {
11759:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
11760:     }
11761:     return $resulttext;
11762: }
11763: 
11764: sub process_captcha {
11765:     my ($container,$changes,$newsettings,$current) = @_;
11766:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
11767:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
11768:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
11769:         $newsettings->{'captcha'} = 'original';
11770:     }
11771:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
11772:         if ($container eq 'cancreate') {
11773:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11774:                 push(@{$changes->{'cancreate'}},'captcha');
11775:             } elsif (!defined($changes->{'cancreate'})) {
11776:                 $changes->{'cancreate'} = ['captcha'];
11777:             }
11778:         } else {
11779:             $changes->{'captcha'} = 1;
11780:         }
11781:     }
11782:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
11783:     if ($newsettings->{'captcha'} eq 'recaptcha') {
11784:         $newpub = $env{'form.'.$container.'_recaptchapub'};
11785:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
11786:         $newpub =~ s/[^\w\-]//g;
11787:         $newpriv =~ s/[^\w\-]//g;
11788:         $newsettings->{'recaptchakeys'} = {
11789:                                              public  => $newpub,
11790:                                              private => $newpriv,
11791:                                           };
11792:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
11793:         $newversion =~ s/\D//g;
11794:         if ($newversion ne '2') {
11795:             $newversion = 1;
11796:         }
11797:         $newsettings->{'recaptchaversion'} = $newversion;
11798:     }
11799:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
11800:         $currpub = $current->{'recaptchakeys'}{'public'};
11801:         $currpriv = $current->{'recaptchakeys'}{'private'};
11802:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
11803:             $newsettings->{'recaptchakeys'} = {
11804:                                                  public  => '',
11805:                                                  private => '',
11806:                                               }
11807:         }
11808:     }
11809:     if ($current->{'captcha'} eq 'recaptcha') {
11810:         $currversion = $current->{'recaptchaversion'};
11811:         if ($currversion ne '2') {
11812:             $currversion = 1;
11813:         }
11814:     }
11815:     if ($currversion ne $newversion) {
11816:         if ($container eq 'cancreate') {
11817:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11818:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
11819:             } elsif (!defined($changes->{'cancreate'})) {
11820:                 $changes->{'cancreate'} = ['recaptchaversion'];
11821:             }
11822:         } else {
11823:             $changes->{'recaptchaversion'} = 1;
11824:         }
11825:     }
11826:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
11827:         if ($container eq 'cancreate') {
11828:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11829:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
11830:             } elsif (!defined($changes->{'cancreate'})) {
11831:                 $changes->{'cancreate'} = ['recaptchakeys'];
11832:             }
11833:         } else {
11834:             $changes->{'recaptchakeys'} = 1;
11835:         }
11836:     }
11837:     return;
11838: }
11839: 
11840: sub modify_usermodification {
11841:     my ($dom,%domconfig) = @_;
11842:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
11843:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
11844:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
11845:             if ($key eq 'selfcreate') {
11846:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
11847:             } else {  
11848:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
11849:             }
11850:         }
11851:     }
11852:     my @contexts = ('author','course');
11853:     my %context_title = (
11854:                            author => 'In author context',
11855:                            course => 'In course context',
11856:                         );
11857:     my @fields = ('lastname','firstname','middlename','generation',
11858:                   'permanentemail','id');
11859:     my %roles = (
11860:                   author => ['ca','aa'],
11861:                   course => ['st','ep','ta','in','cr'],
11862:                 );
11863:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11864:     foreach my $context (@contexts) {
11865:         foreach my $role (@{$roles{$context}}) {
11866:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
11867:             foreach my $item (@fields) {
11868:                 if (grep(/^\Q$item\E$/,@modifiable)) {
11869:                     $modifyhash{$context}{$role}{$item} = 1;
11870:                 } else {
11871:                     $modifyhash{$context}{$role}{$item} = 0;
11872:                 }
11873:             }
11874:         }
11875:         if (ref($curr_usermodification{$context}) eq 'HASH') {
11876:             foreach my $role (@{$roles{$context}}) {
11877:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
11878:                     foreach my $field (@fields) {
11879:                         if ($modifyhash{$context}{$role}{$field} ne 
11880:                                 $curr_usermodification{$context}{$role}{$field}) {
11881:                             push(@{$changes{$context}},$role);
11882:                             last;
11883:                         }
11884:                     }
11885:                 }
11886:             }
11887:         } else {
11888:             foreach my $context (@contexts) {
11889:                 foreach my $role (@{$roles{$context}}) {
11890:                     push(@{$changes{$context}},$role);
11891:                 }
11892:             }
11893:         }
11894:     }
11895:     my %usermodification_hash =  (
11896:                                    usermodification => \%modifyhash,
11897:                                  );
11898:     my $putresult = &Apache::lonnet::put_dom('configuration',
11899:                                              \%usermodification_hash,$dom);
11900:     if ($putresult eq 'ok') {
11901:         if (keys(%changes) > 0) {
11902:             $resulttext = &mt('Changes made: ').'<ul>';
11903:             foreach my $context (@contexts) {
11904:                 if (ref($changes{$context}) eq 'ARRAY') {
11905:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
11906:                     if (ref($changes{$context}) eq 'ARRAY') {
11907:                         foreach my $role (@{$changes{$context}}) {
11908:                             my $rolename;
11909:                             if ($role eq 'cr') {
11910:                                 $rolename = &mt('Custom');
11911:                             } else {
11912:                                 $rolename = &Apache::lonnet::plaintext($role);
11913:                             }
11914:                             my @modifiable;
11915:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
11916:                             foreach my $field (@fields) {
11917:                                 if ($modifyhash{$context}{$role}{$field}) {
11918:                                     push(@modifiable,$fieldtitles{$field});
11919:                                 }
11920:                             }
11921:                             if (@modifiable > 0) {
11922:                                 $resulttext .= join(', ',@modifiable);
11923:                             } else {
11924:                                 $resulttext .= &mt('none'); 
11925:                             }
11926:                             $resulttext .= '</li>';
11927:                         }
11928:                         $resulttext .= '</ul></li>';
11929:                     }
11930:                 }
11931:             }
11932:             $resulttext .= '</ul>';
11933:         } else {
11934:             $resulttext = &mt('No changes made to user modification settings');
11935:         }
11936:     } else {
11937:         $resulttext = '<span class="LC_error">'.
11938:             &mt('An error occurred: [_1]',$putresult).'</span>';
11939:     }
11940:     return $resulttext;
11941: }
11942: 
11943: sub modify_defaults {
11944:     my ($dom,$lastactref,%domconfig) = @_;
11945:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
11946:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11947:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
11948:                  'portal_def','intauth_cost','intauth_check','intauth_switch');
11949:     my @authtypes = ('internal','krb4','krb5','localauth');
11950:     foreach my $item (@items) {
11951:         $newvalues{$item} = $env{'form.'.$item};
11952:         if ($item eq 'auth_def') {
11953:             if ($newvalues{$item} ne '') {
11954:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
11955:                     push(@errors,$item);
11956:                 }
11957:             }
11958:         } elsif ($item eq 'lang_def') {
11959:             if ($newvalues{$item} ne '') {
11960:                 if ($newvalues{$item} =~ /^(\w+)/) {
11961:                     my $langcode = $1;
11962:                     if ($langcode ne 'x_chef') {
11963:                         if (code2language($langcode) eq '') {
11964:                             push(@errors,$item);
11965:                         }
11966:                     }
11967:                 } else {
11968:                     push(@errors,$item);
11969:                 }
11970:             }
11971:         } elsif ($item eq 'timezone_def') {
11972:             if ($newvalues{$item} ne '') {
11973:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
11974:                     push(@errors,$item);   
11975:                 }
11976:             }
11977:         } elsif ($item eq 'datelocale_def') {
11978:             if ($newvalues{$item} ne '') {
11979:                 my @datelocale_ids = DateTime::Locale->ids();
11980:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
11981:                     push(@errors,$item);
11982:                 }
11983:             }
11984:         } elsif ($item eq 'portal_def') {
11985:             if ($newvalues{$item} ne '') {
11986:                 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])\/?$/) {
11987:                     push(@errors,$item);
11988:                 }
11989:             }
11990:         } elsif ($item eq 'intauth_cost') {
11991:             if ($newvalues{$item} ne '') {
11992:                 if ($newvalues{$item} =~ /\D/) {
11993:                     push(@errors,$item);
11994:                 }
11995:             }
11996:         } elsif ($item eq 'intauth_check') {
11997:             if ($newvalues{$item} ne '') {
11998:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
11999:                     push(@errors,$item);
12000:                 }
12001:             }
12002:         } elsif ($item eq 'intauth_switch') {
12003:             if ($newvalues{$item} ne '') {
12004:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
12005:                     push(@errors,$item);
12006:                 }
12007:             }
12008:         }
12009:         if (grep(/^\Q$item\E$/,@errors)) {
12010:             $newvalues{$item} = $domdefaults{$item};
12011:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
12012:             $changes{$item} = 1;
12013:         }
12014:         $domdefaults{$item} = $newvalues{$item};
12015:     }
12016:     my %defaults_hash = (
12017:                          defaults => \%newvalues,
12018:                         );
12019:     my $title = &defaults_titles();
12020: 
12021:     my $currinststatus;
12022:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
12023:         $currinststatus = $domconfig{'inststatus'};
12024:     } else {
12025:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12026:         $currinststatus = {
12027:                              inststatustypes => $usertypes,
12028:                              inststatusorder => $types,
12029:                              inststatusguest => [],
12030:                           };
12031:     }
12032:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
12033:     my @allpos;
12034:     my %guests;
12035:     my %alltypes;
12036:     my ($currtitles,$currguests,$currorder);
12037:     if (ref($currinststatus) eq 'HASH') {
12038:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
12039:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
12040:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
12041:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
12042:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
12043:                     }
12044:                 }
12045:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
12046:                     my $position = $env{'form.inststatus_pos_'.$type};
12047:                     $position =~ s/\D+//g;
12048:                     $allpos[$position] = $type;
12049:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
12050:                     $alltypes{$type} =~ s/`//g;
12051:                     if ($env{'form.inststatus_guest_'.$type}) {
12052:                         $guests{$type} = 1;
12053:                     }
12054:                 }
12055:             }
12056:             if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
12057:                 $currguests = join(',',@{$currinststatus->{'inststatusguest'}});
12058:             }
12059:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
12060:             $currtitles =~ s/,$//;
12061:         }
12062:     }
12063:     if ($env{'form.addinststatus'}) {
12064:         my $newtype = $env{'form.addinststatus'};
12065:         $newtype =~ s/\W//g;
12066:         unless (exists($alltypes{$newtype})) {
12067:             if ($env{'form.addinststatus_guest'}) {
12068:                 $guests{$newtype} = 1;
12069:             }
12070:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
12071:             $alltypes{$newtype} =~ s/`//g; 
12072:             my $position = $env{'form.addinststatus_pos'};
12073:             $position =~ s/\D+//g;
12074:             if ($position ne '') {
12075:                 $allpos[$position] = $newtype;
12076:             }
12077:         }
12078:     }
12079:     my (@orderedstatus,@orderedguests);
12080:     foreach my $type (@allpos) {
12081:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
12082:             push(@orderedstatus,$type);
12083:             if ($guests{$type}) {
12084:                 push(@orderedguests,$type);
12085:             }
12086:         }
12087:     }
12088:     foreach my $type (keys(%alltypes)) {
12089:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
12090:             delete($alltypes{$type});
12091:         }
12092:     }
12093:     $defaults_hash{'inststatus'} = {
12094:                                      inststatustypes => \%alltypes,
12095:                                      inststatusorder => \@orderedstatus,
12096:                                      inststatusguest => \@orderedguests,
12097:                                    };
12098:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
12099:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
12100:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
12101:         }
12102:     }
12103:     if ($currorder ne join(',',@orderedstatus)) {
12104:         $changes{'inststatus'}{'inststatusorder'} = 1;
12105:     }
12106:     if ($currguests ne join(',',@orderedguests)) {
12107:         $changes{'inststatus'}{'inststatusguest'} = 1;
12108:     }
12109:     my $newtitles;
12110:     foreach my $item (@orderedstatus) {
12111:         $newtitles .= $alltypes{$item}.',';
12112:     }
12113:     $newtitles =~ s/,$//;
12114:     if ($currtitles ne $newtitles) {
12115:         $changes{'inststatus'}{'inststatustypes'} = 1;
12116:     }
12117:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
12118:                                              $dom);
12119:     if ($putresult eq 'ok') {
12120:         if (keys(%changes) > 0) {
12121:             $resulttext = &mt('Changes made:').'<ul>';
12122:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
12123:             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";
12124:             foreach my $item (sort(keys(%changes))) {
12125:                 if ($item eq 'inststatus') {
12126:                     if (ref($changes{'inststatus'}) eq 'HASH') {
12127:                         if (($changes{'inststatus'}{'inststatustypes'}) || $changes{'inststatus'}{'inststatusorder'}) {
12128:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
12129:                             foreach my $type (@orderedstatus) { 
12130:                                 $resulttext .= $alltypes{$type}.', ';
12131:                             }
12132:                             $resulttext =~ s/, $//;
12133:                             $resulttext .= '</li>';
12134:                         }
12135:                         if ($changes{'inststatus'}{'inststatusguest'}) {
12136:                             $resulttext .= '<li>'; 
12137:                             if (@orderedguests) {
12138:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to:').' ';
12139:                                 foreach my $type (@orderedguests) {
12140:                                     $resulttext .= $alltypes{$type}.', ';
12141:                                 }
12142:                                 $resulttext =~ s/, $//;
12143:                             } else {
12144:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to none.');
12145:                             }
12146:                             $resulttext .= '</li>';
12147:                         }
12148:                     }
12149:                 } else {
12150:                     my $value = $env{'form.'.$item};
12151:                     if ($value eq '') {
12152:                         $value = &mt('none');
12153:                     } elsif ($item eq 'auth_def') {
12154:                         my %authnames = &authtype_names();
12155:                         my %shortauth = (
12156:                                           internal   => 'int',
12157:                                           krb4       => 'krb4',
12158:                                           krb5       => 'krb5',
12159:                                           localauth  => 'loc',
12160:                         );
12161:                         $value = $authnames{$shortauth{$value}};
12162:                     } elsif ($item eq 'intauth_switch') {
12163:                         my %optiondesc = &Apache::lonlocal::texthash (
12164:                                             0 => 'No',
12165:                                             1 => 'Yes',
12166:                                             2 => 'Yes, and copy existing passwd file to passwd.bak file',
12167:                                          );
12168:                         if ($value =~ /^(0|1|2)$/) {
12169:                             $value = $optiondesc{$value};
12170:                         } else {
12171:                             $value = &mt('none -- defaults to No');
12172:                         }
12173:                     } elsif ($item eq 'intauth_check') {
12174:                         my %optiondesc = &Apache::lonlocal::texthash (
12175:                                              0 => 'No',
12176:                                              1 => 'Yes, allow login then update passwd file using default cost (if higher)',
12177:                                              2 => 'Yes, disallow login if stored cost is less than domain default',
12178:                                          );
12179:                         if ($value =~ /^(0|1|2)$/) {
12180:                             $value = $optiondesc{$value};
12181:                         } else {
12182:                             $value = &mt('none -- defaults to No');
12183:                         }
12184:                     }
12185:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
12186:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
12187:                 }
12188:             }
12189:             $resulttext .= '</ul>';
12190:             $mailmsgtext .= "\n";
12191:             my $cachetime = 24*60*60;
12192:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12193:             if (ref($lastactref) eq 'HASH') {
12194:                 $lastactref->{'domdefaults'} = 1;
12195:             }
12196:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
12197:                 my $notify = 1;
12198:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
12199:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
12200:                         $notify = 0;
12201:                     }
12202:                 }
12203:                 if ($notify) {
12204:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
12205:                                                "LON-CAPA Domain Settings Change - $dom",
12206:                                                $mailmsgtext);
12207:                 }
12208:             }
12209:         } else {
12210:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
12211:         }
12212:     } else {
12213:         $resulttext = '<span class="LC_error">'.
12214:             &mt('An error occurred: [_1]',$putresult).'</span>';
12215:     }
12216:     if (@errors > 0) {
12217:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
12218:         foreach my $item (@errors) {
12219:             $resulttext .= ' "'.$title->{$item}.'",';
12220:         }
12221:         $resulttext =~ s/,$//;
12222:     }
12223:     return $resulttext;
12224: }
12225: 
12226: sub modify_scantron {
12227:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12228:     my ($resulttext,%confhash,%changes,$errors);
12229:     my $custom = 'custom.tab';
12230:     my $default = 'default.tab';
12231:     my $servadm = $r->dir_config('lonAdmEMail');
12232:     my ($configuserok,$author_ok,$switchserver) = 
12233:         &config_check($dom,$confname,$servadm);
12234:     if ($env{'form.scantronformat.filename'} ne '') {
12235:         my $error;
12236:         if ($configuserok eq 'ok') {
12237:             if ($switchserver) {
12238:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
12239:             } else {
12240:                 if ($author_ok eq 'ok') {
12241:                     my ($result,$scantronurl) =
12242:                         &publishlogo($r,'upload','scantronformat',$dom,
12243:                                      $confname,'scantron','','',$custom);
12244:                     if ($result eq 'ok') {
12245:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
12246:                         $changes{'scantronformat'} = 1;
12247:                     } else {
12248:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
12249:                     }
12250:                 } else {
12251:                     $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);
12252:                 }
12253:             }
12254:         } else {
12255:             $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);
12256:         }
12257:         if ($error) {
12258:             &Apache::lonnet::logthis($error);
12259:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12260:         }
12261:     }
12262:     if (ref($domconfig{'scantron'}) eq 'HASH') {
12263:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
12264:             if ($env{'form.scantronformat_del'}) {
12265:                 $confhash{'scantron'}{'scantronformat'} = '';
12266:                 $changes{'scantronformat'} = 1;
12267:             }
12268:         }
12269:     }
12270:     if (keys(%confhash) > 0) {
12271:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
12272:                                                  $dom);
12273:         if ($putresult eq 'ok') {
12274:             if (keys(%changes) > 0) {
12275:                 if (ref($confhash{'scantron'}) eq 'HASH') {
12276:                     $resulttext = &mt('Changes made:').'<ul>';
12277:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
12278:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
12279:                     } else {
12280:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
12281:                     }
12282:                     $resulttext .= '</ul>';
12283:                 } else {
12284:                     $resulttext = &mt('Changes made to bubblesheet format file.');
12285:                 }
12286:                 $resulttext .= '</ul>';
12287:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
12288:                 if (ref($lastactref) eq 'HASH') {
12289:                     $lastactref->{'domainconfig'} = 1;
12290:                 }
12291:             } else {
12292:                 $resulttext = &mt('No changes made to bubblesheet format file');
12293:             }
12294:         } else {
12295:             $resulttext = '<span class="LC_error">'.
12296:                 &mt('An error occurred: [_1]',$putresult).'</span>';
12297:         }
12298:     } else {
12299:         $resulttext = &mt('No changes made to bubblesheet format file'); 
12300:     }
12301:     if ($errors) {
12302:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
12303:                        $errors.'</ul>';
12304:     }
12305:     return $resulttext;
12306: }
12307: 
12308: sub modify_coursecategories {
12309:     my ($dom,$lastactref,%domconfig) = @_;
12310:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
12311:         $cathash);
12312:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
12313:     my @catitems = ('unauth','auth');
12314:     my @cattypes = ('std','domonly','codesrch','none');
12315:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
12316:         $cathash = $domconfig{'coursecategories'}{'cats'};
12317:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
12318:             $changes{'togglecats'} = 1;
12319:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
12320:         }
12321:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
12322:             $changes{'categorize'} = 1;
12323:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
12324:         }
12325:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
12326:             $changes{'togglecatscomm'} = 1;
12327:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
12328:         }
12329:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
12330:             $changes{'categorizecomm'} = 1;
12331:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
12332: 
12333:         }
12334:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
12335:             $changes{'togglecatsplace'} = 1;
12336:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
12337:         }
12338:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
12339:             $changes{'categorizeplace'} = 1;
12340:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
12341:         }
12342:         foreach my $item (@catitems) {
12343:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
12344:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
12345:                     $changes{$item} = 1;
12346:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
12347:                 }
12348:             }
12349:         }
12350:     } else {
12351:         $changes{'togglecats'} = 1;
12352:         $changes{'categorize'} = 1;
12353:         $changes{'togglecatscomm'} = 1;
12354:         $changes{'categorizecomm'} = 1;
12355:         $changes{'togglecatsplace'} = 1;
12356:         $changes{'categorizeplace'} = 1;
12357:         $domconfig{'coursecategories'} = {
12358:                                              togglecats => $env{'form.togglecats'},
12359:                                              categorize => $env{'form.categorize'},
12360:                                              togglecatscomm => $env{'form.togglecatscomm'},
12361:                                              categorizecomm => $env{'form.categorizecomm'},
12362:                                              togglecatsplace => $env{'form.togglecatsplace'},
12363:                                              categorizeplace => $env{'form.categorizeplace'},
12364:                                          };
12365:         foreach my $item (@catitems) {
12366:             if ($env{'form.coursecat_'.$item} ne 'std') {
12367:                 $changes{$item} = 1;
12368:             }
12369:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
12370:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
12371:             }
12372:         }
12373:     }
12374:     if (ref($cathash) eq 'HASH') {
12375:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
12376:             push (@deletecategory,'instcode::0');
12377:         }
12378:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
12379:             push(@deletecategory,'communities::0');
12380:         }
12381:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
12382:             push(@deletecategory,'placement::0');
12383:         }
12384:     }
12385:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
12386:     if (ref($cathash) eq 'HASH') {
12387:         if (@deletecategory > 0) {
12388:             #FIXME Need to remove category from all courses using a deleted category 
12389:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
12390:             foreach my $item (@deletecategory) {
12391:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
12392:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
12393:                     $deletions{$item} = 1;
12394:                     &recurse_cat_deletes($item,$cathash,\%deletions);
12395:                 }
12396:             }
12397:         }
12398:         foreach my $item (keys(%{$cathash})) {
12399:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
12400:             if ($cathash->{$item} ne $env{'form.'.$item}) {
12401:                 $reorderings{$item} = 1;
12402:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
12403:             }
12404:             if ($env{'form.addcategory_name_'.$item} ne '') {
12405:                 my $newcat = $env{'form.addcategory_name_'.$item};
12406:                 my $newdepth = $depth+1;
12407:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
12408:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
12409:                 $adds{$newitem} = 1; 
12410:             }
12411:             if ($env{'form.subcat_'.$item} ne '') {
12412:                 my $newcat = $env{'form.subcat_'.$item};
12413:                 my $newdepth = $depth+1;
12414:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
12415:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
12416:                 $adds{$newitem} = 1;
12417:             }
12418:         }
12419:     }
12420:     if ($env{'form.instcode'} eq '1') {
12421:         if (ref($cathash) eq 'HASH') {
12422:             my $newitem = 'instcode::0';
12423:             if ($cathash->{$newitem} eq '') {  
12424:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
12425:                 $adds{$newitem} = 1;
12426:             }
12427:         } else {
12428:             my $newitem = 'instcode::0';
12429:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
12430:             $adds{$newitem} = 1;
12431:         }
12432:     }
12433:     if ($env{'form.communities'} eq '1') {
12434:         if (ref($cathash) eq 'HASH') {
12435:             my $newitem = 'communities::0';
12436:             if ($cathash->{$newitem} eq '') {
12437:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
12438:                 $adds{$newitem} = 1;
12439:             }
12440:         } else {
12441:             my $newitem = 'communities::0';
12442:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
12443:             $adds{$newitem} = 1;
12444:         }
12445:     }
12446:     if ($env{'form.placement'} eq '1') {
12447:         if (ref($cathash) eq 'HASH') {
12448:             my $newitem = 'placement::0';
12449:             if ($cathash->{$newitem} eq '') {
12450:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
12451:                 $adds{$newitem} = 1;
12452:             }
12453:         } else {
12454:             my $newitem = 'placement::0';
12455:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
12456:             $adds{$newitem} = 1;
12457:         }
12458:     }
12459:     if ($env{'form.addcategory_name'} ne '') {
12460:         if (($env{'form.addcategory_name'} ne 'instcode') &&
12461:             ($env{'form.addcategory_name'} ne 'communities') &&
12462:             ($env{'form.addcategory_name'} ne 'placement')) {
12463:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
12464:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
12465:             $adds{$newitem} = 1;
12466:         }
12467:     }
12468:     my $putresult;
12469:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12470:         if (keys(%deletions) > 0) {
12471:             foreach my $key (keys(%deletions)) {
12472:                 if ($predelallitems{$key} ne '') {
12473:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
12474:                 }
12475:             }
12476:         }
12477:         my (@chkcats,@chktrails,%chkallitems);
12478:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
12479:         if (ref($chkcats[0]) eq 'ARRAY') {
12480:             my $depth = 0;
12481:             my $chg = 0;
12482:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
12483:                 my $name = $chkcats[0][$i];
12484:                 my $item;
12485:                 if ($name eq '') {
12486:                     $chg ++;
12487:                 } else {
12488:                     $item = &escape($name).'::0';
12489:                     if ($chg) {
12490:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
12491:                     }
12492:                     $depth ++; 
12493:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
12494:                     $depth --;
12495:                 }
12496:             }
12497:         }
12498:     }
12499:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12500:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
12501:         if ($putresult eq 'ok') {
12502:             my %title = (
12503:                          togglecats     => 'Show/Hide a course in catalog',
12504:                          categorize     => 'Assign a category to a course',
12505:                          togglecatscomm => 'Show/Hide a community in catalog',
12506:                          categorizecomm => 'Assign a category to a community',
12507:                         );
12508:             my %level = (
12509:                          dom  => 'set in Domain ("Modify Course/Community")',
12510:                          crs  => 'set in Course ("Course Configuration")',
12511:                          comm => 'set in Community ("Community Configuration")',
12512:                          none     => 'No catalog',
12513:                          std      => 'Standard catalog',
12514:                          domonly  => 'Domain-only catalog',
12515:                          codesrch => 'Code search form',
12516:                         );
12517:             $resulttext = &mt('Changes made:').'<ul>';
12518:             if ($changes{'togglecats'}) {
12519:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
12520:             }
12521:             if ($changes{'categorize'}) {
12522:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
12523:             }
12524:             if ($changes{'togglecatscomm'}) {
12525:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
12526:             }
12527:             if ($changes{'categorizecomm'}) {
12528:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
12529:             }
12530:             if ($changes{'unauth'}) {
12531:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
12532:             }
12533:             if ($changes{'auth'}) {
12534:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
12535:             }
12536:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12537:                 my $cathash;
12538:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
12539:                     $cathash = $domconfig{'coursecategories'}{'cats'};
12540:                 } else {
12541:                     $cathash = {};
12542:                 } 
12543:                 my (@cats,@trails,%allitems);
12544:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
12545:                 if (keys(%deletions) > 0) {
12546:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
12547:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
12548:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
12549:                     }
12550:                     $resulttext .= '</ul></li>';
12551:                 }
12552:                 if (keys(%reorderings) > 0) {
12553:                     my %sort_by_trail;
12554:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
12555:                     foreach my $key (keys(%reorderings)) {
12556:                         if ($allitems{$key} ne '') {
12557:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
12558:                         }
12559:                     }
12560:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
12561:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
12562:                     }
12563:                     $resulttext .= '</ul></li>';
12564:                 }
12565:                 if (keys(%adds) > 0) {
12566:                     my %sort_by_trail;
12567:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
12568:                     foreach my $key (keys(%adds)) {
12569:                         if ($allitems{$key} ne '') {
12570:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
12571:                         }
12572:                     }
12573:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
12574:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
12575:                     }
12576:                     $resulttext .= '</ul></li>';
12577:                 }
12578:             }
12579:             $resulttext .= '</ul>';
12580:             if ($changes{'unauth'} || $changes{'auth'}) {
12581:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
12582:                 if ($changes{'auth'}) {
12583:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
12584:                 }
12585:                 if ($changes{'unauth'}) {
12586:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
12587:                 }
12588:                 my $cachetime = 24*60*60;
12589:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12590:                 if (ref($lastactref) eq 'HASH') {
12591:                     $lastactref->{'domdefaults'} = 1;
12592:                 }
12593:             }
12594:         } else {
12595:             $resulttext = '<span class="LC_error">'.
12596:                           &mt('An error occurred: [_1]',$putresult).'</span>';
12597:         }
12598:     } else {
12599:         $resulttext = &mt('No changes made to course and community categories');
12600:     }
12601:     return $resulttext;
12602: }
12603: 
12604: sub modify_serverstatuses {
12605:     my ($dom,%domconfig) = @_;
12606:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
12607:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
12608:         %currserverstatus = %{$domconfig{'serverstatuses'}};
12609:     }
12610:     my @pages = &serverstatus_pages();
12611:     foreach my $type (@pages) {
12612:         $newserverstatus{$type}{'namedusers'} = '';
12613:         $newserverstatus{$type}{'machines'} = '';
12614:         if (defined($env{'form.'.$type.'_namedusers'})) {
12615:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
12616:             my @okusers;
12617:             foreach my $user (@users) {
12618:                 my ($uname,$udom) = split(/:/,$user);
12619:                 if (($udom =~ /^$match_domain$/) &&   
12620:                     (&Apache::lonnet::domain($udom)) &&
12621:                     ($uname =~ /^$match_username$/)) {
12622:                     if (!grep(/^\Q$user\E/,@okusers)) {
12623:                         push(@okusers,$user);
12624:                     }
12625:                 }
12626:             }
12627:             if (@okusers > 0) {
12628:                  @okusers = sort(@okusers);
12629:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
12630:             }
12631:         }
12632:         if (defined($env{'form.'.$type.'_machines'})) {
12633:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
12634:             my @okmachines;
12635:             foreach my $ip (@machines) {
12636:                 my @parts = split(/\./,$ip);
12637:                 next if (@parts < 4);
12638:                 my $badip = 0;
12639:                 for (my $i=0; $i<4; $i++) {
12640:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
12641:                         $badip = 1;
12642:                         last;
12643:                     }
12644:                 }
12645:                 if (!$badip) {
12646:                     push(@okmachines,$ip);     
12647:                 }
12648:             }
12649:             @okmachines = sort(@okmachines);
12650:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
12651:         }
12652:     }
12653:     my %serverstatushash =  (
12654:                                 serverstatuses => \%newserverstatus,
12655:                             );
12656:     foreach my $type (@pages) {
12657:         foreach my $setting ('namedusers','machines') {
12658:             my (@current,@new);
12659:             if (ref($currserverstatus{$type}) eq 'HASH') {
12660:                 if ($currserverstatus{$type}{$setting} ne '') { 
12661:                     @current = split(/,/,$currserverstatus{$type}{$setting});
12662:                 }
12663:             }
12664:             if ($newserverstatus{$type}{$setting} ne '') {
12665:                 @new = split(/,/,$newserverstatus{$type}{$setting});
12666:             }
12667:             if (@current > 0) {
12668:                 if (@new > 0) {
12669:                     foreach my $item (@current) {
12670:                         if (!grep(/^\Q$item\E$/,@new)) {
12671:                             $changes{$type}{$setting} = 1;
12672:                             last;
12673:                         }
12674:                     }
12675:                     foreach my $item (@new) {
12676:                         if (!grep(/^\Q$item\E$/,@current)) {
12677:                             $changes{$type}{$setting} = 1;
12678:                             last;
12679:                         }
12680:                     }
12681:                 } else {
12682:                     $changes{$type}{$setting} = 1;
12683:                 }
12684:             } elsif (@new > 0) {
12685:                 $changes{$type}{$setting} = 1;
12686:             }
12687:         }
12688:     }
12689:     if (keys(%changes) > 0) {
12690:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
12691:         my $putresult = &Apache::lonnet::put_dom('configuration',
12692:                                                  \%serverstatushash,$dom);
12693:         if ($putresult eq 'ok') {
12694:             $resulttext .= &mt('Changes made:').'<ul>';
12695:             foreach my $type (@pages) {
12696:                 if (ref($changes{$type}) eq 'HASH') {
12697:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
12698:                     if ($changes{$type}{'namedusers'}) {
12699:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
12700:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
12701:                         } else {
12702:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
12703:                         }
12704:                     }
12705:                     if ($changes{$type}{'machines'}) {
12706:                         if ($newserverstatus{$type}{'machines'} eq '') {
12707:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
12708:                         } else {
12709:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
12710:                         }
12711: 
12712:                     }
12713:                     $resulttext .= '</ul></li>';
12714:                 }
12715:             }
12716:             $resulttext .= '</ul>';
12717:         } else {
12718:             $resulttext = '<span class="LC_error">'.
12719:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
12720: 
12721:         }
12722:     } else {
12723:         $resulttext = &mt('No changes made to access to server status pages');
12724:     }
12725:     return $resulttext;
12726: }
12727: 
12728: sub modify_helpsettings {
12729:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12730:     my ($resulttext,$errors,%changes,%helphash);
12731:     my %defaultchecked = ('submitbugs' => 'on');
12732:     my @offon = ('off','on');
12733:     my @toggles = ('submitbugs');
12734:     my %current = ('submitbugs' => '',
12735:                    'adhoc'      => {},
12736:                   );
12737:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
12738:         %current = %{$domconfig{'helpsettings'}};
12739:     }
12740:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12741:     foreach my $item (@toggles) {
12742:         if ($defaultchecked{$item} eq 'on') { 
12743:             if ($current{$item} eq '') {
12744:                 if ($env{'form.'.$item} eq '0') {
12745:                     $changes{$item} = 1;
12746:                 }
12747:             } elsif ($current{$item} ne $env{'form.'.$item}) {
12748:                 $changes{$item} = 1;
12749:             }
12750:         } elsif ($defaultchecked{$item} eq 'off') {
12751:             if ($current{$item} eq '') {
12752:                 if ($env{'form.'.$item} eq '1') {
12753:                     $changes{$item} = 1;
12754:                 }
12755:             } elsif ($current{$item} ne $env{'form.'.$item}) {
12756:                 $changes{$item} = 1;
12757:             }
12758:         }
12759:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
12760:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
12761:         }
12762:     }
12763:     my $maxnum = $env{'form.helproles_maxnum'};
12764:     my $confname = $dom.'-domainconfig';
12765:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
12766:     my (@allpos,%newsettings,%changedprivs,$newrole);
12767:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12768:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
12769:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
12770:     my %lt = &Apache::lonlocal::texthash(
12771:                     s      => 'system',
12772:                     d      => 'domain',
12773:                     order  => 'Display order',
12774:                     access => 'Role usage',
12775:                     all    => 'All with domain helpdesk or helpdesk assistant role',
12776:                     dh     => 'All with domain helpdesk role',
12777:                     da     => 'All with domain helpdesk assistant role',
12778:                     none   => 'None',
12779:                     status => 'Determined based on institutional status',
12780:                     inc    => 'Include all, but exclude specific personnel',
12781:                     exc    => 'Exclude all, but include specific personnel',
12782:     );
12783:     for (my $num=0; $num<=$maxnum; $num++) {
12784:         my ($prefix,$identifier,$rolename,%curr);
12785:         if ($num == $maxnum) {
12786:             next unless ($env{'form.newcusthelp'} == $maxnum);
12787:             $identifier = 'custhelp'.$num;
12788:             $prefix = 'helproles_'.$num;
12789:             $rolename = $env{'form.custhelpname'.$num};
12790:             $rolename=~s/[^A-Za-z0-9]//gs;
12791:             next if ($rolename eq '');
12792:             next if (exists($existing{'rolesdef_'.$rolename}));
12793:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
12794:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
12795:                                                      $newprivs{'c'},$confname,$dom);
12796:             if ($result ne 'ok') {
12797:                 $errors .= '<li><span class="LC_error">'.
12798:                            &mt('An error occurred storing the new custom role: [_1]',
12799:                            $result).'</span></li>';
12800:                 next;
12801:             } else {
12802:                 $changedprivs{$rolename} = \%newprivs;
12803:                 $newrole = $rolename;
12804:             }
12805:         } else {
12806:             $prefix = 'helproles_'.$num;
12807:             $rolename = $env{'form.'.$prefix};
12808:             next if ($rolename eq '');
12809:             next unless (exists($existing{'rolesdef_'.$rolename}));
12810:             $identifier = 'custhelp'.$num;
12811:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
12812:             my %currprivs;
12813:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
12814:                 split(/\_/,$existing{'rolesdef_'.$rolename});
12815:             foreach my $level ('c','d','s') {
12816:                 if ($newprivs{$level} ne $currprivs{$level}) {
12817:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
12818:                                                              $newprivs{'c'},$confname,$dom);
12819:                     if ($result ne 'ok') {
12820:                         $errors .= '<li><span class="LC_error">'.
12821:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
12822:                                        $rolename,$result).'</span></li>';
12823:                     } else {
12824:                         $changedprivs{$rolename} = \%newprivs;
12825:                     }
12826:                     last;
12827:                 }
12828:             }
12829:             if (ref($current{'adhoc'}) eq 'HASH') {
12830:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
12831:                     %curr = %{$current{'adhoc'}{$rolename}};
12832:                 }
12833:             }
12834:         }
12835:         my $newpos = $env{'form.'.$prefix.'_pos'};
12836:         $newpos =~ s/\D+//g;
12837:         $allpos[$newpos] = $rolename;
12838:         my $newdesc = $env{'form.'.$prefix.'_desc'};
12839:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
12840:         if ($curr{'desc'}) {
12841:             if ($curr{'desc'} ne $newdesc) {
12842:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
12843:                 $newsettings{$rolename}{'desc'} = $newdesc;
12844:             }
12845:         } elsif ($newdesc ne '') {
12846:             $changes{'customrole'}{$rolename}{'desc'} = 1;
12847:             $newsettings{$rolename}{'desc'} = $newdesc;
12848:         }
12849:         my $access = $env{'form.'.$prefix.'_access'};
12850:         if (grep(/^\Q$access\E$/,@accesstypes)) {
12851:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
12852:             if ($access eq 'status') {
12853:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
12854:                 if (scalar(@statuses) == 0) {
12855:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
12856:                 } else {
12857:                     my (@shownstatus,$numtypes);
12858:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
12859:                     if (ref($types) eq 'ARRAY') {
12860:                         $numtypes = scalar(@{$types});
12861:                         foreach my $type (sort(@statuses)) {
12862:                             if ($type eq 'default') {
12863:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
12864:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
12865:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
12866:                                 push(@shownstatus,$usertypes->{$type});
12867:                             }
12868:                         }
12869:                     }
12870:                     if (grep(/^default$/,@statuses)) {
12871:                         push(@shownstatus,$othertitle);
12872:                     }
12873:                     if (scalar(@shownstatus) == 1+$numtypes) {
12874:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
12875:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
12876:                     } else {
12877:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
12878:                         if (ref($curr{'status'}) eq 'ARRAY') {
12879:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
12880:                             if (@diffs) {
12881:                                 $changes{'customrole'}{$rolename}{$access} = 1;
12882:                             }
12883:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12884:                             $changes{'customrole'}{$rolename}{$access} = 1;
12885:                         }
12886:                     }
12887:                 }
12888:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
12889:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
12890:                 my @newspecstaff;
12891:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
12892:                 foreach my $person (sort(@personnel)) {
12893:                     if ($domhelpdesk{$person}) {
12894:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
12895:                     }
12896:                 }
12897:                 if (ref($curr{$access}) eq 'ARRAY') {
12898:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
12899:                     if (@diffs) {
12900:                         $changes{'customrole'}{$rolename}{$access} = 1;
12901:                     }
12902:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12903:                     $changes{'customrole'}{$rolename}{$access} = 1;
12904:                 }
12905:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12906:                     my ($uname,$udom) = split(/:/,$person);
12907:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
12908:                 }
12909:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
12910:             }
12911:         } else {
12912:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
12913:         }
12914:         unless ($curr{'access'} eq $access) {
12915:             $changes{'customrole'}{$rolename}{'access'} = 1;
12916:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
12917:         }
12918:     }
12919:     if (@allpos > 0) {
12920:         my $idx = 0;
12921:         foreach my $rolename (@allpos) {
12922:             if ($rolename ne '') {
12923:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
12924:                 if (ref($current{'adhoc'}) eq 'HASH') {
12925:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
12926:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
12927:                             $changes{'customrole'}{$rolename}{'order'} = 1;
12928:                             $newsettings{$rolename}{'order'} = $idx+1;
12929:                         }
12930:                     }
12931:                 }
12932:                 $idx ++;
12933:             }
12934:         }
12935:     }
12936:     my $putresult;
12937:     if (keys(%changes) > 0) {
12938:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
12939:         if ($putresult eq 'ok') {
12940:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
12941:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
12942:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
12943:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
12944:                 }
12945:             }
12946:             my $cachetime = 24*60*60;
12947:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12948:             if (ref($lastactref) eq 'HASH') {
12949:                 $lastactref->{'domdefaults'} = 1;
12950:             }
12951:         } else {
12952:             $errors .= '<li><span class="LC_error">'.
12953:                        &mt('An error occurred storing the settings: [_1]',
12954:                            $putresult).'</span></li>';
12955:         }
12956:     }
12957:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
12958:         $resulttext = &mt('Changes made:').'<ul>';
12959:         my (%shownprivs,@levelorder);
12960:         @levelorder = ('c','d','s');
12961:         if ((keys(%changes)) && ($putresult eq 'ok')) {
12962:             foreach my $item (sort(keys(%changes))) {
12963:                 if ($item eq 'submitbugs') {
12964:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
12965:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
12966:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
12967:                 } elsif ($item eq 'customrole') {
12968:                     if (ref($changes{'customrole'}) eq 'HASH') {
12969:                         my @keyorder = ('order','desc','access','status','exc','inc');
12970:                         my %keytext = &Apache::lonlocal::texthash(
12971:                                                                    order  => 'Order',
12972:                                                                    desc   => 'Role description',
12973:                                                                    access => 'Role usage',
12974:                                                                    status => 'Allowed instituional types',
12975:                                                                    exc    => 'Allowed personnel',
12976:                                                                    inc    => 'Disallowed personnel',
12977:                         );
12978:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
12979:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
12980:                                 if ($role eq $newrole) {
12981:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
12982:                                                               $role).'<ul>';
12983:                                 } else {
12984:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
12985:                                                               $role).'<ul>';
12986:                                 }
12987:                                 foreach my $key (@keyorder) {
12988:                                     if ($changes{'customrole'}{$role}{$key}) {
12989:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
12990:                                                                   $keytext{$key},$newsettings{$role}{$key}).
12991:                                                        '</li>';
12992:                                     }
12993:                                 }
12994:                                 if (ref($changedprivs{$role}) eq 'HASH') {
12995:                                     $shownprivs{$role} = 1;
12996:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
12997:                                     foreach my $level (@levelorder) {
12998:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
12999:                                             next if ($item eq '');
13000:                                             my ($priv) = split(/\&/,$item,2);
13001:                                             if (&Apache::lonnet::plaintext($priv)) {
13002:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
13003:                                                 unless ($level eq 'c') {
13004:                                                     $resulttext .= ' ('.$lt{$level}.')';
13005:                                                 }
13006:                                                 $resulttext .= '</li>';
13007:                                             }
13008:                                         }
13009:                                     }
13010:                                     $resulttext .= '</ul>';
13011:                                 }
13012:                                 $resulttext .= '</ul></li>';
13013:                             }
13014:                         }
13015:                     }
13016:                 }
13017:             }
13018:         }
13019:         if (keys(%changedprivs)) {
13020:             foreach my $role (sort(keys(%changedprivs))) {
13021:                 unless ($shownprivs{$role}) {
13022:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
13023:                                               $role).'<ul>'.
13024:                                    '<li>'.&mt('Privileges set to :').'<ul>';
13025:                     foreach my $level (@levelorder) {
13026:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
13027:                             next if ($item eq '');
13028:                             my ($priv) = split(/\&/,$item,2);
13029:                             if (&Apache::lonnet::plaintext($priv)) {
13030:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
13031:                                 unless ($level eq 'c') {
13032:                                     $resulttext .= ' ('.$lt{$level}.')';
13033:                                 }
13034:                                 $resulttext .= '</li>';
13035:                             }
13036:                         }
13037:                     }
13038:                     $resulttext .= '</ul></li></ul></li>';
13039:                 }
13040:             }
13041:         }
13042:         $resulttext .= '</ul>';
13043:     } else {
13044:         $resulttext = &mt('No changes made to help settings');
13045:     }
13046:     if ($errors) {
13047:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
13048:                                     $errors.'</ul>';
13049:     }
13050:     return $resulttext;
13051: }
13052: 
13053: sub modify_coursedefaults {
13054:     my ($dom,$lastactref,%domconfig) = @_;
13055:     my ($resulttext,$errors,%changes,%defaultshash);
13056:     my %defaultchecked = (
13057:                            'canuse_pdfforms' => 'off',
13058:                            'uselcmath'       => 'on',
13059:                            'usejsme'         => 'on'
13060:                          );
13061:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme');
13062:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
13063:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
13064:                    'mysqltables_official','mysqltables_unofficial','mysqltables_community',
13065:                    'mysqltables_textbook','mysqltables_placement');
13066:     my @types = ('official','unofficial','community','textbook','placement');
13067:     my %staticdefaults = (
13068:                            anonsurvey_threshold => 10,
13069:                            uploadquota          => 500,
13070:                            postsubmit           => 60,
13071:                            mysqltables          => 172800,
13072:                          );
13073: 
13074:     $defaultshash{'coursedefaults'} = {};
13075: 
13076:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
13077:         if ($domconfig{'coursedefaults'} eq '') {
13078:             $domconfig{'coursedefaults'} = {};
13079:         }
13080:     }
13081: 
13082:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
13083:         foreach my $item (@toggles) {
13084:             if ($defaultchecked{$item} eq 'on') {
13085:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
13086:                     ($env{'form.'.$item} eq '0')) {
13087:                     $changes{$item} = 1;
13088:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
13089:                     $changes{$item} = 1;
13090:                 }
13091:             } elsif ($defaultchecked{$item} eq 'off') {
13092:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
13093:                     ($env{'form.'.$item} eq '1')) {
13094:                     $changes{$item} = 1;
13095:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
13096:                     $changes{$item} = 1;
13097:                 }
13098:             }
13099:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
13100:         }
13101:         foreach my $item (@numbers) {
13102:             my ($currdef,$newdef);
13103:             $newdef = $env{'form.'.$item};
13104:             if ($item eq 'anonsurvey_threshold') {
13105:                 $currdef = $domconfig{'coursedefaults'}{$item};
13106:                 $newdef =~ s/\D//g;
13107:                 if ($newdef eq '' || $newdef < 1) {
13108:                     $newdef = 1;
13109:                 }
13110:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
13111:             } else {
13112:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
13113:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
13114:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
13115:                 }
13116:                 $newdef =~ s/[^\w.\-]//g;
13117:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
13118:             }
13119:             if ($currdef ne $newdef) {
13120:                 my $staticdef;
13121:                 if ($item eq 'anonsurvey_threshold') {
13122:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
13123:                         $changes{$item} = 1;
13124:                     }
13125:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
13126:                     my $setting = $1;
13127:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
13128:                         $changes{$setting} = 1;
13129:                     }
13130:                 }
13131:             }
13132:         }
13133:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
13134:         my @currclonecode;
13135:         if (ref($currclone) eq 'HASH') {
13136:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
13137:                 @currclonecode = @{$currclone->{'instcode'}};
13138:             }
13139:         }
13140:         my $newclone;
13141:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
13142:             $newclone = $env{'form.canclone'};
13143:         }
13144:         if ($newclone eq 'instcode') {
13145:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
13146:             my (%codedefaults,@code_order,@clonecode);
13147:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
13148:                                                     \@code_order);
13149:             foreach my $item (@code_order) {
13150:                 if (grep(/^\Q$item\E$/,@newcodes)) {
13151:                     push(@clonecode,$item);
13152:                 }
13153:             }
13154:             if (@clonecode) {
13155:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
13156:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
13157:                 if (@diffs) {
13158:                     $changes{'canclone'} = 1;
13159:                 }
13160:             } else {
13161:                 $newclone eq '';
13162:             }
13163:         } elsif ($newclone ne '') {
13164:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
13165:         }
13166:         if ($newclone ne $currclone) {
13167:             $changes{'canclone'} = 1;
13168:         }
13169:         my %credits;
13170:         foreach my $type (@types) {
13171:             unless ($type eq 'community') {
13172:                 $credits{$type} = $env{'form.'.$type.'_credits'};
13173:                 $credits{$type} =~ s/[^\d.]+//g;
13174:             }
13175:         }
13176:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
13177:             ($env{'form.coursecredits'} eq '1')) {
13178:             $changes{'coursecredits'} = 1;
13179:             foreach my $type (keys(%credits)) {
13180:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
13181:             }
13182:         } else {
13183:             if ($env{'form.coursecredits'} eq '1') {
13184:                 foreach my $type (@types) {
13185:                     unless ($type eq 'community') {
13186:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
13187:                             $changes{'coursecredits'} = 1;
13188:                         }
13189:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
13190:                     }
13191:                 }
13192:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13193:                 foreach my $type (@types) {
13194:                     unless ($type eq 'community') {
13195:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
13196:                             $changes{'coursecredits'} = 1;
13197:                             last;
13198:                         }
13199:                     }
13200:                 }
13201:             }
13202:         }
13203:         if ($env{'form.postsubmit'} eq '1') {
13204:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
13205:             my %currtimeout;
13206:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13207:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
13208:                     $changes{'postsubmit'} = 1;
13209:                 }
13210:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13211:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
13212:                 }
13213:             } else {
13214:                 $changes{'postsubmit'} = 1;
13215:             }
13216:             foreach my $type (@types) {
13217:                 my $timeout = $env{'form.'.$type.'_timeout'};
13218:                 $timeout =~ s/\D//g;
13219:                 if ($timeout == $staticdefaults{'postsubmit'}) {
13220:                     $timeout = '';
13221:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
13222:                     $timeout = '0';
13223:                 }
13224:                 unless ($timeout eq '') {
13225:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
13226:                 }
13227:                 if (exists($currtimeout{$type})) {
13228:                     if ($timeout ne $currtimeout{$type}) {
13229:                         $changes{'postsubmit'} = 1;
13230:                     }
13231:                 } elsif ($timeout ne '') {
13232:                     $changes{'postsubmit'} = 1;
13233:                 }
13234:             }
13235:         } else {
13236:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
13237:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13238:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
13239:                     $changes{'postsubmit'} = 1;
13240:                 }
13241:             } else {
13242:                 $changes{'postsubmit'} = 1;
13243:             }
13244:         }
13245:     }
13246:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13247:                                              $dom);
13248:     if ($putresult eq 'ok') {
13249:         if (keys(%changes) > 0) {
13250:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13251:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
13252:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
13253:                 ($changes{'canclone'}) || ($changes{'mysqltables'})) {
13254:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme') { 
13255:                     if ($changes{$item}) {
13256:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
13257:                     }
13258:                 }
13259:                 if ($changes{'coursecredits'}) {
13260:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13261:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
13262:                             $domdefaults{$type.'credits'} =
13263:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
13264:                         }
13265:                     }
13266:                 }
13267:                 if ($changes{'postsubmit'}) {
13268:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13269:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
13270:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13271:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
13272:                                 $domdefaults{$type.'postsubtimeout'} =
13273:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
13274:                             }
13275:                         }
13276:                     }
13277:                 }
13278:                 if ($changes{'uploadquota'}) {
13279:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
13280:                         foreach my $type (@types) {
13281:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
13282:                         }
13283:                     }
13284:                 }
13285:                 if ($changes{'canclone'}) {
13286:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
13287:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
13288:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
13289:                             if (@clonecodes) {
13290:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
13291:                             }
13292:                         }
13293:                     } else {
13294:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
13295:                     }
13296:                 }
13297:                 my $cachetime = 24*60*60;
13298:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13299:                 if (ref($lastactref) eq 'HASH') {
13300:                     $lastactref->{'domdefaults'} = 1;
13301:                 }
13302:             }
13303:             $resulttext = &mt('Changes made:').'<ul>';
13304:             foreach my $item (sort(keys(%changes))) {
13305:                 if ($item eq 'canuse_pdfforms') {
13306:                     if ($env{'form.'.$item} eq '1') {
13307:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
13308:                     } else {
13309:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
13310:                     }
13311:                 } elsif ($item eq 'uselcmath') {
13312:                     if ($env{'form.'.$item} eq '1') {
13313:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
13314:                     } else {
13315:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
13316:                     }
13317:                 } elsif ($item eq 'usejsme') {
13318:                     if ($env{'form.'.$item} eq '1') {
13319:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
13320:                     } else {
13321:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
13322:                     }
13323:                 } elsif ($item eq 'anonsurvey_threshold') {
13324:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
13325:                 } elsif ($item eq 'uploadquota') {
13326:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
13327:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
13328:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
13329:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
13330:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
13331:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
13332:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
13333:                                        '</ul>'.
13334:                                        '</li>';
13335:                     } else {
13336:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
13337:                     }
13338:                 } elsif ($item eq 'mysqltables') {
13339:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
13340:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
13341:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
13342:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
13343:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
13344:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
13345:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
13346:                                        '</ul>'.
13347:                                        '</li>';
13348:                     } else {
13349:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
13350:                     }
13351:                 } elsif ($item eq 'postsubmit') {
13352:                     if ($domdefaults{'postsubmit'} eq 'off') {
13353:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
13354:                     } else {
13355:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
13356:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13357:                             $resulttext .= &mt('durations:').'<ul>';
13358:                             foreach my $type (@types) {
13359:                                 $resulttext .= '<li>';
13360:                                 my $timeout;
13361:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13362:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
13363:                                 }
13364:                                 my $display;
13365:                                 if ($timeout eq '0') {
13366:                                     $display = &mt('unlimited');
13367:                                 } elsif ($timeout eq '') {
13368:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
13369:                                 } else {
13370:                                     $display = &mt('[quant,_1,second]',$timeout);
13371:                                 }
13372:                                 if ($type eq 'community') {
13373:                                     $resulttext .= &mt('Communities');
13374:                                 } elsif ($type eq 'official') {
13375:                                     $resulttext .= &mt('Official courses');
13376:                                 } elsif ($type eq 'unofficial') {
13377:                                     $resulttext .= &mt('Unofficial courses');
13378:                                 } elsif ($type eq 'textbook') {
13379:                                     $resulttext .= &mt('Textbook courses');
13380:                                 } elsif ($type eq 'placement') {
13381:                                     $resulttext .= &mt('Placement tests');
13382:                                 }
13383:                                 $resulttext .= ' -- '.$display.'</li>';
13384:                             }
13385:                             $resulttext .= '</ul>';
13386:                         }
13387:                         $resulttext .= '</li>';
13388:                     }
13389:                 } elsif ($item eq 'coursecredits') {
13390:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13391:                         if (($domdefaults{'officialcredits'} eq '') &&
13392:                             ($domdefaults{'unofficialcredits'} eq '') &&
13393:                             ($domdefaults{'textbookcredits'} eq '')) {
13394:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
13395:                         } else {
13396:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
13397:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
13398:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
13399:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
13400:                                            '</ul>'.
13401:                                            '</li>';
13402:                         }
13403:                     } else {
13404:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
13405:                     }
13406:                 } elsif ($item eq 'canclone') {
13407:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
13408:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
13409:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
13410:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
13411:                         }
13412:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
13413:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
13414:                     } else {
13415:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
13416:                     }
13417:                 }
13418:             }
13419:             $resulttext .= '</ul>';
13420:         } else {
13421:             $resulttext = &mt('No changes made to course defaults');
13422:         }
13423:     } else {
13424:         $resulttext = '<span class="LC_error">'.
13425:             &mt('An error occurred: [_1]',$putresult).'</span>';
13426:     }
13427:     return $resulttext;
13428: }
13429: 
13430: sub modify_selfenrollment {
13431:     my ($dom,$lastactref,%domconfig) = @_;
13432:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
13433:     my @types = ('official','unofficial','community','textbook','placement');
13434:     my %titles = &tool_titles();
13435:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
13436:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
13437:     $ordered{'default'} = ['types','registered','approval','limit'];
13438: 
13439:     my (%roles,%shown,%toplevel);
13440:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
13441: 
13442:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
13443:         if ($domconfig{'selfenrollment'} eq '') {
13444:             $domconfig{'selfenrollment'} = {};
13445:         }
13446:     }
13447:     %toplevel = (
13448:                   admin      => 'Configuration Rights',
13449:                   default    => 'Default settings',
13450:                   validation => 'Validation of self-enrollment requests',
13451:                 );
13452:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
13453: 
13454:     if (ref($ordered{'admin'}) eq 'ARRAY') {
13455:         foreach my $item (@{$ordered{'admin'}}) {
13456:             foreach my $type (@types) {
13457:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
13458:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
13459:                 } else {
13460:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
13461:                 }
13462:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
13463:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
13464:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
13465:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
13466:                             push(@{$changes{'admin'}{$type}},$item);
13467:                         }
13468:                     } else {
13469:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
13470:                             push(@{$changes{'admin'}{$type}},$item);
13471:                         }
13472:                     }
13473:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
13474:                     push(@{$changes{'admin'}{$type}},$item);
13475:                 }
13476:             }
13477:         }
13478:     }
13479: 
13480:     foreach my $item (@{$ordered{'default'}}) {
13481:         foreach my $type (@types) {
13482:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
13483:             if ($item eq 'types') {
13484:                 unless (($value eq 'all') || ($value eq 'dom')) {
13485:                     $value = '';
13486:                 }
13487:             } elsif ($item eq 'registered') {
13488:                 unless ($value eq '1') {
13489:                     $value = 0;
13490:                 }
13491:             } elsif ($item eq 'approval') {
13492:                 unless ($value =~ /^[012]$/) {
13493:                     $value = 0;
13494:                 }
13495:             } else {
13496:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
13497:                     $value = 'none';
13498:                 }
13499:             }
13500:             $selfenrollhash{'default'}{$type}{$item} = $value;
13501:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
13502:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
13503:                     if ($selfenrollhash{'default'}{$type}{$item} ne
13504:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
13505:                          push(@{$changes{'default'}{$type}},$item);
13506:                     }
13507:                 } else {
13508:                     push(@{$changes{'default'}{$type}},$item);
13509:                 }
13510:             } else {
13511:                 push(@{$changes{'default'}{$type}},$item);
13512:             }
13513:             if ($item eq 'limit') {
13514:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
13515:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
13516:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
13517:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
13518:                     }
13519:                 } else {
13520:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
13521:                 }
13522:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
13523:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
13524:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
13525:                          push(@{$changes{'default'}{$type}},'cap');
13526:                     }
13527:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
13528:                     push(@{$changes{'default'}{$type}},'cap');
13529:                 }
13530:             }
13531:         }
13532:     }
13533: 
13534:     foreach my $item (@{$itemsref}) {
13535:         if ($item eq 'fields') {
13536:             my @changed;
13537:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
13538:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
13539:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
13540:             }
13541:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
13542:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
13543:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
13544:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
13545:                 } else {
13546:                     @changed = @{$selfenrollhash{'validation'}{$item}};
13547:                 }
13548:             } else {
13549:                 @changed = @{$selfenrollhash{'validation'}{$item}};
13550:             }
13551:             if (@changed) {
13552:                 if ($selfenrollhash{'validation'}{$item}) { 
13553:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
13554:                 } else {
13555:                     $changes{'validation'}{$item} = &mt('None');
13556:                 }
13557:             }
13558:         } else {
13559:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
13560:             if ($item eq 'markup') {
13561:                if ($env{'form.selfenroll_validation_'.$item}) {
13562:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
13563:                }
13564:             }
13565:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
13566:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
13567:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
13568:                 }
13569:             }
13570:         }
13571:     }
13572: 
13573:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
13574:                                              $dom);
13575:     if ($putresult eq 'ok') {
13576:         if (keys(%changes) > 0) {
13577:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13578:             $resulttext = &mt('Changes made:').'<ul>';
13579:             foreach my $key ('admin','default','validation') {
13580:                 if (ref($changes{$key}) eq 'HASH') {
13581:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
13582:                     if ($key eq 'validation') {
13583:                         foreach my $item (@{$itemsref}) {
13584:                             if (exists($changes{$key}{$item})) {
13585:                                 if ($item eq 'markup') {
13586:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
13587:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
13588:                                 } else {  
13589:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
13590:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
13591:                                 }
13592:                             }
13593:                         }
13594:                     } else {
13595:                         foreach my $type (@types) {
13596:                             if ($type eq 'community') {
13597:                                 $roles{'1'} = &mt('Community personnel');
13598:                             } else {
13599:                                 $roles{'1'} = &mt('Course personnel');
13600:                             }
13601:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
13602:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
13603:                                     if ($key eq 'admin') {
13604:                                         my @mgrdc = ();
13605:                                         if (ref($ordered{$key}) eq 'ARRAY') {
13606:                                             foreach my $item (@{$ordered{'admin'}}) {
13607:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
13608:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
13609:                                                         push(@mgrdc,$item);
13610:                                                     }
13611:                                                 }
13612:                                             }
13613:                                             if (@mgrdc) {
13614:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
13615:                                             } else {
13616:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
13617:                                             }
13618:                                         }
13619:                                     } else {
13620:                                         if (ref($ordered{$key}) eq 'ARRAY') {
13621:                                             foreach my $item (@{$ordered{$key}}) {
13622:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
13623:                                                     $domdefaults{$type.'selfenroll'.$item} =
13624:                                                         $selfenrollhash{$key}{$type}{$item};
13625:                                                 }
13626:                                             }
13627:                                         }
13628:                                     }
13629:                                 }
13630:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
13631:                                 foreach my $item (@{$ordered{$key}}) {
13632:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
13633:                                         $resulttext .= '<li>';
13634:                                         if ($key eq 'admin') {
13635:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
13636:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
13637:                                         } else {
13638:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
13639:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
13640:                                         }
13641:                                         $resulttext .= '</li>';
13642:                                     }
13643:                                 }
13644:                                 $resulttext .= '</ul></li>';
13645:                             }
13646:                         }
13647:                         $resulttext .= '</ul></li>'; 
13648:                     }
13649:                 }
13650:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
13651:                     my $cachetime = 24*60*60;
13652:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13653:                     if (ref($lastactref) eq 'HASH') {
13654:                         $lastactref->{'domdefaults'} = 1;
13655:                     }
13656:                 }
13657:             }
13658:             $resulttext .= '</ul>';
13659:         } else {
13660:             $resulttext = &mt('No changes made to self-enrollment settings');
13661:         }
13662:     } else {
13663:         $resulttext = '<span class="LC_error">'.
13664:             &mt('An error occurred: [_1]',$putresult).'</span>';
13665:     }
13666:     return $resulttext;
13667: }
13668: 
13669: sub modify_usersessions {
13670:     my ($dom,$lastactref,%domconfig) = @_;
13671:     my @hostingtypes = ('version','excludedomain','includedomain');
13672:     my @offloadtypes = ('primary','default');
13673:     my %types = (
13674:                   remote => \@hostingtypes,
13675:                   hosted => \@hostingtypes,
13676:                   spares => \@offloadtypes,
13677:                 );
13678:     my @prefixes = ('remote','hosted','spares');
13679:     my @lcversions = &Apache::lonnet::all_loncaparevs();
13680:     my (%by_ip,%by_location,@intdoms,@instdoms);
13681:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
13682:     my @locations = sort(keys(%by_location));
13683:     my (%defaultshash,%changes);
13684:     foreach my $prefix (@prefixes) {
13685:         $defaultshash{'usersessions'}{$prefix} = {};
13686:     }
13687:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13688:     my $resulttext;
13689:     my %iphost = &Apache::lonnet::get_iphost();
13690:     foreach my $prefix (@prefixes) {
13691:         next if ($prefix eq 'spares');
13692:         foreach my $type (@{$types{$prefix}}) {
13693:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
13694:             if ($type eq 'version') {
13695:                 my $value = $env{'form.'.$prefix.'_'.$type};
13696:                 my $okvalue;
13697:                 if ($value ne '') {
13698:                     if (grep(/^\Q$value\E$/,@lcversions)) {
13699:                         $okvalue = $value;
13700:                     }
13701:                 }
13702:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
13703:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
13704:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
13705:                             if ($inuse == 0) {
13706:                                 $changes{$prefix}{$type} = 1;
13707:                             } else {
13708:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
13709:                                     $changes{$prefix}{$type} = 1;
13710:                                 }
13711:                                 if ($okvalue ne '') {
13712:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13713:                                 } 
13714:                             }
13715:                         } else {
13716:                             if (($inuse == 1) && ($okvalue ne '')) {
13717:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13718:                                 $changes{$prefix}{$type} = 1;
13719:                             }
13720:                         }
13721:                     } else {
13722:                         if (($inuse == 1) && ($okvalue ne '')) {
13723:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13724:                             $changes{$prefix}{$type} = 1;
13725:                         }
13726:                     }
13727:                 } else {
13728:                     if (($inuse == 1) && ($okvalue ne '')) {
13729:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13730:                         $changes{$prefix}{$type} = 1;
13731:                     }
13732:                 }
13733:             } else {
13734:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
13735:                 my @okvals;
13736:                 foreach my $val (@vals) {
13737:                     if ($val =~ /:/) {
13738:                         my @items = split(/:/,$val);
13739:                         foreach my $item (@items) {
13740:                             if (ref($by_location{$item}) eq 'ARRAY') {
13741:                                 push(@okvals,$item);
13742:                             }
13743:                         }
13744:                     } else {
13745:                         if (ref($by_location{$val}) eq 'ARRAY') {
13746:                             push(@okvals,$val);
13747:                         }
13748:                     }
13749:                 }
13750:                 @okvals = sort(@okvals);
13751:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
13752:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
13753:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
13754:                             if ($inuse == 0) {
13755:                                 $changes{$prefix}{$type} = 1; 
13756:                             } else {
13757:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13758:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
13759:                                 if (@changed > 0) {
13760:                                     $changes{$prefix}{$type} = 1;
13761:                                 }
13762:                             }
13763:                         } else {
13764:                             if ($inuse == 1) {
13765:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13766:                                 $changes{$prefix}{$type} = 1;
13767:                             }
13768:                         } 
13769:                     } else {
13770:                         if ($inuse == 1) {
13771:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13772:                             $changes{$prefix}{$type} = 1;
13773:                         }
13774:                     }
13775:                 } else {
13776:                     if ($inuse == 1) {
13777:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13778:                         $changes{$prefix}{$type} = 1;
13779:                     }
13780:                 }
13781:             }
13782:         }
13783:     }
13784: 
13785:     my @alldoms = &Apache::lonnet::all_domains();
13786:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
13787:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
13788:     my $savespares;
13789: 
13790:     foreach my $lonhost (sort(keys(%servers))) {
13791:         my $serverhomeID =
13792:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
13793:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
13794:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
13795:         my %spareschg;
13796:         foreach my $type (@{$types{'spares'}}) {
13797:             my @okspares;
13798:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
13799:             foreach my $server (@checked) {
13800:                 if (&Apache::lonnet::hostname($server) ne '') {
13801:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
13802:                         unless (grep(/^\Q$server\E$/,@okspares)) {
13803:                             push(@okspares,$server);
13804:                         }
13805:                     }
13806:                 }
13807:             }
13808:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
13809:             my $newspare;
13810:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
13811:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
13812:                     $newspare = $new;
13813:                 }
13814:             }
13815:             my @spares;
13816:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
13817:                 @spares = sort(@okspares,$newspare);
13818:             } else {
13819:                 @spares = sort(@okspares);
13820:             }
13821:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
13822:             if (ref($spareid{$lonhost}) eq 'HASH') {
13823:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
13824:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
13825:                     if (@diffs > 0) {
13826:                         $spareschg{$type} = 1;
13827:                     }
13828:                 }
13829:             }
13830:         }
13831:         if (keys(%spareschg) > 0) {
13832:             $changes{'spares'}{$lonhost} = \%spareschg;
13833:         }
13834:     }
13835:     $defaultshash{'usersessions'}{'offloadnow'} = {};
13836:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
13837:     my @okoffload;
13838:     if (@offloadnow) {
13839:         foreach my $server (@offloadnow) {
13840:             if (&Apache::lonnet::hostname($server) ne '') {
13841:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
13842:                     push(@okoffload,$server);
13843:                 }
13844:             }
13845:         }
13846:         if (@okoffload) {
13847:             foreach my $lonhost (@okoffload) {
13848:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
13849:             }
13850:         }
13851:     }
13852:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
13853:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
13854:             if (ref($changes{'spares'}) eq 'HASH') {
13855:                 if (keys(%{$changes{'spares'}}) > 0) {
13856:                     $savespares = 1;
13857:                 }
13858:             }
13859:         } else {
13860:             $savespares = 1;
13861:         }
13862:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
13863:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
13864:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
13865:                     $changes{'offloadnow'} = 1;
13866:                     last;
13867:                 }
13868:             }
13869:             unless ($changes{'offloadnow'}) {
13870:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
13871:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
13872:                         $changes{'offloadnow'} = 1;
13873:                         last;
13874:                     }
13875:                 }
13876:             }
13877:         } elsif (@okoffload) {
13878:             $changes{'offloadnow'} = 1;
13879:         }
13880:     } elsif (@okoffload) {
13881:         $changes{'offloadnow'} = 1;
13882:     }
13883:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
13884:     if ((keys(%changes) > 0) || ($savespares)) {
13885:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13886:                                                  $dom);
13887:         if ($putresult eq 'ok') {
13888:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
13889:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
13890:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
13891:                 }
13892:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
13893:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
13894:                 }
13895:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
13896:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
13897:                 }
13898:             }
13899:             my $cachetime = 24*60*60;
13900:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13901:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
13902:             if (ref($lastactref) eq 'HASH') {
13903:                 $lastactref->{'domdefaults'} = 1;
13904:                 $lastactref->{'usersessions'} = 1;
13905:             }
13906:             if (keys(%changes) > 0) {
13907:                 my %lt = &usersession_titles();
13908:                 $resulttext = &mt('Changes made:').'<ul>';
13909:                 foreach my $prefix (@prefixes) {
13910:                     if (ref($changes{$prefix}) eq 'HASH') {
13911:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
13912:                         if ($prefix eq 'spares') {
13913:                             if (ref($changes{$prefix}) eq 'HASH') {
13914:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
13915:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
13916:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
13917:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
13918:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
13919:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
13920:                                         foreach my $type (@{$types{$prefix}}) {
13921:                                             if ($changes{$prefix}{$lonhost}{$type}) {
13922:                                                 my $offloadto = &mt('None');
13923:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
13924:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
13925:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
13926:                                                     }
13927:                                                 }
13928:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
13929:                                             }
13930:                                         }
13931:                                     }
13932:                                     $resulttext .= '</li>';
13933:                                 }
13934:                             }
13935:                         } else {
13936:                             foreach my $type (@{$types{$prefix}}) {
13937:                                 if (defined($changes{$prefix}{$type})) {
13938:                                     my $newvalue;
13939:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
13940:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
13941:                                             if ($type eq 'version') {
13942:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
13943:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
13944:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
13945:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
13946:                                                 }
13947:                                             }
13948:                                         }
13949:                                     }
13950:                                     if ($newvalue eq '') {
13951:                                         if ($type eq 'version') {
13952:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
13953:                                         } else {
13954:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
13955:                                         }
13956:                                     } else {
13957:                                         if ($type eq 'version') {
13958:                                             $newvalue .= ' '.&mt('(or later)'); 
13959:                                         }
13960:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
13961:                                     }
13962:                                 }
13963:                             }
13964:                         }
13965:                         $resulttext .= '</ul>';
13966:                     }
13967:                 }
13968:                 if ($changes{'offloadnow'}) {
13969:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
13970:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
13971:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
13972:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
13973:                                 $resulttext .= '<li>'.$lonhost.'</li>';
13974:                             }
13975:                             $resulttext .= '</ul>';
13976:                         } else {
13977:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
13978:                         }
13979:                     } else {
13980:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
13981:                     }
13982:                 }
13983:                 $resulttext .= '</ul>';
13984:             } else {
13985:                 $resulttext = $nochgmsg;
13986:             }
13987:         } else {
13988:             $resulttext = '<span class="LC_error">'.
13989:                           &mt('An error occurred: [_1]',$putresult).'</span>';
13990:         }
13991:     } else {
13992:         $resulttext = $nochgmsg;
13993:     }
13994:     return $resulttext;
13995: }
13996: 
13997: sub modify_ssl {
13998:     my ($dom,$lastactref,%domconfig) = @_;
13999:     my (%by_ip,%by_location,@intdoms,@instdoms);
14000:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
14001:     my @locations = sort(keys(%by_location));
14002:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14003:     my (%defaultshash,%changes);
14004:     my $action = 'ssl';
14005:     my @prefixes = ('connto','connfrom','replication');
14006:     foreach my $prefix (@prefixes) {
14007:         $defaultshash{$action}{$prefix} = {};
14008:     }
14009:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14010:     my $resulttext;
14011:     my %iphost = &Apache::lonnet::get_iphost();
14012:     my @reptypes = ('certreq','nocertreq');
14013:     my @connecttypes = ('dom','intdom','other');
14014:     my %types = (
14015:                   connto      => \@connecttypes,
14016:                   connfrom    => \@connecttypes,
14017:                   replication => \@reptypes,
14018:                 );
14019:     foreach my $prefix (sort(keys(%types))) {
14020:         foreach my $type (@{$types{$prefix}}) {
14021:             if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
14022:                 my $value = 'yes';
14023:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
14024:                     $value = $env{'form.'.$prefix.'_'.$type};
14025:                 }
14026:                 if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
14027:                     if ($domconfig{$action}{$prefix}{$type} ne '') {
14028:                         if ($value ne $domconfig{$action}{$prefix}{$type}) {
14029:                             $changes{$prefix}{$type} = 1;
14030:                         }
14031:                         $defaultshash{$action}{$prefix}{$type} = $value;
14032:                     } else {
14033:                         $defaultshash{$action}{$prefix}{$type} = $value;
14034:                         $changes{$prefix}{$type} = 1;
14035:                     }
14036:                 } else {
14037:                     $defaultshash{$action}{$prefix}{$type} = $value;
14038:                     $changes{$prefix}{$type} = 1;
14039:                 }
14040:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
14041:                     delete($changes{$prefix}{$type});
14042:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
14043:                     delete($changes{$prefix}{$type});
14044:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
14045:                     delete($changes{$prefix}{$type});
14046:                 }
14047:             } elsif ($prefix eq 'replication') {
14048:                 if (@locations > 0) {
14049:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
14050:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
14051:                     my @okvals;
14052:                     foreach my $val (@vals) {
14053:                         if ($val =~ /:/) {
14054:                             my @items = split(/:/,$val);
14055:                             foreach my $item (@items) {
14056:                                 if (ref($by_location{$item}) eq 'ARRAY') {
14057:                                     push(@okvals,$item);
14058:                                 }
14059:                             }
14060:                         } else {
14061:                             if (ref($by_location{$val}) eq 'ARRAY') {
14062:                                 push(@okvals,$val);
14063:                             }
14064:                         }
14065:                     }
14066:                     @okvals = sort(@okvals);
14067:                     if (ref($domconfig{$action}) eq 'HASH') {
14068:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
14069:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
14070:                                 if ($inuse == 0) {
14071:                                     $changes{$prefix}{$type} = 1;
14072:                                 } else {
14073:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
14074:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
14075:                                     if (@changed > 0) {
14076:                                         $changes{$prefix}{$type} = 1;
14077:                                     }
14078:                                 }
14079:                             } else {
14080:                                 if ($inuse == 1) {
14081:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
14082:                                     $changes{$prefix}{$type} = 1;
14083:                                 }
14084:                             }
14085:                         } else {
14086:                             if ($inuse == 1) {
14087:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
14088:                                 $changes{$prefix}{$type} = 1;
14089:                             }
14090:                         }
14091:                     } else {
14092:                         if ($inuse == 1) {
14093:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
14094:                             $changes{$prefix}{$type} = 1;
14095:                         }
14096:                     }
14097:                 }
14098:             }
14099:         }
14100:     }
14101:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
14102:     if (keys(%changes) > 0) {
14103:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
14104:                                                  $dom);
14105:         if ($putresult eq 'ok') {
14106:             if (ref($defaultshash{$action}) eq 'HASH') {
14107:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
14108:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
14109:                 }
14110:                 if (ref($defaultshash{$action}{'connto'}) eq 'HASH') {
14111:                     $domdefaults{'connto'} = $domconfig{$action}{'connto'};
14112:                 }
14113:                 if (ref($defaultshash{$action}{'connfrom'}) eq 'HASH') {
14114:                     $domdefaults{'connfrom'} = $domconfig{$action}{'connfrom'};
14115:                 }
14116:             }
14117:             my $cachetime = 24*60*60;
14118:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14119:             if (ref($lastactref) eq 'HASH') {
14120:                 $lastactref->{'domdefaults'} = 1;
14121:             }
14122:             if (keys(%changes) > 0) {
14123:                 my %titles = &ssl_titles();
14124:                 $resulttext = &mt('Changes made:').'<ul>';
14125:                 foreach my $prefix (@prefixes) {
14126:                     if (ref($changes{$prefix}) eq 'HASH') {
14127:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
14128:                         foreach my $type (@{$types{$prefix}}) {
14129:                             if (defined($changes{$prefix}{$type})) {
14130:                                 my $newvalue;
14131:                                 if (ref($defaultshash{$action}) eq 'HASH') {
14132:                                     if (ref($defaultshash{$action}{$prefix})) {
14133:                                         if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
14134:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
14135:                                         } elsif (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
14136:                                             if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
14137:                                                 $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
14138:                                             }
14139:                                         }
14140:                                     }
14141:                                     if ($newvalue eq '') {
14142:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
14143:                                     } else {
14144:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
14145:                                     }
14146:                                 }
14147:                             }
14148:                         }
14149:                         $resulttext .= '</ul>';
14150:                     }
14151:                 }
14152:             } else {
14153:                 $resulttext = $nochgmsg;
14154:             }
14155:         } else {
14156:             $resulttext = '<span class="LC_error">'.
14157:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14158:         }
14159:     } else {
14160:         $resulttext = $nochgmsg;
14161:     }
14162:     return $resulttext;
14163: }
14164: 
14165: sub modify_trust {
14166:     my ($dom,$lastactref,%domconfig) = @_;
14167:     my (%by_ip,%by_location,@intdoms,@instdoms);
14168:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
14169:     my @locations = sort(keys(%by_location));
14170:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
14171:     my @types = ('exc','inc');
14172:     my (%defaultshash,%changes);
14173:     foreach my $prefix (@prefixes) {
14174:         $defaultshash{'trust'}{$prefix} = {};
14175:     }
14176:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14177:     my $resulttext;
14178:     foreach my $prefix (@prefixes) {
14179:         foreach my $type (@types) {
14180:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
14181:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
14182:             my @okvals;
14183:             foreach my $val (@vals) {
14184:                 if ($val =~ /:/) {
14185:                     my @items = split(/:/,$val);
14186:                     foreach my $item (@items) {
14187:                         if (ref($by_location{$item}) eq 'ARRAY') {
14188:                             push(@okvals,$item);
14189:                         }
14190:                     }
14191:                 } else {
14192:                     if (ref($by_location{$val}) eq 'ARRAY') {
14193:                         push(@okvals,$val);
14194:                     }
14195:                 }
14196:             }
14197:             @okvals = sort(@okvals);
14198:             if (ref($domconfig{'trust'}) eq 'HASH') {
14199:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
14200:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
14201:                         if ($inuse == 0) {
14202:                             $changes{$prefix}{$type} = 1;
14203:                         } else {
14204:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
14205:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
14206:                             if (@changed > 0) {
14207:                                 $changes{$prefix}{$type} = 1;
14208:                             }
14209:                         }
14210:                     } else {
14211:                         if ($inuse == 1) {
14212:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
14213:                             $changes{$prefix}{$type} = 1;
14214:                         }
14215:                     }
14216:                 } else {
14217:                     if ($inuse == 1) {
14218:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
14219:                         $changes{$prefix}{$type} = 1;
14220:                     }
14221:                 }
14222:             } else {
14223:                 if ($inuse == 1) {
14224:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
14225:                     $changes{$prefix}{$type} = 1;
14226:                 }
14227:             }
14228:         }
14229:     }
14230:     my $nochgmsg = &mt('No changes made to trust settings.');
14231:     if (keys(%changes) > 0) {
14232:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
14233:                                                  $dom);
14234:         if ($putresult eq 'ok') {
14235:             if (ref($defaultshash{'trust'}) eq 'HASH') {
14236:                 foreach my $prefix (@prefixes) {
14237:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
14238:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
14239:                     }
14240:                 }
14241:             }
14242:             my $cachetime = 24*60*60;
14243:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14244:             if (ref($lastactref) eq 'HASH') {
14245:                 $lastactref->{'domdefaults'} = 1;
14246:             }
14247:             if (keys(%changes) > 0) {
14248:                 my %lt = &trust_titles();
14249:                 $resulttext = &mt('Changes made:').'<ul>';
14250:                 foreach my $prefix (@prefixes) {
14251:                     if (ref($changes{$prefix}) eq 'HASH') {
14252:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
14253:                         foreach my $type (@types) {
14254:                             if (defined($changes{$prefix}{$type})) {
14255:                                 my $newvalue;
14256:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
14257:                                     if (ref($defaultshash{'trust'}{$prefix})) {
14258:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
14259:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
14260:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
14261:                                             }
14262:                                         }
14263:                                     }
14264:                                 }
14265:                                 if ($newvalue eq '') {
14266:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
14267:                                 } else {
14268:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
14269:                                 }
14270:                             }
14271:                         }
14272:                         $resulttext .= '</ul>';
14273:                     }
14274:                 }
14275:                 $resulttext .= '</ul>';
14276:             } else {
14277:                 $resulttext = $nochgmsg;
14278:             }
14279:         } else {
14280:             $resulttext = '<span class="LC_error">'.
14281:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14282:         }
14283:     } else {
14284:         $resulttext = $nochgmsg;
14285:     }
14286:     return $resulttext;
14287: }
14288: 
14289: sub modify_loadbalancing {
14290:     my ($dom,%domconfig) = @_;
14291:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
14292:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
14293:     my ($othertitle,$usertypes,$types) =
14294:         &Apache::loncommon::sorted_inst_types($dom);
14295:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14296:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
14297:     my @sparestypes = ('primary','default');
14298:     my %typetitles = &sparestype_titles();
14299:     my $resulttext;
14300:     my (%currbalancer,%currtargets,%currrules,%existing);
14301:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
14302:         %existing = %{$domconfig{'loadbalancing'}};
14303:     }
14304:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
14305:                               \%currtargets,\%currrules);
14306:     my ($saveloadbalancing,%defaultshash,%changes);
14307:     my ($alltypes,$othertypes,$titles) =
14308:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
14309:     my %ruletitles = &offloadtype_text();
14310:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
14311:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
14312:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
14313:         if ($balancer eq '') {
14314:             next;
14315:         }
14316:         if (!exists($servers{$balancer})) {
14317:             if (exists($currbalancer{$balancer})) {
14318:                 push(@{$changes{'delete'}},$balancer);
14319:             }
14320:             next;
14321:         }
14322:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
14323:             push(@{$changes{'delete'}},$balancer);
14324:             next;
14325:         }
14326:         if (!exists($currbalancer{$balancer})) {
14327:             push(@{$changes{'add'}},$balancer);
14328:         }
14329:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
14330:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
14331:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
14332:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
14333:             $saveloadbalancing = 1;
14334:         }
14335:         foreach my $sparetype (@sparestypes) {
14336:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
14337:             my @offloadto;
14338:             foreach my $target (@targets) {
14339:                 if (($servers{$target}) && ($target ne $balancer)) {
14340:                     if ($sparetype eq 'default') {
14341:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
14342:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
14343:                         }
14344:                     }
14345:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
14346:                         push(@offloadto,$target);
14347:                     }
14348:                 }
14349:             }
14350:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
14351:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
14352:                     push(@offloadto,$balancer);
14353:                 }
14354:             }
14355:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
14356:         }
14357:         if (ref($currtargets{$balancer}) eq 'HASH') {
14358:             foreach my $sparetype (@sparestypes) {
14359:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
14360:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
14361:                     if (@targetdiffs > 0) {
14362:                         $changes{'curr'}{$balancer}{'targets'} = 1;
14363:                     }
14364:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14365:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14366:                         $changes{'curr'}{$balancer}{'targets'} = 1;
14367:                     }
14368:                 }
14369:             }
14370:         } else {
14371:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
14372:                 foreach my $sparetype (@sparestypes) {
14373:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14374:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14375:                             $changes{'curr'}{$balancer}{'targets'} = 1;
14376:                         }
14377:                     }
14378:                 }
14379:             }
14380:         }
14381:         my $ishomedom;
14382:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
14383:             $ishomedom = 1;
14384:         }
14385:         if (ref($alltypes) eq 'ARRAY') {
14386:             foreach my $type (@{$alltypes}) {
14387:                 my $rule;
14388:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
14389:                          (!$ishomedom)) {
14390:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
14391:                 }
14392:                 if ($rule eq 'specific') {
14393:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
14394:                     if (exists($servers{$specifiedhost})) {
14395:                         $rule = $specifiedhost;
14396:                     }
14397:                 }
14398:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
14399:                 if (ref($currrules{$balancer}) eq 'HASH') {
14400:                     if ($rule ne $currrules{$balancer}{$type}) {
14401:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
14402:                     }
14403:                 } elsif ($rule ne '') {
14404:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
14405:                 }
14406:             }
14407:         }
14408:     }
14409:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
14410:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
14411:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
14412:             $defaultshash{'loadbalancing'} = {};
14413:         }
14414:         my $putresult = &Apache::lonnet::put_dom('configuration',
14415:                                                  \%defaultshash,$dom);
14416:         if ($putresult eq 'ok') {
14417:             if (keys(%changes) > 0) {
14418:                 my %toupdate;
14419:                 if (ref($changes{'delete'}) eq 'ARRAY') {
14420:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
14421:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
14422:                         $toupdate{$balancer} = 1;
14423:                     }
14424:                 }
14425:                 if (ref($changes{'add'}) eq 'ARRAY') {
14426:                     foreach my $balancer (sort(@{$changes{'add'}})) {
14427:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
14428:                         $toupdate{$balancer} = 1;
14429:                     }
14430:                 }
14431:                 if (ref($changes{'curr'}) eq 'HASH') {
14432:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
14433:                         $toupdate{$balancer} = 1;
14434:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
14435:                             if ($changes{'curr'}{$balancer}{'targets'}) {
14436:                                 my %offloadstr;
14437:                                 foreach my $sparetype (@sparestypes) {
14438:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14439:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14440:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
14441:                                         }
14442:                                     }
14443:                                 }
14444:                                 if (keys(%offloadstr) == 0) {
14445:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
14446:                                 } else {
14447:                                     my $showoffload;
14448:                                     foreach my $sparetype (@sparestypes) {
14449:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
14450:                                         if (defined($offloadstr{$sparetype})) {
14451:                                             $showoffload .= $offloadstr{$sparetype};
14452:                                         } else {
14453:                                             $showoffload .= &mt('None');
14454:                                         }
14455:                                         $showoffload .= ('&nbsp;'x3);
14456:                                     }
14457:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
14458:                                 }
14459:                             }
14460:                         }
14461:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
14462:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
14463:                                 foreach my $type (@{$alltypes}) {
14464:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
14465:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
14466:                                         my $balancetext;
14467:                                         if ($rule eq '') {
14468:                                             $balancetext =  $ruletitles{'default'};
14469:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
14470:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
14471:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
14472:                                                 foreach my $sparetype (@sparestypes) {
14473:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14474:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
14475:                                                     }
14476:                                                 }
14477:                                                 foreach my $item (@{$alltypes}) {
14478:                                                     next if ($item =~  /^_LC_ipchange/);
14479:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
14480:                                                     if ($hasrule eq 'homeserver') {
14481:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
14482:                                                     } else {
14483:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
14484:                                                             if ($servers{$hasrule}) {
14485:                                                                 $toupdate{$hasrule} = 1;
14486:                                                             }
14487:                                                         }
14488:                                                     }
14489:                                                 }
14490:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
14491:                                                     $balancetext =  $ruletitles{$rule};
14492:                                                 } else {
14493:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
14494:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
14495:                                                     if ($receiver) {
14496:                                                         $toupdate{$receiver};
14497:                                                     }
14498:                                                 }
14499:                                             } else {
14500:                                                 $balancetext =  $ruletitles{$rule};
14501:                                             }
14502:                                         } else {
14503:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
14504:                                         }
14505:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
14506:                                     }
14507:                                 }
14508:                             }
14509:                         }
14510:                         if (keys(%toupdate)) {
14511:                             my %thismachine;
14512:                             my $updatedhere;
14513:                             my $cachetime = 60*60*24;
14514:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
14515:                             foreach my $lonhost (keys(%toupdate)) {
14516:                                 if ($thismachine{$lonhost}) {
14517:                                     unless ($updatedhere) {
14518:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
14519:                                                                       $defaultshash{'loadbalancing'},
14520:                                                                       $cachetime);
14521:                                         $updatedhere = 1;
14522:                                     }
14523:                                 } else {
14524:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
14525:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
14526:                                 }
14527:                             }
14528:                         }
14529:                     }
14530:                 }
14531:                 if ($resulttext ne '') {
14532:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
14533:                 } else {
14534:                     $resulttext = $nochgmsg;
14535:                 }
14536:             } else {
14537:                 $resulttext = $nochgmsg;
14538:             }
14539:         } else {
14540:             $resulttext = '<span class="LC_error">'.
14541:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14542:         }
14543:     } else {
14544:         $resulttext = $nochgmsg;
14545:     }
14546:     return $resulttext;
14547: }
14548: 
14549: sub recurse_check {
14550:     my ($chkcats,$categories,$depth,$name) = @_;
14551:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
14552:         my $chg = 0;
14553:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
14554:             my $category = $chkcats->[$depth]{$name}[$j];
14555:             my $item;
14556:             if ($category eq '') {
14557:                 $chg ++;
14558:             } else {
14559:                 my $deeper = $depth + 1;
14560:                 $item = &escape($category).':'.&escape($name).':'.$depth;
14561:                 if ($chg) {
14562:                     $categories->{$item} -= $chg;
14563:                 }
14564:                 &recurse_check($chkcats,$categories,$deeper,$category);
14565:                 $deeper --;
14566:             }
14567:         }
14568:     }
14569:     return;
14570: }
14571: 
14572: sub recurse_cat_deletes {
14573:     my ($item,$coursecategories,$deletions) = @_;
14574:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
14575:     my $subdepth = $depth + 1;
14576:     if (ref($coursecategories) eq 'HASH') {
14577:         foreach my $subitem (keys(%{$coursecategories})) {
14578:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
14579:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
14580:                 delete($coursecategories->{$subitem});
14581:                 $deletions->{$subitem} = 1;
14582:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
14583:             }
14584:         }
14585:     }
14586:     return;
14587: }
14588: 
14589: sub active_dc_picker {
14590:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
14591:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
14592:     my @domcoord = keys(%domcoords);
14593:     if (keys(%currhash)) {
14594:         foreach my $dc (keys(%currhash)) {
14595:             unless (exists($domcoords{$dc})) {
14596:                 push(@domcoord,$dc);
14597:             }
14598:         }
14599:     }
14600:     @domcoord = sort(@domcoord);
14601:     my $numdcs = scalar(@domcoord);
14602:     my $rows = 0;
14603:     my $table;
14604:     if ($numdcs > 1) {
14605:         $table = '<table>';
14606:         for (my $i=0; $i<@domcoord; $i++) {
14607:             my $rem = $i%($numinrow);
14608:             if ($rem == 0) {
14609:                 if ($i > 0) {
14610:                     $table .= '</tr>';
14611:                 }
14612:                 $table .= '<tr>';
14613:                 $rows ++;
14614:             }
14615:             my $check = '';
14616:             if ($inputtype eq 'radio') {
14617:                 if (keys(%currhash) == 0) {
14618:                     if (!$i) {
14619:                         $check = ' checked="checked"';
14620:                     }
14621:                 } elsif (exists($currhash{$domcoord[$i]})) {
14622:                     $check = ' checked="checked"';
14623:                 }
14624:             } else {
14625:                 if (exists($currhash{$domcoord[$i]})) {
14626:                     $check = ' checked="checked"';
14627:                 }
14628:             }
14629:             if ($i == @domcoord - 1) {
14630:                 my $colsleft = $numinrow - $rem;
14631:                 if ($colsleft > 1) {
14632:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
14633:                 } else {
14634:                     $table .= '<td class="LC_left_item">';
14635:                 }
14636:             } else {
14637:                 $table .= '<td class="LC_left_item">';
14638:             }
14639:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
14640:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
14641:             $table .= '<span class="LC_nobreak"><label>'.
14642:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
14643:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
14644:             if ($user ne $dcname.':'.$dcdom) {
14645:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14646:             }
14647:             $table .= '</label></span></td>';
14648:         }
14649:         $table .= '</tr></table>';
14650:     } elsif ($numdcs == 1) {
14651:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
14652:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
14653:         if ($inputtype eq 'radio') {
14654:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
14655:             if ($user ne $dcname.':'.$dcdom) {
14656:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14657:             }
14658:         } else {
14659:             my $check;
14660:             if (exists($currhash{$domcoord[0]})) {
14661:                 $check = ' checked="checked"';
14662:             }
14663:             $table = '<span class="LC_nobreak"><label>'.
14664:                      '<input type="checkbox" name="'.$name.'" '.
14665:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
14666:             if ($user ne $dcname.':'.$dcdom) {
14667:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14668:             }
14669:             $table .= '</label></span>';
14670:             $rows ++;
14671:         }
14672:     }
14673:     return ($numdcs,$table,$rows);
14674: }
14675: 
14676: sub usersession_titles {
14677:     return &Apache::lonlocal::texthash(
14678:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
14679:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
14680:                spares => 'Servers offloaded to, when busy',
14681:                version => 'LON-CAPA version requirement',
14682:                excludedomain => 'Allow all, but exclude specific domains',
14683:                includedomain => 'Deny all, but include specific domains',
14684:                primary => 'Primary (checked first)',
14685:                default => 'Default',
14686:            );
14687: }
14688: 
14689: sub id_for_thisdom {
14690:     my (%servers) = @_;
14691:     my %altids;
14692:     foreach my $server (keys(%servers)) {
14693:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
14694:         if ($serverhome ne $server) {
14695:             $altids{$serverhome} = $server;
14696:         }
14697:     }
14698:     return %altids;
14699: }
14700: 
14701: sub count_servers {
14702:     my ($currbalancer,%servers) = @_;
14703:     my (@spares,$numspares);
14704:     foreach my $lonhost (sort(keys(%servers))) {
14705:         next if ($currbalancer eq $lonhost);
14706:         push(@spares,$lonhost);
14707:     }
14708:     if ($currbalancer) {
14709:         $numspares = scalar(@spares);
14710:     } else {
14711:         $numspares = scalar(@spares) - 1;
14712:     }
14713:     return ($numspares,@spares);
14714: }
14715: 
14716: sub lonbalance_targets_js {
14717:     my ($dom,$types,$servers,$settings) = @_;
14718:     my $select = &mt('Select');
14719:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
14720:     if (ref($servers) eq 'HASH') {
14721:         $alltargets = join("','",sort(keys(%{$servers})));
14722:         my @homedoms;
14723:         foreach my $server (sort(keys(%{$servers}))) {
14724:             if (&Apache::lonnet::host_domain($server) eq $dom) {
14725:                 push(@homedoms,'1');
14726:             } else {
14727:                 push(@homedoms,'0');
14728:             }
14729:         }
14730:         $allishome = join("','",@homedoms);
14731:     }
14732:     if (ref($types) eq 'ARRAY') {
14733:         if (@{$types} > 0) {
14734:             @alltypes = @{$types};
14735:         }
14736:     }
14737:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
14738:     $allinsttypes = join("','",@alltypes);
14739:     my (%currbalancer,%currtargets,%currrules,%existing);
14740:     if (ref($settings) eq 'HASH') {
14741:         %existing = %{$settings};
14742:     }
14743:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
14744:                               \%currtargets,\%currrules);
14745:     my $balancers = join("','",sort(keys(%currbalancer)));
14746:     return <<"END";
14747: 
14748: <script type="text/javascript">
14749: // <![CDATA[
14750: 
14751: currBalancers = new Array('$balancers');
14752: 
14753: function toggleTargets(balnum) {
14754:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
14755:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
14756:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
14757:     var prevbalancer = prevhostitem.value;
14758:     var baltotal = document.getElementById('loadbalancing_total').value;
14759:     prevhostitem.value = balancer;
14760:     if (prevbalancer != '') {
14761:         var prevIdx = currBalancers.indexOf(prevbalancer);
14762:         if (prevIdx != -1) {
14763:             currBalancers.splice(prevIdx,1);
14764:         }
14765:     }
14766:     if (balancer == '') {
14767:         hideSpares(balnum);
14768:     } else {
14769:         var currIdx = currBalancers.indexOf(balancer);
14770:         if (currIdx == -1) {
14771:             currBalancers.push(balancer);
14772:         }
14773:         var homedoms = new Array('$allishome');
14774:         var ishomedom = homedoms[lonhostitem.selectedIndex];
14775:         showSpares(balancer,ishomedom,balnum);
14776:     }
14777:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
14778:     return;
14779: }
14780: 
14781: function showSpares(balancer,ishomedom,balnum) {
14782:     var alltargets = new Array('$alltargets');
14783:     var insttypes = new Array('$allinsttypes');
14784:     var offloadtypes = new Array('primary','default');
14785: 
14786:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
14787:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
14788:  
14789:     for (var i=0; i<offloadtypes.length; i++) {
14790:         var count = 0;
14791:         for (var j=0; j<alltargets.length; j++) {
14792:             if (alltargets[j] != balancer) {
14793:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
14794:                 item.value = alltargets[j];
14795:                 item.style.textAlign='left';
14796:                 item.style.textFace='normal';
14797:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
14798:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
14799:                     item.disabled = '';
14800:                 } else {
14801:                     item.disabled = 'disabled';
14802:                     item.checked = false;
14803:                 }
14804:                 count ++;
14805:             }
14806:         }
14807:     }
14808:     for (var k=0; k<insttypes.length; k++) {
14809:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
14810:             if (ishomedom == 1) {
14811:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
14812:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
14813:             } else {
14814:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
14815:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
14816:             }
14817:         } else {
14818:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
14819:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
14820:         }
14821:         if ((insttypes[k] != '_LC_external') && 
14822:             ((insttypes[k] != '_LC_internetdom') ||
14823:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
14824:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
14825:             item.options.length = 0;
14826:             item.options[0] = new Option("","",true,true);
14827:             var idx = 0;
14828:             for (var m=0; m<alltargets.length; m++) {
14829:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
14830:                     idx ++;
14831:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
14832:                 }
14833:             }
14834:         }
14835:     }
14836:     return;
14837: }
14838: 
14839: function hideSpares(balnum) {
14840:     var alltargets = new Array('$alltargets');
14841:     var insttypes = new Array('$allinsttypes');
14842:     var offloadtypes = new Array('primary','default');
14843: 
14844:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
14845:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
14846: 
14847:     var total = alltargets.length - 1;
14848:     for (var i=0; i<offloadtypes; i++) {
14849:         for (var j=0; j<total; j++) {
14850:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
14851:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
14852:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
14853:         }
14854:     }
14855:     for (var k=0; k<insttypes.length; k++) {
14856:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
14857:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
14858:         if (insttypes[k] != '_LC_external') {
14859:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
14860:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
14861:         }
14862:     }
14863:     return;
14864: }
14865: 
14866: function checkOffloads(item,balnum,type) {
14867:     var alltargets = new Array('$alltargets');
14868:     var offloadtypes = new Array('primary','default');
14869:     if (item.checked) {
14870:         var total = alltargets.length - 1;
14871:         var other;
14872:         if (type == offloadtypes[0]) {
14873:             other = offloadtypes[1];
14874:         } else {
14875:             other = offloadtypes[0];
14876:         }
14877:         for (var i=0; i<total; i++) {
14878:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
14879:             if (server == item.value) {
14880:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
14881:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
14882:                 }
14883:             }
14884:         }
14885:     }
14886:     return;
14887: }
14888: 
14889: function singleServerToggle(balnum,type) {
14890:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
14891:     if (offloadtoSelIdx == 0) {
14892:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
14893:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
14894: 
14895:     } else {
14896:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
14897:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
14898:     }
14899:     return;
14900: }
14901: 
14902: function balanceruleChange(formname,balnum,type) {
14903:     if (type == '_LC_external') {
14904:         return;
14905:     }
14906:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
14907:     for (var i=0; i<typesRules.length; i++) {
14908:         if (formname.elements[typesRules[i]].checked) {
14909:             if (formname.elements[typesRules[i]].value != 'specific') {
14910:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
14911:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
14912:             } else {
14913:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
14914:             }
14915:         }
14916:     }
14917:     return;
14918: }
14919: 
14920: function balancerDeleteChange(balnum) {
14921:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
14922:     var baltotal = document.getElementById('loadbalancing_total').value;
14923:     var addtarget;
14924:     var removetarget;
14925:     var action = 'delete';
14926:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
14927:         var lonhost = hostitem.value;
14928:         var currIdx = currBalancers.indexOf(lonhost);
14929:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
14930:             if (currIdx != -1) {
14931:                 currBalancers.splice(currIdx,1);
14932:             }
14933:             addtarget = lonhost;
14934:         } else {
14935:             if (currIdx == -1) {
14936:                 currBalancers.push(lonhost);
14937:             }
14938:             removetarget = lonhost;
14939:             action = 'undelete';
14940:         }
14941:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
14942:     }
14943:     return;
14944: }
14945: 
14946: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
14947:     if (baltotal > 1) {
14948:         var offloadtypes = new Array('primary','default');
14949:         var alltargets = new Array('$alltargets');
14950:         var insttypes = new Array('$allinsttypes');
14951:         for (var i=0; i<baltotal; i++) {
14952:             if (i != balnum) {
14953:                 for (var j=0; j<offloadtypes.length; j++) {
14954:                     var total = alltargets.length - 1;
14955:                     for (var k=0; k<total; k++) {
14956:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
14957:                         var server = serveritem.value;
14958:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
14959:                             if (server == addtarget) {
14960:                                 serveritem.disabled = '';
14961:                             }
14962:                         }
14963:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
14964:                             if (server == removetarget) {
14965:                                 serveritem.disabled = 'disabled';
14966:                                 serveritem.checked = false;
14967:                             }
14968:                         }
14969:                     }
14970:                 }
14971:                 for (var j=0; j<insttypes.length; j++) {
14972:                     if (insttypes[j] != '_LC_external') {
14973:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
14974:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
14975:                             var currSel = singleserver.selectedIndex;
14976:                             var currVal = singleserver.options[currSel].value;
14977:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
14978:                                 var numoptions = singleserver.options.length;
14979:                                 var needsnew = 1;
14980:                                 for (var k=0; k<numoptions; k++) {
14981:                                     if (singleserver.options[k] == addtarget) {
14982:                                         needsnew = 0;
14983:                                         break;
14984:                                     }
14985:                                 }
14986:                                 if (needsnew == 1) {
14987:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
14988:                                 }
14989:                             }
14990:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
14991:                                 singleserver.options.length = 0;
14992:                                 if ((currVal) && (currVal != removetarget)) {
14993:                                     singleserver.options[0] = new Option("","",false,false);
14994:                                 } else {
14995:                                     singleserver.options[0] = new Option("","",true,true);
14996:                                 }
14997:                                 var idx = 0;
14998:                                 for (var m=0; m<alltargets.length; m++) {
14999:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
15000:                                         idx ++;
15001:                                         if (currVal == alltargets[m]) {
15002:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
15003:                                         } else {
15004:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
15005:                                         }
15006:                                     }
15007:                                 }
15008:                             }
15009:                         }
15010:                     }
15011:                 }
15012:             }
15013:         }
15014:     }
15015:     return;
15016: }
15017: 
15018: // ]]>
15019: </script>
15020: 
15021: END
15022: }
15023: 
15024: sub new_spares_js {
15025:     my @sparestypes = ('primary','default');
15026:     my $types = join("','",@sparestypes);
15027:     my $select = &mt('Select');
15028:     return <<"END";
15029: 
15030: <script type="text/javascript">
15031: // <![CDATA[
15032: 
15033: function updateNewSpares(formname,lonhost) {
15034:     var types = new Array('$types');
15035:     var include = new Array();
15036:     var exclude = new Array();
15037:     for (var i=0; i<types.length; i++) {
15038:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
15039:         for (var j=0; j<spareboxes.length; j++) {
15040:             if (formname.elements[spareboxes[j]].checked) {
15041:                 exclude.push(formname.elements[spareboxes[j]].value);
15042:             } else {
15043:                 include.push(formname.elements[spareboxes[j]].value);
15044:             }
15045:         }
15046:     }
15047:     for (var i=0; i<types.length; i++) {
15048:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
15049:         var selIdx = newSpare.selectedIndex;
15050:         var currnew = newSpare.options[selIdx].value;
15051:         var okSpares = new Array();
15052:         for (var j=0; j<newSpare.options.length; j++) {
15053:             var possible = newSpare.options[j].value;
15054:             if (possible != '') {
15055:                 if (exclude.indexOf(possible) == -1) {
15056:                     okSpares.push(possible);
15057:                 } else {
15058:                     if (currnew == possible) {
15059:                         selIdx = 0;
15060:                     }
15061:                 }
15062:             }
15063:         }
15064:         for (var k=0; k<include.length; k++) {
15065:             if (okSpares.indexOf(include[k]) == -1) {
15066:                 okSpares.push(include[k]);
15067:             }
15068:         }
15069:         okSpares.sort();
15070:         newSpare.options.length = 0;
15071:         if (selIdx == 0) {
15072:             newSpare.options[0] = new Option("$select","",true,true);
15073:         } else {
15074:             newSpare.options[0] = new Option("$select","",false,false);
15075:         }
15076:         for (var m=0; m<okSpares.length; m++) {
15077:             var idx = m+1;
15078:             var selThis = 0;
15079:             if (selIdx != 0) {
15080:                 if (okSpares[m] == currnew) {
15081:                     selThis = 1;
15082:                 }
15083:             }
15084:             if (selThis == 1) {
15085:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
15086:             } else {
15087:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
15088:             }
15089:         }
15090:     }
15091:     return;
15092: }
15093: 
15094: function checkNewSpares(lonhost,type) {
15095:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
15096:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
15097:     if (chosen != '') { 
15098:         var othertype;
15099:         var othernewSpare;
15100:         if (type == 'primary') {
15101:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
15102:         }
15103:         if (type == 'default') {
15104:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
15105:         }
15106:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
15107:             othernewSpare.selectedIndex = 0;
15108:         }
15109:     }
15110:     return;
15111: }
15112: 
15113: // ]]>
15114: </script>
15115: 
15116: END
15117: 
15118: }
15119: 
15120: sub common_domprefs_js {
15121:     return <<"END";
15122: 
15123: <script type="text/javascript">
15124: // <![CDATA[
15125: 
15126: function getIndicesByName(formname,item) {
15127:     var group = new Array();
15128:     for (var i=0;i<formname.elements.length;i++) {
15129:         if (formname.elements[i].name == item) {
15130:             group.push(formname.elements[i].id);
15131:         }
15132:     }
15133:     return group;
15134: }
15135: 
15136: // ]]>
15137: </script>
15138: 
15139: END
15140: 
15141: }
15142: 
15143: sub recaptcha_js {
15144:     my %lt = &captcha_phrases();
15145:     return <<"END";
15146: 
15147: <script type="text/javascript">
15148: // <![CDATA[
15149: 
15150: function updateCaptcha(caller,context) {
15151:     var privitem;
15152:     var pubitem;
15153:     var privtext;
15154:     var pubtext;
15155:     var versionitem;
15156:     var versiontext;
15157:     if (document.getElementById(context+'_recaptchapub')) {
15158:         pubitem = document.getElementById(context+'_recaptchapub');
15159:     } else {
15160:         return;
15161:     }
15162:     if (document.getElementById(context+'_recaptchapriv')) {
15163:         privitem = document.getElementById(context+'_recaptchapriv');
15164:     } else {
15165:         return;
15166:     }
15167:     if (document.getElementById(context+'_recaptchapubtxt')) {
15168:         pubtext = document.getElementById(context+'_recaptchapubtxt');
15169:     } else {
15170:         return;
15171:     }
15172:     if (document.getElementById(context+'_recaptchaprivtxt')) {
15173:         privtext = document.getElementById(context+'_recaptchaprivtxt');
15174:     } else {
15175:         return;
15176:     }
15177:     if (document.getElementById(context+'_recaptchaversion')) {
15178:         versionitem = document.getElementById(context+'_recaptchaversion');
15179:     } else {
15180:         return;
15181:     }
15182:     if (document.getElementById(context+'_recaptchavertxt')) {
15183:         versiontext = document.getElementById(context+'_recaptchavertxt');
15184:     } else {
15185:         return;
15186:     }
15187:     if (caller.checked) {
15188:         if (caller.value == 'recaptcha') {
15189:             pubitem.type = 'text';
15190:             privitem.type = 'text';
15191:             pubitem.size = '40';
15192:             privitem.size = '40';
15193:             pubtext.innerHTML = "$lt{'pub'}";
15194:             privtext.innerHTML = "$lt{'priv'}";
15195:             versionitem.type = 'text';
15196:             versionitem.size = '3';
15197:             versiontext.innerHTML = "$lt{'ver'}";
15198:         } else {
15199:             pubitem.type = 'hidden';
15200:             privitem.type = 'hidden';
15201:             versionitem.type = 'hidden';
15202:             pubtext.innerHTML = '';
15203:             privtext.innerHTML = '';
15204:             versiontext.innerHTML = '';
15205:         }
15206:     }
15207:     return;
15208: }
15209: 
15210: // ]]>
15211: </script>
15212: 
15213: END
15214: 
15215: }
15216: 
15217: sub toggle_display_js {
15218:     return <<"END";
15219: 
15220: <script type="text/javascript">
15221: // <![CDATA[
15222: 
15223: function toggleDisplay(domForm,caller) {
15224:     if (document.getElementById(caller)) {
15225:         var divitem = document.getElementById(caller);
15226:         var optionsElement = domForm.coursecredits;
15227:         var checkval = 1;
15228:         var dispval = 'block';
15229:         if (caller == 'emailoptions') {
15230:             optionsElement = domForm.cancreate_email; 
15231:         }
15232:         if (caller == 'studentsubmission') {
15233:             optionsElement = domForm.postsubmit;
15234:         }
15235:         if (caller == 'cloneinstcode') {
15236:             optionsElement = domForm.canclone;
15237:             checkval = 'instcode';
15238:         }
15239:         if (optionsElement.length) {
15240:             var currval;
15241:             for (var i=0; i<optionsElement.length; i++) {
15242:                 if (optionsElement[i].checked) {
15243:                    currval = optionsElement[i].value;
15244:                 }
15245:             }
15246:             if (currval == checkval) {
15247:                 divitem.style.display = dispval;
15248:             } else {
15249:                 divitem.style.display = 'none';
15250:             }
15251:         }
15252:     }
15253:     return;
15254: }
15255: 
15256: // ]]>
15257: </script>
15258: 
15259: END
15260: 
15261: }
15262: 
15263: sub captcha_phrases {
15264:     return &Apache::lonlocal::texthash (
15265:                  priv => 'Private key',
15266:                  pub  => 'Public key',
15267:                  original  => 'original (CAPTCHA)',
15268:                  recaptcha => 'successor (ReCAPTCHA)',
15269:                  notused   => 'unused',
15270:                  ver => 'ReCAPTCHA version (1 or 2)',
15271:     );
15272: }
15273: 
15274: sub devalidate_remote_domconfs {
15275:     my ($dom,$cachekeys) = @_;
15276:     return unless (ref($cachekeys) eq 'HASH');
15277:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
15278:     my %thismachine;
15279:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
15280:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions','directorysrch');
15281:     if (keys(%servers)) {
15282:         foreach my $server (keys(%servers)) {
15283:             next if ($thismachine{$server});
15284:             my @cached;
15285:             foreach my $name (@posscached) {
15286:                 if ($cachekeys->{$name}) {
15287:                     push(@cached,&escape($name).':'.&escape($dom));
15288:                 }
15289:             }
15290:             if (@cached) {
15291:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
15292:             }
15293:         }
15294:     }
15295:     return;
15296: }
15297: 
15298: 1;

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