File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.302: download - view: text, annotated - select for diffs
Tue Jul 18 16:03:08 2017 UTC (6 years, 11 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Bug 6854.
  Addition of 'default' to status types, and (All Users or Other Users)
  to usertypes in print_selfcreation() needs to be made to copies of
  $types array ref and $usertypes hash ref to avoid affecting other routines
  using loncommon::sorted_inst_types(() in the same request.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.302 2017/07/18 16:03:08 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) eq 'HASH') {
 3265:             if (ref($settings->{'adhoc'}) eq 'HASH') {
 3266:                 %current = %{$settings->{'adhoc'}};
 3267:             }
 3268:         }
 3269:         my $count = 0;
 3270:         foreach my $key (sort(keys(%existing))) {
 3271:             if ($key=~/^rolesdef\_(\w+)$/) {
 3272:                 my $rolename = $1;
 3273:                 my (%privs,$order);
 3274:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3275:                 $customroles{$rolename} = \%privs;
 3276:                 if (ref($current{$rolename}) eq 'HASH') {
 3277:                     $order = $current{$rolename}{'order'};
 3278:                 }
 3279:                 if ($order eq '') {
 3280:                     $order = $count;
 3281:                 }
 3282:                 $ordered{$order} = $rolename;
 3283:                 $count++;
 3284:             }
 3285:         }
 3286:         my $maxnum = scalar(keys(%ordered));
 3287:         my @roles_by_num = ();
 3288:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3289:             push(@roles_by_num,$item);
 3290:         }
 3291:         my $context = 'domprefs';
 3292:         my $crstype = 'Course';
 3293:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3294:         my @accesstypes = ('all','dh','da','none');
 3295:         my ($numstatustypes,@jsarray);
 3296:         if (ref($types) eq 'ARRAY') {
 3297:             if (@{$types} > 0) {
 3298:                 $numstatustypes = scalar(@{$types});
 3299:                 push(@accesstypes,'status');
 3300:                 @jsarray = ('bystatus');
 3301:             }
 3302:         }
 3303:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 3304:         if (keys(%domhelpdesk)) {
 3305:             push(@accesstypes,('inc','exc'));
 3306:             push(@jsarray,('notinc','notexc'));
 3307:         }
 3308:         my $hiddenstr = join("','",@jsarray);
 3309:         $datatable .= &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname);
 3310:         my $context = 'domprefs';
 3311:         my $crstype = 'Course';
 3312:         my $prefix = 'helproles_';
 3313:         my $add_class = 'LC_hidden';
 3314:         foreach my $num (@roles_by_num) {
 3315:             my $role = $ordered{$num};
 3316:             my ($desc,$access,@statuses);
 3317:             if (ref($current{$role}) eq 'HASH') {
 3318:                 $desc = $current{$role}{'desc'};
 3319:                 $access = $current{$role}{'access'};
 3320:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 3321:                     @statuses = @{$current{$role}{'insttypes'}};
 3322:                 }
 3323:             }
 3324:             if ($desc eq '') {
 3325:                 $desc = $role;
 3326:             }
 3327:             my $identifier = 'custhelp'.$num;
 3328:             my %full=();
 3329:             my %levels= (
 3330:                          course => {},
 3331:                          domain => {},
 3332:                          system => {},
 3333:                         );
 3334:             my %levelscurrent=(
 3335:                                course => {},
 3336:                                domain => {},
 3337:                                system => {},
 3338:                               );
 3339:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 3340:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3341:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3342:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 3343:             $datatable .= '<tr '.$css_class.'><td valign="top"><b>'.$role.'</b><br />'.
 3344:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 3345:             for (my $k=0; $k<=$maxnum; $k++) {
 3346:                 my $vpos = $k+1;
 3347:                 my $selstr;
 3348:                 if ($k == $num) {
 3349:                     $selstr = ' selected="selected" ';
 3350:                 }
 3351:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3352:             }
 3353:             $datatable .= '</select>'.('&nbsp;'x2).
 3354:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 3355:                           '</td>'.
 3356:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3357:                           &mt('Name shown to users:').
 3358:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 3359:                           '</fieldset>'.
 3360:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 3361:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 3362:                           '<fieldset>'.
 3363:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 3364:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 3365:                                                                    \%levelscurrent,$identifier,
 3366:                                                                    'LC_hidden',$prefix.$num.'_privs').
 3367:                           '</fieldset></td>';
 3368:             $itemcount ++;
 3369:         }
 3370:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3371:         my $newcust = 'custhelp'.$count;
 3372:         my (%privs,%levelscurrent);
 3373:         my %full=();
 3374:         my %levels= (
 3375:                      course => {},
 3376:                      domain => {},
 3377:                      system => {},
 3378:                     );
 3379:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 3380:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3381:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 3382:         $datatable .= '<tr '.$css_class.'><td valign="top"><span class="LC_nobreak"><label>'.
 3383:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 3384:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 3385:         for (my $k=0; $k<$maxnum+1; $k++) {
 3386:             my $vpos = $k+1;
 3387:             my $selstr;
 3388:             if ($k == $maxnum) {
 3389:                 $selstr = ' selected="selected" ';
 3390:             }
 3391:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3392:         }
 3393:         $datatable .= '</select>&nbsp;'."\n".
 3394:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 3395:                       '</label></span></td>'.
 3396:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3397:                       '<span class="LC_nobreak">'.
 3398:                       &mt('Internal name:').
 3399:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 3400:                       '</span>'.('&nbsp;'x4).
 3401:                       '<span class="LC_nobreak">'.
 3402:                       &mt('Name shown to users:').
 3403:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 3404:                       '</span></fieldset>'.
 3405:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 3406:                                              $usertypes,$types,\%domhelpdesk).
 3407:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 3408:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 3409:                                                                 \@templateroles,$newcust).
 3410:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 3411:                                                                \%levelscurrent,$newcust).
 3412:                       '</fieldset></td></tr>';
 3413:         $count ++;
 3414:         $$rowtotal += $count;
 3415:     }
 3416:     return $datatable;
 3417: }
 3418: 
 3419: sub adhocbutton {
 3420:     my ($prefix,$num,$field,$visibility) = @_;
 3421:     my %lt = &Apache::lonlocal::texthash(
 3422:                                           show => 'Show details',
 3423:                                           hide => 'Hide details',
 3424:                                         );
 3425:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 3426:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 3427:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 3428:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 3429: }
 3430: 
 3431: sub helpsettings_javascript {
 3432:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 3433:     return unless(ref($roles_by_num) eq 'ARRAY');
 3434:     my %html_js_lt = &Apache::lonlocal::texthash(
 3435:                                           show => 'Show details',
 3436:                                           hide => 'Hide details',
 3437:                                         );
 3438:     &html_escape(\%html_js_lt);
 3439:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 3440:     return <<"ENDSCRIPT";
 3441: <script type="text/javascript">
 3442: // <![CDATA[
 3443: 
 3444: function reorderHelpRoles(form,item) {
 3445:     var changedVal;
 3446: $jstext
 3447:     var newpos = 'helproles_${total}_pos';
 3448:     var maxh = 1 + $total;
 3449:     var current = new Array();
 3450:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3451:     if (item == newpos) {
 3452:         changedVal = newitemVal;
 3453:     } else {
 3454:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3455:         current[newitemVal] = newpos;
 3456:     }
 3457:     for (var i=0; i<helproles.length; i++) {
 3458:         var elementName = 'helproles_'+helproles[i]+'_pos';
 3459:         if (elementName != item) {
 3460:             if (form.elements[elementName]) {
 3461:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3462:                 current[currVal] = elementName;
 3463:             }
 3464:         }
 3465:     }
 3466:     var oldVal;
 3467:     for (var j=0; j<maxh; j++) {
 3468:         if (current[j] == undefined) {
 3469:             oldVal = j;
 3470:         }
 3471:     }
 3472:     if (oldVal < changedVal) {
 3473:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3474:            var elementName = current[k];
 3475:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3476:         }
 3477:     } else {
 3478:         for (var k=changedVal; k<oldVal; k++) {
 3479:             var elementName = current[k];
 3480:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3481:         }
 3482:     }
 3483:     return;
 3484: }
 3485: 
 3486: function helpdeskAccess(num) {
 3487:     var curraccess = null;
 3488:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 3489:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 3490:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 3491:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 3492:             }
 3493:         }
 3494:     }
 3495:     var shown = Array();
 3496:     var hidden = Array();
 3497:     if (curraccess == 'none') {
 3498:         hidden = Array('$hiddenstr');
 3499:     } else {
 3500:         if (curraccess == 'status') {
 3501:             shown = Array('bystatus');
 3502:             hidden = Array('notinc','notexc');
 3503:         } else {
 3504:             if (curraccess == 'exc') {
 3505:                 shown = Array('notexc');
 3506:                 hidden = Array('notinc','bystatus');
 3507:             }
 3508:             if (curraccess == 'inc') {
 3509:                 shown = Array('notinc');
 3510:                 hidden = Array('notexc','bystatus');
 3511:             }
 3512:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 3513:                 hidden = Array('notinc','notexc','bystatus');
 3514:             }
 3515:         }
 3516:     }
 3517:     if (hidden.length > 0) {
 3518:         for (var i=0; i<hidden.length; i++) {
 3519:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 3520:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 3521:             }
 3522:         }
 3523:     }
 3524:     if (shown.length > 0) {
 3525:         for (var i=0; i<shown.length; i++) {
 3526:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 3527:                 if (shown[i] == 'privs') {
 3528:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 3529:                 } else {
 3530:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 3531:                 }
 3532:             }
 3533:         }
 3534:     }
 3535:     return;
 3536: }
 3537: 
 3538: function toggleHelpdeskItem(num,field) {
 3539:     if (document.getElementById('helproles_'+num+'_'+field)) {
 3540:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 3541:             document.getElementById('helproles_'+num+'_'+field).className =
 3542:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 3543:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3544:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 3545:             }
 3546:         } else {
 3547:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 3548:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3549:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 3550:             }
 3551:         }
 3552:     }
 3553:     return;
 3554: }
 3555: 
 3556: // ]]>
 3557: </script>
 3558: 
 3559: ENDSCRIPT
 3560: }
 3561: 
 3562: sub helpdeskroles_access {
 3563:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 3564:         $usertypes,$types,$domhelpdesk) = @_;
 3565:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 3566:     my %lt = &Apache::lonlocal::texthash(
 3567:                     'rou'    => 'Role usage',
 3568:                     'whi'    => 'Which helpdesk personnel may use this role?',
 3569:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 3570:                     'dh'     => 'All with domain helpdesk role',
 3571:                     'da'     => 'All with domain helpdesk assistant role',
 3572:                     'none'   => 'None',
 3573:                     'status' => 'Determined based on institutional status',
 3574:                     'inc'    => 'Include all, but exclude specific personnel',
 3575:                     'exc'    => 'Exclude all, but include specific personnel',
 3576:                   );
 3577:     my %usecheck = (
 3578:                      all => ' checked="checked"',
 3579:                    );
 3580:     my %displaydiv = (
 3581:                       status => 'none',
 3582:                       inc    => 'none',
 3583:                       exc    => 'none',
 3584:                       priv   => 'block',
 3585:                      );
 3586:     my $output;
 3587:     if (ref($current) eq 'HASH') {
 3588:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 3589:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 3590:                 $usecheck{$current->{access}} = $usecheck{'all'};
 3591:                 delete($usecheck{'all'});
 3592:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 3593:                     my $access = $1;
 3594:                     $displaydiv{$access} = 'inline';
 3595:                 } elsif ($current->{access} eq 'none') {
 3596:                     $displaydiv{'priv'} = 'none';
 3597:                 }
 3598:             }
 3599:         }
 3600:     }
 3601:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 3602:               '<p>'.$lt{'whi'}.'</p>';
 3603:     foreach my $access (@{$accesstypes}) {
 3604:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 3605:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 3606:                    $lt{$access}.'</label>';
 3607:         if ($access eq 'status') {
 3608:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 3609:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 3610:                                                                  $othertitle,$usertypes,$types).
 3611:                        '</div>';
 3612:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 3613:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 3614:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3615:                        '</div>';
 3616:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 3617:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 3618:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3619:                        '</div>';
 3620:         }
 3621:         $output .= '</p>';
 3622:     }
 3623:     $output .= '</fieldset>';
 3624:     return $output;
 3625: }
 3626: 
 3627: sub radiobutton_prefs {
 3628:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 3629:         $additional,$align) = @_;
 3630:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 3631:                    (ref($choices) eq 'HASH'));
 3632: 
 3633:     my (%checkedon,%checkedoff,$datatable,$css_class);
 3634: 
 3635:     foreach my $item (@{$toggles}) {
 3636:         if ($defaultchecked->{$item} eq 'on') {
 3637:             $checkedon{$item} = ' checked="checked" ';
 3638:             $checkedoff{$item} = ' ';
 3639:         } elsif ($defaultchecked->{$item} eq 'off') {
 3640:             $checkedoff{$item} = ' checked="checked" ';
 3641:             $checkedon{$item} = ' ';
 3642:         }
 3643:     }
 3644:     if (ref($settings) eq 'HASH') {
 3645:         foreach my $item (@{$toggles}) {
 3646:             if ($settings->{$item} eq '1') {
 3647:                 $checkedon{$item} =  ' checked="checked" ';
 3648:                 $checkedoff{$item} = ' ';
 3649:             } elsif ($settings->{$item} eq '0') {
 3650:                 $checkedoff{$item} =  ' checked="checked" ';
 3651:                 $checkedon{$item} = ' ';
 3652:             }
 3653:         }
 3654:     }
 3655:     if ($onclick) {
 3656:         $onclick = ' onclick="'.$onclick.'"';
 3657:     }
 3658:     foreach my $item (@{$toggles}) {
 3659:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3660:         $datatable .=
 3661:             '<tr'.$css_class.'><td valign="top">'.
 3662:             '<span class="LC_nobreak">'.$choices->{$item}.
 3663:             '</span></td>';
 3664:         if ($align eq 'left') {
 3665:             $datatable .= '<td class="LC_left_item">';
 3666:         } else {
 3667:             $datatable .= '<td class="LC_right_item">';
 3668:         }
 3669:         $datatable .=
 3670:             '<span class="LC_nobreak">'.
 3671:             '<label><input type="radio" name="'.
 3672:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 3673:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 3674:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3675:             '</span>'.$additional.
 3676:             '</td>'.
 3677:             '</tr>';
 3678:         $itemcount ++;
 3679:     }
 3680:     return ($datatable,$itemcount);
 3681: }
 3682: 
 3683: sub print_ltitools {
 3684:     my ($dom,$settings,$rowtotal) = @_;
 3685:     my $rownum = 0;
 3686:     my $css_class;
 3687:     my $itemcount = 1;
 3688:     my $maxnum = 0;
 3689:     my %ordered;
 3690:     if (ref($settings) eq 'HASH') {
 3691:         foreach my $item (keys(%{$settings})) {
 3692:             if (ref($settings->{$item}) eq 'HASH') {
 3693:                 my $num = $settings->{$item}{'order'};
 3694:                 $ordered{$num} = $item;
 3695:             }
 3696:         }
 3697:     }
 3698:     my $confname = $dom.'-domainconfig';
 3699:     my $switchserver = &check_switchserver($dom,$confname);
 3700:     my $maxnum = scalar(keys(%ordered));
 3701:     my $datatable = &ltitools_javascript($settings);
 3702:     my %lt = &ltitools_names();
 3703:     my @courseroles = ('cc','in','ta','ep','st');
 3704:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 3705:     my @fields = ('fullname','firstname','lastname','email','user','roles');
 3706:     if (keys(%ordered)) {
 3707:         my @items = sort { $a <=> $b } keys(%ordered);
 3708:         for (my $i=0; $i<@items; $i++) {
 3709:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3710:             my $item = $ordered{$items[$i]};
 3711:             my ($title,$key,$secret,$url,$imgsrc,$version);
 3712:             if (ref($settings->{$item}) eq 'HASH') {
 3713:                 $title = $settings->{$item}->{'title'};
 3714:                 $url = $settings->{$item}->{'url'};
 3715:                 $key = $settings->{$item}->{'key'};
 3716:                 $secret = $settings->{$item}->{'secret'};
 3717:                 my $image = $settings->{$item}->{'image'};
 3718:                 if ($image ne '') {
 3719:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 3720:                 }
 3721:             }
 3722:             my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_".$item."'".');"';
 3723:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3724:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 3725:             for (my $k=0; $k<=$maxnum; $k++) {
 3726:                 my $vpos = $k+1;
 3727:                 my $selstr;
 3728:                 if ($k == $i) {
 3729:                     $selstr = ' selected="selected" ';
 3730:                 }
 3731:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3732:             }
 3733:             $datatable .= '</select>'.('&nbsp;'x2).
 3734:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 3735:                 &mt('Delete?').'</label></span></td>'.
 3736:                 '<td colspan="2">'.
 3737:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 3738:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 3739:                 ('&nbsp;'x2).
 3740:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 3741:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 3742:                 ('&nbsp;'x2).
 3743:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 3744:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 3745:                 '<br /><br />'.
 3746:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_url_'.$i.'"'.
 3747:                 ' value="'.$url.'" /></span>'.
 3748:                 ('&nbsp;'x2).
 3749:                 '<span class="LC_nobreak">'.$lt{'key'}.
 3750:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 3751:                 ('&nbsp;'x2).
 3752:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 3753:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 3754:                 '<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>'.
 3755:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 3756:                 '</fieldset>'.
 3757:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 3758:                 '<span class="LC_nobreak">'.&mt('Display target:');
 3759:             my %currdisp;
 3760:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 3761:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 3762:                     $currdisp{'window'} = ' checked="checked"';
 3763:                 } elsif ($settings->{$item}->{'display'}->{'target'} eq 'tab') {
 3764:                     $currdisp{'tab'} = ' checked="checked"';
 3765:                 } else {
 3766:                     $currdisp{'iframe'} = ' checked="checked"';
 3767:                 }
 3768:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 3769:                     $currdisp{'width'} = $1;
 3770:                 }
 3771:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 3772:                      $currdisp{'height'} = $1;
 3773:                 }
 3774:                 $currdisp{'linktext'} = $settings->{$item}->{'display'}->{'linktext'};
 3775:                 $currdisp{'explanation'} = $settings->{$item}->{'display'}->{'explanation'};
 3776:             } else {
 3777:                 $currdisp{'iframe'} = ' checked="checked"';
 3778:             }
 3779:             foreach my $disp ('iframe','tab','window') {
 3780:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 3781:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 3782:             }
 3783:             $datatable .= ('&nbsp;'x4);
 3784:             foreach my $dimen ('width','height') {
 3785:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 3786:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 3787:                               ('&nbsp;'x2);
 3788:             }
 3789:             $datatable .= '<br />'.
 3790:                           '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 3791:                           '<input type="text" name="ltitools_linktext_'.$i.'" size="25" value="'.$currdisp{'linktext'}.'" /></label></div>'.
 3792:                           '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 3793:                           '<textarea name="ltitools_explanation_'.$i.'" rows="5" cols="40">'.$currdisp{'explanation'}.
 3794:                           '</textarea></div><div style=""></div><br />';
 3795:             $datatable .= '<br />';
 3796:             foreach my $extra ('passback','roster') {
 3797:                 my $checkedon = '';
 3798:                 my $checkedoff = ' checked="checked"';
 3799:                 if ($settings->{$item}->{$extra}) {
 3800:                     $checkedon = $checkedoff;
 3801:                     $checkedoff = '';
 3802:                 }
 3803:                 $datatable .= $lt{$extra}.'&nbsp;'.
 3804:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.' />'.
 3805:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 3806:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.' />'.
 3807:                               &mt('No').'</label>'.('&nbsp;'x4);
 3808:             }
 3809:             $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 3810:             if ($imgsrc) {
 3811:                 $datatable .= $imgsrc.
 3812:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 3813:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 3814:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 3815:             } else {
 3816:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 3817:             }
 3818:             if ($switchserver) {
 3819:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3820:             } else {
 3821:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 3822:             }
 3823:             $datatable .= '</span></fieldset>';
 3824:             my (%checkedfields,%rolemaps);
 3825:             if (ref($settings->{$item}) eq 'HASH') {
 3826:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 3827:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 3828:                 }
 3829:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 3830:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 3831:                     $checkedfields{'roles'} = 1;
 3832:                 }
 3833:             }
 3834:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 3835:                           '<span class="LC_nobreak">';
 3836:             foreach my $field (@fields) {
 3837:                 my $checked;
 3838:                 if ($checkedfields{$field}) {
 3839:                     $checked = ' checked="checked"';
 3840:                 }
 3841:                 $datatable .= '<label>'.
 3842:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$checked.' />'.
 3843:                               $lt{$field}.'</label>'.('&nbsp;' x2);
 3844:             }
 3845:             $datatable .= '</span></fieldset>'.
 3846:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 3847:             foreach my $role (@courseroles) {
 3848:                 my ($selected,$selectnone);
 3849:                 if (!$rolemaps{$role}) {
 3850:                     $selectnone = ' selected="selected"';
 3851:                 }
 3852:                 $datatable .= '<td align="center">'. 
 3853:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 3854:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 3855:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 3856:                 foreach my $ltirole (@ltiroles) {
 3857:                     unless ($selectnone) {
 3858:                         if ($rolemaps{$role} eq $ltirole) {
 3859:                             $selected = ' selected="selected"';
 3860:                         } else {
 3861:                             $selected = '';
 3862:                         }
 3863:                     }
 3864:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 3865:                 }
 3866:                 $datatable .= '</select></td>';
 3867:             }
 3868:             $datatable .= '</tr></table></fieldset>';
 3869:             my %courseconfig;
 3870:             if (ref($settings->{$item}) eq 'HASH') {
 3871:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 3872:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 3873:                 }
 3874:             }
 3875:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 3876:             foreach my $item ('label','title','target','linktext','explanation') {
 3877:                 my $checked;
 3878:                 if ($courseconfig{$item}) {
 3879:                     $checked = ' checked="checked"';
 3880:                 }
 3881:                 $datatable .= '<label>'.
 3882:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 3883:                        $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 3884:             }
 3885:             $datatable .= '</span></fieldset>'.
 3886:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 3887:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 3888:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 3889:                 my %custom = %{$settings->{$item}->{'custom'}};
 3890:                 if (keys(%custom) > 0) {
 3891:                     foreach my $key (sort(keys(%custom))) {
 3892:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 3893:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 3894:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 3895:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 3896:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 3897:                     }
 3898:                 }
 3899:             }
 3900:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 3901:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 3902:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 3903:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 3904:             $datatable .= '</table></fieldset></td></tr>'."\n";
 3905:             $itemcount ++;
 3906:         }
 3907:     }
 3908:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3909:     my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_add_pos'".');"';
 3910:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 3911:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 3912:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 3913:     for (my $k=0; $k<$maxnum+1; $k++) {
 3914:         my $vpos = $k+1;
 3915:         my $selstr;
 3916:         if ($k == $maxnum) {
 3917:             $selstr = ' selected="selected" ';
 3918:         }
 3919:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3920:     }
 3921:     $datatable .= '</select>&nbsp;'."\n".
 3922:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</td>'."\n".
 3923:                   '<td colspan="2">'.
 3924:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 3925:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_add_title" value="" /></span> '."\n".
 3926:                   ('&nbsp;'x2).
 3927:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 3928:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 3929:                   ('&nbsp;'x2).
 3930:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 3931:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 3932:                   '<br />'.
 3933:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_add_url" value="" /></span> '."\n".
 3934:                   ('&nbsp;'x2).
 3935:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 3936:                   ('&nbsp;'x2).
 3937:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 3938:                   '<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".
 3939:                   '</fieldset>'.
 3940:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 3941:                   '<span class="LC_nobreak">'.&mt('Display target:');
 3942:     my %defaultdisp;
 3943:     $defaultdisp{'iframe'} = ' checked="checked"';
 3944:     foreach my $disp ('iframe','tab','window') {
 3945:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 3946:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 3947:     }
 3948:     $datatable .= ('&nbsp;'x4);
 3949:     foreach my $dimen ('width','height') {
 3950:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 3951:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 3952:                       ('&nbsp;'x2);
 3953:     }
 3954:     $datatable .= '<br />'.
 3955:                   '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 3956:                   '<input type="text" name="ltitools_add_linktext" size="5" /></label></div>'.
 3957:                   '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 3958:                   '<textarea name=ltitools_add_explanation" rows="5" cols="40"></textarea>'.
 3959:                   '</div><div style=""></div><br />';
 3960:     foreach my $extra ('passback','roster') {
 3961:         $datatable .= $lt{$extra}.'&nbsp;'.
 3962:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="1" />'.
 3963:                       &mt('Yes').'</label>'.('&nbsp;'x2).
 3964:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="0" checked="checked" />'.
 3965:                       &mt('No').'</label>'.('&nbsp;'x4);
 3966:     }
 3967:     $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 3968:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 3969:     if ($switchserver) {
 3970:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3971:     } else {
 3972:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 3973:     }
 3974:     $datatable .= '</span></fieldset>'.
 3975:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 3976:                   '<span class="LC_nobreak">';
 3977:     foreach my $field (@fields) {
 3978:         $datatable .= '<label>'.
 3979:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'" />'.
 3980:                       $lt{$field}.'</label>'.('&nbsp;' x2);
 3981:     }
 3982:     $datatable .= '</span></fieldset>'.
 3983:                   '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 3984:     foreach my $role (@courseroles) {
 3985:         my ($checked,$checkednone);
 3986:         $datatable .= '<td align="center">'.
 3987:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 3988:                       '<select name="ltitools_add_roles_'.$role.'">'.
 3989:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 3990:         foreach my $ltirole (@ltiroles) {
 3991:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 3992:         }
 3993:         $datatable .= '</select></td>';
 3994:     }
 3995:     $datatable .= '</tr></table></fieldset>'.
 3996:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 3997:     foreach my $item ('label','title','target','linktext','explanation') {
 3998:         $datatable .= '<label>'.
 3999:                       '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 4000:                       $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 4001:     }
 4002:     $datatable .= '</span></fieldset>'.
 4003:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4004:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 4005:                   '<tr><td><span class="LC_nobreak">'.
 4006:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 4007:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 4008:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 4009:                   '</table></fieldset></td></tr>'."\n".
 4010:                   '</td>'."\n".
 4011:                   '</tr>'."\n";
 4012:     $itemcount ++;
 4013:     return $datatable;
 4014: }
 4015: 
 4016: sub ltitools_names {
 4017:     my %lt = &Apache::lonlocal::texthash(
 4018:                                           'title'          => 'Title',
 4019:                                           'version'        => 'Version',
 4020:                                           'msgtype'        => 'Message Type',
 4021:                                           'url'            => 'URL',
 4022:                                           'key'            => 'Key',
 4023:                                           'secret'         => 'Secret',
 4024:                                           'icon'           => 'Icon',   
 4025:                                           'user'           => 'Username:domain',
 4026:                                           'fullname'       => 'Full Name',
 4027:                                           'firstname'      => 'First Name',
 4028:                                           'lastname'       => 'Last Name',
 4029:                                           'email'          => 'E-mail',
 4030:                                           'roles'          => 'Role',
 4031:                                           'window'         => 'Window',
 4032:                                           'tab'            => 'Tab',
 4033:                                           'iframe'         => 'iFrame',
 4034:                                           'height'         => 'Height',
 4035:                                           'width'          => 'Width',
 4036:                                           'linktext'       => 'Default Link Text',
 4037:                                           'explanation'    => 'Default Explanation',
 4038:                                           'passback'       => 'Tool can return grades:',
 4039:                                           'roster'         => 'Tool can retrieve roster:',
 4040:                                           'crstarget'      => 'Display target',
 4041:                                           'crslabel'       => 'Course label',
 4042:                                           'crstitle'       => 'Course title', 
 4043:                                           'crslinktext'    => 'Link Text',
 4044:                                           'crsexplanation' => 'Explanation',
 4045:                                         );
 4046:     return %lt;
 4047: }
 4048: 
 4049: sub print_coursedefaults {
 4050:     my ($position,$dom,$settings,$rowtotal) = @_;
 4051:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 4052:     my $itemcount = 1;
 4053:     my %choices =  &Apache::lonlocal::texthash (
 4054:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 4055:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 4056:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 4057:         coursecredits        => 'Credits can be specified for courses',
 4058:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 4059:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 4060:         postsubmit           => 'Disable submit button/keypress following student submission',
 4061:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 4062:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 4063:     );
 4064:     my %staticdefaults = (
 4065:                            anonsurvey_threshold => 10,
 4066:                            uploadquota          => 500,
 4067:                            postsubmit           => 60,
 4068:                            mysqltables          => 172800,
 4069:                          );
 4070:     if ($position eq 'top') {
 4071:         %defaultchecked = (
 4072:                             'canuse_pdfforms' => 'off',
 4073:                             'uselcmath'       => 'on',
 4074:                             'usejsme'         => 'on',
 4075:                             'canclone'        => 'none',
 4076:                           );
 4077:         @toggles = ('canuse_pdfforms','uselcmath','usejsme');
 4078:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4079:                                                      \%choices,$itemcount);
 4080:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4081:         $datatable .=
 4082:             '<tr'.$css_class.'><td valign="top">'.
 4083:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 4084:             '</span></td><td class="LC_left_item">';
 4085:         my $currcanclone = 'none';
 4086:         my $onclick;
 4087:         my @cloneoptions = ('none','domain');
 4088:         my %clonetitles = (
 4089:                              none     => 'No additional course requesters',
 4090:                              domain   => "Any course requester in course's domain",
 4091:                              instcode => 'Course requests for official courses ...',
 4092:                           );
 4093:         my (%codedefaults,@code_order,@posscodes);
 4094:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 4095:                                                     \@code_order) eq 'ok') {
 4096:             if (@code_order > 0) {
 4097:                 push(@cloneoptions,'instcode');
 4098:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 4099:             }
 4100:         }
 4101:         if (ref($settings) eq 'HASH') {
 4102:             if ($settings->{'canclone'}) {
 4103:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 4104:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 4105:                         if (@code_order > 0) {
 4106:                             $currcanclone = 'instcode';
 4107:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 4108:                         }
 4109:                     }
 4110:                 } elsif ($settings->{'canclone'} eq 'domain') {
 4111:                     $currcanclone = $settings->{'canclone'};
 4112:                 }
 4113:             }
 4114:         }
 4115:         foreach my $option (@cloneoptions) {
 4116:             my ($checked,$additional);
 4117:             if ($currcanclone eq $option) {
 4118:                 $checked = ' checked="checked"';
 4119:             }
 4120:             if ($option eq 'instcode') {
 4121:                 if (@code_order) {
 4122:                     my $show = 'none';
 4123:                     if ($checked) {
 4124:                         $show = 'block';
 4125:                     }
 4126:                     $additional = '<div id="cloneinstcode" style="display:'.$show.'" />'.
 4127:                                   &mt('Institutional codes for new and cloned course have identical:').
 4128:                                   '<br />';
 4129:                     foreach my $item (@code_order) {
 4130:                         my $codechk;
 4131:                         if ($checked) {
 4132:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 4133:                                 $codechk = ' checked="checked"';
 4134:                             }
 4135:                         }
 4136:                         $additional .= '<label>'.
 4137:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 4138:                                        $item.'</label>';
 4139:                     }
 4140:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 4141:                 }
 4142:             }
 4143:             $datatable .=
 4144:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 4145:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 4146:                 '</label>&nbsp;'.$additional.'</span><br />';
 4147:         }
 4148:         $datatable .= '</td>'.
 4149:                       '</tr>';
 4150:         $itemcount ++;
 4151:     } else {
 4152:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4153:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 4154:         my $currusecredits = 0;
 4155:         my $postsubmitclient = 1;
 4156:         my @types = ('official','unofficial','community','textbook','placement');
 4157:         if (ref($settings) eq 'HASH') {
 4158:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 4159:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 4160:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 4161:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 4162:                 }
 4163:             }
 4164:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 4165:                 foreach my $type (@types) {
 4166:                     next if ($type eq 'community');
 4167:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 4168:                     if ($defcredits{$type} ne '') {
 4169:                         $currusecredits = 1;
 4170:                     }
 4171:                 }
 4172:             }
 4173:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 4174:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 4175:                     $postsubmitclient = 0;
 4176:                     foreach my $type (@types) {
 4177:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4178:                     }
 4179:                 } else {
 4180:                     foreach my $type (@types) {
 4181:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 4182:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 4183:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 4184:                             } else {
 4185:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4186:                             }
 4187:                         } else {
 4188:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4189:                         }
 4190:                     }
 4191:                 }
 4192:             } else {
 4193:                 foreach my $type (@types) {
 4194:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4195:                 }
 4196:             }
 4197:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 4198:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 4199:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 4200:                 }
 4201:             } else {
 4202:                 foreach my $type (@types) {
 4203:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 4204:                 }
 4205:             }
 4206:         } else {
 4207:             foreach my $type (@types) {
 4208:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4209:             }
 4210:         }
 4211:         if (!$currdefresponder) {
 4212:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 4213:         } elsif ($currdefresponder < 1) {
 4214:             $currdefresponder = 1;
 4215:         }
 4216:         foreach my $type (@types) {
 4217:             if ($curruploadquota{$type} eq '') {
 4218:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 4219:             }
 4220:         }
 4221:         $datatable .=
 4222:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4223:                 $choices{'anonsurvey_threshold'}.
 4224:                 '</span></td>'.
 4225:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 4226:                 '<input type="text" name="anonsurvey_threshold"'.
 4227:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 4228:                 '</td></tr>'."\n";
 4229:         $itemcount ++;
 4230:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4231:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4232:                       $choices{'uploadquota'}.
 4233:                       '</span></td>'.
 4234:                       '<td align="right" class="LC_right_item">'.
 4235:                       '<table><tr>';
 4236:         foreach my $type (@types) {
 4237:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 4238:                            '<input type="text" name="uploadquota_'.$type.'"'.
 4239:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 4240:         }
 4241:         $datatable .= '</tr></table></td></tr>'."\n";
 4242:         $itemcount ++;
 4243:         my $onclick = "toggleDisplay(this.form,'credits');";
 4244:         my $display = 'none';
 4245:         if ($currusecredits) {
 4246:             $display = 'block';
 4247:         }
 4248:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 4249:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 4250:         foreach my $type (@types) {
 4251:             next if ($type eq 'community');
 4252:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 4253:                            '<input type="text" name="'.$type.'_credits"'.
 4254:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 4255:         }
 4256:         $additional .= '</tr></table></div>'."\n";
 4257:         %defaultchecked = ('coursecredits' => 'off');
 4258:         @toggles = ('coursecredits');
 4259:         my $current = {
 4260:                         'coursecredits' => $currusecredits,
 4261:                       };
 4262:         (my $table,$itemcount) =
 4263:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4264:                                \%choices,$itemcount,$onclick,$additional,'left');
 4265:         $datatable .= $table;
 4266:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 4267:         my $display = 'none';
 4268:         if ($postsubmitclient) {
 4269:             $display = 'block';
 4270:         }
 4271:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 4272:                       &mt('Number of seconds submit is disabled').'<br />'.
 4273:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 4274:                       '<table><tr>';
 4275:         foreach my $type (@types) {
 4276:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 4277:                            '<input type="text" name="'.$type.'_timeout" value="'.
 4278:                            $deftimeout{$type}.'" size="5" /></td>';
 4279:         }
 4280:         $additional .= '</tr></table></div>'."\n";
 4281:         %defaultchecked = ('postsubmit' => 'on');
 4282:         @toggles = ('postsubmit');
 4283:         $current = {
 4284:                        'postsubmit' => $postsubmitclient,
 4285:                    };
 4286:         ($table,$itemcount) =
 4287:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4288:                                \%choices,$itemcount,$onclick,$additional,'left');
 4289:         $datatable .= $table;
 4290:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4291:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4292:                       $choices{'mysqltables'}.
 4293:                       '</span></td>'.
 4294:                       '<td align="right" class="LC_right_item">'.
 4295:                       '<table><tr>';
 4296:         foreach my $type (@types) {
 4297:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 4298:                            '<input type="text" name="mysqltables_'.$type.'"'.
 4299:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 4300:         }
 4301:         $datatable .= '</tr></table></td></tr>'."\n";
 4302:         $itemcount ++;
 4303: 
 4304:     }
 4305:     $$rowtotal += $itemcount;
 4306:     return $datatable;
 4307: }
 4308: 
 4309: sub print_selfenrollment {
 4310:     my ($position,$dom,$settings,$rowtotal) = @_;
 4311:     my ($css_class,$datatable);
 4312:     my $itemcount = 1;
 4313:     my @types = ('official','unofficial','community','textbook','placement');
 4314:     if (($position eq 'top') || ($position eq 'middle')) {
 4315:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 4316:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 4317:         my @rows;
 4318:         my $key;
 4319:         if ($position eq 'top') {
 4320:             $key = 'admin'; 
 4321:             if (ref($rowsref) eq 'ARRAY') {
 4322:                 @rows = @{$rowsref};
 4323:             }
 4324:         } elsif ($position eq 'middle') {
 4325:             $key = 'default';
 4326:             @rows = ('types','registered','approval','limit');
 4327:         }
 4328:         foreach my $row (@rows) {
 4329:             if (defined($titlesref->{$row})) {
 4330:                 $itemcount ++;
 4331:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4332:                 $datatable .= '<tr'.$css_class.'>'.
 4333:                               '<td>'.$titlesref->{$row}.'</td>'.
 4334:                               '<td class="LC_left_item">'.
 4335:                               '<table><tr>';
 4336:                 my (%current,%currentcap);
 4337:                 if (ref($settings) eq 'HASH') {
 4338:                     if (ref($settings->{$key}) eq 'HASH') {
 4339:                         foreach my $type (@types) {
 4340:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4341:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 4342:                             }
 4343:                             if (($row eq 'limit') && ($key eq 'default')) {
 4344:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4345:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 4346:                                 }
 4347:                             }
 4348:                         }
 4349:                     }
 4350:                 }
 4351:                 my %roles = (
 4352:                              '0' => &Apache::lonnet::plaintext('dc'),
 4353:                             ); 
 4354:             
 4355:                 foreach my $type (@types) {
 4356:                     unless (($row eq 'registered') && ($key eq 'default')) {
 4357:                         $datatable .= '<th>'.&mt($type).'</th>';
 4358:                     }
 4359:                 }
 4360:                 unless (($row eq 'registered') && ($key eq 'default')) {
 4361:                     $datatable .= '</tr><tr>';
 4362:                 }
 4363:                 foreach my $type (@types) {
 4364:                     if ($type eq 'community') {
 4365:                         $roles{'1'} = &mt('Community personnel');
 4366:                     } else {
 4367:                         $roles{'1'} = &mt('Course personnel');
 4368:                     }
 4369:                     $datatable .= '<td style="vertical-align: top">';
 4370:                     if ($position eq 'top') {
 4371:                         my %checked;
 4372:                         if ($current{$type} eq '0') {
 4373:                             $checked{'0'} = ' checked="checked"';
 4374:                         } else {
 4375:                             $checked{'1'} = ' checked="checked"';
 4376:                         }
 4377:                         foreach my $role ('1','0') {
 4378:                             $datatable .= '<span class="LC_nobreak"><label>'.
 4379:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 4380:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 4381:                                           $roles{$role}.'</label></span> ';
 4382:                         }
 4383:                     } else {
 4384:                         if ($row eq 'types') {
 4385:                             my %checked;
 4386:                             if ($current{$type} =~ /^(all|dom)$/) {
 4387:                                 $checked{$1} = ' checked="checked"';
 4388:                             } else {
 4389:                                 $checked{''} = ' checked="checked"';
 4390:                             }
 4391:                             foreach my $val ('','dom','all') {
 4392:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4393:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4394:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4395:                             }
 4396:                         } elsif ($row eq 'registered') {
 4397:                             my %checked;
 4398:                             if ($current{$type} eq '1') {
 4399:                                 $checked{'1'} = ' checked="checked"';
 4400:                             } else {
 4401:                                 $checked{'0'} = ' checked="checked"';
 4402:                             }
 4403:                             foreach my $val ('0','1') {
 4404:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4405:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4406:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4407:                             }
 4408:                         } elsif ($row eq 'approval') {
 4409:                             my %checked;
 4410:                             if ($current{$type} =~ /^([12])$/) {
 4411:                                 $checked{$1} = ' checked="checked"';
 4412:                             } else {
 4413:                                 $checked{'0'} = ' checked="checked"';
 4414:                             }
 4415:                             for my $val (0..2) {
 4416:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4417:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4418:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4419:                             }
 4420:                         } elsif ($row eq 'limit') {
 4421:                             my %checked;
 4422:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 4423:                                 $checked{$1} = ' checked="checked"';
 4424:                             } else {
 4425:                                 $checked{'none'} = ' checked="checked"';
 4426:                             }
 4427:                             my $cap;
 4428:                             if ($currentcap{$type} =~ /^\d+$/) {
 4429:                                 $cap = $currentcap{$type};
 4430:                             }
 4431:                             foreach my $val ('none','allstudents','selfenrolled') {
 4432:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4433:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4434:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4435:                             }
 4436:                             $datatable .= '<br />'.
 4437:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 4438:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 4439:                                           '</span>'; 
 4440:                         }
 4441:                     }
 4442:                     $datatable .= '</td>';
 4443:                 }
 4444:                 $datatable .= '</tr>';
 4445:             }
 4446:             $datatable .= '</table></td></tr>';
 4447:         }
 4448:     } elsif ($position eq 'bottom') {
 4449:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 4450:     }
 4451:     $$rowtotal += $itemcount;
 4452:     return $datatable;
 4453: }
 4454: 
 4455: sub print_validation_rows {
 4456:     my ($caller,$dom,$settings,$rowtotal) = @_;
 4457:     my ($itemsref,$namesref,$fieldsref);
 4458:     if ($caller eq 'selfenroll') { 
 4459:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 4460:     } elsif ($caller eq 'requestcourses') {
 4461:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 4462:     }
 4463:     my %currvalidation;
 4464:     if (ref($settings) eq 'HASH') {
 4465:         if (ref($settings->{'validation'}) eq 'HASH') {
 4466:             %currvalidation = %{$settings->{'validation'}};
 4467:         }
 4468:     }
 4469:     my $datatable;
 4470:     my $itemcount = 0;
 4471:     foreach my $item (@{$itemsref}) {
 4472:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4473:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4474:                       $namesref->{$item}.
 4475:                       '</span></td>'.
 4476:                       '<td class="LC_left_item">';
 4477:         if (($item eq 'url') || ($item eq 'button')) {
 4478:             $datatable .= '<span class="LC_nobreak">'.
 4479:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 4480:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 4481:         } elsif ($item eq 'fields') {
 4482:             my @currfields;
 4483:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 4484:                 @currfields = @{$currvalidation{$item}};
 4485:             }
 4486:             foreach my $field (@{$fieldsref}) {
 4487:                 my $check = '';
 4488:                 if (grep(/^\Q$field\E$/,@currfields)) {
 4489:                     $check = ' checked="checked"';
 4490:                 }
 4491:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4492:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 4493:                               ' value="'.$field.'"'.$check.' />'.$field.
 4494:                               '</label></span> ';
 4495:             }
 4496:         } elsif ($item eq 'markup') {
 4497:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 4498:                            $currvalidation{$item}.
 4499:                               '</textarea>';
 4500:         }
 4501:         $datatable .= '</td></tr>'."\n";
 4502:         if (ref($rowtotal)) {
 4503:             $itemcount ++;
 4504:         }
 4505:     }
 4506:     if ($caller eq 'requestcourses') {
 4507:         my %currhash;
 4508:         if (ref($settings) eq 'HASH') {
 4509:             if (ref($settings->{'validation'}) eq 'HASH') {
 4510:                 if ($settings->{'validation'}{'dc'} ne '') {
 4511:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 4512:                 }
 4513:             }
 4514:         }
 4515:         my $numinrow = 2;
 4516:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4517:                                                        'validationdc',%currhash);
 4518:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4519:         $datatable .= '</td></tr><tr'.$css_class.'><td>';
 4520:         if ($numdc > 1) {
 4521:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 4522:         } else {
 4523:             $datatable .=  &mt('Course creation processed as: ');
 4524:         }
 4525:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 4526:         $itemcount ++;
 4527:     }
 4528:     if (ref($rowtotal)) {
 4529:         $$rowtotal += $itemcount;
 4530:     }
 4531:     return $datatable;
 4532: }
 4533: 
 4534: sub print_usersessions {
 4535:     my ($position,$dom,$settings,$rowtotal) = @_;
 4536:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 4537:     my (%by_ip,%by_location,@intdoms,@instdoms);
 4538:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 4539: 
 4540:     my @alldoms = &Apache::lonnet::all_domains();
 4541:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 4542:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4543:     my %altids = &id_for_thisdom(%servers);
 4544:     if ($position eq 'top') {
 4545:         if (keys(%serverhomes) > 1) {
 4546:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 4547:             my $curroffloadnow;
 4548:             if (ref($settings) eq 'HASH') {
 4549:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 4550:                     $curroffloadnow = $settings->{'offloadnow'};
 4551:                 }
 4552:             }
 4553:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 4554:         } else {
 4555:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 4556:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 4557:                           '</td></tr>';
 4558:         }
 4559:     } else {
 4560:         my %titles = &usersession_titles();
 4561:         my ($prefix,@types);
 4562:         if ($position eq 'bottom') {
 4563:             $prefix = 'remote';
 4564:             @types = ('version','excludedomain','includedomain');
 4565:         } else {
 4566:             $prefix = 'hosted';
 4567:             @types = ('excludedomain','includedomain');
 4568:         }
 4569:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 4570:     }
 4571:     $$rowtotal += $itemcount;
 4572:     return $datatable;
 4573: }
 4574: 
 4575: sub rules_by_location {
 4576:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 4577:     my ($datatable,$itemcount,$css_class);
 4578:     if (keys(%{$by_location}) == 0) {
 4579:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4580:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 4581:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 4582:                      '</td></tr>';
 4583:         $itemcount = 1;
 4584:     } else {
 4585:         $itemcount = 0;
 4586:         my $numinrow = 5;
 4587:         my (%current,%checkedon,%checkedoff);
 4588:         my @locations = sort(keys(%{$by_location}));
 4589:         foreach my $type (@{$types}) {
 4590:             $checkedon{$type} = '';
 4591:             $checkedoff{$type} = ' checked="checked"';
 4592:         }
 4593:         if (ref($settings) eq 'HASH') {
 4594:             if (ref($settings->{$prefix}) eq 'HASH') {
 4595:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 4596:                     $current{$key} = $settings->{$prefix}{$key};
 4597:                     if ($key eq 'version') {
 4598:                         if ($current{$key} ne '') {
 4599:                             $checkedon{$key} = ' checked="checked"';
 4600:                             $checkedoff{$key} = '';
 4601:                         }
 4602:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 4603:                         $checkedon{$key} = ' checked="checked"';
 4604:                         $checkedoff{$key} = '';
 4605:                     }
 4606:                 }
 4607:             }
 4608:         }
 4609:         foreach my $type (@{$types}) {
 4610:             next if ($type ne 'version' && !@locations);
 4611:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4612:             $datatable .= '<tr'.$css_class.'>
 4613:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 4614:                            <span class="LC_nobreak">&nbsp;
 4615:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 4616:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 4617:             if ($type eq 'version') {
 4618:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 4619:                 my $selector = '<select name="'.$prefix.'_version">';
 4620:                 foreach my $version (@lcversions) {
 4621:                     my $selected = '';
 4622:                     if ($current{'version'} eq $version) {
 4623:                         $selected = ' selected="selected"';
 4624:                     }
 4625:                     $selector .= ' <option value="'.$version.'"'.
 4626:                                  $selected.'>'.$version.'</option>';
 4627:                 }
 4628:                 $selector .= '</select> ';
 4629:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 4630:             } else {
 4631:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 4632:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 4633:                              ' />'.('&nbsp;'x2).
 4634:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 4635:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 4636:                              "\n".
 4637:                              '</div><div><table>';
 4638:                 my $rem;
 4639:                 for (my $i=0; $i<@locations; $i++) {
 4640:                     my ($showloc,$value,$checkedtype);
 4641:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 4642:                         my $ip = $by_location->{$locations[$i]}->[0];
 4643:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4644:                             $value = join(':',@{$by_ip->{$ip}});
 4645:                             $showloc = join(', ',@{$by_ip->{$ip}});
 4646:                             if (ref($current{$type}) eq 'ARRAY') {
 4647:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 4648:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 4649:                                         $checkedtype = ' checked="checked"';
 4650:                                         last;
 4651:                                     }
 4652:                                 }
 4653:                             }
 4654:                         }
 4655:                     }
 4656:                     $rem = $i%($numinrow);
 4657:                     if ($rem == 0) {
 4658:                         if ($i > 0) {
 4659:                             $datatable .= '</tr>';
 4660:                         }
 4661:                         $datatable .= '<tr>';
 4662:                     }
 4663:                     $datatable .= '<td class="LC_left_item">'.
 4664:                                   '<span class="LC_nobreak"><label>'.
 4665:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 4666:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 4667:                                   '</label></span></td>';
 4668:                 }
 4669:                 $rem = @locations%($numinrow);
 4670:                 my $colsleft = $numinrow - $rem;
 4671:                 if ($colsleft > 1 ) {
 4672:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4673:                                   '&nbsp;</td>';
 4674:                 } elsif ($colsleft == 1) {
 4675:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4676:                 }
 4677:                 $datatable .= '</tr></table>';
 4678:             }
 4679:             $datatable .= '</td></tr>';
 4680:             $itemcount ++;
 4681:         }
 4682:     }
 4683:     return ($datatable,$itemcount);
 4684: }
 4685: 
 4686: sub print_ssl {
 4687:     my ($position,$dom,$settings,$rowtotal) = @_;
 4688:     my ($css_class,$datatable);
 4689:     my $itemcount = 1;
 4690:     if ($position eq 'top') {
 4691:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 4692:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 4693:         my $same_institution;
 4694:         if ($intdom ne '') {
 4695:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 4696:             if (ref($internet_names) eq 'ARRAY') {
 4697:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 4698:                     $same_institution = 1;
 4699:                 }
 4700:             }
 4701:         }
 4702:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4703:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 4704:         if ($same_institution) {
 4705:             my %domservers = &Apache::lonnet::get_servers($dom);
 4706:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 4707:         } else {
 4708:             $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.");
 4709:         }
 4710:         $datatable .= '</td></tr>';
 4711:         $itemcount ++;
 4712:     } else {
 4713:         my %titles = &ssl_titles();
 4714:         my (%by_ip,%by_location,@intdoms,@instdoms);
 4715:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 4716:         my @alldoms = &Apache::lonnet::all_domains();
 4717:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 4718:         my @domservers = &Apache::lonnet::get_servers($dom);
 4719:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4720:         my %altids = &id_for_thisdom(%servers);
 4721:         if (($position eq 'connto') || ($position eq 'connfrom')) {
 4722:             my $legacy;
 4723:             unless (ref($settings) eq 'HASH') {
 4724:                 my $name;
 4725:                 if ($position eq 'connto') {
 4726:                     $name = 'loncAllowInsecure';
 4727:                 } else {
 4728:                     $name = 'londAllowInsecure';
 4729:                 }
 4730:                 my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
 4731:                 my @ids=&Apache::lonnet::current_machine_ids();
 4732:                 if (($primarylibserv ne '') && (!grep(/^\Q$primarylibserv\E$/,@ids))) {
 4733:                     my %what = (
 4734:                                    $name => 1,
 4735:                                );
 4736:                     my ($result,$returnhash) =
 4737:                         &Apache::lonnet::get_remote_globals($primarylibserv,\%what);
 4738:                     if ($result eq 'ok') {
 4739:                         if (ref($returnhash) eq 'HASH') {
 4740:                             $legacy = $returnhash->{$name};
 4741:                         }
 4742:                     }
 4743:                 } else {
 4744:                     $legacy = $Apache::lonnet::perlvar{$name};
 4745:                 }
 4746:             }
 4747:             foreach my $type ('dom','intdom','other') {
 4748:                 my %checked;
 4749:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4750:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 4751:                               '<td class="LC_right_item">';
 4752:                 my $skip; 
 4753:                 if ($type eq 'dom') {
 4754:                     unless (keys(%servers) > 1) {
 4755:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 4756:                         $skip = 1;
 4757:                     }
 4758:                 }
 4759:                 if ($type eq 'intdom') {
 4760:                     unless (@instdoms > 1) {
 4761:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 4762:                         $skip = 1;
 4763:                     } 
 4764:                 } elsif ($type eq 'other') {
 4765:                     if (keys(%by_location) == 0) {
 4766:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 4767:                         $skip = 1;
 4768:                     }
 4769:                 }
 4770:                 unless ($skip) {
 4771:                     $checked{'yes'} = ' checked="checked"'; 
 4772:                     if (ref($settings) eq 'HASH') {
 4773:                         if (ref($settings->{$position}) eq 'HASH') {
 4774:                             if ($settings->{$position}->{$type} =~ /^(no|req)$/) {
 4775:                                 $checked{$1} = $checked{'yes'};
 4776:                                 delete($checked{'yes'}); 
 4777:                             }
 4778:                         }
 4779:                     } else {
 4780:                         if ($legacy == 0) {
 4781:                             $checked{'req'} = $checked{'yes'};
 4782:                             delete($checked{'yes'});    
 4783:                         }
 4784:                     }
 4785:                     foreach my $option ('no','yes','req') {
 4786:                         $datatable .= '<span class="LC_nobreak"><label>'.
 4787:                                       '<input type="radio" name="'.$position.'_'.$type.'" '.
 4788:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 4789:                                       '</label></span>'.('&nbsp;'x2);
 4790:                     }
 4791:                 }
 4792:                 $datatable .= '</td></tr>';
 4793:                 $itemcount ++; 
 4794:             }
 4795:         } else {
 4796:             my $prefix = 'replication';
 4797:             my @types = ('certreq','nocertreq');
 4798:             if (keys(%by_location) == 0) {
 4799:                 $datatable .= '<tr'.$css_class.'><td>'.
 4800:                               &mt('Nothing to set here, as there are no other institutions').
 4801:                               '</td></tr>';
 4802:                 $itemcount ++;
 4803:             } else {
 4804:                 ($datatable,$itemcount) = 
 4805:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 4806:             }
 4807:         }
 4808:     }
 4809:     $$rowtotal += $itemcount;
 4810:     return $datatable;
 4811: }
 4812: 
 4813: sub ssl_titles {
 4814:     return &Apache::lonlocal::texthash (
 4815:                dom           => 'LON-CAPA servers/VMs from same domain',
 4816:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 4817:                other         => 'External LON-CAPA servers/VMs',
 4818:                connto        => 'Connections to other servers',
 4819:                connfrom      => 'Connections from other servers',
 4820:                replication   => 'Replicating content to other institutions',
 4821:                certreq       => 'Client certificate required, but specific domains exempt',
 4822:                nocertreq     => 'No client certificate required, except for specific domains',
 4823:                no            => 'SSL not used',
 4824:                yes           => 'SSL Optional (used if available)',
 4825:                req           => 'SSL Required',
 4826:     );
 4827: }
 4828: 
 4829: sub print_trust {
 4830:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 4831:     my ($css_class,$datatable,%checked,%choices);
 4832:     my (%by_ip,%by_location,@intdoms,@instdoms);
 4833:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 4834:     my $itemcount = 1;
 4835:     my %titles = &trust_titles();
 4836:     my @types = ('exc','inc');
 4837:     if ($prefix eq 'top') {
 4838:         $prefix = 'content';
 4839:     } elsif ($prefix eq 'bottom') {
 4840:         $prefix = 'msg';
 4841:     }
 4842:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 4843:     $$rowtotal += $itemcount;
 4844:     return $datatable;
 4845: }
 4846: 
 4847: sub trust_titles {
 4848:     return &Apache::lonlocal::texthash(
 4849:                content  => "Access to this domain's content by others",
 4850:                shared   => "Access to other domain's content by this domain",
 4851:                enroll   => "Enrollment in this domain's courses by others", 
 4852:                othcoau  => "Co-author roles in this domain for others",
 4853:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 4854:                domroles => "Domain roles in this domain assignable to others",
 4855:                catalog  => "Course Catalog for this domain displayed elsewhere",
 4856:                reqcrs   => "Requests for creation of courses in this domain by others",
 4857:                msg      => "Users in other domains can send messages to this domain",
 4858:                exc      => "Allow all, but exclude specific domains",
 4859:                inc      => "Deny all, but include specific domains",
 4860:            );
 4861: } 
 4862: 
 4863: sub build_location_hashes {
 4864:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 4865:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 4866:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 4867:     my %iphost = &Apache::lonnet::get_iphost();
 4868:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 4869:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 4870:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 4871:         foreach my $id (@{$iphost{$primary_ip}}) {
 4872:             my $intdom = &Apache::lonnet::internet_dom($id);
 4873:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 4874:                 push(@{$intdoms},$intdom);
 4875:             }
 4876:         }
 4877:     }
 4878:     foreach my $ip (keys(%iphost)) {
 4879:         if (ref($iphost{$ip}) eq 'ARRAY') {
 4880:             foreach my $id (@{$iphost{$ip}}) {
 4881:                 my $location = &Apache::lonnet::internet_dom($id);
 4882:                 if ($location) {
 4883:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 4884:                         my $dom = &Apache::lonnet::host_domain($id);
 4885:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 4886:                             push(@{$instdoms},$dom);
 4887:                         }
 4888:                         next;
 4889:                     }
 4890:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4891:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 4892:                             push(@{$by_ip->{$ip}},$location);
 4893:                         }
 4894:                     } else {
 4895:                         $by_ip->{$ip} = [$location];
 4896:                     }
 4897:                 }
 4898:             }
 4899:         }
 4900:     }
 4901:     foreach my $ip (sort(keys(%{$by_ip}))) {
 4902:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4903:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 4904:             my $first = $by_ip->{$ip}->[0];
 4905:             if (ref($by_location->{$first}) eq 'ARRAY') {
 4906:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 4907:                     push(@{$by_location->{$first}},$ip);
 4908:                 }
 4909:             } else {
 4910:                 $by_location->{$first} = [$ip];
 4911:             }
 4912:         }
 4913:     }
 4914:     return;
 4915: }
 4916: 
 4917: sub current_offloads_to {
 4918:     my ($dom,$settings,$servers) = @_;
 4919:     my (%spareid,%otherdomconfigs);
 4920:     if (ref($servers) eq 'HASH') {
 4921:         foreach my $lonhost (sort(keys(%{$servers}))) {
 4922:             my $gotspares;
 4923:             if (ref($settings) eq 'HASH') {
 4924:                 if (ref($settings->{'spares'}) eq 'HASH') {
 4925:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 4926:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 4927:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 4928:                         $gotspares = 1;
 4929:                     }
 4930:                 }
 4931:             }
 4932:             unless ($gotspares) {
 4933:                 my $gotspares;
 4934:                 my $serverhomeID =
 4935:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 4936:                 my $serverhomedom =
 4937:                     &Apache::lonnet::host_domain($serverhomeID);
 4938:                 if ($serverhomedom ne $dom) {
 4939:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 4940:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4941:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4942:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4943:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4944:                                 $gotspares = 1;
 4945:                             }
 4946:                         }
 4947:                     } else {
 4948:                         $otherdomconfigs{$serverhomedom} =
 4949:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 4950:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 4951:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4952:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4953:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 4954:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4955:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4956:                                         $gotspares = 1;
 4957:                                     }
 4958:                                 }
 4959:                             }
 4960:                         }
 4961:                     }
 4962:                 }
 4963:             }
 4964:             unless ($gotspares) {
 4965:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4966:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4967:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4968:                } else {
 4969:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 4970:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 4971:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4972:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4973:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4974:                     } else {
 4975:                         my %what = (
 4976:                              spareid => 1,
 4977:                         );
 4978:                         my ($result,$returnhash) = 
 4979:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 4980:                         if ($result eq 'ok') { 
 4981:                             if (ref($returnhash) eq 'HASH') {
 4982:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 4983:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 4984:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 4985:                                 }
 4986:                             }
 4987:                         }
 4988:                     }
 4989:                 }
 4990:             }
 4991:         }
 4992:     }
 4993:     return %spareid;
 4994: }
 4995: 
 4996: sub spares_row {
 4997:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 4998:     my $css_class;
 4999:     my $numinrow = 4;
 5000:     my $itemcount = 1;
 5001:     my $datatable;
 5002:     my %typetitles = &sparestype_titles();
 5003:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 5004:         foreach my $server (sort(keys(%{$servers}))) {
 5005:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 5006:             my ($othercontrol,$serverdom);
 5007:             if ($serverhome ne $server) {
 5008:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 5009:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 5010:             } else {
 5011:                 $serverdom = &Apache::lonnet::host_domain($server);
 5012:                 if ($serverdom ne $dom) {
 5013:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 5014:                 }
 5015:             }
 5016:             next unless (ref($spareid->{$server}) eq 'HASH');
 5017:             my $checkednow;
 5018:             if (ref($curroffloadnow) eq 'HASH') {
 5019:                 if ($curroffloadnow->{$server}) {
 5020:                     $checkednow = ' checked="checked"';
 5021:                 }
 5022:             }
 5023:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5024:             $datatable .= '<tr'.$css_class.'>
 5025:                            <td rowspan="2">
 5026:                             <span class="LC_nobreak">'.
 5027:                           &mt('[_1] when busy, offloads to:'
 5028:                               ,'<b>'.$server.'</b>').'</span><br />'.
 5029:                           '<span class="LC_nobreak">'."\n".
 5030:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 5031:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 5032:                           "\n";
 5033:             my (%current,%canselect);
 5034:             my @choices = 
 5035:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 5036:             foreach my $type ('primary','default') {
 5037:                 if (ref($spareid->{$server}) eq 'HASH') {
 5038:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 5039:                         my @spares = @{$spareid->{$server}{$type}};
 5040:                         if (@spares > 0) {
 5041:                             if ($othercontrol) {
 5042:                                 $current{$type} = join(', ',@spares);
 5043:                             } else {
 5044:                                 $current{$type} .= '<table>';
 5045:                                 my $numspares = scalar(@spares);
 5046:                                 for (my $i=0;  $i<@spares; $i++) {
 5047:                                     my $rem = $i%($numinrow);
 5048:                                     if ($rem == 0) {
 5049:                                         if ($i > 0) {
 5050:                                             $current{$type} .= '</tr>';
 5051:                                         }
 5052:                                         $current{$type} .= '<tr>';
 5053:                                     }
 5054:                                     $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;'.
 5055:                                                        $spareid->{$server}{$type}[$i].
 5056:                                                        '</label></td>'."\n";
 5057:                                 }
 5058:                                 my $rem = @spares%($numinrow);
 5059:                                 my $colsleft = $numinrow - $rem;
 5060:                                 if ($colsleft > 1 ) {
 5061:                                     $current{$type} .= '<td colspan="'.$colsleft.
 5062:                                                        '" class="LC_left_item">'.
 5063:                                                        '&nbsp;</td>';
 5064:                                 } elsif ($colsleft == 1) {
 5065:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 5066:                                 }
 5067:                                 $current{$type} .= '</tr></table>';
 5068:                             }
 5069:                         }
 5070:                     }
 5071:                     if ($current{$type} eq '') {
 5072:                         $current{$type} = &mt('None specified');
 5073:                     }
 5074:                     if ($othercontrol) {
 5075:                         if ($type eq 'primary') {
 5076:                             $canselect{$type} = $othercontrol;
 5077:                         }
 5078:                     } else {
 5079:                         $canselect{$type} = 
 5080:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 5081:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 5082:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 5083:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 5084:                         if (@choices > 0) {
 5085:                             foreach my $lonhost (@choices) {
 5086:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 5087:                             }
 5088:                         }
 5089:                         $canselect{$type} .= '</select>'."\n";
 5090:                     }
 5091:                 } else {
 5092:                     $current{$type} = &mt('Could not be determined');
 5093:                     if ($type eq 'primary') {
 5094:                         $canselect{$type} =  $othercontrol;
 5095:                     }
 5096:                 }
 5097:                 if ($type eq 'default') {
 5098:                     $datatable .= '<tr'.$css_class.'>';
 5099:                 }
 5100:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 5101:                               '<td>'.$current{$type}.'</td>'."\n".
 5102:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 5103:             }
 5104:             $itemcount ++;
 5105:         }
 5106:     }
 5107:     $$rowtotal += $itemcount;
 5108:     return $datatable;
 5109: }
 5110: 
 5111: sub possible_newspares {
 5112:     my ($server,$currspares,$serverhomes,$altids) = @_;
 5113:     my $serverhostname = &Apache::lonnet::hostname($server);
 5114:     my %excluded;
 5115:     if ($serverhostname ne '') {
 5116:         %excluded = (
 5117:                        $serverhostname => 1,
 5118:                     );
 5119:     }
 5120:     if (ref($currspares) eq 'HASH') {
 5121:         foreach my $type (keys(%{$currspares})) {
 5122:             if (ref($currspares->{$type}) eq 'ARRAY') {
 5123:                 if (@{$currspares->{$type}} > 0) {
 5124:                     foreach my $curr (@{$currspares->{$type}}) {
 5125:                         my $hostname = &Apache::lonnet::hostname($curr);
 5126:                         $excluded{$hostname} = 1;
 5127:                     }
 5128:                 }
 5129:             }
 5130:         }
 5131:     }
 5132:     my @choices;
 5133:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 5134:         if (keys(%{$serverhomes}) > 1) {
 5135:             foreach my $name (sort(keys(%{$serverhomes}))) {
 5136:                 unless ($excluded{$name}) {
 5137:                     if (exists($altids->{$serverhomes->{$name}})) {
 5138:                         push(@choices,$altids->{$serverhomes->{$name}});
 5139:                     } else {
 5140:                         push(@choices,$serverhomes->{$name});
 5141:                     }
 5142:                 }
 5143:             }
 5144:         }
 5145:     }
 5146:     return sort(@choices);
 5147: }
 5148: 
 5149: sub print_loadbalancing {
 5150:     my ($dom,$settings,$rowtotal) = @_;
 5151:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 5152:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 5153:     my $numinrow = 1;
 5154:     my $datatable;
 5155:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5156:     my (%currbalancer,%currtargets,%currrules,%existing);
 5157:     if (ref($settings) eq 'HASH') {
 5158:         %existing = %{$settings};
 5159:     }
 5160:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 5161:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 5162:                                   \%currtargets,\%currrules);
 5163:     } else {
 5164:         return;
 5165:     }
 5166:     my ($othertitle,$usertypes,$types) =
 5167:         &Apache::loncommon::sorted_inst_types($dom);
 5168:     my $rownum = 8;
 5169:     if (ref($types) eq 'ARRAY') {
 5170:         $rownum += scalar(@{$types});
 5171:     }
 5172:     my @css_class = ('LC_odd_row','LC_even_row');
 5173:     my $balnum = 0;
 5174:     my $islast;
 5175:     my (@toshow,$disabledtext);
 5176:     if (keys(%currbalancer) > 0) {
 5177:         @toshow = sort(keys(%currbalancer));
 5178:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 5179:             push(@toshow,'');
 5180:         }
 5181:     } else {
 5182:         @toshow = ('');
 5183:         $disabledtext = &mt('No existing load balancer');
 5184:     }
 5185:     foreach my $lonhost (@toshow) {
 5186:         if ($balnum == scalar(@toshow)-1) {
 5187:             $islast = 1;
 5188:         } else {
 5189:             $islast = 0;
 5190:         }
 5191:         my $cssidx = $balnum%2;
 5192:         my $targets_div_style = 'display: none';
 5193:         my $disabled_div_style = 'display: block';
 5194:         my $homedom_div_style = 'display: none';
 5195:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 5196:                       '<td rowspan="'.$rownum.'" valign="top">'.
 5197:                       '<p>';
 5198:         if ($lonhost eq '') {
 5199:             $datatable .= '<span class="LC_nobreak">';
 5200:             if (keys(%currbalancer) > 0) {
 5201:                 $datatable .= &mt('Add balancer:');
 5202:             } else {
 5203:                 $datatable .= &mt('Enable balancer:');
 5204:             }
 5205:             $datatable .= '&nbsp;'.
 5206:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 5207:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 5208:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 5209:                           '<option value="" selected="selected">'.&mt('None').
 5210:                           '</option>'."\n";
 5211:             foreach my $server (sort(keys(%servers))) {
 5212:                 next if ($currbalancer{$server});
 5213:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 5214:             }
 5215:             $datatable .=
 5216:                 '</select>'."\n".
 5217:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 5218:         } else {
 5219:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 5220:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 5221:                            &mt('Stop balancing').'</label>'.
 5222:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 5223:             $targets_div_style = 'display: block';
 5224:             $disabled_div_style = 'display: none';
 5225:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 5226:                 $homedom_div_style = 'display: block';
 5227:             }
 5228:         }
 5229:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 5230:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 5231:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 5232:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 5233:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 5234:         my @sparestypes = ('primary','default');
 5235:         my %typetitles = &sparestype_titles();
 5236:         my %hostherechecked = (
 5237:                                   no => ' checked="checked"',
 5238:                               );
 5239:         foreach my $sparetype (@sparestypes) {
 5240:             my $targettable;
 5241:             for (my $i=0; $i<$numspares; $i++) {
 5242:                 my $checked;
 5243:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 5244:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5245:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5246:                             $checked = ' checked="checked"';
 5247:                         }
 5248:                     }
 5249:                 }
 5250:                 my ($chkboxval,$disabled);
 5251:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 5252:                     $chkboxval = $spares[$i];
 5253:                 }
 5254:                 if (exists($currbalancer{$spares[$i]})) {
 5255:                     $disabled = ' disabled="disabled"';
 5256:                 }
 5257:                 $targettable .=
 5258:                     '<td><span class="LC_nobreak"><label>'.
 5259:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 5260:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 5261:                     '</span></label></span></td>';
 5262:                 my $rem = $i%($numinrow);
 5263:                 if ($rem == 0) {
 5264:                     if (($i > 0) && ($i < $numspares-1)) {
 5265:                         $targettable .= '</tr>';
 5266:                     }
 5267:                     if ($i < $numspares-1) {
 5268:                         $targettable .= '<tr>';
 5269:                     }
 5270:                 }
 5271:             }
 5272:             if ($targettable ne '') {
 5273:                 my $rem = $numspares%($numinrow);
 5274:                 my $colsleft = $numinrow - $rem;
 5275:                 if ($colsleft > 1 ) {
 5276:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5277:                                     '&nbsp;</td>';
 5278:                 } elsif ($colsleft == 1) {
 5279:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 5280:                 }
 5281:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 5282:                                '<table><tr>'.$targettable.'</tr></table><br />';
 5283:             }
 5284:             $hostherechecked{$sparetype} = '';
 5285:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 5286:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5287:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5288:                         $hostherechecked{$sparetype} = ' checked="checked"';
 5289:                         $hostherechecked{'no'} = '';
 5290:                     }
 5291:                 }
 5292:             }
 5293:         }
 5294:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 5295:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 5296:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 5297:         foreach my $sparetype (@sparestypes) {
 5298:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 5299:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 5300:                           '</i></label><br />';
 5301:         }
 5302:         $datatable .= '</div></td></tr>'.
 5303:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 5304:                                            $othertitle,$usertypes,$types,\%servers,
 5305:                                            \%currbalancer,$lonhost,
 5306:                                            $targets_div_style,$homedom_div_style,
 5307:                                            $css_class[$cssidx],$balnum,$islast);
 5308:         $$rowtotal += $rownum;
 5309:         $balnum ++;
 5310:     }
 5311:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 5312:     return $datatable;
 5313: }
 5314: 
 5315: sub get_loadbalancers_config {
 5316:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 5317:     return unless ((ref($servers) eq 'HASH') &&
 5318:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 5319:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 5320:     if (keys(%{$existing}) > 0) {
 5321:         my $oldlonhost;
 5322:         foreach my $key (sort(keys(%{$existing}))) {
 5323:             if ($key eq 'lonhost') {
 5324:                 $oldlonhost = $existing->{'lonhost'};
 5325:                 $currbalancer->{$oldlonhost} = 1;
 5326:             } elsif ($key eq 'targets') {
 5327:                 if ($oldlonhost) {
 5328:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 5329:                 }
 5330:             } elsif ($key eq 'rules') {
 5331:                 if ($oldlonhost) {
 5332:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 5333:                 }
 5334:             } elsif (ref($existing->{$key}) eq 'HASH') {
 5335:                 $currbalancer->{$key} = 1;
 5336:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 5337:                 $currrules->{$key} = $existing->{$key}{'rules'};
 5338:             }
 5339:         }
 5340:     } else {
 5341:         my ($balancerref,$targetsref) =
 5342:                 &Apache::lonnet::get_lonbalancer_config($servers);
 5343:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 5344:             foreach my $server (sort(keys(%{$balancerref}))) {
 5345:                 $currbalancer->{$server} = 1;
 5346:                 $currtargets->{$server} = $targetsref->{$server};
 5347:             }
 5348:         }
 5349:     }
 5350:     return;
 5351: }
 5352: 
 5353: sub loadbalancing_rules {
 5354:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 5355:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 5356:         $css_class,$balnum,$islast) = @_;
 5357:     my $output;
 5358:     my $num = 0;
 5359:     my ($alltypes,$othertypes,$titles) =
 5360:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 5361:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 5362:         foreach my $type (@{$alltypes}) {
 5363:             $num ++;
 5364:             my $current;
 5365:             if (ref($currrules) eq 'HASH') {
 5366:                 $current = $currrules->{$type};
 5367:             }
 5368:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5369:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 5370:                     $current = '';
 5371:                 }
 5372:             }
 5373:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 5374:                                              $servers,$currbalancer,$lonhost,$dom,
 5375:                                              $targets_div_style,$homedom_div_style,
 5376:                                              $css_class,$balnum,$num,$islast);
 5377:         }
 5378:     }
 5379:     return $output;
 5380: }
 5381: 
 5382: sub loadbalancing_titles {
 5383:     my ($dom,$intdom,$usertypes,$types) = @_;
 5384:     my %othertypes = (
 5385:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 5386:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 5387:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 5388:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 5389:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 5390:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 5391:                      );
 5392:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 5393:     my @available;
 5394:     if (ref($types) eq 'ARRAY') {
 5395:         @available = @{$types};
 5396:     }
 5397:     unless (grep(/^default$/,@available)) {
 5398:         push(@available,'default');
 5399:     }
 5400:     unshift(@alltypes,@available);
 5401:     my %titles;
 5402:     foreach my $type (@alltypes) {
 5403:         if ($type =~ /^_LC_/) {
 5404:             $titles{$type} = $othertypes{$type};
 5405:         } elsif ($type eq 'default') {
 5406:             $titles{$type} = &mt('All users from [_1]',$dom);
 5407:             if (ref($types) eq 'ARRAY') {
 5408:                 if (@{$types} > 0) {
 5409:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 5410:                 }
 5411:             }
 5412:         } elsif (ref($usertypes) eq 'HASH') {
 5413:             $titles{$type} = $usertypes->{$type};
 5414:         }
 5415:     }
 5416:     return (\@alltypes,\%othertypes,\%titles);
 5417: }
 5418: 
 5419: sub loadbalance_rule_row {
 5420:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 5421:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 5422:     my @rulenames;
 5423:     my %ruletitles = &offloadtype_text();
 5424:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 5425:         @rulenames = ('balancer','offloadedto','specific');
 5426:     } else {
 5427:         @rulenames = ('default','homeserver');
 5428:         if ($type eq '_LC_external') {
 5429:             push(@rulenames,'externalbalancer');
 5430:         } else {
 5431:             push(@rulenames,'specific');
 5432:         }
 5433:         push(@rulenames,'none');
 5434:     }
 5435:     my $style = $targets_div_style;
 5436:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5437:         $style = $homedom_div_style;
 5438:     }
 5439:     my $space;
 5440:     if ($islast && $num == 1) {
 5441:         $space = '<div display="inline-block">&nbsp;</div>';
 5442:     }
 5443:     my $output =
 5444:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 5445:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 5446:         '<td valaign="top">'.$space.
 5447:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 5448:     for (my $i=0; $i<@rulenames; $i++) {
 5449:         my $rule = $rulenames[$i];
 5450:         my ($checked,$extra);
 5451:         if ($rulenames[$i] eq 'default') {
 5452:             $rule = '';
 5453:         }
 5454:         if ($rulenames[$i] eq 'specific') {
 5455:             if (ref($servers) eq 'HASH') {
 5456:                 my $default;
 5457:                 if (($current ne '') && (exists($servers->{$current}))) {
 5458:                     $checked = ' checked="checked"';
 5459:                 }
 5460:                 unless ($checked) {
 5461:                     $default = ' selected="selected"';
 5462:                 }
 5463:                 $extra =
 5464:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5465:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5466:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 5467:                     '<option value=""'.$default.'></option>'."\n";
 5468:                 foreach my $server (sort(keys(%{$servers}))) {
 5469:                     if (ref($currbalancer) eq 'HASH') {
 5470:                         next if (exists($currbalancer->{$server}));
 5471:                     }
 5472:                     my $selected;
 5473:                     if ($server eq $current) {
 5474:                         $selected = ' selected="selected"';
 5475:                     }
 5476:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 5477:                 }
 5478:                 $extra .= '</select>';
 5479:             }
 5480:         } elsif ($rule eq $current) {
 5481:             $checked = ' checked="checked"';
 5482:         }
 5483:         $output .= '<span class="LC_nobreak"><label>'.
 5484:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 5485:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 5486:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 5487:                    ')"'.$checked.' />&nbsp;';
 5488:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 5489:             $output .= $ruletitles{'particular'};
 5490:         } else {
 5491:             $output .= $ruletitles{$rulenames[$i]};
 5492:         }
 5493:         $output .= '</label>'.$extra.'</span><br />'."\n";
 5494:     }
 5495:     $output .= '</div></td></tr>'."\n";
 5496:     return $output;
 5497: }
 5498: 
 5499: sub offloadtype_text {
 5500:     my %ruletitles = &Apache::lonlocal::texthash (
 5501:            'default'          => 'Offloads to default destinations',
 5502:            'homeserver'       => "Offloads to user's home server",
 5503:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 5504:            'specific'         => 'Offloads to specific server',
 5505:            'none'             => 'No offload',
 5506:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 5507:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 5508:            'particular'       => 'Session hosted (after re-auth) on server:',
 5509:     );
 5510:     return %ruletitles;
 5511: }
 5512: 
 5513: sub sparestype_titles {
 5514:     my %typestitles = &Apache::lonlocal::texthash (
 5515:                           'primary' => 'primary',
 5516:                           'default' => 'default',
 5517:                       );
 5518:     return %typestitles;
 5519: }
 5520: 
 5521: sub contact_titles {
 5522:     my %titles = &Apache::lonlocal::texthash (
 5523:                    'supportemail'    => 'Support E-mail address',
 5524:                    'adminemail'      => 'Default Server Admin E-mail address',
 5525:                    'errormail'       => 'Error reports to be e-mailed to',
 5526:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 5527:                    'helpdeskmail'    => "Helpdesk requests for this domain's users",
 5528:                    'otherdomsmail'   => 'Helpdesk requests for other (unconfigured) domains',
 5529:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 5530:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 5531:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 5532:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 5533:                  );
 5534:     my %short_titles = &Apache::lonlocal::texthash (
 5535:                            adminemail   => 'Admin E-mail address',
 5536:                            supportemail => 'Support E-mail',
 5537:                        );   
 5538:     return (\%titles,\%short_titles);
 5539: }
 5540: 
 5541: sub helpform_fields {
 5542:     my %titles =  &Apache::lonlocal::texthash (
 5543:                        'username'   => 'Name',
 5544:                        'user'       => 'Username/domain',
 5545:                        'phone'      => 'Phone',
 5546:                        'cc'         => 'Cc e-mail',
 5547:                        'course'     => 'Course Details',
 5548:                        'section'    => 'Sections',
 5549:                        'screenshot' => 'File upload',
 5550:     );
 5551:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 5552:     my %possoptions = (
 5553:                         username     => ['yes','no','req'],
 5554:                         phone        => ['yes','no','req'],
 5555:                         user         => ['yes','no'],
 5556:                         cc           => ['yes','no'],
 5557:                         course       => ['yes','no'],
 5558:                         section      => ['yes','no'],
 5559:                         screenshot   => ['yes','no'],
 5560:                       );
 5561:     my %fieldoptions = &Apache::lonlocal::texthash (
 5562:                          'yes'  => 'Optional',
 5563:                          'req'  => 'Required',
 5564:                          'no'   => "Not shown",
 5565:     );
 5566:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 5567: }
 5568: 
 5569: sub tool_titles {
 5570:     my %titles = &Apache::lonlocal::texthash (
 5571:                      aboutme    => 'Personal web page',
 5572:                      blog       => 'Blog',
 5573:                      webdav     => 'WebDAV',
 5574:                      portfolio  => 'Portfolio',
 5575:                      official   => 'Official courses (with institutional codes)',
 5576:                      unofficial => 'Unofficial courses',
 5577:                      community  => 'Communities',
 5578:                      textbook   => 'Textbook courses',
 5579:                      placement  => 'Placement tests',
 5580:                  );
 5581:     return %titles;
 5582: }
 5583: 
 5584: sub courserequest_titles {
 5585:     my %titles = &Apache::lonlocal::texthash (
 5586:                                    official   => 'Official',
 5587:                                    unofficial => 'Unofficial',
 5588:                                    community  => 'Communities',
 5589:                                    textbook   => 'Textbook',
 5590:                                    placement  => 'Placement tests',
 5591:                                    norequest  => 'Not allowed',
 5592:                                    approval   => 'Approval by Dom. Coord.',
 5593:                                    validate   => 'With validation',
 5594:                                    autolimit  => 'Numerical limit',
 5595:                                    unlimited  => '(blank for unlimited)',
 5596:                  );
 5597:     return %titles;
 5598: }
 5599: 
 5600: sub authorrequest_titles {
 5601:     my %titles = &Apache::lonlocal::texthash (
 5602:                                    norequest  => 'Not allowed',
 5603:                                    approval   => 'Approval by Dom. Coord.',
 5604:                                    automatic  => 'Automatic approval',
 5605:                  );
 5606:     return %titles;
 5607: }
 5608: 
 5609: sub courserequest_conditions {
 5610:     my %conditions = &Apache::lonlocal::texthash (
 5611:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 5612:        validate   => '(Processing of request subject to institutional validation).',
 5613:                  );
 5614:     return %conditions;
 5615: }
 5616: 
 5617: 
 5618: sub print_usercreation {
 5619:     my ($position,$dom,$settings,$rowtotal) = @_;
 5620:     my $numinrow = 4;
 5621:     my $datatable;
 5622:     if ($position eq 'top') {
 5623:         $$rowtotal ++;
 5624:         my $rowcount = 0;
 5625:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 5626:         if (ref($rules) eq 'HASH') {
 5627:             if (keys(%{$rules}) > 0) {
 5628:                 $datatable .= &user_formats_row('username',$settings,$rules,
 5629:                                                 $ruleorder,$numinrow,$rowcount);
 5630:                 $$rowtotal ++;
 5631:                 $rowcount ++;
 5632:             }
 5633:         }
 5634:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 5635:         if (ref($idrules) eq 'HASH') {
 5636:             if (keys(%{$idrules}) > 0) {
 5637:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 5638:                                                 $idruleorder,$numinrow,$rowcount);
 5639:                 $$rowtotal ++;
 5640:                 $rowcount ++;
 5641:             }
 5642:         }
 5643:         if ($rowcount == 0) {
 5644:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 5645:             $$rowtotal ++;
 5646:             $rowcount ++;
 5647:         }
 5648:     } elsif ($position eq 'middle') {
 5649:         my @creators = ('author','course','requestcrs');
 5650:         my ($rules,$ruleorder) =
 5651:             &Apache::lonnet::inst_userrules($dom,'username');
 5652:         my %lt = &usercreation_types();
 5653:         my %checked;
 5654:         if (ref($settings) eq 'HASH') {
 5655:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 5656:                 foreach my $item (@creators) {
 5657:                     $checked{$item} = $settings->{'cancreate'}{$item};
 5658:                 }
 5659:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 5660:                 foreach my $item (@creators) {
 5661:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 5662:                         $checked{$item} = 'none';
 5663:                     }
 5664:                 }
 5665:             }
 5666:         }
 5667:         my $rownum = 0;
 5668:         foreach my $item (@creators) {
 5669:             $rownum ++;
 5670:             if ($checked{$item} eq '') {
 5671:                 $checked{$item} = 'any';
 5672:             }
 5673:             my $css_class;
 5674:             if ($rownum%2) {
 5675:                 $css_class = '';
 5676:             } else {
 5677:                 $css_class = ' class="LC_odd_row" ';
 5678:             }
 5679:             $datatable .= '<tr'.$css_class.'>'.
 5680:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 5681:                          '</span></td><td align="right">';
 5682:             my @options = ('any');
 5683:             if (ref($rules) eq 'HASH') {
 5684:                 if (keys(%{$rules}) > 0) {
 5685:                     push(@options,('official','unofficial'));
 5686:                 }
 5687:             }
 5688:             push(@options,'none');
 5689:             foreach my $option (@options) {
 5690:                 my $type = 'radio';
 5691:                 my $check = ' ';
 5692:                 if ($checked{$item} eq $option) {
 5693:                     $check = ' checked="checked" ';
 5694:                 } 
 5695:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5696:                               '<input type="'.$type.'" name="can_createuser_'.
 5697:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 5698:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 5699:             }
 5700:             $datatable .= '</td></tr>';
 5701:         }
 5702:     } else {
 5703:         my @contexts = ('author','course','domain');
 5704:         my @authtypes = ('int','krb4','krb5','loc');
 5705:         my %checked;
 5706:         if (ref($settings) eq 'HASH') {
 5707:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 5708:                 foreach my $item (@contexts) {
 5709:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 5710:                         foreach my $auth (@authtypes) {
 5711:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 5712:                                 $checked{$item}{$auth} = ' checked="checked" ';
 5713:                             }
 5714:                         }
 5715:                     }
 5716:                 }
 5717:             }
 5718:         } else {
 5719:             foreach my $item (@contexts) {
 5720:                 foreach my $auth (@authtypes) {
 5721:                     $checked{$item}{$auth} = ' checked="checked" ';
 5722:                 }
 5723:             }
 5724:         }
 5725:         my %title = &context_names();
 5726:         my %authname = &authtype_names();
 5727:         my $rownum = 0;
 5728:         my $css_class; 
 5729:         foreach my $item (@contexts) {
 5730:             if ($rownum%2) {
 5731:                 $css_class = '';
 5732:             } else {
 5733:                 $css_class = ' class="LC_odd_row" ';
 5734:             }
 5735:             $datatable .=   '<tr'.$css_class.'>'.
 5736:                             '<td>'.$title{$item}.
 5737:                             '</td><td class="LC_left_item">'.
 5738:                             '<span class="LC_nobreak">';
 5739:             foreach my $auth (@authtypes) {
 5740:                 $datatable .= '<label>'. 
 5741:                               '<input type="checkbox" name="'.$item.'_auth" '.
 5742:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 5743:                               $authname{$auth}.'</label>&nbsp;';
 5744:             }
 5745:             $datatable .= '</span></td></tr>';
 5746:             $rownum ++;
 5747:         }
 5748:         $$rowtotal += $rownum;
 5749:     }
 5750:     return $datatable;
 5751: }
 5752: 
 5753: sub print_selfcreation {
 5754:     my ($position,$dom,$settings,$rowtotal) = @_;
 5755:     my (@selfcreate,$createsettings,$processing,$datatable);
 5756:     if (ref($settings) eq 'HASH') {
 5757:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 5758:             $createsettings = $settings->{'cancreate'};
 5759:             if (ref($createsettings) eq 'HASH') {
 5760:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 5761:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 5762:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 5763:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 5764:                         @selfcreate = ('email','login','sso');
 5765:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 5766:                         @selfcreate = ($createsettings->{'selfcreate'});
 5767:                     }
 5768:                 }
 5769:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 5770:                     $processing = $createsettings->{'selfcreateprocessing'};
 5771:                 }
 5772:             }
 5773:         }
 5774:     }
 5775:     my %radiohash;
 5776:     my $numinrow = 4;
 5777:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 5778:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5779:     if ($position eq 'top') {
 5780:         my %choices = &Apache::lonlocal::texthash (
 5781:                                                       cancreate_login      => 'Institutional Login',
 5782:                                                       cancreate_sso        => 'Institutional Single Sign On',
 5783:                                                   );
 5784:         my @toggles = sort(keys(%choices));
 5785:         my %defaultchecked = (
 5786:                                'cancreate_login' => 'off',
 5787:                                'cancreate_sso'   => 'off',
 5788:                              );
 5789:         my ($onclick,$itemcount);
 5790:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5791:                                                      \%choices,$itemcount,$onclick);
 5792:         $$rowtotal += $itemcount;
 5793:         
 5794:         if (ref($usertypes) eq 'HASH') {
 5795:             if (keys(%{$usertypes}) > 0) {
 5796:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 5797:                                              $dom,$numinrow,$othertitle,
 5798:                                              'statustocreate',$$rowtotal);
 5799:                 $$rowtotal ++;
 5800:             }
 5801:         }
 5802:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 5803:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5804:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 5805:         my $rem;
 5806:         my $numperrow = 2;
 5807:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 5808:         $datatable .= '<tr'.$css_class.'>'.
 5809:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 5810:                      '<td class="LC_left_item">'."\n".
 5811:                      '<table><tr><td>'."\n";
 5812:         for (my $i=0; $i<@fields; $i++) {
 5813:             $rem = $i%($numperrow);
 5814:             if ($rem == 0) {
 5815:                 if ($i > 0) {
 5816:                     $datatable .= '</tr>';
 5817:                 }
 5818:                 $datatable .= '<tr>';
 5819:             }
 5820:             my $currval;
 5821:             if (ref($createsettings) eq 'HASH') {
 5822:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 5823:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 5824:                 }
 5825:             }
 5826:             $datatable .= '<td class="LC_left_item">'.
 5827:                           '<span class="LC_nobreak">'.
 5828:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 5829:                           'value="'.$currval.'" size="10" />&nbsp;'.
 5830:                           $fieldtitles{$fields[$i]}.'</span></td>';
 5831:         }
 5832:         my $colsleft = $numperrow - $rem;
 5833:         if ($colsleft > 1 ) {
 5834:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5835:                          '&nbsp;</td>';
 5836:         } elsif ($colsleft == 1) {
 5837:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5838:         }
 5839:         $datatable .= '</tr></table></td></tr>';
 5840:         $$rowtotal ++;
 5841:     } elsif ($position eq 'middle') {
 5842:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 5843:         my @posstypes;
 5844:         if (ref($types) eq 'ARRAY') {
 5845:             @posstypes = @{$types};
 5846:         }
 5847:         unless (grep(/^default$/,@posstypes)) {
 5848:             push(@posstypes,'default');
 5849:         }
 5850:         my %usertypeshash;
 5851:         if (ref($usertypes) eq 'HASH') {
 5852:             %usertypeshash = %{$usertypes};
 5853:         }
 5854:         $usertypeshash{'default'} = $othertitle;
 5855:         foreach my $status (@posstypes) {
 5856:             $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 5857:                                                    $numinrow,$$rowtotal,\%usertypeshash);
 5858:             $$rowtotal ++;
 5859:         }
 5860:     } else {
 5861:         my %choices = &Apache::lonlocal::texthash (
 5862:                                                       cancreate_email => 'E-mail address as username',
 5863:                                                   );
 5864:         my @toggles = sort(keys(%choices));
 5865:         my %defaultchecked = (
 5866:                                'cancreate_email' => 'off',
 5867:                              );
 5868:         my $itemcount = 0;
 5869:         my $display = 'none';
 5870:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 5871:             $display = 'block';
 5872:         }
 5873:         my $onclick = "toggleDisplay(this.form,'emailoptions');";
 5874:         my $additional = '<div id="emailoptions" style="display: '.$display.'">';
 5875:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5876:         my $order;
 5877:         if (ref($domdefaults{'inststatusguest'}) eq 'ARRAY') {
 5878:             $order = $domdefaults{'inststatusguest'};
 5879:         }
 5880:         my (@ordered,%usertypeshash);
 5881:         if (ref($order) eq 'ARRAY') {
 5882:             @ordered = @{$order};
 5883:         }
 5884:         if (@ordered) {
 5885:             unless (grep(/^default$/,@ordered)) {
 5886:                 push(@ordered,'default');
 5887:             }
 5888:             if (ref($usertypes) eq 'HASH') {
 5889:                 %usertypeshash = %{$usertypes};
 5890:             }
 5891:             $usertypeshash{'default'} = $othertitle;
 5892:             $additional .= '<table><tr>';
 5893:             foreach my $status (@ordered) {
 5894:                 $additional .= '<th>'.$usertypeshash{$status}.'</th>';
 5895:             }
 5896:             $additional .= '</tr><tr>';
 5897:             foreach my $status (@ordered) {
 5898:                 $additional .= '<td>'.&email_as_username($rowtotal,$processing,$status).'</td>';
 5899:             }
 5900:             $additional .= '</tr></table>';
 5901:         } else {
 5902:             $usertypeshash{'default'} = $othertitle;
 5903:             $additional .= &email_as_username($rowtotal,$processing);
 5904:         }
 5905:         $additional .= '</div>'."\n";
 5906: 
 5907:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5908:                                                      \%choices,$$rowtotal,$onclick,$additional);
 5909:         $$rowtotal ++;
 5910:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 5911:         $$rowtotal ++;
 5912:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 5913:         $numinrow = 1;
 5914:         foreach my $status (@ordered) {
 5915:             $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 5916:                                                    $numinrow,$$rowtotal,\%usertypeshash,$infofields,$infotitles);
 5917:             $$rowtotal ++;
 5918:         }
 5919:         my ($emailrules,$emailruleorder) =
 5920:             &Apache::lonnet::inst_userrules($dom,'email');
 5921:         if (ref($emailrules) eq 'HASH') {
 5922:             if (keys(%{$emailrules}) > 0) {
 5923:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 5924:                                                 $emailruleorder,$numinrow,$$rowtotal);
 5925:                 $$rowtotal ++;
 5926:             }
 5927:         }
 5928:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 5929:     }
 5930:     return $datatable;
 5931: }
 5932: 
 5933: sub email_as_username {
 5934:     my ($rowtotal,$processing,$type) = @_;
 5935:     my %choices =
 5936:         &Apache::lonlocal::texthash (
 5937:                                       automatic => 'Automatic approval',
 5938:                                       approval  => 'Queued for approval',
 5939:                                     );
 5940:     my $output;
 5941:     foreach my $option ('automatic','approval') {
 5942:         my $checked;
 5943:         if (ref($processing) eq 'HASH') {
 5944:             if ($type eq '') {   
 5945:                 if (!exists($processing->{'default'})) {
 5946:                     if ($option eq 'automatic') {
 5947:                         $checked = ' checked="checked"';
 5948:                     }
 5949:                 } else {
 5950:                     if ($processing->{'default'} eq $option) {
 5951:                         $checked = ' checked="checked"';
 5952:                     }
 5953:                 }
 5954:             } else {
 5955:                 if (!exists($processing->{$type})) {
 5956:                     if ($option eq 'automatic') {
 5957:                         $checked = ' checked="checked"';
 5958:                     }
 5959:                 } else {
 5960:                     if ($processing->{$type} eq $option) {
 5961:                         $checked = ' checked="checked"';
 5962:                     }
 5963:                 }
 5964:             }
 5965:         } elsif ($option eq 'automatic') {
 5966:             $checked = ' checked="checked"'; 
 5967:         }
 5968:         my $name = 'cancreate_emailprocess';
 5969:         if (($type ne '') && ($type ne 'default')) {
 5970:             $name .= '_'.$type;
 5971:         }
 5972:         $output .= '<span class="LC_nobreak"><label>'.
 5973:                    '<input type="radio" name="'.$name.'"'.
 5974:                    $checked.' value="'.$option.'" />'.
 5975:                    $choices{$option}.'</label></span>';
 5976:         if ($type eq '') {
 5977:             $output .= '&nbsp;';
 5978:         } else {
 5979:             $output .= '<br />';
 5980:         }
 5981:     }
 5982:     $$rowtotal ++;
 5983:     return $output;
 5984: }
 5985: 
 5986: sub captcha_choice {
 5987:     my ($context,$settings,$itemcount) = @_;
 5988:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 5989:         $vertext,$currver);
 5990:     my %lt = &captcha_phrases();
 5991:     $keyentry = 'hidden';
 5992:     if ($context eq 'cancreate') {
 5993:         $rowname = &mt('CAPTCHA validation');
 5994:     } elsif ($context eq 'login') {
 5995:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 5996:     }
 5997:     if (ref($settings) eq 'HASH') {
 5998:         if ($settings->{'captcha'}) {
 5999:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 6000:         } else {
 6001:             $checked{'original'} = ' checked="checked"';
 6002:         }
 6003:         if ($settings->{'captcha'} eq 'recaptcha') {
 6004:             $pubtext = $lt{'pub'};
 6005:             $privtext = $lt{'priv'};
 6006:             $keyentry = 'text';
 6007:             $vertext = $lt{'ver'};
 6008:             $currver = $settings->{'recaptchaversion'};
 6009:             if ($currver ne '2') {
 6010:                 $currver = 1;
 6011:             }
 6012:         }
 6013:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 6014:             $currpub = $settings->{'recaptchakeys'}{'public'};
 6015:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 6016:         }
 6017:     } else {
 6018:         $checked{'original'} = ' checked="checked"';
 6019:     }
 6020:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6021:     my $output = '<tr'.$css_class.'>'.
 6022:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 6023:                  '<table><tr><td>'."\n";
 6024:     foreach my $option ('original','recaptcha','notused') {
 6025:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 6026:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 6027:                    $lt{$option}.'</label></span>';
 6028:         unless ($option eq 'notused') {
 6029:             $output .= ('&nbsp;'x2)."\n";
 6030:         }
 6031:     }
 6032: #
 6033: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 6034: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 6035: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 6036: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 6037: #
 6038:     $output .= '</td></tr>'."\n".
 6039:                '<tr><td>'."\n".
 6040:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 6041:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 6042:                $currpub.'" size="40" /></span><br />'."\n".
 6043:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 6044:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 6045:                $currpriv.'" size="40" /></span><br />'.
 6046:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 6047:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 6048:                $currver.'" size="3" /></span><br />'.
 6049:                '</td></tr></table>'."\n".
 6050:                '</td></tr>';
 6051:     return $output;
 6052: }
 6053: 
 6054: sub user_formats_row {
 6055:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 6056:     my $output;
 6057:     my %text = (
 6058:                    'username' => 'new usernames',
 6059:                    'id'       => 'IDs',
 6060:                    'email'    => 'self-created accounts (e-mail)',
 6061:                );
 6062:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 6063:     $output = '<tr '.$css_class.'>'.
 6064:               '<td><span class="LC_nobreak">';
 6065:     if ($type eq 'email') {
 6066:         $output .= &mt("Formats disallowed for $text{$type}: ");
 6067:     } else {
 6068:         $output .= &mt("Format rules to check for $text{$type}: ");
 6069:     }
 6070:     $output .= '</span></td>'.
 6071:                '<td class="LC_left_item" colspan="2"><table>';
 6072:     my $rem;
 6073:     if (ref($ruleorder) eq 'ARRAY') {
 6074:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 6075:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 6076:                 my $rem = $i%($numinrow);
 6077:                 if ($rem == 0) {
 6078:                     if ($i > 0) {
 6079:                         $output .= '</tr>';
 6080:                     }
 6081:                     $output .= '<tr>';
 6082:                 }
 6083:                 my $check = ' ';
 6084:                 if (ref($settings) eq 'HASH') {
 6085:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 6086:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 6087:                             $check = ' checked="checked" ';
 6088:                         }
 6089:                     }
 6090:                 }
 6091:                 $output .= '<td class="LC_left_item">'.
 6092:                            '<span class="LC_nobreak"><label>'.
 6093:                            '<input type="checkbox" name="'.$type.'_rule" '.
 6094:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 6095:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 6096:             }
 6097:         }
 6098:         $rem = @{$ruleorder}%($numinrow);
 6099:     }
 6100:     my $colsleft = $numinrow - $rem;
 6101:     if ($colsleft > 1 ) {
 6102:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6103:                    '&nbsp;</td>';
 6104:     } elsif ($colsleft == 1) {
 6105:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 6106:     }
 6107:     $output .= '</tr></table></td></tr>';
 6108:     return $output;
 6109: }
 6110: 
 6111: sub usercreation_types {
 6112:     my %lt = &Apache::lonlocal::texthash (
 6113:                     author     => 'When adding a co-author',
 6114:                     course     => 'When adding a user to a course',
 6115:                     requestcrs => 'When requesting a course',
 6116:                     any        => 'Any',
 6117:                     official   => 'Institutional only ',
 6118:                     unofficial => 'Non-institutional only',
 6119:                     none       => 'None',
 6120:     );
 6121:     return %lt;
 6122: }
 6123: 
 6124: sub selfcreation_types {
 6125:     my %lt = &Apache::lonlocal::texthash (
 6126:                     selfcreate => 'User creates own account',
 6127:                     any        => 'Any',
 6128:                     official   => 'Institutional only ',
 6129:                     unofficial => 'Non-institutional only',
 6130:                     email      => 'E-mail address',
 6131:                     login      => 'Institutional Login',
 6132:                     sso        => 'SSO',
 6133:              );
 6134: }
 6135: 
 6136: sub authtype_names {
 6137:     my %lt = &Apache::lonlocal::texthash(
 6138:                       int    => 'Internal',
 6139:                       krb4   => 'Kerberos 4',
 6140:                       krb5   => 'Kerberos 5',
 6141:                       loc    => 'Local',
 6142:                   );
 6143:     return %lt;
 6144: }
 6145: 
 6146: sub context_names {
 6147:     my %context_title = &Apache::lonlocal::texthash(
 6148:        author => 'Creating users when an Author',
 6149:        course => 'Creating users when in a course',
 6150:        domain => 'Creating users when a Domain Coordinator',
 6151:     );
 6152:     return %context_title;
 6153: }
 6154: 
 6155: sub print_usermodification {
 6156:     my ($position,$dom,$settings,$rowtotal) = @_;
 6157:     my $numinrow = 4;
 6158:     my ($context,$datatable,$rowcount);
 6159:     if ($position eq 'top') {
 6160:         $rowcount = 0;
 6161:         $context = 'author'; 
 6162:         foreach my $role ('ca','aa') {
 6163:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6164:                                                    $numinrow,$rowcount);
 6165:             $$rowtotal ++;
 6166:             $rowcount ++;
 6167:         }
 6168:     } elsif ($position eq 'bottom') {
 6169:         $context = 'course';
 6170:         $rowcount = 0;
 6171:         foreach my $role ('st','ep','ta','in','cr') {
 6172:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6173:                                                    $numinrow,$rowcount);
 6174:             $$rowtotal ++;
 6175:             $rowcount ++;
 6176:         }
 6177:     }
 6178:     return $datatable;
 6179: }
 6180: 
 6181: sub print_defaults {
 6182:     my ($position,$dom,$settings,$rowtotal) = @_;
 6183:     my $rownum = 0;
 6184:     my ($datatable,$css_class,$titles);
 6185:     unless ($position eq 'bottom') {
 6186:         $titles = &defaults_titles($dom);
 6187:     }
 6188:     if ($position eq 'top') {
 6189:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 6190:                      'datelocale_def','portal_def');
 6191:         my %defaults;
 6192:         if (ref($settings) eq 'HASH') {
 6193:             %defaults = %{$settings};
 6194:         } else {
 6195:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6196:             foreach my $item (@items) {
 6197:                 $defaults{$item} = $domdefaults{$item};
 6198:             }
 6199:         }
 6200:         foreach my $item (@items) {
 6201:             if ($rownum%2) {
 6202:                 $css_class = '';
 6203:             } else {
 6204:                 $css_class = ' class="LC_odd_row" ';
 6205:             }
 6206:             $datatable .= '<tr'.$css_class.'>'.
 6207:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 6208:                           '</span></td><td class="LC_right_item" colspan="3">';
 6209:             if ($item eq 'auth_def') {
 6210:                 my @authtypes = ('internal','krb4','krb5','localauth');
 6211:                 my %shortauth = (
 6212:                                  internal => 'int',
 6213:                                  krb4 => 'krb4',
 6214:                                  krb5 => 'krb5',
 6215:                                  localauth  => 'loc'
 6216:                                 );
 6217:                 my %authnames = &authtype_names();
 6218:                 foreach my $auth (@authtypes) {
 6219:                     my $checked = ' ';
 6220:                     if ($defaults{$item} eq $auth) {
 6221:                         $checked = ' checked="checked" ';
 6222:                     }
 6223:                     $datatable .= '<label><input type="radio" name="'.$item.
 6224:                                   '" value="'.$auth.'"'.$checked.'/>'.
 6225:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 6226:                 }
 6227:             } elsif ($item eq 'timezone_def') {
 6228:                 my $includeempty = 1;
 6229:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 6230:             } elsif ($item eq 'datelocale_def') {
 6231:                 my $includeempty = 1;
 6232:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 6233:             } elsif ($item eq 'lang_def') {
 6234:                 my $includeempty = 1;
 6235:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 6236:             } else {
 6237:                 my $size;
 6238:                 if ($item eq 'portal_def') {
 6239:                     $size = ' size="25"';
 6240:                 }
 6241:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6242:                               $defaults{$item}.'"'.$size.' />';
 6243:             }
 6244:             $datatable .= '</td></tr>';
 6245:             $rownum ++;
 6246:         }
 6247:     } elsif ($position eq 'middle') {
 6248:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 6249:         my %defaults;
 6250:         if (ref($settings) eq 'HASH') {
 6251:             %defaults = %{$settings};
 6252:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 6253:                 $defaults{'intauth_cost'} = 10;
 6254:             }
 6255:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 6256:                 $defaults{'intauth_check'} = 0;
 6257:             }
 6258:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 6259:                 $defaults{'intauth_switch'} = 0;
 6260:             }
 6261:         } else {
 6262:             %defaults = (
 6263:                           'intauth_cost'   => 10,
 6264:                           'intauth_check'  => 0,
 6265:                           'intauth_switch' => 0,
 6266:                         );
 6267:         }
 6268:         foreach my $item (@items) {
 6269:             if ($rownum%2) {
 6270:                 $css_class = '';
 6271:             } else {
 6272:                 $css_class = ' class="LC_odd_row" ';
 6273:             }
 6274:             $datatable .= '<tr'.$css_class.'>'.
 6275:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 6276:                           '</span></td><td class="LC_left_item" colspan="3">';
 6277:             if ($item eq 'intauth_switch') {
 6278:                 my @options = (0,1,2);
 6279:                 my %optiondesc = &Apache::lonlocal::texthash (
 6280:                                    0 => 'No',
 6281:                                    1 => 'Yes',
 6282:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 6283:                                  );
 6284:                 $datatable .= '<table width="100%">';
 6285:                 foreach my $option (@options) {
 6286:                     my $checked = ' ';
 6287:                     if ($defaults{$item} eq $option) {
 6288:                         $checked = ' checked="checked"';
 6289:                     }
 6290:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6291:                                   '<label><input type="radio" name="'.$item.
 6292:                                   '" value="'.$option.'"'.$checked.' />'.
 6293:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6294:                 }
 6295:                 $datatable .= '</table>';
 6296:             } elsif ($item eq 'intauth_check') {
 6297:                 my @options = (0,1,2);
 6298:                 my %optiondesc = &Apache::lonlocal::texthash (
 6299:                                    0 => 'No',
 6300:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 6301:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 6302:                                  );
 6303:                 $datatable .= '<table wisth="100%">';
 6304:                 foreach my $option (@options) {
 6305:                     my $checked = ' ';
 6306:                     my $onclick;
 6307:                     if ($defaults{$item} eq $option) {
 6308:                         $checked = ' checked="checked"';
 6309:                     }
 6310:                     if ($option == 2) {
 6311:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 6312:                     }
 6313:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6314:                                   '<label><input type="radio" name="'.$item.
 6315:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 6316:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6317:                 }
 6318:                 $datatable .= '</table>';
 6319:             } else {
 6320:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6321:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />'; 
 6322:             }
 6323:             $datatable .= '</td></tr>';
 6324:             $rownum ++;
 6325:         }
 6326:     } else {
 6327:         my %defaults;
 6328:         if (ref($settings) eq 'HASH') {
 6329:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH') &&
 6330:                 (ref($settings->{'inststatusguest'}) eq 'ARRAY')) {
 6331:                 my $maxnum = @{$settings->{'inststatusorder'}};
 6332:                 for (my $i=0; $i<$maxnum; $i++) {
 6333:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 6334:                     my $item = $settings->{'inststatusorder'}->[$i];
 6335:                     my $title = $settings->{'inststatustypes'}->{$item};
 6336:                     my $guestok;
 6337:                     if (grep(/^\Q$item\E$/,@{$settings->{'inststatusguest'}})) {
 6338:                         $guestok = 1;
 6339:                     }
 6340:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 6341:                     $datatable .= '<tr'.$css_class.'>'.
 6342:                                   '<td><span class="LC_nobreak">'.
 6343:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 6344:                     for (my $k=0; $k<=$maxnum; $k++) {
 6345:                         my $vpos = $k+1;
 6346:                         my $selstr;
 6347:                         if ($k == $i) {
 6348:                             $selstr = ' selected="selected" ';
 6349:                         }
 6350:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6351:                     }
 6352:                     my ($checkedon,$checkedoff);
 6353:                     $checkedoff = ' checked="checked"';
 6354:                     if ($guestok) {
 6355:                         $checkedon = $checkedoff;
 6356:                         $checkedoff = ''; 
 6357:                     }
 6358:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 6359:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 6360:                                   &mt('delete').'</span></td>'.
 6361:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 6362:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 6363:                                   '</span></td>'.
 6364:                                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6365:                                   '<label><input type="radio" value="1" name="inststatus_guest_'.$item.'"'.$checkedon.' />'.
 6366:                                   &mt('Yes').'</label>'.('&nbsp;'x2).
 6367:                                   '<label><input type="radio" value="0" name="inststatus_guest_'.$item.'"'.$checkedoff.' />'.
 6368:                                   &mt('No').'</label></span></td></tr>';
 6369:                 }
 6370:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 6371:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 6372:                 $datatable .= '<tr '.$css_class.'>'.
 6373:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 6374:                 for (my $k=0; $k<=$maxnum; $k++) {
 6375:                     my $vpos = $k+1;
 6376:                     my $selstr;
 6377:                     if ($k == $maxnum) {
 6378:                         $selstr = ' selected="selected" ';
 6379:                     }
 6380:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6381:                 }
 6382:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 6383:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 6384:                               '&nbsp;'.&mt('(new)').
 6385:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 6386:                               &mt('Name displayed:').
 6387:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 6388:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 6389:                               '<label><input type="radio" value="1" name="addinststatus_guest" />'.
 6390:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 6391:                               '<label><input type="radio" value="0" name="addinststatus_guest" />'.
 6392:                               &mt('No').'</label></span></td></tr>';
 6393:                               '</tr>'."\n";
 6394:                 $rownum ++;
 6395:             }
 6396:         }
 6397:     }
 6398:     $$rowtotal += $rownum;
 6399:     return $datatable;
 6400: }
 6401: 
 6402: sub get_languages_hash {
 6403:     my %langchoices;
 6404:     foreach my $id (&Apache::loncommon::languageids()) {
 6405:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 6406:         if ($code ne '') {
 6407:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 6408:         }
 6409:     }
 6410:     return %langchoices;
 6411: }
 6412: 
 6413: sub defaults_titles {
 6414:     my ($dom) = @_;
 6415:     my %titles = &Apache::lonlocal::texthash (
 6416:                    'auth_def'      => 'Default authentication type',
 6417:                    'auth_arg_def'  => 'Default authentication argument',
 6418:                    'lang_def'      => 'Default language',
 6419:                    'timezone_def'  => 'Default timezone',
 6420:                    'datelocale_def' => 'Default locale for dates',
 6421:                    'portal_def'     => 'Portal/Default URL',
 6422:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 6423:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 6424:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 6425:                  );
 6426:     if ($dom) {
 6427:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 6428:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 6429:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 6430:         $protocol = 'http' if ($protocol ne 'https');
 6431:         if ($uint_dom) {
 6432:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 6433:                                          $uint_dom);
 6434:         }
 6435:     }
 6436:     return (\%titles);
 6437: }
 6438: 
 6439: sub print_scantronformat {
 6440:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 6441:     my $itemcount = 1;
 6442:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 6443:         %confhash);
 6444:     my $switchserver = &check_switchserver($dom,$confname);
 6445:     my %lt = &Apache::lonlocal::texthash (
 6446:                 default => 'Default bubblesheet format file error',
 6447:                 custom  => 'Custom bubblesheet format file error',
 6448:              );
 6449:     my %scantronfiles = (
 6450:         default => 'default.tab',
 6451:         custom => 'custom.tab',
 6452:     );
 6453:     foreach my $key (keys(%scantronfiles)) {
 6454:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 6455:                               .$scantronfiles{$key};
 6456:     }
 6457:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 6458:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 6459:         if (!$switchserver) {
 6460:             my $servadm = $r->dir_config('lonAdmEMail');
 6461:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 6462:             if ($configuserok eq 'ok') {
 6463:                 if ($author_ok eq 'ok') {
 6464:                     my %legacyfile = (
 6465:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 6466:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 6467:                     );
 6468:                     my %md5chk;
 6469:                     foreach my $type (keys(%legacyfile)) {
 6470:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 6471:                         chomp($md5chk{$type});
 6472:                     }
 6473:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 6474:                         foreach my $type (keys(%legacyfile)) {
 6475:                             ($scantronurls{$type},my $error) = 
 6476:                                 &legacy_scantronformat($r,$dom,$confname,
 6477:                                                  $type,$legacyfile{$type},
 6478:                                                  $scantronurls{$type},
 6479:                                                  $scantronfiles{$type});
 6480:                             if ($error ne '') {
 6481:                                 $error{$type} = $error;
 6482:                             }
 6483:                         }
 6484:                         if (keys(%error) == 0) {
 6485:                             $is_custom = 1;
 6486:                             $confhash{'scantron'}{'scantronformat'} = 
 6487:                                 $scantronurls{'custom'};
 6488:                             my $putresult = 
 6489:                                 &Apache::lonnet::put_dom('configuration',
 6490:                                                          \%confhash,$dom);
 6491:                             if ($putresult ne 'ok') {
 6492:                                 $error{'custom'} = 
 6493:                                     '<span class="LC_error">'.
 6494:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6495:                             }
 6496:                         }
 6497:                     } else {
 6498:                         ($scantronurls{'default'},my $error) =
 6499:                             &legacy_scantronformat($r,$dom,$confname,
 6500:                                           'default',$legacyfile{'default'},
 6501:                                           $scantronurls{'default'},
 6502:                                           $scantronfiles{'default'});
 6503:                         if ($error eq '') {
 6504:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 6505:                             my $putresult =
 6506:                                 &Apache::lonnet::put_dom('configuration',
 6507:                                                          \%confhash,$dom);
 6508:                             if ($putresult ne 'ok') {
 6509:                                 $error{'default'} =
 6510:                                     '<span class="LC_error">'.
 6511:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6512:                             }
 6513:                         } else {
 6514:                             $error{'default'} = $error;
 6515:                         }
 6516:                     }
 6517:                 }
 6518:             }
 6519:         } else {
 6520:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 6521:         }
 6522:     }
 6523:     if (ref($settings) eq 'HASH') {
 6524:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 6525:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 6526:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 6527:                 $scantronurl = '';
 6528:             } else {
 6529:                 $scantronurl = $settings->{'scantronformat'};
 6530:             }
 6531:             $is_custom = 1;
 6532:         } else {
 6533:             $scantronurl = $scantronurls{'default'};
 6534:         }
 6535:     } else {
 6536:         if ($is_custom) {
 6537:             $scantronurl = $scantronurls{'custom'};
 6538:         } else {
 6539:             $scantronurl = $scantronurls{'default'};
 6540:         }
 6541:     }
 6542:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6543:     $datatable .= '<tr'.$css_class.'>';
 6544:     if (!$is_custom) {
 6545:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 6546:                       '<span class="LC_nobreak">';
 6547:         if ($scantronurl) {
 6548:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 6549:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 6550:         } else {
 6551:             $datatable = &mt('File unavailable for display');
 6552:         }
 6553:         $datatable .= '</span></td>';
 6554:         if (keys(%error) == 0) { 
 6555:             $datatable .= '<td valign="bottom">';
 6556:             if (!$switchserver) {
 6557:                 $datatable .= &mt('Upload:').'<br />';
 6558:             }
 6559:         } else {
 6560:             my $errorstr;
 6561:             foreach my $key (sort(keys(%error))) {
 6562:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6563:             }
 6564:             $datatable .= '<td>'.$errorstr;
 6565:         }
 6566:     } else {
 6567:         if (keys(%error) > 0) {
 6568:             my $errorstr;
 6569:             foreach my $key (sort(keys(%error))) {
 6570:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6571:             } 
 6572:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 6573:         } elsif ($scantronurl) {
 6574:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 6575:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 6576:             $datatable .= '<td><span class="LC_nobreak">'.
 6577:                           $link.
 6578:                           '<label><input type="checkbox" name="scantronformat_del"'.
 6579:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 6580:                           '<td><span class="LC_nobreak">&nbsp;'.
 6581:                           &mt('Replace:').'</span><br />';
 6582:         }
 6583:     }
 6584:     if (keys(%error) == 0) {
 6585:         if ($switchserver) {
 6586:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 6587:         } else {
 6588:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 6589:                          '<input type="file" name="scantronformat" /></span>';
 6590:         }
 6591:     }
 6592:     $datatable .= '</td></tr>';
 6593:     $$rowtotal ++;
 6594:     return $datatable;
 6595: }
 6596: 
 6597: sub legacy_scantronformat {
 6598:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 6599:     my ($url,$error);
 6600:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 6601:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 6602:         (my $result,$url) =
 6603:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 6604:                          '','',$newfile);
 6605:         if ($result ne 'ok') {
 6606:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 6607:         }
 6608:     }
 6609:     return ($url,$error);
 6610: }
 6611: 
 6612: sub print_coursecategories {
 6613:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 6614:     my $datatable;
 6615:     if ($position eq 'top') {
 6616:         my (%checked);
 6617:         my @catitems = ('unauth','auth');
 6618:         my @cattypes = ('std','domonly','codesrch','none');
 6619:         $checked{'unauth'} = 'std';
 6620:         $checked{'auth'} = 'std';
 6621:         if (ref($settings) eq 'HASH') {
 6622:             foreach my $type (@cattypes) {
 6623:                 if ($type eq $settings->{'unauth'}) {
 6624:                     $checked{'unauth'} = $type;
 6625:                 }
 6626:                 if ($type eq $settings->{'auth'}) {
 6627:                     $checked{'auth'} = $type;
 6628:                 }
 6629:             }
 6630:         }
 6631:         my %lt = &Apache::lonlocal::texthash (
 6632:                                                unauth   => 'Catalog type for unauthenticated users',
 6633:                                                auth     => 'Catalog type for authenticated users',
 6634:                                                none     => 'No catalog',
 6635:                                                std      => 'Standard catalog',
 6636:                                                domonly  => 'Domain-only catalog',
 6637:                                                codesrch => "Code search form",
 6638:                                              );
 6639:        my $itemcount = 0;
 6640:        foreach my $item (@catitems) {
 6641:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 6642:            $datatable .= '<tr '.$css_class.'>'.
 6643:                          '<td>'.$lt{$item}.'</td>'.
 6644:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 6645:            foreach my $type (@cattypes) {
 6646:                my $ischecked;
 6647:                if ($checked{$item} eq $type) {
 6648:                    $ischecked=' checked="checked"';
 6649:                }
 6650:                $datatable .= '<label>'.
 6651:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 6652:                              ' />'.$lt{$type}.'</label>&nbsp;';
 6653:            }
 6654:            $datatable .= '</td></tr>';
 6655:            $itemcount ++;
 6656:         }
 6657:         $$rowtotal += $itemcount;
 6658:     } elsif ($position eq 'middle') {
 6659:         my $toggle_cats_crs = ' ';
 6660:         my $toggle_cats_dom = ' checked="checked" ';
 6661:         my $can_cat_crs = ' ';
 6662:         my $can_cat_dom = ' checked="checked" ';
 6663:         my $toggle_catscomm_comm = ' ';
 6664:         my $toggle_catscomm_dom = ' checked="checked" ';
 6665:         my $can_catcomm_comm = ' ';
 6666:         my $can_catcomm_dom = ' checked="checked" ';
 6667:         my $toggle_catsplace_place = ' ';
 6668:         my $toggle_catsplace_dom = ' checked="checked" ';
 6669:         my $can_catplace_place = ' ';
 6670:         my $can_catplace_dom = ' checked="checked" ';
 6671: 
 6672:         if (ref($settings) eq 'HASH') {
 6673:             if ($settings->{'togglecats'} eq 'crs') {
 6674:                 $toggle_cats_crs = $toggle_cats_dom;
 6675:                 $toggle_cats_dom = ' ';
 6676:             }
 6677:             if ($settings->{'categorize'} eq 'crs') {
 6678:                 $can_cat_crs = $can_cat_dom;
 6679:                 $can_cat_dom = ' ';
 6680:             }
 6681:             if ($settings->{'togglecatscomm'} eq 'comm') {
 6682:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 6683:                 $toggle_catscomm_dom = ' ';
 6684:             }
 6685:             if ($settings->{'categorizecomm'} eq 'comm') {
 6686:                 $can_catcomm_comm = $can_catcomm_dom;
 6687:                 $can_catcomm_dom = ' ';
 6688:             }
 6689:             if ($settings->{'togglecatsplace'} eq 'place') {
 6690:                 $toggle_catsplace_place = $toggle_catsplace_dom;
 6691:                 $toggle_catsplace_dom = ' ';
 6692:             }
 6693:             if ($settings->{'categorizeplace'} eq 'place') {
 6694:                 $can_catplace_place = $can_catplace_dom;
 6695:                 $can_catplace_dom = ' ';
 6696:             }
 6697:         }
 6698:         my %title = &Apache::lonlocal::texthash (
 6699:                      togglecats      => 'Show/Hide a course in catalog',
 6700:                      togglecatscomm  => 'Show/Hide a community in catalog',
 6701:                      togglecatsplace => 'Show/Hide a placement test in catalog',
 6702:                      categorize      => 'Assign a category to a course',
 6703:                      categorizecomm  => 'Assign a category to a community',
 6704:                      categorizeplace => 'Assign a category to a placement test',
 6705:                     );
 6706:         my %level = &Apache::lonlocal::texthash (
 6707:                      dom   => 'Set in Domain',
 6708:                      crs   => 'Set in Course',
 6709:                      comm  => 'Set in Community',
 6710:                      place => 'Set in Placement Test',
 6711:                     );
 6712:         $datatable = '<tr class="LC_odd_row">'.
 6713:                   '<td>'.$title{'togglecats'}.'</td>'.
 6714:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6715:                   '<input type="radio" name="togglecats"'.
 6716:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6717:                   '<label><input type="radio" name="togglecats"'.
 6718:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 6719:                   '</tr><tr>'.
 6720:                   '<td>'.$title{'categorize'}.'</td>'.
 6721:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6722:                   '<label><input type="radio" name="categorize"'.
 6723:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6724:                   '<label><input type="radio" name="categorize"'.
 6725:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 6726:                   '</tr><tr class="LC_odd_row">'.
 6727:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 6728:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6729:                   '<input type="radio" name="togglecatscomm"'.
 6730:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6731:                   '<label><input type="radio" name="togglecatscomm"'.
 6732:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 6733:                   '</tr><tr>'.
 6734:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 6735:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6736:                   '<label><input type="radio" name="categorizecomm"'.
 6737:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6738:                   '<label><input type="radio" name="categorizecomm"'.
 6739:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 6740:                   '</tr><tr>'.
 6741:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
 6742:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6743:                   '<input type="radio" name="togglecatsplace"'.
 6744:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6745:                   '<label><input type="radio" name="togglecatscomm"'.
 6746:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
 6747:                   '</tr><tr>'.
 6748:                   '<td>'.$title{'categorizeplace'}.'</td>'.
 6749:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6750:                   '<label><input type="radio" name="categorizeplace"'.
 6751:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6752:                   '<label><input type="radio" name="categorizeplace"'.
 6753:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
 6754:                   '</tr>';
 6755:         $$rowtotal += 6;
 6756:     } else {
 6757:         my $css_class;
 6758:         my $itemcount = 1;
 6759:         my $cathash; 
 6760:         if (ref($settings) eq 'HASH') {
 6761:             $cathash = $settings->{'cats'};
 6762:         }
 6763:         if (ref($cathash) eq 'HASH') {
 6764:             my (@cats,@trails,%allitems,%idx,@jsarray);
 6765:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 6766:                                                    \%allitems,\%idx,\@jsarray);
 6767:             my $maxdepth = scalar(@cats);
 6768:             my $colattrib = '';
 6769:             if ($maxdepth > 2) {
 6770:                 $colattrib = ' colspan="2" ';
 6771:             }
 6772:             my @path;
 6773:             if (@cats > 0) {
 6774:                 if (ref($cats[0]) eq 'ARRAY') {
 6775:                     my $numtop = @{$cats[0]};
 6776:                     my $maxnum = $numtop;
 6777:                     my %default_names = (
 6778:                           instcode    => &mt('Official courses'),
 6779:                           communities => &mt('Communities'),
 6780:                           placement   => &mt('Placement Tests'),
 6781:                     );
 6782: 
 6783:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 6784:                         ($cathash->{'instcode::0'} eq '') ||
 6785:                         (!grep(/^communities$/,@{$cats[0]})) || 
 6786:                         ($cathash->{'communities::0'} eq '') ||
 6787:                         (!grep(/^placement$/,@{$cats[0]})) ||
 6788:                         ($cathash->{'placement::0'} eq '')) {
 6789:                         $maxnum ++;
 6790:                     }
 6791:                     my $lastidx;
 6792:                     for (my $i=0; $i<$numtop; $i++) {
 6793:                         my $parent = $cats[0][$i];
 6794:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6795:                         my $item = &escape($parent).'::0';
 6796:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 6797:                         $lastidx = $idx{$item};
 6798:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6799:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 6800:                         for (my $k=0; $k<=$maxnum; $k++) {
 6801:                             my $vpos = $k+1;
 6802:                             my $selstr;
 6803:                             if ($k == $i) {
 6804:                                 $selstr = ' selected="selected" ';
 6805:                             }
 6806:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6807:                         }
 6808:                         $datatable .= '</select></span></td><td>';
 6809:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
 6810:                             $datatable .=  '<span class="LC_nobreak">'
 6811:                                            .$default_names{$parent}.'</span>';
 6812:                             if ($parent eq 'instcode') {
 6813:                                 $datatable .= '<br /><span class="LC_nobreak">('
 6814:                                               .&mt('with institutional codes')
 6815:                                               .')</span></td><td'.$colattrib.'>';
 6816:                             } else {
 6817:                                 $datatable .= '<table><tr><td>';
 6818:                             }
 6819:                             $datatable .= '<span class="LC_nobreak">'
 6820:                                           .'<label><input type="radio" name="'
 6821:                                           .$parent.'" value="1" checked="checked" />'
 6822:                                           .&mt('Display').'</label>';
 6823:                             if ($parent eq 'instcode') {
 6824:                                 $datatable .= '&nbsp;';
 6825:                             } else {
 6826:                                 $datatable .= '</span></td></tr><tr><td>'
 6827:                                               .'<span class="LC_nobreak">';
 6828:                             }
 6829:                             $datatable .= '<label><input type="radio" name="'
 6830:                                           .$parent.'" value="0" />'
 6831:                                           .&mt('Do not display').'</label></span>';
 6832:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
 6833:                                 $datatable .= '</td></tr></table>';
 6834:                             }
 6835:                             $datatable .= '</td>';
 6836:                         } else {
 6837:                             $datatable .= $parent
 6838:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 6839:                                           .'<input type="checkbox" name="deletecategory" '
 6840:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 6841:                         }
 6842:                         my $depth = 1;
 6843:                         push(@path,$parent);
 6844:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 6845:                         pop(@path);
 6846:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 6847:                         $itemcount ++;
 6848:                     }
 6849:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6850:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 6851:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 6852:                     for (my $k=0; $k<=$maxnum; $k++) {
 6853:                         my $vpos = $k+1;
 6854:                         my $selstr;
 6855:                         if ($k == $numtop) {
 6856:                             $selstr = ' selected="selected" ';
 6857:                         }
 6858:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6859:                     }
 6860:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 6861:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 6862:                                   .'</tr>'."\n";
 6863:                     $itemcount ++;
 6864:                     foreach my $default ('instcode','communities','placement') {
 6865:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 6866:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6867:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 6868:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 6869:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 6870:                             for (my $k=0; $k<=$maxnum; $k++) {
 6871:                                 my $vpos = $k+1;
 6872:                                 my $selstr;
 6873:                                 if ($k == $maxnum) {
 6874:                                     $selstr = ' selected="selected" ';
 6875:                                 }
 6876:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6877:                             }
 6878:                             $datatable .= '</select></span></td>'.
 6879:                                           '<td><span class="LC_nobreak">'.
 6880:                                           $default_names{$default}.'</span>';
 6881:                             if ($default eq 'instcode') {
 6882:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 6883:                                               .&mt('with institutional codes').')</span>';
 6884:                             }
 6885:                             $datatable .= '</td>'
 6886:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 6887:                                           .&mt('Display').'</label>&nbsp;'
 6888:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 6889:                                           .&mt('Do not display').'</label></span></td></tr>';
 6890:                         }
 6891:                     }
 6892:                 }
 6893:             } else {
 6894:                 $datatable .= &initialize_categories($itemcount);
 6895:             }
 6896:         } else {
 6897:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 6898:                           .&initialize_categories($itemcount);
 6899:         }
 6900:         $$rowtotal += $itemcount;
 6901:     }
 6902:     return $datatable;
 6903: }
 6904: 
 6905: sub print_serverstatuses {
 6906:     my ($dom,$settings,$rowtotal) = @_;
 6907:     my $datatable;
 6908:     my @pages = &serverstatus_pages();
 6909:     my (%namedaccess,%machineaccess);
 6910:     foreach my $type (@pages) {
 6911:         $namedaccess{$type} = '';
 6912:         $machineaccess{$type}= '';
 6913:     }
 6914:     if (ref($settings) eq 'HASH') {
 6915:         foreach my $type (@pages) {
 6916:             if (exists($settings->{$type})) {
 6917:                 if (ref($settings->{$type}) eq 'HASH') {
 6918:                     foreach my $key (keys(%{$settings->{$type}})) {
 6919:                         if ($key eq 'namedusers') {
 6920:                             $namedaccess{$type} = $settings->{$type}->{$key};
 6921:                         } elsif ($key eq 'machines') {
 6922:                             $machineaccess{$type} = $settings->{$type}->{$key};
 6923:                         }
 6924:                     }
 6925:                 }
 6926:             }
 6927:         }
 6928:     }
 6929:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 6930:     my $rownum = 0;
 6931:     my $css_class;
 6932:     foreach my $type (@pages) {
 6933:         $rownum ++;
 6934:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 6935:         $datatable .= '<tr'.$css_class.'>'.
 6936:                       '<td><span class="LC_nobreak">'.
 6937:                       $titles->{$type}.'</span></td>'.
 6938:                       '<td class="LC_left_item">'.
 6939:                       '<input type="text" name="'.$type.'_namedusers" '.
 6940:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 6941:                       '<td class="LC_right_item">'.
 6942:                       '<span class="LC_nobreak">'.
 6943:                       '<input type="text" name="'.$type.'_machines" '.
 6944:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 6945:                       '</td></tr>'."\n";
 6946:     }
 6947:     $$rowtotal += $rownum;
 6948:     return $datatable;
 6949: }
 6950: 
 6951: sub serverstatus_pages {
 6952:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 6953:             'checksums','clusterstatus','certstatus','metadata_keywords',
 6954:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
 6955:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
 6956: }
 6957: 
 6958: sub defaults_javascript {
 6959:     my ($settings) = @_;
 6960:     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.');
 6961:     my $intauthcost = &mt('Warning: bcrypt encryption cost for internal authentication must be an integer.');
 6962:     &js_escape(\$intauthcheck);
 6963:     &js_escape(\$intauthcost);
 6964:     my $intauthjs = <<"ENDSCRIPT";
 6965: 
 6966: function warnIntAuth(field) {
 6967:     if (field.name == 'intauth_check') {
 6968:         if (field.value == '2') {
 6969:             alert('$intauthcheck');
 6970:         }
 6971:     }
 6972:     if (field.name == 'intauth_cost') {
 6973:         field.value.replace(/\s/g,'');
 6974:         if (field.value != '') {
 6975:             var regexdigit=/^\\d+\$/;
 6976:             if (!regexdigit.test(field.value)) {
 6977:                 alert('$intauthcost');
 6978:             }
 6979:         }
 6980:     }
 6981:     return;
 6982: }
 6983: 
 6984: ENDSCRIPT
 6985: 
 6986:     if (ref($settings) ne 'HASH') {
 6987:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 6988:     }
 6989:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 6990:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 6991:         if ($maxnum eq '') {
 6992:             $maxnum = 0;
 6993:         }
 6994:         $maxnum ++;
 6995:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 6996:         return <<"ENDSCRIPT";
 6997: <script type="text/javascript">
 6998: // <![CDATA[
 6999: function reorderTypes(form,caller) {
 7000:     var changedVal;
 7001: $jstext 
 7002:     var newpos = 'addinststatus_pos';
 7003:     var current = new Array;
 7004:     var maxh = $maxnum;
 7005:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 7006:     var oldVal;
 7007:     if (caller == newpos) {
 7008:         changedVal = newitemVal;
 7009:     } else {
 7010:         var curritem = 'inststatus_pos_'+caller;
 7011:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 7012:         current[newitemVal] = newpos;
 7013:     }
 7014:     for (var i=0; i<inststatuses.length; i++) {
 7015:         if (inststatuses[i] != caller) {
 7016:             var elementName = 'inststatus_pos_'+inststatuses[i];
 7017:             if (form.elements[elementName]) {
 7018:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 7019:                 current[currVal] = elementName;
 7020:             }
 7021:         }
 7022:     }
 7023:     for (var j=0; j<maxh; j++) {
 7024:         if (current[j] == undefined) {
 7025:             oldVal = j;
 7026:         }
 7027:     }
 7028:     if (oldVal < changedVal) {
 7029:         for (var k=oldVal+1; k<=changedVal ; k++) {
 7030:            var elementName = current[k];
 7031:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 7032:         }
 7033:     } else {
 7034:         for (var k=changedVal; k<oldVal; k++) {
 7035:             var elementName = current[k];
 7036:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 7037:         }
 7038:     }
 7039:     return;
 7040: }
 7041: 
 7042: $intauthjs
 7043: 
 7044: // ]]>
 7045: </script>
 7046: 
 7047: ENDSCRIPT
 7048:     } else {
 7049:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 7050:     }
 7051: }
 7052: 
 7053: sub coursecategories_javascript {
 7054:     my ($settings) = @_;
 7055:     my ($output,$jstext,$cathash);
 7056:     if (ref($settings) eq 'HASH') {
 7057:         $cathash = $settings->{'cats'};
 7058:     }
 7059:     if (ref($cathash) eq 'HASH') {
 7060:         my (@cats,@jsarray,%idx);
 7061:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 7062:         if (@jsarray > 0) {
 7063:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 7064:             for (my $i=0; $i<@jsarray; $i++) {
 7065:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 7066:                     my $catstr = join('","',@{$jsarray[$i]});
 7067:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 7068:                 }
 7069:             }
 7070:         }
 7071:     } else {
 7072:         $jstext  = '    var categories = Array(1);'."\n".
 7073:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 7074:     }
 7075:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 7076:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 7077:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
 7078:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 7079:     &js_escape(\$instcode_reserved);
 7080:     &js_escape(\$communities_reserved);
 7081:     &js_escape(\$placement_reserved);
 7082:     &js_escape(\$choose_again);
 7083:     $output = <<"ENDSCRIPT";
 7084: <script type="text/javascript">
 7085: // <![CDATA[
 7086: function reorderCats(form,parent,item,idx) {
 7087:     var changedVal;
 7088: $jstext
 7089:     var newpos = 'addcategory_pos';
 7090:     if (parent == '') {
 7091:         var has_instcode = 0;
 7092:         var maxtop = categories[idx].length;
 7093:         for (var j=0; j<maxtop; j++) {
 7094:             if (categories[idx][j] == 'instcode::0') {
 7095:                 has_instcode == 1;
 7096:             }
 7097:         }
 7098:         if (has_instcode == 0) {
 7099:             categories[idx][maxtop] = 'instcode_pos';
 7100:         }
 7101:     } else {
 7102:         newpos += '_'+parent;
 7103:     }
 7104:     var maxh = 1 + categories[idx].length;
 7105:     var current = new Array;
 7106:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 7107:     if (item == newpos) {
 7108:         changedVal = newitemVal;
 7109:     } else {
 7110:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 7111:         current[newitemVal] = newpos;
 7112:     }
 7113:     for (var i=0; i<categories[idx].length; i++) {
 7114:         var elementName = categories[idx][i];
 7115:         if (elementName != item) {
 7116:             if (form.elements[elementName]) {
 7117:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 7118:                 current[currVal] = elementName;
 7119:             }
 7120:         }
 7121:     }
 7122:     var oldVal;
 7123:     for (var j=0; j<maxh; j++) {
 7124:         if (current[j] == undefined) {
 7125:             oldVal = j;
 7126:         }
 7127:     }
 7128:     if (oldVal < changedVal) {
 7129:         for (var k=oldVal+1; k<=changedVal ; k++) {
 7130:            var elementName = current[k];
 7131:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 7132:         }
 7133:     } else {
 7134:         for (var k=changedVal; k<oldVal; k++) {
 7135:             var elementName = current[k];
 7136:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 7137:         }
 7138:     }
 7139:     return;
 7140: }
 7141: 
 7142: function categoryCheck(form) {
 7143:     if (form.elements['addcategory_name'].value == 'instcode') {
 7144:         alert('$instcode_reserved\\n$choose_again');
 7145:         return false;
 7146:     }
 7147:     if (form.elements['addcategory_name'].value == 'communities') {
 7148:         alert('$communities_reserved\\n$choose_again');
 7149:         return false;
 7150:     }
 7151:     if (form.elements['addcategory_name'].value == 'placement') {
 7152:         alert('$placement_reserved\\n$choose_again');
 7153:         return false;
 7154:     }
 7155:     return true;
 7156: }
 7157: 
 7158: // ]]>
 7159: </script>
 7160: 
 7161: ENDSCRIPT
 7162:     return $output;
 7163: }
 7164: 
 7165: sub initialize_categories {
 7166:     my ($itemcount) = @_;
 7167:     my ($datatable,$css_class,$chgstr);
 7168:     my %default_names = (
 7169:                       instcode    => 'Official courses (with institutional codes)',
 7170:                       communities => 'Communities',
 7171:                       placement   => 'Placement Tests',
 7172:                         );
 7173:     my $select0 = ' selected="selected"';
 7174:     my $select1 = '';
 7175:     foreach my $default ('instcode','communities','placement') {
 7176:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7177:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 7178:         if (($default eq 'communities') || ($default eq 'placement')) {
 7179:             $select1 = $select0;
 7180:             $select0 = '';
 7181:         }
 7182:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7183:                      .'<select name="'.$default.'_pos">'
 7184:                      .'<option value="0"'.$select0.'>1</option>'
 7185:                      .'<option value="1"'.$select1.'>2</option>'
 7186:                      .'<option value="2">3</option></select>&nbsp;'
 7187:                      .$default_names{$default}
 7188:                      .'</span></td><td><span class="LC_nobreak">'
 7189:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 7190:                      .&mt('Display').'</label>&nbsp;<label>'
 7191:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 7192:                  .'</label></span></td></tr>';
 7193:         $itemcount ++;
 7194:     }
 7195:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7196:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 7197:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7198:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 7199:                   .'<option value="0">1</option>'
 7200:                   .'<option value="1">2</option>'
 7201:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 7202:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 7203:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 7204:     return $datatable;
 7205: }
 7206: 
 7207: sub build_category_rows {
 7208:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 7209:     my ($text,$name,$item,$chgstr);
 7210:     if (ref($cats) eq 'ARRAY') {
 7211:         my $maxdepth = scalar(@{$cats});
 7212:         if (ref($cats->[$depth]) eq 'HASH') {
 7213:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 7214:                 my $numchildren = @{$cats->[$depth]{$parent}};
 7215:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7216:                 $text .= '<td><table class="LC_data_table">';
 7217:                 my ($idxnum,$parent_name,$parent_item);
 7218:                 my $higher = $depth - 1;
 7219:                 if ($higher == 0) {
 7220:                     $parent_name = &escape($parent).'::'.$higher;
 7221:                 } else {
 7222:                     if (ref($path) eq 'ARRAY') {
 7223:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7224:                     }
 7225:                 }
 7226:                 $parent_item = 'addcategory_pos_'.$parent_name;
 7227:                 for (my $j=0; $j<=$numchildren; $j++) {
 7228:                     if ($j < $numchildren) {
 7229:                         $name = $cats->[$depth]{$parent}[$j];
 7230:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 7231:                         $idxnum = $idx->{$item};
 7232:                     } else {
 7233:                         $name = $parent_name;
 7234:                         $item = $parent_item;
 7235:                     }
 7236:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 7237:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 7238:                     for (my $i=0; $i<=$numchildren; $i++) {
 7239:                         my $vpos = $i+1;
 7240:                         my $selstr;
 7241:                         if ($j == $i) {
 7242:                             $selstr = ' selected="selected" ';
 7243:                         }
 7244:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 7245:                     }
 7246:                     $text .= '</select>&nbsp;';
 7247:                     if ($j < $numchildren) {
 7248:                         my $deeper = $depth+1;
 7249:                         $text .= $name.'&nbsp;'
 7250:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 7251:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 7252:                         if(ref($path) eq 'ARRAY') {
 7253:                             push(@{$path},$name);
 7254:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 7255:                             pop(@{$path});
 7256:                         }
 7257:                     } else {
 7258:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 7259:                         if ($j == $numchildren) {
 7260:                             $text .= $name;
 7261:                         } else {
 7262:                             $text .= $item;
 7263:                         }
 7264:                         $text .= '" value="" />';
 7265:                     }
 7266:                     $text .= '</td></tr>';
 7267:                 }
 7268:                 $text .= '</table></td>';
 7269:             } else {
 7270:                 my $higher = $depth-1;
 7271:                 if ($higher == 0) {
 7272:                     $name = &escape($parent).'::'.$higher;
 7273:                 } else {
 7274:                     if (ref($path) eq 'ARRAY') {
 7275:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7276:                     }
 7277:                 }
 7278:                 my $colspan;
 7279:                 if ($parent ne 'instcode') {
 7280:                     $colspan = $maxdepth - $depth - 1;
 7281:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 7282:                 }
 7283:             }
 7284:         }
 7285:     }
 7286:     return $text;
 7287: }
 7288: 
 7289: sub modifiable_userdata_row {
 7290:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 7291:     my ($role,$rolename,$statustype);
 7292:     $role = $item;
 7293:     if ($context eq 'cancreate') {
 7294:         if ($item =~ /^emailusername_(.+)$/) {
 7295:             $statustype = $1;
 7296:             $role = 'emailusername';
 7297:             if (ref($usertypes) eq 'HASH') {
 7298:                 if ($usertypes->{$statustype}) {
 7299:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 7300:                 } else {
 7301:                     $rolename = &mt('Data provided by user');
 7302:                 }
 7303:             }
 7304:         }
 7305:     } elsif ($context eq 'selfcreate') {
 7306:         if (ref($usertypes) eq 'HASH') {
 7307:             $rolename = $usertypes->{$role};
 7308:         } else {
 7309:             $rolename = $role;
 7310:         }
 7311:     } else {
 7312:         if ($role eq 'cr') {
 7313:             $rolename = &mt('Custom role');
 7314:         } else {
 7315:             $rolename = &Apache::lonnet::plaintext($role);
 7316:         }
 7317:     }
 7318:     my (@fields,%fieldtitles);
 7319:     if (ref($fieldsref) eq 'ARRAY') {
 7320:         @fields = @{$fieldsref};
 7321:     } else {
 7322:         @fields = ('lastname','firstname','middlename','generation',
 7323:                    'permanentemail','id');
 7324:     }
 7325:     if ((ref($titlesref) eq 'HASH')) {
 7326:         %fieldtitles = %{$titlesref};
 7327:     } else {
 7328:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7329:     }
 7330:     my $output;
 7331:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 7332:     $output = '<tr '.$css_class.'>'.
 7333:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 7334:               '<td class="LC_left_item" colspan="2"><table>';
 7335:     my $rem;
 7336:     my %checks;
 7337:     if (ref($settings) eq 'HASH') {
 7338:         if (ref($settings->{$context}) eq 'HASH') {
 7339:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 7340:                 my $hashref = $settings->{$context}->{$role};
 7341:                 if ($role eq 'emailusername') {
 7342:                     if ($statustype) {
 7343:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 7344:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 7345:                             if (ref($hashref) eq 'HASH') { 
 7346:                                 foreach my $field (@fields) {
 7347:                                     if ($hashref->{$field}) {
 7348:                                         $checks{$field} = $hashref->{$field};
 7349:                                     }
 7350:                                 }
 7351:                             }
 7352:                         }
 7353:                     }
 7354:                 } else {
 7355:                     if (ref($hashref) eq 'HASH') {
 7356:                         foreach my $field (@fields) {
 7357:                             if ($hashref->{$field}) {
 7358:                                 $checks{$field} = ' checked="checked" ';
 7359:                             }
 7360:                         }
 7361:                     }
 7362:                 }
 7363:             }
 7364:         }
 7365:     }
 7366:      
 7367:     for (my $i=0; $i<@fields; $i++) {
 7368:         my $rem = $i%($numinrow);
 7369:         if ($rem == 0) {
 7370:             if ($i > 0) {
 7371:                 $output .= '</tr>';
 7372:             }
 7373:             $output .= '<tr>';
 7374:         }
 7375:         my $check = ' ';
 7376:         unless ($role eq 'emailusername') {
 7377:             if (exists($checks{$fields[$i]})) {
 7378:                 $check = $checks{$fields[$i]}
 7379:             } else {
 7380:                 if ($role eq 'st') {
 7381:                     if (ref($settings) ne 'HASH') {
 7382:                         $check = ' checked="checked" '; 
 7383:                     }
 7384:                 }
 7385:             }
 7386:         }
 7387:         $output .= '<td class="LC_left_item">'.
 7388:                    '<span class="LC_nobreak">';
 7389:         if ($role eq 'emailusername') {
 7390:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 7391:                 $checks{$fields[$i]} = 'omit';
 7392:             }
 7393:             foreach my $option ('required','optional','omit') {
 7394:                 my $checked='';
 7395:                 if ($checks{$fields[$i]} eq $option) {
 7396:                     $checked='checked="checked" ';
 7397:                 }
 7398:                 $output .= '<label>'.
 7399:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 7400:                            &mt($option).'</label>'.('&nbsp;' x2);
 7401:             }
 7402:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 7403:         } else {
 7404:             $output .= '<label>'.
 7405:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 7406:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 7407:                        '</label>';
 7408:         }
 7409:         $output .= '</span></td>';
 7410:         $rem = @fields%($numinrow);
 7411:     }
 7412:     my $colsleft = $numinrow - $rem;
 7413:     if ($colsleft > 1 ) {
 7414:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7415:                    '&nbsp;</td>';
 7416:     } elsif ($colsleft == 1) {
 7417:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 7418:     }
 7419:     $output .= '</tr></table></td></tr>';
 7420:     return $output;
 7421: }
 7422: 
 7423: sub insttypes_row {
 7424:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 7425:     my %lt = &Apache::lonlocal::texthash (
 7426:                       cansearch => 'Users allowed to search',
 7427:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 7428:                       lockablenames => 'User preference to lock name',
 7429:              );
 7430:     my $showdom;
 7431:     if ($context eq 'cansearch') {
 7432:         $showdom = ' ('.$dom.')';
 7433:     }
 7434:     my $class = 'LC_left_item';
 7435:     if ($context eq 'statustocreate') {
 7436:         $class = 'LC_right_item';
 7437:     }
 7438:     my $css_class = ' class="LC_odd_row"';
 7439:     if ($rownum ne '') { 
 7440:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 7441:     }
 7442:     my $output = '<tr'.$css_class.'>'.
 7443:                  '<td>'.$lt{$context}.$showdom.
 7444:                  '</td><td class="'.$class.'" colspan="2"><table>';
 7445:     my $rem;
 7446:     if (ref($types) eq 'ARRAY') {
 7447:         for (my $i=0; $i<@{$types}; $i++) {
 7448:             if (defined($usertypes->{$types->[$i]})) {
 7449:                 my $rem = $i%($numinrow);
 7450:                 if ($rem == 0) {
 7451:                     if ($i > 0) {
 7452:                         $output .= '</tr>';
 7453:                     }
 7454:                     $output .= '<tr>';
 7455:                 }
 7456:                 my $check = ' ';
 7457:                 if (ref($settings) eq 'HASH') {
 7458:                     if (ref($settings->{$context}) eq 'ARRAY') {
 7459:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 7460:                             $check = ' checked="checked" ';
 7461:                         }
 7462:                     } elsif ($context eq 'statustocreate') {
 7463:                         $check = ' checked="checked" ';
 7464:                     }
 7465:                 }
 7466:                 $output .= '<td class="LC_left_item">'.
 7467:                            '<span class="LC_nobreak"><label>'.
 7468:                            '<input type="checkbox" name="'.$context.'" '.
 7469:                            'value="'.$types->[$i].'"'.$check.'/>'.
 7470:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 7471:             }
 7472:         }
 7473:         $rem = @{$types}%($numinrow);
 7474:     }
 7475:     my $colsleft = $numinrow - $rem;
 7476:     if (($rem == 0) && (@{$types} > 0)) {
 7477:         $output .= '<tr>';
 7478:     }
 7479:     if ($colsleft > 1) {
 7480:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 7481:     } else {
 7482:         $output .= '<td class="LC_left_item">';
 7483:     }
 7484:     my $defcheck = ' ';
 7485:     if (ref($settings) eq 'HASH') {  
 7486:         if (ref($settings->{$context}) eq 'ARRAY') {
 7487:             if (grep(/^default$/,@{$settings->{$context}})) {
 7488:                 $defcheck = ' checked="checked" ';
 7489:             }
 7490:         } elsif ($context eq 'statustocreate') {
 7491:             $defcheck = ' checked="checked" ';
 7492:         }
 7493:     }
 7494:     $output .= '<span class="LC_nobreak"><label>'.
 7495:                '<input type="checkbox" name="'.$context.'" '.
 7496:                'value="default"'.$defcheck.'/>'.
 7497:                $othertitle.'</label></span></td>'.
 7498:                '</tr></table></td></tr>';
 7499:     return $output;
 7500: }
 7501: 
 7502: sub sorted_searchtitles {
 7503:     my %searchtitles = &Apache::lonlocal::texthash(
 7504:                          'uname' => 'username',
 7505:                          'lastname' => 'last name',
 7506:                          'lastfirst' => 'last name, first name',
 7507:                      );
 7508:     my @titleorder = ('uname','lastname','lastfirst');
 7509:     return (\%searchtitles,\@titleorder);
 7510: }
 7511: 
 7512: sub sorted_searchtypes {
 7513:     my %srchtypes_desc = (
 7514:                            exact    => 'is exact match',
 7515:                            contains => 'contains ..',
 7516:                            begins   => 'begins with ..',
 7517:                          );
 7518:     my @srchtypeorder = ('exact','begins','contains');
 7519:     return (\%srchtypes_desc,\@srchtypeorder);
 7520: }
 7521: 
 7522: sub usertype_update_row {
 7523:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 7524:     my $datatable;
 7525:     my $numinrow = 4;
 7526:     foreach my $type (@{$types}) {
 7527:         if (defined($usertypes->{$type})) {
 7528:             $$rownums ++;
 7529:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 7530:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 7531:                           '</td><td class="LC_left_item"><table>';
 7532:             for (my $i=0; $i<@{$fields}; $i++) {
 7533:                 my $rem = $i%($numinrow);
 7534:                 if ($rem == 0) {
 7535:                     if ($i > 0) {
 7536:                         $datatable .= '</tr>';
 7537:                     }
 7538:                     $datatable .= '<tr>';
 7539:                 }
 7540:                 my $check = ' ';
 7541:                 if (ref($settings) eq 'HASH') {
 7542:                     if (ref($settings->{'fields'}) eq 'HASH') {
 7543:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 7544:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 7545:                                 $check = ' checked="checked" ';
 7546:                             }
 7547:                         }
 7548:                     }
 7549:                 }
 7550: 
 7551:                 if ($i == @{$fields}-1) {
 7552:                     my $colsleft = $numinrow - $rem;
 7553:                     if ($colsleft > 1) {
 7554:                         $datatable .= '<td colspan="'.$colsleft.'">';
 7555:                     } else {
 7556:                         $datatable .= '<td>';
 7557:                     }
 7558:                 } else {
 7559:                     $datatable .= '<td>';
 7560:                 }
 7561:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7562:                               '<input type="checkbox" name="updateable_'.$type.
 7563:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 7564:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 7565:             }
 7566:             $datatable .= '</tr></table></td></tr>';
 7567:         }
 7568:     }
 7569:     return $datatable;
 7570: }
 7571: 
 7572: sub modify_login {
 7573:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 7574:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 7575:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 7576:     %title = ( coursecatalog => 'Display course catalog',
 7577:                adminmail => 'Display administrator E-mail address',
 7578:                helpdesk  => 'Display "Contact Helpdesk" link',
 7579:                newuser => 'Link for visitors to create a user account',
 7580:                loginheader => 'Log-in box header');
 7581:     @offon = ('off','on');
 7582:     if (ref($domconfig{login}) eq 'HASH') {
 7583:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 7584:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 7585:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 7586:             }
 7587:         }
 7588:     }
 7589:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 7590:                                            \%domconfig,\%loginhash);
 7591:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7592:     foreach my $item (@toggles) {
 7593:         $loginhash{login}{$item} = $env{'form.'.$item};
 7594:     }
 7595:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 7596:     if (ref($colchanges{'login'}) eq 'HASH') {  
 7597:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 7598:                                          \%loginhash);
 7599:     }
 7600: 
 7601:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7602:     my %domservers = &Apache::lonnet::get_servers($dom);
 7603:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 7604:     if (keys(%servers) > 1) {
 7605:         foreach my $lonhost (keys(%servers)) {
 7606:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 7607:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 7608:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 7609:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 7610:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 7611:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7612:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7613:                         $changes{'loginvia'}{$lonhost} = 1;
 7614:                     } else {
 7615:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 7616:                         $changes{'loginvia'}{$lonhost} = 1;
 7617:                     }
 7618:                 } else {
 7619:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7620:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7621:                         $changes{'loginvia'}{$lonhost} = 1;
 7622:                     }
 7623:                 }
 7624:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 7625:                     foreach my $item (@loginvia_attribs) {
 7626:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 7627:                     }
 7628:                 } else {
 7629:                     foreach my $item (@loginvia_attribs) {
 7630:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7631:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7632:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 7633:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7634:                                 $new = '/';
 7635:                             }
 7636:                         }
 7637:                         if (($item eq 'custompath') && 
 7638:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 7639:                             $new = '';
 7640:                         }
 7641:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 7642:                             $changes{'loginvia'}{$lonhost} = 1;
 7643:                         }
 7644:                         if ($item eq 'exempt') {
 7645:                             $new = &check_exempt_addresses($new);
 7646:                         }
 7647:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 7648:                     }
 7649:                 }
 7650:             } else {
 7651:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7652:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7653:                     $changes{'loginvia'}{$lonhost} = 1;
 7654:                     foreach my $item (@loginvia_attribs) {
 7655:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7656:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7657:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7658:                                 $new = '/';
 7659:                             }
 7660:                         }
 7661:                         if (($item eq 'custompath') && 
 7662:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 7663:                             $new = '';
 7664:                         }
 7665:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 7666:                     }
 7667:                 }
 7668:             }
 7669:         }
 7670:     }
 7671: 
 7672:     my $servadm = $r->dir_config('lonAdmEMail');
 7673:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 7674:     if (ref($domconfig{'login'}) eq 'HASH') {
 7675:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 7676:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 7677:                 if ($lang eq 'nolang') {
 7678:                     push(@currlangs,$lang);
 7679:                 } elsif (defined($langchoices{$lang})) {
 7680:                     push(@currlangs,$lang);
 7681:                 } else {
 7682:                     next;
 7683:                 }
 7684:             }
 7685:         }
 7686:     }
 7687:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 7688:     if (@currlangs > 0) {
 7689:         foreach my $lang (@currlangs) {
 7690:             if (grep(/^\Q$lang\E$/,@delurls)) {
 7691:                 $changes{'helpurl'}{$lang} = 1;
 7692:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 7693:                 $changes{'helpurl'}{$lang} = 1;
 7694:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 7695:                 push(@newlangs,$lang);
 7696:             } else {
 7697:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 7698:             }
 7699:         }
 7700:     }
 7701:     unless (grep(/^nolang$/,@currlangs)) {
 7702:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 7703:             $changes{'helpurl'}{'nolang'} = 1;
 7704:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 7705:             push(@newlangs,'nolang');
 7706:         }
 7707:     }
 7708:     if ($env{'form.loginhelpurl_add_lang'}) {
 7709:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 7710:             ($env{'form.loginhelpurl_add_file.filename'})) {
 7711:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 7712:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 7713:         }
 7714:     }
 7715:     if ((@newlangs > 0) || ($addedfile)) {
 7716:         my $error;
 7717:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7718:         if ($configuserok eq 'ok') {
 7719:             if ($switchserver) {
 7720:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 7721:             } elsif ($author_ok eq 'ok') {
 7722:                 my @allnew = @newlangs;
 7723:                 if ($addedfile ne '') {
 7724:                     push(@allnew,$addedfile);
 7725:                 }
 7726:                 foreach my $lang (@allnew) {
 7727:                     my $formelem = 'loginhelpurl_'.$lang;
 7728:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 7729:                         $formelem = 'loginhelpurl_add_file';
 7730:                     }
 7731:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 7732:                                                                "help/$lang",'','',$newfile{$lang});
 7733:                     if ($result eq 'ok') {
 7734:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 7735:                         $changes{'helpurl'}{$lang} = 1;
 7736:                     } else {
 7737:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 7738:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 7739:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 7740:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 7741:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 7742:                         }
 7743:                     }
 7744:                 }
 7745:             } else {
 7746:                 $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);
 7747:             }
 7748:         } else {
 7749:             $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);
 7750:         }
 7751:         if ($error) {
 7752:             &Apache::lonnet::logthis($error);
 7753:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7754:         }
 7755:     }
 7756: 
 7757:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 7758:     if (ref($domconfig{'login'}) eq 'HASH') {
 7759:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 7760:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 7761:                 if ($domservers{$lonhost}) {
 7762:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 7763:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 7764:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 7765:                     }
 7766:                 }
 7767:             }
 7768:         }
 7769:     }
 7770:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 7771:     foreach my $lonhost (sort(keys(%domservers))) {
 7772:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 7773:             $changes{'headtag'}{$lonhost} = 1;
 7774:         } else {
 7775:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 7776:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 7777:             }
 7778:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 7779:                 push(@newhosts,$lonhost);
 7780:             } elsif ($currheadtagurls{$lonhost}) {
 7781:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 7782:                 if ($currexempt{$lonhost}) {
 7783:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 7784:                         $changes{'headtag'}{$lonhost} = 1;
 7785:                     }
 7786:                 } elsif ($possexempt{$lonhost}) {
 7787:                     $changes{'headtag'}{$lonhost} = 1;
 7788:                 }
 7789:                 if ($possexempt{$lonhost}) {
 7790:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 7791:                 }
 7792:             }
 7793:         }
 7794:     }
 7795:     if (@newhosts) {
 7796:         my $error;
 7797:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7798:         if ($configuserok eq 'ok') {
 7799:             if ($switchserver) {
 7800:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 7801:             } elsif ($author_ok eq 'ok') {
 7802:                 foreach my $lonhost (@newhosts) {
 7803:                     my $formelem = 'loginheadtag_'.$lonhost;
 7804:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 7805:                                                                           "login/headtag/$lonhost",'','',
 7806:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 7807:                     if ($result eq 'ok') {
 7808:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 7809:                         $changes{'headtag'}{$lonhost} = 1;
 7810:                         if ($possexempt{$lonhost}) {
 7811:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 7812:                         }
 7813:                     } else {
 7814:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 7815:                                            $newheadtagurls{$lonhost},$result);
 7816:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 7817:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 7818:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 7819:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 7820:                         }
 7821:                     }
 7822:                 }
 7823:             } else {
 7824:                 $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);
 7825:             }
 7826:         } else {
 7827:             $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);
 7828:         }
 7829:         if ($error) {
 7830:             &Apache::lonnet::logthis($error);
 7831:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7832:         }
 7833:     }
 7834:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 7835: 
 7836:     my $defaulthelpfile = '/adm/loginproblems.html';
 7837:     my $defaulttext = &mt('Default in use');
 7838: 
 7839:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 7840:                                              $dom);
 7841:     if ($putresult eq 'ok') {
 7842:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7843:         my %defaultchecked = (
 7844:                     'coursecatalog' => 'on',
 7845:                     'helpdesk'      => 'on',
 7846:                     'adminmail'     => 'off',
 7847:                     'newuser'       => 'off',
 7848:         );
 7849:         if (ref($domconfig{'login'}) eq 'HASH') {
 7850:             foreach my $item (@toggles) {
 7851:                 if ($defaultchecked{$item} eq 'on') { 
 7852:                     if (($domconfig{'login'}{$item} eq '0') &&
 7853:                         ($env{'form.'.$item} eq '1')) {
 7854:                         $changes{$item} = 1;
 7855:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 7856:                               $domconfig{'login'}{$item} eq '1') &&
 7857:                              ($env{'form.'.$item} eq '0')) {
 7858:                         $changes{$item} = 1;
 7859:                     }
 7860:                 } elsif ($defaultchecked{$item} eq 'off') {
 7861:                     if (($domconfig{'login'}{$item} eq '1') &&
 7862:                         ($env{'form.'.$item} eq '0')) {
 7863:                         $changes{$item} = 1;
 7864:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 7865:                               $domconfig{'login'}{$item} eq '0') &&
 7866:                              ($env{'form.'.$item} eq '1')) {
 7867:                         $changes{$item} = 1;
 7868:                     }
 7869:                 }
 7870:             }
 7871:         }
 7872:         if (keys(%changes) > 0 || $colchgtext) {
 7873:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7874:             if (ref($lastactref) eq 'HASH') {
 7875:                 $lastactref->{'domainconfig'} = 1;
 7876:             }
 7877:             $resulttext = &mt('Changes made:').'<ul>';
 7878:             foreach my $item (sort(keys(%changes))) {
 7879:                 if ($item eq 'loginvia') {
 7880:                     if (ref($changes{$item}) eq 'HASH') {
 7881:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 7882:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 7883:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 7884:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 7885:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 7886:                                     $protocol = 'http' if ($protocol ne 'https');
 7887:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 7888: 
 7889:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 7890:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 7891:                                     } else {
 7892:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 7893:                                     }
 7894:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 7895:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 7896:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 7897:                                     }
 7898:                                     $resulttext .= '</li>';
 7899:                                 } else {
 7900:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 7901:                                 }
 7902:                             } else {
 7903:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 7904:                             }
 7905:                         }
 7906:                         $resulttext .= '</ul></li>';
 7907:                     }
 7908:                 } elsif ($item eq 'helpurl') {
 7909:                     if (ref($changes{$item}) eq 'HASH') {
 7910:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 7911:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 7912:                                 my ($chg,$link);
 7913:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 7914:                                 if ($lang eq 'nolang') {
 7915:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 7916:                                 } else {
 7917:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 7918:                                 }
 7919:                                 $resulttext .= '<li>'.$chg.'</li>';
 7920:                             } else {
 7921:                                 my $chg;
 7922:                                 if ($lang eq 'nolang') {
 7923:                                     $chg = &mt('custom log-in help file for no preferred language');
 7924:                                 } else {
 7925:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 7926:                                 }
 7927:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 7928:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 7929:                                                       '?inhibitmenu=yes',$chg,600,500).
 7930:                                                '</li>';
 7931:                             }
 7932:                         }
 7933:                     }
 7934:                 } elsif ($item eq 'headtag') {
 7935:                     if (ref($changes{$item}) eq 'HASH') {
 7936:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 7937:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 7938:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 7939:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 7940:                                 $resulttext .= '<li><a href="'.
 7941:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 7942:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 7943:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 7944:                                 if ($possexempt{$lonhost}) {
 7945:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 7946:                                 } else {
 7947:                                     $resulttext .= &mt('included for any client IP');
 7948:                                 }
 7949:                                 $resulttext .= '</li>';
 7950:                             }
 7951:                         }
 7952:                     }
 7953:                 } elsif ($item eq 'captcha') {
 7954:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7955:                         my $chgtxt;
 7956:                         if ($loginhash{'login'}{$item} eq 'notused') {
 7957:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 7958:                         } else {
 7959:                             my %captchas = &captcha_phrases();
 7960:                             if ($captchas{$loginhash{'login'}{$item}}) {
 7961:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 7962:                             } else {
 7963:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 7964:                             }
 7965:                         }
 7966:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 7967:                     }
 7968:                 } elsif ($item eq 'recaptchakeys') {
 7969:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7970:                         my ($privkey,$pubkey);
 7971:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 7972:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 7973:                             $privkey = $loginhash{'login'}{$item}{'private'};
 7974:                         }
 7975:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 7976:                         if (!$pubkey) {
 7977:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 7978:                         } else {
 7979:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 7980:                         }
 7981:                         if (!$privkey) {
 7982:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 7983:                         } else {
 7984:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 7985:                         }
 7986:                         $chgtxt .= '</ul>';
 7987:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 7988:                     }
 7989:                 } elsif ($item eq 'recaptchaversion') {
 7990:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7991:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 7992:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 7993:                                            '</li>';
 7994:                         }
 7995:                     }
 7996:                 } else {
 7997:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 7998:                 }
 7999:             }
 8000:             $resulttext .= $colchgtext.'</ul>';
 8001:         } else {
 8002:             $resulttext = &mt('No changes made to log-in page settings');
 8003:         }
 8004:     } else {
 8005:         $resulttext = '<span class="LC_error">'.
 8006: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8007:     }
 8008:     if ($errors) {
 8009:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 8010:                        $errors.'</ul>';
 8011:     }
 8012:     return $resulttext;
 8013: }
 8014: 
 8015: sub check_exempt_addresses {
 8016:     my ($iplist) = @_;
 8017:     $iplist =~ s/^\s+//;
 8018:     $iplist =~ s/\s+$//;
 8019:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 8020:     my (@okips,$new);
 8021:     foreach my $ip (@poss_ips) {
 8022:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 8023:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 8024:                 push(@okips,$ip);
 8025:             }
 8026:         }
 8027:     }
 8028:     if (@okips > 0) {
 8029:         $new = join(',',@okips);
 8030:     } else {
 8031:         $new = '';
 8032:     }
 8033:     return $new;
 8034: }
 8035: 
 8036: sub color_font_choices {
 8037:     my %choices =
 8038:         &Apache::lonlocal::texthash (
 8039:             img => "Header",
 8040:             bgs => "Background colors",
 8041:             links => "Link colors",
 8042:             images => "Images",
 8043:             font => "Font color",
 8044:             fontmenu => "Font menu",
 8045:             pgbg => "Page",
 8046:             tabbg => "Header",
 8047:             sidebg => "Border",
 8048:             link => "Link",
 8049:             alink => "Active link",
 8050:             vlink => "Visited link",
 8051:         );
 8052:     return %choices;
 8053: }
 8054: 
 8055: sub modify_rolecolors {
 8056:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 8057:     my ($resulttext,%rolehash);
 8058:     $rolehash{'rolecolors'} = {};
 8059:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 8060:         if ($domconfig{'rolecolors'} eq '') {
 8061:             $domconfig{'rolecolors'} = {};
 8062:         }
 8063:     }
 8064:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 8065:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 8066:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 8067:                                              $dom);
 8068:     if ($putresult eq 'ok') {
 8069:         if (keys(%changes) > 0) {
 8070:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8071:             if (ref($lastactref) eq 'HASH') {
 8072:                 $lastactref->{'domainconfig'} = 1;
 8073:             }
 8074:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 8075:                                              $rolehash{'rolecolors'});
 8076:         } else {
 8077:             $resulttext = &mt('No changes made to default color schemes');
 8078:         }
 8079:     } else {
 8080:         $resulttext = '<span class="LC_error">'.
 8081: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8082:     }
 8083:     if ($errors) {
 8084:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 8085:                        $errors.'</ul>';
 8086:     }
 8087:     return $resulttext;
 8088: }
 8089: 
 8090: sub modify_colors {
 8091:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 8092:     my (%changes,%choices);
 8093:     my @bgs;
 8094:     my @links = ('link','alink','vlink');
 8095:     my @logintext;
 8096:     my @images;
 8097:     my $servadm = $r->dir_config('lonAdmEMail');
 8098:     my $errors;
 8099:     my %defaults;
 8100:     foreach my $role (@{$roles}) {
 8101:         if ($role eq 'login') {
 8102:             %choices = &login_choices();
 8103:             @logintext = ('textcol','bgcol');
 8104:         } else {
 8105:             %choices = &color_font_choices();
 8106:         }
 8107:         if ($role eq 'login') {
 8108:             @images = ('img','logo','domlogo','login');
 8109:             @bgs = ('pgbg','mainbg','sidebg');
 8110:         } else {
 8111:             @images = ('img');
 8112:             @bgs = ('pgbg','tabbg','sidebg');
 8113:         }
 8114:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 8115:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 8116:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 8117:         }
 8118:         if ($role eq 'login') {
 8119:             foreach my $item (@logintext) {
 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{'logintext'}{$item})) {
 8125:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8126:                 }
 8127:             }
 8128:         } else {
 8129:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 8130:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 8131:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 8132:             }
 8133:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 8134:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 8135:             }
 8136:         }
 8137:         foreach my $item (@bgs) {
 8138:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8139:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8140:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8141:             }
 8142:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 8143:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8144:             }
 8145:         }
 8146:         foreach my $item (@links) {
 8147:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8148:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8149:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8150:             }
 8151:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 8152:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8153:             }
 8154:         }
 8155:         my ($configuserok,$author_ok,$switchserver) = 
 8156:             &config_check($dom,$confname,$servadm);
 8157:         my ($width,$height) = &thumb_dimensions();
 8158:         if (ref($domconfig->{$role}) ne 'HASH') {
 8159:             $domconfig->{$role} = {};
 8160:         }
 8161:         foreach my $img (@images) {
 8162:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 8163:                 if (defined($env{'form.login_showlogo_'.$img})) {
 8164:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 8165:                 } else { 
 8166:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 8167:                 }
 8168:             } 
 8169: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 8170: 		 && !defined($domconfig->{$role}{$img})
 8171: 		 && !$env{'form.'.$role.'_del_'.$img}
 8172: 		 && $env{'form.'.$role.'_import_'.$img}) {
 8173: 		# import the old configured image from the .tab setting
 8174: 		# if they haven't provided a new one 
 8175: 		$domconfig->{$role}{$img} = 
 8176: 		    $env{'form.'.$role.'_import_'.$img};
 8177: 	    }
 8178:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 8179:                 my $error;
 8180:                 if ($configuserok eq 'ok') {
 8181:                     if ($switchserver) {
 8182:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 8183:                     } else {
 8184:                         if ($author_ok eq 'ok') {
 8185:                             my ($result,$logourl) = 
 8186:                                 &publishlogo($r,'upload',$role.'_'.$img,
 8187:                                            $dom,$confname,$img,$width,$height);
 8188:                             if ($result eq 'ok') {
 8189:                                 $confhash->{$role}{$img} = $logourl;
 8190:                                 $changes{$role}{'images'}{$img} = 1;
 8191:                             } else {
 8192:                                 $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);
 8193:                             }
 8194:                         } else {
 8195:                             $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);
 8196:                         }
 8197:                     }
 8198:                 } else {
 8199:                     $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);
 8200:                 }
 8201:                 if ($error) {
 8202:                     &Apache::lonnet::logthis($error);
 8203:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8204:                 }
 8205:             } elsif ($domconfig->{$role}{$img} ne '') {
 8206:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 8207:                     my $error;
 8208:                     if ($configuserok eq 'ok') {
 8209: # is confname an author?
 8210:                         if ($switchserver eq '') {
 8211:                             if ($author_ok eq 'ok') {
 8212:                                 my ($result,$logourl) = 
 8213:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 8214:                                             $dom,$confname,$img,$width,$height);
 8215:                                 if ($result eq 'ok') {
 8216:                                     $confhash->{$role}{$img} = $logourl;
 8217: 				    $changes{$role}{'images'}{$img} = 1;
 8218:                                 }
 8219:                             }
 8220:                         }
 8221:                     }
 8222:                 }
 8223:             }
 8224:         }
 8225:         if (ref($domconfig) eq 'HASH') {
 8226:             if (ref($domconfig->{$role}) eq 'HASH') {
 8227:                 foreach my $img (@images) {
 8228:                     if ($domconfig->{$role}{$img} ne '') {
 8229:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8230:                             $confhash->{$role}{$img} = '';
 8231:                             $changes{$role}{'images'}{$img} = 1;
 8232:                         } else {
 8233:                             if ($confhash->{$role}{$img} eq '') {
 8234:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 8235:                             }
 8236:                         }
 8237:                     } else {
 8238:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8239:                             $confhash->{$role}{$img} = '';
 8240:                             $changes{$role}{'images'}{$img} = 1;
 8241:                         } 
 8242:                     }
 8243:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 8244:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 8245:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 8246:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 8247:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 8248:                             }
 8249:                         } else {
 8250:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8251:                                 $changes{$role}{'showlogo'}{$img} = 1;
 8252:                             }
 8253:                         }
 8254:                     }
 8255:                 }
 8256:                 if ($domconfig->{$role}{'font'} ne '') {
 8257:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 8258:                         $changes{$role}{'font'} = 1;
 8259:                     }
 8260:                 } else {
 8261:                     if ($confhash->{$role}{'font'}) {
 8262:                         $changes{$role}{'font'} = 1;
 8263:                     }
 8264:                 }
 8265:                 if ($role ne 'login') {
 8266:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 8267:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 8268:                             $changes{$role}{'fontmenu'} = 1;
 8269:                         }
 8270:                     } else {
 8271:                         if ($confhash->{$role}{'fontmenu'}) {
 8272:                             $changes{$role}{'fontmenu'} = 1;
 8273:                         }
 8274:                     }
 8275:                 }
 8276:                 foreach my $item (@bgs) {
 8277:                     if ($domconfig->{$role}{$item} ne '') {
 8278:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8279:                             $changes{$role}{'bgs'}{$item} = 1;
 8280:                         } 
 8281:                     } else {
 8282:                         if ($confhash->{$role}{$item}) {
 8283:                             $changes{$role}{'bgs'}{$item} = 1;
 8284:                         }
 8285:                     }
 8286:                 }
 8287:                 foreach my $item (@links) {
 8288:                     if ($domconfig->{$role}{$item} ne '') {
 8289:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8290:                             $changes{$role}{'links'}{$item} = 1;
 8291:                         }
 8292:                     } else {
 8293:                         if ($confhash->{$role}{$item}) {
 8294:                             $changes{$role}{'links'}{$item} = 1;
 8295:                         }
 8296:                     }
 8297:                 }
 8298:                 foreach my $item (@logintext) {
 8299:                     if ($domconfig->{$role}{$item} ne '') {
 8300:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8301:                             $changes{$role}{'logintext'}{$item} = 1;
 8302:                         }
 8303:                     } else {
 8304:                         if ($confhash->{$role}{$item}) {
 8305:                             $changes{$role}{'logintext'}{$item} = 1;
 8306:                         }
 8307:                     }
 8308:                 }
 8309:             } else {
 8310:                 &default_change_checker($role,\@images,\@links,\@bgs,
 8311:                                         \@logintext,$confhash,\%changes); 
 8312:             }
 8313:         } else {
 8314:             &default_change_checker($role,\@images,\@links,\@bgs,
 8315:                                     \@logintext,$confhash,\%changes); 
 8316:         }
 8317:     }
 8318:     return ($errors,%changes);
 8319: }
 8320: 
 8321: sub config_check {
 8322:     my ($dom,$confname,$servadm) = @_;
 8323:     my ($configuserok,$author_ok,$switchserver,%currroles);
 8324:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 8325:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 8326:                                                    $confname,$servadm);
 8327:     if ($configuserok eq 'ok') {
 8328:         $switchserver = &check_switchserver($dom,$confname);
 8329:         if ($switchserver eq '') {
 8330:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 8331:         }
 8332:     }
 8333:     return ($configuserok,$author_ok,$switchserver);
 8334: }
 8335: 
 8336: sub default_change_checker {
 8337:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 8338:     foreach my $item (@{$links}) {
 8339:         if ($confhash->{$role}{$item}) {
 8340:             $changes->{$role}{'links'}{$item} = 1;
 8341:         }
 8342:     }
 8343:     foreach my $item (@{$bgs}) {
 8344:         if ($confhash->{$role}{$item}) {
 8345:             $changes->{$role}{'bgs'}{$item} = 1;
 8346:         }
 8347:     }
 8348:     foreach my $item (@{$logintext}) {
 8349:         if ($confhash->{$role}{$item}) {
 8350:             $changes->{$role}{'logintext'}{$item} = 1;
 8351:         }
 8352:     }
 8353:     foreach my $img (@{$images}) {
 8354:         if ($env{'form.'.$role.'_del_'.$img}) {
 8355:             $confhash->{$role}{$img} = '';
 8356:             $changes->{$role}{'images'}{$img} = 1;
 8357:         }
 8358:         if ($role eq 'login') {
 8359:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8360:                 $changes->{$role}{'showlogo'}{$img} = 1;
 8361:             }
 8362:         }
 8363:     }
 8364:     if ($confhash->{$role}{'font'}) {
 8365:         $changes->{$role}{'font'} = 1;
 8366:     }
 8367: }
 8368: 
 8369: sub display_colorchgs {
 8370:     my ($dom,$changes,$roles,$confhash) = @_;
 8371:     my (%choices,$resulttext);
 8372:     if (!grep(/^login$/,@{$roles})) {
 8373:         $resulttext = &mt('Changes made:').'<br />';
 8374:     }
 8375:     foreach my $role (@{$roles}) {
 8376:         if ($role eq 'login') {
 8377:             %choices = &login_choices();
 8378:         } else {
 8379:             %choices = &color_font_choices();
 8380:         }
 8381:         if (ref($changes->{$role}) eq 'HASH') {
 8382:             if ($role ne 'login') {
 8383:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 8384:             }
 8385:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 8386:                 if ($role ne 'login') {
 8387:                     $resulttext .= '<ul>';
 8388:                 }
 8389:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 8390:                     if ($role ne 'login') {
 8391:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 8392:                     }
 8393:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 8394:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 8395:                             if ($confhash->{$role}{$key}{$item}) {
 8396:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 8397:                             } else {
 8398:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 8399:                             }
 8400:                         } elsif ($confhash->{$role}{$item} eq '') {
 8401:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 8402:                         } else {
 8403:                             my $newitem = $confhash->{$role}{$item};
 8404:                             if ($key eq 'images') {
 8405:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 8406:                             }
 8407:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 8408:                         }
 8409:                     }
 8410:                     if ($role ne 'login') {
 8411:                         $resulttext .= '</ul></li>';
 8412:                     }
 8413:                 } else {
 8414:                     if ($confhash->{$role}{$key} eq '') {
 8415:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 8416:                     } else {
 8417:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 8418:                     }
 8419:                 }
 8420:                 if ($role ne 'login') {
 8421:                     $resulttext .= '</ul>';
 8422:                 }
 8423:             }
 8424:         }
 8425:     }
 8426:     return $resulttext;
 8427: }
 8428: 
 8429: sub thumb_dimensions {
 8430:     return ('200','50');
 8431: }
 8432: 
 8433: sub check_dimensions {
 8434:     my ($inputfile) = @_;
 8435:     my ($fullwidth,$fullheight);
 8436:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 8437:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 8438:             my $imageinfo = <PIPE>;
 8439:             if (!close(PIPE)) {
 8440:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 8441:             }
 8442:             chomp($imageinfo);
 8443:             my ($fullsize) = 
 8444:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 8445:             if ($fullsize) {
 8446:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 8447:             }
 8448:         }
 8449:     }
 8450:     return ($fullwidth,$fullheight);
 8451: }
 8452: 
 8453: sub check_configuser {
 8454:     my ($uhome,$dom,$confname,$servadm) = @_;
 8455:     my ($configuserok,%currroles);
 8456:     if ($uhome eq 'no_host') {
 8457:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 8458:         my $configpass = &LONCAPA::Enrollment::create_password();
 8459:         $configuserok = 
 8460:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 8461:                              $configpass,'','','','','',undef,$servadm);
 8462:     } else {
 8463:         $configuserok = 'ok';
 8464:         %currroles = 
 8465:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 8466:     }
 8467:     return ($configuserok,%currroles);
 8468: }
 8469: 
 8470: sub check_authorstatus {
 8471:     my ($dom,$confname,%currroles) = @_;
 8472:     my $author_ok;
 8473:     if (!$currroles{':'.$dom.':au'}) {
 8474:         my $start = time;
 8475:         my $end = 0;
 8476:         $author_ok = 
 8477:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 8478:                                         'au',$end,$start,'','','domconfig');
 8479:     } else {
 8480:         $author_ok = 'ok';
 8481:     }
 8482:     return $author_ok;
 8483: }
 8484: 
 8485: sub publishlogo {
 8486:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 8487:     my ($output,$fname,$logourl,$madethumb);
 8488:     if ($action eq 'upload') {
 8489:         $fname=$env{'form.'.$formname.'.filename'};
 8490:         chop($env{'form.'.$formname});
 8491:     } else {
 8492:         ($fname) = ($formname =~ /([^\/]+)$/);
 8493:     }
 8494:     if ($savefileas ne '') {
 8495:         $fname = $savefileas;
 8496:     }
 8497:     $fname=&Apache::lonnet::clean_filename($fname);
 8498: # See if there is anything left
 8499:     unless ($fname) { return ('error: no uploaded file'); }
 8500:     $fname="$subdir/$fname";
 8501:     my $docroot=$r->dir_config('lonDocRoot');
 8502:     my $filepath="$docroot/priv";
 8503:     my $relpath = "$dom/$confname";
 8504:     my ($fnamepath,$file,$fetchthumb);
 8505:     $file=$fname;
 8506:     if ($fname=~m|/|) {
 8507:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 8508:     }
 8509:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 8510:     my $count;
 8511:     for ($count=5;$count<=$#parts;$count++) {
 8512:         $filepath.="/$parts[$count]";
 8513:         if ((-e $filepath)!=1) {
 8514:             mkdir($filepath,02770);
 8515:         }
 8516:     }
 8517:     # Check for bad extension and disallow upload
 8518:     if ($file=~/\.(\w+)$/ &&
 8519:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 8520:         $output = 
 8521:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 8522:     } elsif ($file=~/\.(\w+)$/ &&
 8523:         !defined(&Apache::loncommon::fileembstyle($1))) {
 8524:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 8525:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 8526:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 8527:     } elsif (-d "$filepath/$file") {
 8528:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 8529:     } else {
 8530:         my $source = $filepath.'/'.$file;
 8531:         my $logfile;
 8532:         if (!open($logfile,">>$source".'.log')) {
 8533:             return (&mt('No write permission to Authoring Space'));
 8534:         }
 8535:         print $logfile
 8536: "\n================= Publish ".localtime()." ================\n".
 8537: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 8538: # Save the file
 8539:         if (!open(FH,'>'.$source)) {
 8540:             &Apache::lonnet::logthis('Failed to create '.$source);
 8541:             return (&mt('Failed to create file'));
 8542:         }
 8543:         if ($action eq 'upload') {
 8544:             if (!print FH ($env{'form.'.$formname})) {
 8545:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 8546:                 return (&mt('Failed to write file'));
 8547:             }
 8548:         } else {
 8549:             my $original = &Apache::lonnet::filelocation('',$formname);
 8550:             if(!copy($original,$source)) {
 8551:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 8552:                 return (&mt('Failed to write file'));
 8553:             }
 8554:         }
 8555:         close(FH);
 8556:         chmod(0660, $source); # Permissions to rw-rw---.
 8557: 
 8558:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 8559:         my $copyfile=$targetdir.'/'.$file;
 8560: 
 8561:         my @parts=split(/\//,$targetdir);
 8562:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 8563:         for (my $count=5;$count<=$#parts;$count++) {
 8564:             $path.="/$parts[$count]";
 8565:             if (!-e $path) {
 8566:                 print $logfile "\nCreating directory ".$path;
 8567:                 mkdir($path,02770);
 8568:             }
 8569:         }
 8570:         my $versionresult;
 8571:         if (-e $copyfile) {
 8572:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 8573:         } else {
 8574:             $versionresult = 'ok';
 8575:         }
 8576:         if ($versionresult eq 'ok') {
 8577:             if (copy($source,$copyfile)) {
 8578:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 8579:                 $output = 'ok';
 8580:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 8581:                 push(@{$modified_urls},[$copyfile,$source]);
 8582:                 my $metaoutput = 
 8583:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 8584:                 unless ($registered_cleanup) {
 8585:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8586:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8587:                     $registered_cleanup=1;
 8588:                 }
 8589:             } else {
 8590:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 8591:                 $output = &mt('Failed to copy file to RES space').", $!";
 8592:             }
 8593:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 8594:                 my $inputfile = $filepath.'/'.$file;
 8595:                 my $outfile = $filepath.'/'.'tn-'.$file;
 8596:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 8597:                 if ($fullwidth ne '' && $fullheight ne '') { 
 8598:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 8599:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 8600:                         system("convert -sample $thumbsize $inputfile $outfile");
 8601:                         chmod(0660, $filepath.'/tn-'.$file);
 8602:                         if (-e $outfile) {
 8603:                             my $copyfile=$targetdir.'/tn-'.$file;
 8604:                             if (copy($outfile,$copyfile)) {
 8605:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 8606:                                 my $thumb_metaoutput = 
 8607:                                     &write_metadata($dom,$confname,$formname,
 8608:                                                     $targetdir,'tn-'.$file,$logfile);
 8609:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 8610:                                 unless ($registered_cleanup) {
 8611:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8612:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8613:                                     $registered_cleanup=1;
 8614:                                 }
 8615:                                 $madethumb = 1;
 8616:                             } else {
 8617:                                 print $logfile "\nUnable to write ".$copyfile.
 8618:                                                ':'.$!."\n";
 8619:                             }
 8620:                         }
 8621:                     }
 8622:                 }
 8623:             }
 8624:         } else {
 8625:             $output = $versionresult;
 8626:         }
 8627:     }
 8628:     return ($output,$logourl,$madethumb);
 8629: }
 8630: 
 8631: sub logo_versioning {
 8632:     my ($targetdir,$file,$logfile) = @_;
 8633:     my $target = $targetdir.'/'.$file;
 8634:     my ($maxversion,$fn,$extn,$output);
 8635:     $maxversion = 0;
 8636:     if ($file =~ /^(.+)\.(\w+)$/) {
 8637:         $fn=$1;
 8638:         $extn=$2;
 8639:     }
 8640:     opendir(DIR,$targetdir);
 8641:     while (my $filename=readdir(DIR)) {
 8642:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 8643:             $maxversion=($1>$maxversion)?$1:$maxversion;
 8644:         }
 8645:     }
 8646:     $maxversion++;
 8647:     print $logfile "\nCreating old version ".$maxversion."\n";
 8648:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 8649:     if (copy($target,$copyfile)) {
 8650:         print $logfile "Copied old target to ".$copyfile."\n";
 8651:         $copyfile=$copyfile.'.meta';
 8652:         if (copy($target.'.meta',$copyfile)) {
 8653:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 8654:             $output = 'ok';
 8655:         } else {
 8656:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 8657:             $output = &mt('Failed to copy old meta').", $!, ";
 8658:         }
 8659:     } else {
 8660:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 8661:         $output = &mt('Failed to copy old target').", $!, ";
 8662:     }
 8663:     return $output;
 8664: }
 8665: 
 8666: sub write_metadata {
 8667:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 8668:     my (%metadatafields,%metadatakeys,$output);
 8669:     $metadatafields{'title'}=$formname;
 8670:     $metadatafields{'creationdate'}=time;
 8671:     $metadatafields{'lastrevisiondate'}=time;
 8672:     $metadatafields{'copyright'}='public';
 8673:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 8674:                                          $env{'user.domain'};
 8675:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 8676:     $metadatafields{'domain'}=$dom;
 8677:     {
 8678:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 8679:         my $mfh;
 8680:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 8681:             foreach (sort(keys(%metadatafields))) {
 8682:                 unless ($_=~/\./) {
 8683:                     my $unikey=$_;
 8684:                     $unikey=~/^([A-Za-z]+)/;
 8685:                     my $tag=$1;
 8686:                     $tag=~tr/A-Z/a-z/;
 8687:                     print $mfh "\n\<$tag";
 8688:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 8689:                         my $value=$metadatafields{$unikey.'.'.$_};
 8690:                         $value=~s/\"/\'\'/g;
 8691:                         print $mfh ' '.$_.'="'.$value.'"';
 8692:                     }
 8693:                     print $mfh '>'.
 8694:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 8695:                             .'</'.$tag.'>';
 8696:                 }
 8697:             }
 8698:             $output = 'ok';
 8699:             print $logfile "\nWrote metadata";
 8700:             close($mfh);
 8701:         } else {
 8702:             print $logfile "\nFailed to open metadata file";
 8703:             $output = &mt('Could not write metadata');
 8704:         }
 8705:     }
 8706:     return $output;
 8707: }
 8708: 
 8709: sub notifysubscribed {
 8710:     foreach my $targetsource (@{$modified_urls}){
 8711:         next unless (ref($targetsource) eq 'ARRAY');
 8712:         my ($target,$source)=@{$targetsource};
 8713:         if ($source ne '') {
 8714:             if (open(my $logfh,'>>'.$source.'.log')) {
 8715:                 print $logfh "\nCleanup phase: Notifications\n";
 8716:                 my @subscribed=&subscribed_hosts($target);
 8717:                 foreach my $subhost (@subscribed) {
 8718:                     print $logfh "\nNotifying host ".$subhost.':';
 8719:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 8720:                     print $logfh $reply;
 8721:                 }
 8722:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 8723:                 foreach my $subhost (@subscribedmeta) {
 8724:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 8725:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 8726:                                                         $subhost);
 8727:                     print $logfh $reply;
 8728:                 }
 8729:                 print $logfh "\n============ Done ============\n";
 8730:                 close($logfh);
 8731:             }
 8732:         }
 8733:     }
 8734:     return OK;
 8735: }
 8736: 
 8737: sub subscribed_hosts {
 8738:     my ($target) = @_;
 8739:     my @subscribed;
 8740:     if (open(my $fh,"<$target.subscription")) {
 8741:         while (my $subline=<$fh>) {
 8742:             if ($subline =~ /^($match_lonid):/) {
 8743:                 my $host = $1;
 8744:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 8745:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 8746:                         push(@subscribed,$host);
 8747:                     }
 8748:                 }
 8749:             }
 8750:         }
 8751:     }
 8752:     return @subscribed;
 8753: }
 8754: 
 8755: sub check_switchserver {
 8756:     my ($dom,$confname) = @_;
 8757:     my ($allowed,$switchserver);
 8758:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 8759:     if ($home eq 'no_host') {
 8760:         $home = &Apache::lonnet::domain($dom,'primary');
 8761:     }
 8762:     my @ids=&Apache::lonnet::current_machine_ids();
 8763:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 8764:     if (!$allowed) {
 8765: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 8766:     }
 8767:     return $switchserver;
 8768: }
 8769: 
 8770: sub modify_quotas {
 8771:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 8772:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 8773:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 8774:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 8775:         $validationfieldsref);
 8776:     if ($action eq 'quotas') {
 8777:         $context = 'tools'; 
 8778:     } else {
 8779:         $context = $action;
 8780:     }
 8781:     if ($context eq 'requestcourses') {
 8782:         @usertools = ('official','unofficial','community','textbook','placement');
 8783:         @options =('norequest','approval','validate','autolimit');
 8784:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 8785:         %titles = &courserequest_titles();
 8786:         $toolregexp = join('|',@usertools);
 8787:         %conditions = &courserequest_conditions();
 8788:         $confname = $dom.'-domainconfig';
 8789:         my $servadm = $r->dir_config('lonAdmEMail');
 8790:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8791:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 8792:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 8793:     } elsif ($context eq 'requestauthor') {
 8794:         @usertools = ('author');
 8795:         %titles = &authorrequest_titles();
 8796:     } else {
 8797:         @usertools = ('aboutme','blog','webdav','portfolio');
 8798:         %titles = &tool_titles();
 8799:     }
 8800:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8801:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8802:     foreach my $key (keys(%env)) {
 8803:         if ($context eq 'requestcourses') {
 8804:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 8805:                 my $item = $1;
 8806:                 my $type = $2;
 8807:                 if ($type =~ /^limit_(.+)/) {
 8808:                     $limithash{$item}{$1} = $env{$key};
 8809:                 } else {
 8810:                     $confhash{$item}{$type} = $env{$key};
 8811:                 }
 8812:             }
 8813:         } elsif ($context eq 'requestauthor') {
 8814:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 8815:                 $confhash{$1} = $env{$key};
 8816:             }
 8817:         } else {
 8818:             if ($key =~ /^form\.quota_(.+)$/) {
 8819:                 $confhash{'defaultquota'}{$1} = $env{$key};
 8820:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 8821:                 $confhash{'authorquota'}{$1} = $env{$key};
 8822:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 8823:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 8824:             }
 8825:         }
 8826:     }
 8827:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 8828:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 8829:         @approvalnotify = sort(@approvalnotify);
 8830:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 8831:         my @crstypes = ('official','unofficial','community','textbook','placement');
 8832:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 8833:         foreach my $type (@hasuniquecode) {
 8834:             if (grep(/^\Q$type\E$/,@crstypes)) {
 8835:                 $confhash{'uniquecode'}{$type} = 1;
 8836:             }
 8837:         }
 8838:         my (%newbook,%allpos);
 8839:         if ($context eq 'requestcourses') {
 8840:             foreach my $type ('textbooks','templates') {
 8841:                 @{$allpos{$type}} = (); 
 8842:                 my $invalid;
 8843:                 if ($type eq 'textbooks') {
 8844:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 8845:                 } else {
 8846:                     $invalid = &mt('Invalid LON-CAPA course for template');
 8847:                 }
 8848:                 if ($env{'form.'.$type.'_addbook'}) {
 8849:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 8850:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 8851:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 8852:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 8853:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 8854:                         } else {
 8855:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 8856:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 8857:                             $position =~ s/\D+//g;
 8858:                             if ($position ne '') {
 8859:                                 $allpos{$type}[$position] = $newbook{$type};
 8860:                             }
 8861:                         }
 8862:                     } else {
 8863:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 8864:                     }
 8865:                 }
 8866:             } 
 8867:         }
 8868:         if (ref($domconfig{$action}) eq 'HASH') {
 8869:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 8870:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 8871:                     $changes{'notify'}{'approval'} = 1;
 8872:                 }
 8873:             } else {
 8874:                 if ($confhash{'notify'}{'approval'}) {
 8875:                     $changes{'notify'}{'approval'} = 1;
 8876:                 }
 8877:             }
 8878:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 8879:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 8880:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 8881:                         unless ($confhash{'uniquecode'}{$crstype}) {
 8882:                             $changes{'uniquecode'} = 1;
 8883:                         }
 8884:                     }
 8885:                     unless ($changes{'uniquecode'}) {
 8886:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 8887:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 8888:                                 $changes{'uniquecode'} = 1;
 8889:                             }
 8890:                         }
 8891:                     }
 8892:                } else {
 8893:                    $changes{'uniquecode'} = 1;
 8894:                }
 8895:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 8896:                 $changes{'uniquecode'} = 1;
 8897:             }
 8898:             if ($context eq 'requestcourses') {
 8899:                 foreach my $type ('textbooks','templates') {
 8900:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 8901:                         my %deletions;
 8902:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 8903:                         if (@todelete) {
 8904:                             map { $deletions{$_} = 1; } @todelete;
 8905:                         }
 8906:                         my %imgdeletions;
 8907:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 8908:                         if (@todeleteimages) {
 8909:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 8910:                         }
 8911:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 8912:                         for (my $i=0; $i<=$maxnum; $i++) {
 8913:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 8914:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 8915:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 8916:                                 if ($deletions{$key}) {
 8917:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 8918:                                         #FIXME need to obsolete item in RES space
 8919:                                     }
 8920:                                     next;
 8921:                                 } else {
 8922:                                     my $newpos = $env{'form.'.$itemid};
 8923:                                     $newpos =~ s/\D+//g;
 8924:                                     foreach my $item ('subject','title','publisher','author') {
 8925:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8926:                                                  ($type eq 'templates'));
 8927:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 8928:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 8929:                                             $changes{$type}{$key} = 1;
 8930:                                         }
 8931:                                     }
 8932:                                     $allpos{$type}[$newpos] = $key;
 8933:                                 }
 8934:                                 if ($imgdeletions{$key}) {
 8935:                                     $changes{$type}{$key} = 1;
 8936:                                     #FIXME need to obsolete item in RES space
 8937:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 8938:                                     my ($cdom,$cnum) = split(/_/,$key);
 8939:                                     my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 8940:                                                                                   $cdom,$cnum,$type,$configuserok,
 8941:                                                                                   $switchserver,$author_ok);
 8942:                                     if ($imgurl) {
 8943:                                         $confhash{$type}{$key}{'image'} = $imgurl;
 8944:                                         $changes{$type}{$key} = 1; 
 8945:                                     }
 8946:                                     if ($error) {
 8947:                                         &Apache::lonnet::logthis($error);
 8948:                                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8949:                                     } 
 8950:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 8951:                                     $confhash{$type}{$key}{'image'} = 
 8952:                                         $domconfig{$action}{$type}{$key}{'image'};
 8953:                                 }
 8954:                             }
 8955:                         }
 8956:                     }
 8957:                 }
 8958:             }
 8959:         } else {
 8960:             if ($confhash{'notify'}{'approval'}) {
 8961:                 $changes{'notify'}{'approval'} = 1;
 8962:             }
 8963:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 8964:                 $changes{'uniquecode'} = 1;
 8965:             }
 8966:         }
 8967:         if ($context eq 'requestcourses') {
 8968:             foreach my $type ('textbooks','templates') {
 8969:                 if ($newbook{$type}) {
 8970:                     $changes{$type}{$newbook{$type}} = 1;
 8971:                     foreach my $item ('subject','title','publisher','author') {
 8972:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8973:                                  ($type eq 'template'));
 8974:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 8975:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 8976:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 8977:                         }
 8978:                     }
 8979:                     if ($type eq 'textbooks') {
 8980:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 8981:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 8982:                             my ($imageurl,$error) =
 8983:                                 &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 8984:                                                         $configuserok,$switchserver,$author_ok);
 8985:                             if ($imageurl) {
 8986:                                 $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 8987:                             }
 8988:                             if ($error) {
 8989:                                 &Apache::lonnet::logthis($error);
 8990:                                 $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8991:                             }
 8992:                         }
 8993:                     }
 8994:                 }
 8995:                 if (@{$allpos{$type}} > 0) {
 8996:                     my $idx = 0;
 8997:                     foreach my $item (@{$allpos{$type}}) {
 8998:                         if ($item ne '') {
 8999:                             $confhash{$type}{$item}{'order'} = $idx;
 9000:                             if (ref($domconfig{$action}) eq 'HASH') {
 9001:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 9002:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 9003:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 9004:                                             $changes{$type}{$item} = 1;
 9005:                                         }
 9006:                                     }
 9007:                                 }
 9008:                             }
 9009:                             $idx ++;
 9010:                         }
 9011:                     }
 9012:                 }
 9013:             }
 9014:             if (ref($validationitemsref) eq 'ARRAY') {
 9015:                 foreach my $item (@{$validationitemsref}) {
 9016:                     if ($item eq 'fields') {
 9017:                         my @changed;
 9018:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 9019:                         if (@{$confhash{'validation'}{$item}} > 0) {
 9020:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 9021:                         }
 9022:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9023:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9024:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 9025:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 9026:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
 9027:                                 } else {
 9028:                                     @changed = @{$confhash{'validation'}{$item}};
 9029:                                 }
 9030:                             } else {
 9031:                                 @changed = @{$confhash{'validation'}{$item}};
 9032:                             }
 9033:                         } else {
 9034:                             @changed = @{$confhash{'validation'}{$item}};
 9035:                         }
 9036:                         if (@changed) {
 9037:                             if ($confhash{'validation'}{$item}) {
 9038:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 9039:                             } else {
 9040:                                 $changes{'validation'}{$item} = &mt('None');
 9041:                             }
 9042:                         }
 9043:                     } else {
 9044:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 9045:                         if ($item eq 'markup') {
 9046:                             if ($env{'form.requestcourses_validation_'.$item}) {
 9047:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 9048:                             }
 9049:                         }
 9050:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9051:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9052:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 9053:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9054:                                 }
 9055:                             } else {
 9056:                                 if ($confhash{'validation'}{$item} ne '') {
 9057:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9058:                                 }
 9059:                             }
 9060:                         } else {
 9061:                             if ($confhash{'validation'}{$item} ne '') {
 9062:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9063:                             }
 9064:                         }
 9065:                     }
 9066:                 }
 9067:             }
 9068:             if ($env{'form.validationdc'}) {
 9069:                 my $newval = $env{'form.validationdc'};
 9070:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 9071:                 if (exists($domcoords{$newval})) {
 9072:                     $confhash{'validation'}{'dc'} = $newval;
 9073:                 }
 9074:             }
 9075:             if (ref($confhash{'validation'}) eq 'HASH') {
 9076:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9077:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9078:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9079:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9080:                                 if ($confhash{'validation'}{'dc'} eq '') {
 9081:                                     $changes{'validation'}{'dc'} = &mt('None');
 9082:                                 } else {
 9083:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9084:                                 }
 9085:                             }
 9086:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
 9087:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9088:                         }
 9089:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 9090:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9091:                     }
 9092:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 9093:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9094:                 }
 9095:             } else {
 9096:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9097:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9098:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9099:                             $changes{'validation'}{'dc'} = &mt('None');
 9100:                         }
 9101:                     }
 9102:                 }
 9103:             }
 9104:         }
 9105:     } else {
 9106:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 9107:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 9108:     }
 9109:     foreach my $item (@usertools) {
 9110:         foreach my $type (@{$types},'default','_LC_adv') {
 9111:             my $unset; 
 9112:             if ($context eq 'requestcourses') {
 9113:                 $unset = '0';
 9114:                 if ($type eq '_LC_adv') {
 9115:                     $unset = '';
 9116:                 }
 9117:                 if ($confhash{$item}{$type} eq 'autolimit') {
 9118:                     $confhash{$item}{$type} .= '=';
 9119:                     unless ($limithash{$item}{$type} =~ /\D/) {
 9120:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 9121:                     }
 9122:                 }
 9123:             } elsif ($context eq 'requestauthor') {
 9124:                 $unset = '0';
 9125:                 if ($type eq '_LC_adv') {
 9126:                     $unset = '';
 9127:                 }
 9128:             } else {
 9129:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 9130:                     $confhash{$item}{$type} = 1;
 9131:                 } else {
 9132:                     $confhash{$item}{$type} = 0;
 9133:                 }
 9134:             }
 9135:             if (ref($domconfig{$action}) eq 'HASH') {
 9136:                 if ($action eq 'requestauthor') {
 9137:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 9138:                         $changes{$type} = 1;
 9139:                     }
 9140:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 9141:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 9142:                         $changes{$item}{$type} = 1;
 9143:                     }
 9144:                 } else {
 9145:                     if ($context eq 'requestcourses') {
 9146:                         if ($confhash{$item}{$type} ne $unset) {
 9147:                             $changes{$item}{$type} = 1;
 9148:                         }
 9149:                     } else {
 9150:                         if (!$confhash{$item}{$type}) {
 9151:                             $changes{$item}{$type} = 1;
 9152:                         }
 9153:                     }
 9154:                 }
 9155:             } else {
 9156:                 if ($context eq 'requestcourses') {
 9157:                     if ($confhash{$item}{$type} ne $unset) {
 9158:                         $changes{$item}{$type} = 1;
 9159:                     }
 9160:                 } elsif ($context eq 'requestauthor') {
 9161:                     if ($confhash{$type} ne $unset) {
 9162:                         $changes{$type} = 1;
 9163:                     }
 9164:                 } else {
 9165:                     if (!$confhash{$item}{$type}) {
 9166:                         $changes{$item}{$type} = 1;
 9167:                     }
 9168:                 }
 9169:             }
 9170:         }
 9171:     }
 9172:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 9173:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 9174:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 9175:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 9176:                     if (exists($confhash{'defaultquota'}{$key})) {
 9177:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 9178:                             $changes{'defaultquota'}{$key} = 1;
 9179:                         }
 9180:                     } else {
 9181:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 9182:                     }
 9183:                 }
 9184:             } else {
 9185:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 9186:                     if (exists($confhash{'defaultquota'}{$key})) {
 9187:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 9188:                             $changes{'defaultquota'}{$key} = 1;
 9189:                         }
 9190:                     } else {
 9191:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 9192:                     }
 9193:                 }
 9194:             }
 9195:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9196:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 9197:                     if (exists($confhash{'authorquota'}{$key})) {
 9198:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 9199:                             $changes{'authorquota'}{$key} = 1;
 9200:                         }
 9201:                     } else {
 9202:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 9203:                     }
 9204:                 }
 9205:             }
 9206:         }
 9207:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 9208:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 9209:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9210:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 9211:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 9212:                             $changes{'defaultquota'}{$key} = 1;
 9213:                         }
 9214:                     } else {
 9215:                         if (!exists($domconfig{'quotas'}{$key})) {
 9216:                             $changes{'defaultquota'}{$key} = 1;
 9217:                         }
 9218:                     }
 9219:                 } else {
 9220:                     $changes{'defaultquota'}{$key} = 1;
 9221:                 }
 9222:             }
 9223:         }
 9224:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 9225:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 9226:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9227:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9228:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 9229:                             $changes{'authorquota'}{$key} = 1;
 9230:                         }
 9231:                     } else {
 9232:                         $changes{'authorquota'}{$key} = 1;
 9233:                     }
 9234:                 } else {
 9235:                     $changes{'authorquota'}{$key} = 1;
 9236:                 }
 9237:             }
 9238:         }
 9239:     }
 9240: 
 9241:     if ($context eq 'requestauthor') {
 9242:         $domdefaults{'requestauthor'} = \%confhash;
 9243:     } else {
 9244:         foreach my $key (keys(%confhash)) {
 9245:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 9246:                 $domdefaults{$key} = $confhash{$key};
 9247:             }
 9248:         }
 9249:     }
 9250: 
 9251:     my %quotahash = (
 9252:                       $action => { %confhash }
 9253:                     );
 9254:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 9255:                                              $dom);
 9256:     if ($putresult eq 'ok') {
 9257:         if (keys(%changes) > 0) {
 9258:             my $cachetime = 24*60*60;
 9259:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9260:             if (ref($lastactref) eq 'HASH') {
 9261:                 $lastactref->{'domdefaults'} = 1;
 9262:             }
 9263:             $resulttext = &mt('Changes made:').'<ul>';
 9264:             unless (($context eq 'requestcourses') ||
 9265:                     ($context eq 'requestauthor')) {
 9266:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 9267:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 9268:                     foreach my $type (@{$types},'default') {
 9269:                         if (defined($changes{'defaultquota'}{$type})) {
 9270:                             my $typetitle = $usertypes->{$type};
 9271:                             if ($type eq 'default') {
 9272:                                 $typetitle = $othertitle;
 9273:                             }
 9274:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 9275:                         }
 9276:                     }
 9277:                     $resulttext .= '</ul></li>';
 9278:                 }
 9279:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 9280:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 9281:                     foreach my $type (@{$types},'default') {
 9282:                         if (defined($changes{'authorquota'}{$type})) {
 9283:                             my $typetitle = $usertypes->{$type};
 9284:                             if ($type eq 'default') {
 9285:                                 $typetitle = $othertitle;
 9286:                             }
 9287:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 9288:                         }
 9289:                     }
 9290:                     $resulttext .= '</ul></li>';
 9291:                 }
 9292:             }
 9293:             my %newenv;
 9294:             foreach my $item (@usertools) {
 9295:                 my (%haschgs,%inconf);
 9296:                 if ($context eq 'requestauthor') {
 9297:                     %haschgs = %changes;
 9298:                     %inconf = %confhash;
 9299:                 } else {
 9300:                     if (ref($changes{$item}) eq 'HASH') {
 9301:                         %haschgs = %{$changes{$item}};
 9302:                     }
 9303:                     if (ref($confhash{$item}) eq 'HASH') {
 9304:                         %inconf = %{$confhash{$item}};
 9305:                     }
 9306:                 }
 9307:                 if (keys(%haschgs) > 0) {
 9308:                     my $newacc = 
 9309:                         &Apache::lonnet::usertools_access($env{'user.name'},
 9310:                                                           $env{'user.domain'},
 9311:                                                           $item,'reload',$context);
 9312:                     if (($context eq 'requestcourses') ||
 9313:                         ($context eq 'requestauthor')) {
 9314:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 9315:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 9316:                         }
 9317:                     } else {
 9318:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 9319:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 9320:                         }
 9321:                     }
 9322:                     unless ($context eq 'requestauthor') {
 9323:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 9324:                     }
 9325:                     foreach my $type (@{$types},'default','_LC_adv') {
 9326:                         if ($haschgs{$type}) {
 9327:                             my $typetitle = $usertypes->{$type};
 9328:                             if ($type eq 'default') {
 9329:                                 $typetitle = $othertitle;
 9330:                             } elsif ($type eq '_LC_adv') {
 9331:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 9332:                             }
 9333:                             if ($inconf{$type}) {
 9334:                                 if ($context eq 'requestcourses') {
 9335:                                     my $cond;
 9336:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 9337:                                         if ($1 eq '') {
 9338:                                             $cond = &mt('(Automatic processing of any request).');
 9339:                                         } else {
 9340:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 9341:                                         }
 9342:                                     } else { 
 9343:                                         $cond = $conditions{$inconf{$type}};
 9344:                                     }
 9345:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 9346:                                 } elsif ($context eq 'requestauthor') {
 9347:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 9348:                                                              $titles{$inconf{$type}},$typetitle);
 9349: 
 9350:                                 } else {
 9351:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 9352:                                 }
 9353:                             } else {
 9354:                                 if ($type eq '_LC_adv') {
 9355:                                     if ($inconf{$type} eq '0') {
 9356:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9357:                                     } else { 
 9358:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 9359:                                     }
 9360:                                 } else {
 9361:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9362:                                 }
 9363:                             }
 9364:                         }
 9365:                     }
 9366:                     unless ($context eq 'requestauthor') {
 9367:                         $resulttext .= '</ul></li>';
 9368:                     }
 9369:                 }
 9370:             }
 9371:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 9372:                 if (ref($changes{'notify'}) eq 'HASH') {
 9373:                     if ($changes{'notify'}{'approval'}) {
 9374:                         if (ref($confhash{'notify'}) eq 'HASH') {
 9375:                             if ($confhash{'notify'}{'approval'}) {
 9376:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 9377:                             } else {
 9378:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 9379:                             }
 9380:                         }
 9381:                     }
 9382:                 }
 9383:             }
 9384:             if ($action eq 'requestcourses') {
 9385:                 my @offon = ('off','on');
 9386:                 if ($changes{'uniquecode'}) {
 9387:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 9388:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 9389:                         $resulttext .= '<li>'.
 9390:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 9391:                                        '</li>';
 9392:                     } else {
 9393:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 9394:                                        '</li>';
 9395:                     }
 9396:                 }
 9397:                 foreach my $type ('textbooks','templates') {
 9398:                     if (ref($changes{$type}) eq 'HASH') {
 9399:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 9400:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 9401:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 9402:                             my $coursetitle = $coursehash{'description'};
 9403:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 9404:                             $resulttext .= '<li>';
 9405:                             foreach my $item ('subject','title','publisher','author') {
 9406:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9407:                                          ($type eq 'templates'));
 9408:                                 my $name = $item.':';
 9409:                                 $name =~ s/^(\w)/\U$1/;
 9410:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 9411:                             }
 9412:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 9413:                             if ($type eq 'textbooks') {
 9414:                                 if ($confhash{$type}{$key}{'image'}) {
 9415:                                     $resulttext .= ' '.&mt('Image: [_1]',
 9416:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 9417:                                                    ' alt="Textbook cover" />').'<br />';
 9418:                                 }
 9419:                             }
 9420:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 9421:                         }
 9422:                         $resulttext .= '</ul></li>';
 9423:                     }
 9424:                 }
 9425:                 if (ref($changes{'validation'}) eq 'HASH') {
 9426:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 9427:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 9428:                         foreach my $item (@{$validationitemsref}) {
 9429:                             if (exists($changes{'validation'}{$item})) {
 9430:                                 if ($item eq 'markup') {
 9431:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9432:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 9433:                                 } else {
 9434:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9435:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 9436:                                 }
 9437:                             }
 9438:                         }
 9439:                         if (exists($changes{'validation'}{'dc'})) {
 9440:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 9441:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 9442:                         }
 9443:                     }
 9444:                 }
 9445:             }
 9446:             $resulttext .= '</ul>';
 9447:             if (keys(%newenv)) {
 9448:                 &Apache::lonnet::appenv(\%newenv);
 9449:             }
 9450:         } else {
 9451:             if ($context eq 'requestcourses') {
 9452:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 9453:             } elsif ($context eq 'requestauthor') {
 9454:                 $resulttext = &mt('No changes made to rights to request author space.');
 9455:             } else {
 9456:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 9457:             }
 9458:         }
 9459:     } else {
 9460:         $resulttext = '<span class="LC_error">'.
 9461: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9462:     }
 9463:     if ($errors) {
 9464:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 9465:                        '<ul>'.$errors.'</ul></p>';
 9466:     }
 9467:     return $resulttext;
 9468: }
 9469: 
 9470: sub process_textbook_image {
 9471:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 9472:     my $filename = $env{'form.'.$caller.'.filename'};
 9473:     my ($error,$url);
 9474:     my ($width,$height) = (50,50);
 9475:     if ($configuserok eq 'ok') {
 9476:         if ($switchserver) {
 9477:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 9478:                          $switchserver);
 9479:         } elsif ($author_ok eq 'ok') {
 9480:             my ($result,$imageurl) =
 9481:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 9482:                              "$type/$dom/$cnum/cover",$width,$height);
 9483:             if ($result eq 'ok') {
 9484:                 $url = $imageurl;
 9485:             } else {
 9486:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 9487:             }
 9488:         } else {
 9489:             $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);
 9490:         }
 9491:     } else {
 9492:         $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);
 9493:     }
 9494:     return ($url,$error);
 9495: }
 9496: 
 9497: sub modify_ltitools {
 9498:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 9499:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9500:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
 9501:     my $confname = $dom.'-domainconfig';
 9502:     my $servadm = $r->dir_config('lonAdmEMail');
 9503:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9504:     my (%posslti,%possfield);
 9505:     my @courseroles = ('cc','in','ta','ep','st');
 9506:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 9507:     map { $posslti{$_} = 1; } @ltiroles;
 9508:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
 9509:     map { $possfield{$_} = 1; } @allfields;
 9510:     my %lt = &ltitools_names(); 
 9511:     if ($env{'form.ltitools_add'}) {
 9512:         my $title = $env{'form.ltitools_add_title'};
 9513:         $title =~ s/(`)/'/g;
 9514:         ($newid,my $error) = &get_ltitools_id($dom,$title);
 9515:         if ($newid) {
 9516:             my $position = $env{'form.ltitools_add_pos'};
 9517:             $position =~ s/\D+//g;
 9518:             if ($position ne '') {
 9519:                 $allpos[$position] = $newid;
 9520:             }
 9521:             $changes{$newid} = 1;
 9522:             foreach my $item ('title','url','key','secret') {
 9523:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
 9524:                 if ($env{'form.ltitools_add_'.$item}) {
 9525:                     if (($item eq 'key') || ($item eq 'secret')) {
 9526:                         $encconfig{$newid}{$item} = $env{'form.ltitools_add_'.$item};
 9527:                     } else {
 9528:                         $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
 9529:                     }
 9530:                 }
 9531:             }
 9532:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
 9533:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
 9534:             }
 9535:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
 9536:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
 9537:             }
 9538:             foreach my $item ('width','height','linktext','explanation') {
 9539:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
 9540:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
 9541:                 if (($item eq 'width') || ($item eq 'height')) {
 9542:                     if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
 9543:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
 9544:                     }
 9545:                 } else {
 9546:                     if ($env{'form.ltitools_add_'.$item} ne '') {
 9547:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item}; 
 9548:                     }
 9549:                 }
 9550:             }
 9551:             if ($env{'form.ltitools_add_target'} eq 'window') {
 9552:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
 9553:             } elsif ($env{'form.ltitools_add_target'} eq 'tab') {
 9554:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
 9555:             } else {
 9556:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
 9557:             }
 9558:             foreach my $item ('passback','roster') {
 9559:                 if ($env{'form.ltitools_add_'.$item}) {
 9560:                     $confhash{$newid}{$item} = 1;
 9561:                 }
 9562:             }
 9563:             if ($env{'form.ltitools_add_image.filename'} ne '') {
 9564:                 my ($imageurl,$error) =
 9565:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$dom,
 9566:                                             $configuserok,$switchserver,$author_ok);
 9567:                 if ($imageurl) {
 9568:                     $confhash{$newid}{'image'} = $imageurl;
 9569:                 }
 9570:                 if ($error) {
 9571:                     &Apache::lonnet::logthis($error);
 9572:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9573:                 }
 9574:             }
 9575:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
 9576:             foreach my $field (@fields) {
 9577:                 if ($possfield{$field}) {
 9578:                     if ($field eq 'roles') {
 9579:                         foreach my $role (@courseroles) {
 9580:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
 9581:                             if (($choice ne '') && ($posslti{$choice})) {
 9582:                                 $confhash{$newid}{'roles'}{$role} = $choice;
 9583:                                 if ($role eq 'cc') {
 9584:                                     $confhash{$newid}{'roles'}{'co'} = $choice; 
 9585:                                 }
 9586:                             }
 9587:                         }
 9588:                     } else {
 9589:                         $confhash{$newid}{'fields'}{$field} = 1;
 9590:                     }
 9591:                 }
 9592:             }
 9593:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
 9594:             foreach my $item (@courseconfig) {
 9595:                 $confhash{$newid}{'crsconf'}{$item} = 1;
 9596:             }
 9597:             if ($env{'form.ltitools_add_custom'}) {
 9598:                 my $name = $env{'form.ltitools_add_custom_name'};
 9599:                 my $value = $env{'form.ltitools_add_custom_value'};
 9600:                 $value =~ s/(`)/'/g;
 9601:                 $name =~ s/(`)/'/g;
 9602:                 $confhash{$newid}{'custom'}{$name} = $value;
 9603:             }
 9604:         } else {
 9605:             my $error = &mt('Failed to acquire unique ID for new external tool');   
 9606:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9607:         }
 9608:     }
 9609:     if (ref($domconfig{$action}) eq 'HASH') {
 9610:         my %deletions;
 9611:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
 9612:         if (@todelete) {
 9613:             map { $deletions{$_} = 1; } @todelete;
 9614:         }
 9615:         my %customadds;
 9616:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
 9617:         if (@newcustom) {
 9618:             map { $customadds{$_} = 1; } @newcustom;
 9619:         } 
 9620:         my %imgdeletions;
 9621:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
 9622:         if (@todeleteimages) {
 9623:             map { $imgdeletions{$_} = 1; } @todeleteimages;
 9624:         }
 9625:         my $maxnum = $env{'form.ltitools_maxnum'};
 9626:         for (my $i=0; $i<=$maxnum; $i++) {
 9627:             my $itemid = $env{'form.ltitools_id_'.$i};
 9628:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
 9629:                 if ($deletions{$itemid}) {
 9630:                     if ($domconfig{$action}{$itemid}{'image'}) {
 9631:                         #FIXME need to obsolete item in RES space
 9632:                     }
 9633:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
 9634:                     next;
 9635:                 } else {
 9636:                     my $newpos = $env{'form.ltitools_'.$itemid};
 9637:                     $newpos =~ s/\D+//g;
 9638:                     foreach my $item ('title','url') {
 9639:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
 9640:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
 9641:                             $changes{$itemid} = 1;
 9642:                         }
 9643:                     }
 9644:                     foreach my $item ('key','secret') {
 9645:                         $encconfig{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
 9646:                         if ($domconfig{$action}{$itemid}{$item} ne $encconfig{$itemid}{$item}) {
 9647:                             $changes{$itemid} = 1;
 9648:                         }
 9649:                     }
 9650:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
 9651:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
 9652:                     }
 9653:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
 9654:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
 9655:                     }
 9656:                     foreach my $size ('width','height') {
 9657:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
 9658:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
 9659:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
 9660:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
 9661:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9662:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
 9663:                                     $changes{$itemid} = 1;
 9664:                                 }
 9665:                             } else {
 9666:                                 $changes{$itemid} = 1;
 9667:                             }
 9668:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9669:                             if ($domconfig{$action}{$itemid}{'display'}{$size} ne '') {
 9670:                                 $changes{$itemid} = 1;
 9671:                             }
 9672:                         }
 9673:                     }
 9674:                     foreach my $item ('linktext','explanation') {
 9675:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/^\s+//;
 9676:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/\s+$//;
 9677:                         if ($env{'form.ltitools_'.$item.'_'.$i} ne '') {
 9678:                             $confhash{$itemid}{'display'}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
 9679:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9680:                                 if ($domconfig{$action}{$itemid}{'display'}{$item} ne $confhash{$itemid}{'display'}{$item}) {
 9681:                                     $changes{$itemid} = 1;
 9682:                                 }
 9683:                             } else {
 9684:                                 $changes{$itemid} = 1;
 9685:                             }
 9686:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9687:                             if ($domconfig{$action}{$itemid}{'display'}{$item} ne '') {
 9688:                                 $changes{$itemid} = 1;
 9689:                             }
 9690:                         }
 9691:                     }
 9692:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
 9693:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
 9694:                     } elsif ($env{'form.ltitools_target_'.$i} eq 'tab') {
 9695:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
 9696:                     } else {
 9697:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
 9698:                     }
 9699:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9700:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
 9701:                             $changes{$itemid} = 1;
 9702:                         }
 9703:                     } else {
 9704:                         $changes{$itemid} = 1;
 9705:                     }
 9706:                     foreach my $extra ('passback','roster') {
 9707:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
 9708:                             $confhash{$itemid}{$extra} = 1;
 9709:                         }
 9710:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
 9711:                             $changes{$itemid} = 1;
 9712:                         }
 9713:                     }
 9714:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
 9715:                     foreach my $item ('label','title','target','linktext','explanation') {
 9716:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
 9717:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
 9718:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
 9719:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
 9720:                                     $changes{$itemid} = 1;
 9721:                                 }
 9722:                             } else {
 9723:                                 $changes{$itemid} = 1;
 9724:                             }
 9725:                         }
 9726:                     }
 9727:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
 9728:                     foreach my $field (@fields) {
 9729:                         if ($possfield{$field}) {
 9730:                             if ($field eq 'roles') {
 9731:                                 foreach my $role (@courseroles) {
 9732:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
 9733:                                     if (($choice ne '') && ($posslti{$choice})) {
 9734:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
 9735:                                         if ($role eq 'cc') {
 9736:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
 9737:                                         }
 9738:                                     }
 9739:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
 9740:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
 9741:                                             $changes{$itemid} = 1;
 9742:                                         }
 9743:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
 9744:                                         $changes{$itemid} = 1;
 9745:                                     }
 9746:                                 }
 9747:                             } else {
 9748:                                 $confhash{$itemid}{'fields'}{$field} = 1;
 9749:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
 9750:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
 9751:                                         $changes{$itemid} = 1;
 9752:                                     }
 9753:                                 } else {
 9754:                                     $changes{$itemid} = 1;
 9755:                                 }
 9756:                             }
 9757:                         }
 9758:                     }
 9759:                     $allpos[$newpos] = $itemid;
 9760:                 }
 9761:                 if ($imgdeletions{$itemid}) {
 9762:                     $changes{$itemid} = 1;
 9763:                     #FIXME need to obsolete item in RES space
 9764:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
 9765:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
 9766:                                                                  $itemid,$configuserok,$switchserver,
 9767:                                                                  $author_ok);
 9768:                     if ($imgurl) {
 9769:                         $confhash{$itemid}{'image'} = $imgurl;
 9770:                         $changes{$itemid} = 1;
 9771:                     }
 9772:                     if ($error) {
 9773:                         &Apache::lonnet::logthis($error);
 9774:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9775:                     }
 9776:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
 9777:                     $confhash{$itemid}{'image'} =
 9778:                        $domconfig{$action}{$itemid}{'image'};
 9779:                 }
 9780:                 if ($customadds{$i}) {
 9781:                     my $name = $env{'form.ltitools_custom_name_'.$i};
 9782:                     $name =~ s/(`)/'/g;
 9783:                     $name =~ s/^\s+//;
 9784:                     $name =~ s/\s+$//;
 9785:                     my $value = $env{'form.ltitools_custom_value_'.$i};
 9786:                     $value =~ s/(`)/'/g;
 9787:                     $value =~ s/^\s+//;
 9788:                     $value =~ s/\s+$//;
 9789:                     if ($name ne '') {
 9790:                         $confhash{$itemid}{'custom'}{$name} = $value;
 9791:                         $changes{$itemid} = 1;
 9792:                     }
 9793:                 }
 9794:                 my %customdels;
 9795:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i); 
 9796:                 if (@customdeletions) {
 9797:                     $changes{$itemid} = 1;
 9798:                 }
 9799:                 map { $customdels{$_} = 1; } @customdeletions;
 9800:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
 9801:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
 9802:                         unless ($customdels{$key}) {
 9803:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
 9804:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i}; 
 9805:                             }
 9806:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
 9807:                                 $changes{$itemid} = 1;
 9808:                             }
 9809:                         }
 9810:                     }
 9811:                 }
 9812:                 unless ($changes{$itemid}) {
 9813:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
 9814:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
 9815:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
 9816:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
 9817:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
 9818:                                         $changes{$itemid} = 1;
 9819:                                         last;
 9820:                                     }
 9821:                                 }
 9822:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
 9823:                                 $changes{$itemid} = 1;
 9824:                             }
 9825:                         }
 9826:                         last if ($changes{$itemid});
 9827:                     }
 9828:                 }
 9829:             }
 9830:         }
 9831:     }
 9832:     if (@allpos > 0) {
 9833:         my $idx = 0;
 9834:         foreach my $itemid (@allpos) {
 9835:             if ($itemid ne '') {
 9836:                 $confhash{$itemid}{'order'} = $idx;
 9837:                 if (ref($domconfig{$action}) eq 'HASH') {
 9838:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
 9839:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
 9840:                             $changes{$itemid} = 1;
 9841:                         }
 9842:                     }
 9843:                 }
 9844:                 $idx ++;
 9845:             }
 9846:         }
 9847:     }
 9848:     my %ltitoolshash = (
 9849:                           $action => { %confhash }
 9850:                        );
 9851:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
 9852:                                              $dom);
 9853:     if ($putresult eq 'ok') {
 9854:         my %ltienchash = (
 9855:                              $action => { %encconfig }
 9856:                          );
 9857:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
 9858:         if (keys(%changes) > 0) {
 9859:             my $cachetime = 24*60*60;
 9860:             my %ltiall = %confhash;
 9861:             foreach my $id (keys(%ltiall)) {
 9862:                 if (ref($encconfig{$id}) eq 'HASH') {
 9863:                     foreach my $item ('key','secret') {
 9864:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
 9865:                     }
 9866:                 }
 9867:             }
 9868:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%ltiall,$cachetime);
 9869:             if (ref($lastactref) eq 'HASH') {
 9870:                 $lastactref->{'ltitools'} = 1;
 9871:             }
 9872:             $resulttext = &mt('Changes made:').'<ul>';
 9873:             my %bynum;
 9874:             foreach my $itemid (sort(keys(%changes))) {
 9875:                 my $position = $confhash{$itemid}{'order'};
 9876:                 $bynum{$position} = $itemid;
 9877:             }
 9878:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
 9879:                 my $itemid = $bynum{$pos}; 
 9880:                 if (ref($confhash{$itemid}) ne 'HASH') {
 9881:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
 9882:                 } else {
 9883:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
 9884:                     if ($confhash{$itemid}{'image'}) {
 9885:                         $resulttext .= '&nbsp;'.
 9886:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
 9887:                                        ' alt="'.&mt('Tool Provider icon').'" />';
 9888:                     }
 9889:                     $resulttext .= '</li><ul>';
 9890:                     my $position = $pos + 1;
 9891:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
 9892:                     foreach my $item ('version','msgtype','url') {
 9893:                         if ($confhash{$itemid}{$item} ne '') {
 9894:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
 9895:                         }
 9896:                     }
 9897:                     if ($encconfig{$itemid}{'key'} ne '') {
 9898:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
 9899:                     }
 9900:                     if ($encconfig{$itemid}{'secret'} ne '') {
 9901:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
 9902:                         my $num = length($encconfig{$itemid}{'secret'});
 9903:                         $resulttext .= ('*'x$num).'</li>';
 9904:                     }
 9905:                     $resulttext .= '<li>'.&mt('Configurable in course:');
 9906:                     my @possconfig = ('label','title','target','linktext','explanation');
 9907:                     my $numconfig = 0; 
 9908:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') { 
 9909:                         foreach my $item (@possconfig) {
 9910:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
 9911:                                 $numconfig ++;
 9912:                                 $resulttext .= ' "'.$lt{'crs'.$item}.'"';
 9913:                             }
 9914:                         }
 9915:                     }
 9916:                     if (!$numconfig) {
 9917:                         $resulttext .= &mt('None');
 9918:                     }
 9919:                     $resulttext .= '</li>';
 9920:                     foreach my $item ('passback','roster') {
 9921:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
 9922:                         if ($confhash{$itemid}{$item}) {
 9923:                             $resulttext .= &mt('Yes');
 9924:                         } else {
 9925:                             $resulttext .= &mt('No');
 9926:                         }
 9927:                         $resulttext .= '</li>';
 9928:                     }
 9929:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
 9930:                         my $displaylist;
 9931:                         if ($confhash{$itemid}{'display'}{'target'}) {
 9932:                             $displaylist = &mt('Display target').':&nbsp;'.
 9933:                                            $confhash{$itemid}{'display'}{'target'}.',';
 9934:                         }
 9935:                         foreach my $size ('width','height') { 
 9936:                             if ($confhash{$itemid}{'display'}{$size}) {
 9937:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
 9938:                                                 $confhash{$itemid}{'display'}{$size}.',';
 9939:                             }
 9940:                         }
 9941:                         if ($displaylist) {
 9942:                             $displaylist =~ s/,$//;
 9943:                             $resulttext .= '<li>'.$displaylist.'</li>';
 9944:                         }
 9945:                         foreach my $item ('linktext','explanation') {
 9946:                             if ($confhash{$itemid}{'display'}{$item}) {
 9947:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{'display'}{$item}.'</li>';
 9948:                             }
 9949:                         }
 9950:                     }
 9951:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
 9952:                         my $fieldlist;
 9953:                         foreach my $field (@allfields) {
 9954:                             if ($confhash{$itemid}{'fields'}{$field}) {
 9955:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
 9956:                             }
 9957:                         }
 9958:                         if ($fieldlist) {
 9959:                             $fieldlist =~ s/,$//;
 9960:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
 9961:                         }
 9962:                     }
 9963:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
 9964:                         my $rolemaps;
 9965:                         foreach my $role (@courseroles) {
 9966:                             if ($confhash{$itemid}{'roles'}{$role}) {
 9967:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
 9968:                                              $confhash{$itemid}{'roles'}{$role}.',';
 9969:                             }
 9970:                         }
 9971:                         if ($rolemaps) {
 9972:                             $rolemaps =~ s/,$//; 
 9973:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
 9974:                         }
 9975:                     }
 9976:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
 9977:                         my $customlist;
 9978:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
 9979:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
 9980:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
 9981:                             } 
 9982:                         }
 9983:                         if ($customlist) {
 9984:                             $resulttext .= '<li>'.&mt('Custom items').':'.$customlist.'</li>';
 9985:                         }
 9986:                     } 
 9987:                     $resulttext .= '</ul></li>';
 9988:                 }
 9989:             }
 9990:             $resulttext .= '</ul>';
 9991:         } else {
 9992:             $resulttext = &mt('No changes made.');
 9993:         }
 9994:     } else {
 9995:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
 9996:     }
 9997:     if ($errors) {
 9998:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9999:                        $errors.'</ul>';
10000:     }
10001:     return $resulttext;
10002: }
10003: 
10004: sub process_ltitools_image {
10005:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
10006:     my $filename = $env{'form.'.$caller.'.filename'};
10007:     my ($error,$url);
10008:     my ($width,$height) = (21,21);
10009:     if ($configuserok eq 'ok') {
10010:         if ($switchserver) {
10011:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
10012:                          $switchserver);
10013:         } elsif ($author_ok eq 'ok') {
10014:             my ($result,$imageurl,$madethumb) =
10015:                 &publishlogo($r,'upload',$caller,$dom,$confname,
10016:                              "ltitools/$itemid/icon",$width,$height);
10017:             if ($result eq 'ok') {
10018:                 if ($madethumb) {
10019:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
10020:                     my $imagethumb = "$path/tn-".$imagefile;
10021:                     $url = $imagethumb;
10022:                 } else {
10023:                     $url = $imageurl;
10024:                 }
10025:             } else {
10026:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
10027:             }
10028:         } else {
10029:             $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);
10030:         }
10031:     } else {
10032:         $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);
10033:     }
10034:     return ($url,$error);
10035: }
10036: 
10037: sub get_ltitools_id {
10038:     my ($cdom,$title) = @_;
10039:     # get lock on ltitools db
10040:     my $lockhash = {
10041:                       lock => $env{'user.name'}.
10042:                               ':'.$env{'user.domain'},
10043:                    };
10044:     my $tries = 0;
10045:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
10046:     my ($id,$error);
10047:  
10048:     while (($gotlock ne 'ok') && ($tries<10)) {
10049:         $tries ++;
10050:         sleep (0.1);
10051:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
10052:     }
10053:     if ($gotlock eq 'ok') {
10054:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
10055:         if ($currids{'lock'}) {
10056:             delete($currids{'lock'});
10057:             if (keys(%currids)) {
10058:                 my @curr = sort { $a <=> $b } keys(%currids);
10059:                 if ($curr[-1] =~ /^\d+$/) {
10060:                     $id = 1 + $curr[-1];
10061:                 }
10062:             } else {
10063:                 $id = 1;
10064:             }
10065:             if ($id) {
10066:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
10067:                     $error = 'nostore';
10068:                 }
10069:             } else {
10070:                 $error = 'nonumber';
10071:             }
10072:         }
10073:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
10074:     } else {
10075:         $error = 'nolock';
10076:     }
10077:     return ($id,$error);
10078: }
10079: 
10080: sub modify_autoenroll {
10081:     my ($dom,$lastactref,%domconfig) = @_;
10082:     my ($resulttext,%changes);
10083:     my %currautoenroll;
10084:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
10085:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
10086:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
10087:         }
10088:     }
10089:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
10090:     my %title = ( run => 'Auto-enrollment active',
10091:                   sender => 'Sender for notification messages',
10092:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
10093:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
10094:     my @offon = ('off','on');
10095:     my $sender_uname = $env{'form.sender_uname'};
10096:     my $sender_domain = $env{'form.sender_domain'};
10097:     if ($sender_domain eq '') {
10098:         $sender_uname = '';
10099:     } elsif ($sender_uname eq '') {
10100:         $sender_domain = '';
10101:     }
10102:     my $coowners = $env{'form.autoassign_coowners'};
10103:     my $failsafe = $env{'form.autoenroll_failsafe'};
10104:     $failsafe =~ s{^\s+|\s+$}{}g;
10105:     if ($failsafe =~ /\D/) {
10106:         undef($failsafe);
10107:     }
10108:     my %autoenrollhash =  (
10109:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
10110:                                        'sender_uname' => $sender_uname,
10111:                                        'sender_domain' => $sender_domain,
10112:                                        'co-owners' => $coowners,
10113:                                        'autofailsafe' => $failsafe,
10114:                                 }
10115:                      );
10116:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
10117:                                              $dom);
10118:     if ($putresult eq 'ok') {
10119:         if (exists($currautoenroll{'run'})) {
10120:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
10121:                  $changes{'run'} = 1;
10122:              }
10123:         } elsif ($autorun) {
10124:             if ($env{'form.autoenroll_run'} ne '1') {
10125:                  $changes{'run'} = 1;
10126:             }
10127:         }
10128:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
10129:             $changes{'sender'} = 1;
10130:         }
10131:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
10132:             $changes{'sender'} = 1;
10133:         }
10134:         if ($currautoenroll{'co-owners'} ne '') {
10135:             if ($currautoenroll{'co-owners'} ne $coowners) {
10136:                 $changes{'coowners'} = 1;
10137:             }
10138:         } elsif ($coowners) {
10139:             $changes{'coowners'} = 1;
10140:         }
10141:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
10142:             $changes{'autofailsafe'} = 1;
10143:         }
10144:         if (keys(%changes) > 0) {
10145:             $resulttext = &mt('Changes made:').'<ul>';
10146:             if ($changes{'run'}) {
10147:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
10148:             }
10149:             if ($changes{'sender'}) {
10150:                 if ($sender_uname eq '' || $sender_domain eq '') {
10151:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
10152:                 } else {
10153:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
10154:                 }
10155:             }
10156:             if ($changes{'coowners'}) {
10157:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
10158:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
10159:                 if (ref($lastactref) eq 'HASH') {
10160:                     $lastactref->{'domainconfig'} = 1;
10161:                 }
10162:             }
10163:             if ($changes{'autofailsafe'}) {
10164:                 if ($failsafe ne '') {
10165:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
10166:                 } else {
10167:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
10168:                 }
10169:                 &Apache::lonnet::get_domain_defaults($dom,1);
10170:                 if (ref($lastactref) eq 'HASH') {
10171:                     $lastactref->{'domdefaults'} = 1;
10172:                 }
10173:             }
10174:             $resulttext .= '</ul>';
10175:         } else {
10176:             $resulttext = &mt('No changes made to auto-enrollment settings');
10177:         }
10178:     } else {
10179:         $resulttext = '<span class="LC_error">'.
10180: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10181:     }
10182:     return $resulttext;
10183: }
10184: 
10185: sub modify_autoupdate {
10186:     my ($dom,%domconfig) = @_;
10187:     my ($resulttext,%currautoupdate,%fields,%changes);
10188:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
10189:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
10190:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
10191:         }
10192:     }
10193:     my @offon = ('off','on');
10194:     my %title = &Apache::lonlocal::texthash (
10195:                    run => 'Auto-update:',
10196:                    classlists => 'Updates to user information in classlists?'
10197:                 );
10198:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10199:     my %fieldtitles = &Apache::lonlocal::texthash (
10200:                         id => 'Student/Employee ID',
10201:                         permanentemail => 'E-mail address',
10202:                         lastname => 'Last Name',
10203:                         firstname => 'First Name',
10204:                         middlename => 'Middle Name',
10205:                         generation => 'Generation',
10206:                       );
10207:     $othertitle = &mt('All users');
10208:     if (keys(%{$usertypes}) >  0) {
10209:         $othertitle = &mt('Other users');
10210:     }
10211:     foreach my $key (keys(%env)) {
10212:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
10213:             my ($usertype,$item) = ($1,$2);
10214:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
10215:                 if ($usertype eq 'default') {   
10216:                     push(@{$fields{$1}},$2);
10217:                 } elsif (ref($types) eq 'ARRAY') {
10218:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
10219:                         push(@{$fields{$1}},$2);
10220:                     }
10221:                 }
10222:             }
10223:         }
10224:     }
10225:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
10226:     @lockablenames = sort(@lockablenames);
10227:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
10228:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
10229:         if (@changed) {
10230:             $changes{'lockablenames'} = 1;
10231:         }
10232:     } else {
10233:         if (@lockablenames) {
10234:             $changes{'lockablenames'} = 1;
10235:         }
10236:     }
10237:     my %updatehash = (
10238:                       autoupdate => { run => $env{'form.autoupdate_run'},
10239:                                       classlists => $env{'form.classlists'},
10240:                                       fields => {%fields},
10241:                                       lockablenames => \@lockablenames,
10242:                                     }
10243:                      );
10244:     foreach my $key (keys(%currautoupdate)) {
10245:         if (($key eq 'run') || ($key eq 'classlists')) {
10246:             if (exists($updatehash{autoupdate}{$key})) {
10247:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
10248:                     $changes{$key} = 1;
10249:                 }
10250:             }
10251:         } elsif ($key eq 'fields') {
10252:             if (ref($currautoupdate{$key}) eq 'HASH') {
10253:                 foreach my $item (@{$types},'default') {
10254:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
10255:                         my $change = 0;
10256:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
10257:                             if (!exists($fields{$item})) {
10258:                                 $change = 1;
10259:                                 last;
10260:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
10261:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
10262:                                     $change = 1;
10263:                                     last;
10264:                                 }
10265:                             }
10266:                         }
10267:                         if ($change) {
10268:                             push(@{$changes{$key}},$item);
10269:                         }
10270:                     } 
10271:                 }
10272:             }
10273:         } elsif ($key eq 'lockablenames') {
10274:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
10275:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
10276:                 if (@changed) {
10277:                     $changes{'lockablenames'} = 1;
10278:                 }
10279:             } else {
10280:                 if (@lockablenames) {
10281:                     $changes{'lockablenames'} = 1;
10282:                 }
10283:             }
10284:         }
10285:     }
10286:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
10287:         if (@lockablenames) {
10288:             $changes{'lockablenames'} = 1;
10289:         }
10290:     }
10291:     foreach my $item (@{$types},'default') {
10292:         if (defined($fields{$item})) {
10293:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
10294:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
10295:                     my $change = 0;
10296:                     if (ref($fields{$item}) eq 'ARRAY') {
10297:                         foreach my $type (@{$fields{$item}}) {
10298:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
10299:                                 $change = 1;
10300:                                 last;
10301:                             }
10302:                         }
10303:                     }
10304:                     if ($change) {
10305:                         push(@{$changes{'fields'}},$item);
10306:                     }
10307:                 } else {
10308:                     push(@{$changes{'fields'}},$item);
10309:                 }
10310:             } else {
10311:                 push(@{$changes{'fields'}},$item);
10312:             }
10313:         }
10314:     }
10315:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
10316:                                              $dom);
10317:     if ($putresult eq 'ok') {
10318:         if (keys(%changes) > 0) {
10319:             $resulttext = &mt('Changes made:').'<ul>';
10320:             foreach my $key (sort(keys(%changes))) {
10321:                 if ($key eq 'lockablenames') {
10322:                     $resulttext .= '<li>';
10323:                     if (@lockablenames) {
10324:                         $usertypes->{'default'} = $othertitle;
10325:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
10326:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
10327:                     } else {
10328:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
10329:                     }
10330:                     $resulttext .= '</li>';
10331:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
10332:                     foreach my $item (@{$changes{$key}}) {
10333:                         my @newvalues;
10334:                         foreach my $type (@{$fields{$item}}) {
10335:                             push(@newvalues,$fieldtitles{$type});
10336:                         }
10337:                         my $newvaluestr;
10338:                         if (@newvalues > 0) {
10339:                             $newvaluestr = join(', ',@newvalues);
10340:                         } else {
10341:                             $newvaluestr = &mt('none');
10342:                         }
10343:                         if ($item eq 'default') {
10344:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
10345:                         } else {
10346:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
10347:                         }
10348:                     }
10349:                 } else {
10350:                     my $newvalue;
10351:                     if ($key eq 'run') {
10352:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
10353:                     } else {
10354:                         $newvalue = $offon[$env{'form.'.$key}];
10355:                     }
10356:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
10357:                 }
10358:             }
10359:             $resulttext .= '</ul>';
10360:         } else {
10361:             $resulttext = &mt('No changes made to autoupdates');
10362:         }
10363:     } else {
10364:         $resulttext = '<span class="LC_error">'.
10365: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10366:     }
10367:     return $resulttext;
10368: }
10369: 
10370: sub modify_autocreate {
10371:     my ($dom,%domconfig) = @_;
10372:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
10373:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
10374:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
10375:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
10376:         }
10377:     }
10378:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
10379:                  req => 'Auto-creation of validated requests for official courses',
10380:                  xmldc => 'Identity of course creator of courses from XML files',
10381:                );
10382:     my @types = ('xml','req');
10383:     foreach my $item (@types) {
10384:         $newvals{$item} = $env{'form.autocreate_'.$item};
10385:         $newvals{$item} =~ s/\D//g;
10386:         $newvals{$item} = 0 if ($newvals{$item} eq '');
10387:     }
10388:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
10389:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
10390:     unless (exists($domcoords{$newvals{'xmldc'}})) {
10391:         $newvals{'xmldc'} = '';
10392:     } 
10393:     %autocreatehash =  (
10394:                         autocreate => { xml => $newvals{'xml'},
10395:                                         req => $newvals{'req'},
10396:                                       }
10397:                        );
10398:     if ($newvals{'xmldc'} ne '') {
10399:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
10400:     }
10401:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
10402:                                              $dom);
10403:     if ($putresult eq 'ok') {
10404:         my @items = @types;
10405:         if ($newvals{'xml'}) {
10406:             push(@items,'xmldc');
10407:         }
10408:         foreach my $item (@items) {
10409:             if (exists($currautocreate{$item})) {
10410:                 if ($currautocreate{$item} ne $newvals{$item}) {
10411:                     $changes{$item} = 1;
10412:                 }
10413:             } elsif ($newvals{$item}) {
10414:                 $changes{$item} = 1;
10415:             }
10416:         }
10417:         if (keys(%changes) > 0) {
10418:             my @offon = ('off','on'); 
10419:             $resulttext = &mt('Changes made:').'<ul>';
10420:             foreach my $item (@types) {
10421:                 if ($changes{$item}) {
10422:                     my $newtxt = $offon[$newvals{$item}];
10423:                     $resulttext .= '<li>'.
10424:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
10425:                                        '<b>','</b>').
10426:                                    '</li>';
10427:                 }
10428:             }
10429:             if ($changes{'xmldc'}) {
10430:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
10431:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
10432:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
10433:             }
10434:             $resulttext .= '</ul>';
10435:         } else {
10436:             $resulttext = &mt('No changes made to auto-creation settings');
10437:         }
10438:     } else {
10439:         $resulttext = '<span class="LC_error">'.
10440:             &mt('An error occurred: [_1]',$putresult).'</span>';
10441:     }
10442:     return $resulttext;
10443: }
10444: 
10445: sub modify_directorysrch {
10446:     my ($dom,$lastactref,%domconfig) = @_;
10447:     my ($resulttext,%changes);
10448:     my %currdirsrch;
10449:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
10450:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
10451:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
10452:         }
10453:     }
10454:     my %title = ( available => 'Institutional directory search available',
10455:                   localonly => 'Other domains can search institution',
10456:                   lcavailable => 'LON-CAPA directory search available',
10457:                   lclocalonly => 'Other domains can search LON-CAPA domain',
10458:                   searchby => 'Search types',
10459:                   searchtypes => 'Search latitude');
10460:     my @offon = ('off','on');
10461:     my @otherdoms = ('Yes','No');
10462: 
10463:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
10464:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
10465:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
10466: 
10467:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10468:     if (keys(%{$usertypes}) == 0) {
10469:         @cansearch = ('default');
10470:     } else {
10471:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
10472:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
10473:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
10474:                     push(@{$changes{'cansearch'}},$type);
10475:                 }
10476:             }
10477:             foreach my $type (@cansearch) {
10478:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
10479:                     push(@{$changes{'cansearch'}},$type);
10480:                 }
10481:             }
10482:         } else {
10483:             push(@{$changes{'cansearch'}},@cansearch);
10484:         }
10485:     }
10486: 
10487:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
10488:         foreach my $by (@{$currdirsrch{'searchby'}}) {
10489:             if (!grep(/^\Q$by\E$/,@searchby)) {
10490:                 push(@{$changes{'searchby'}},$by);
10491:             }
10492:         }
10493:         foreach my $by (@searchby) {
10494:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
10495:                 push(@{$changes{'searchby'}},$by);
10496:             }
10497:         }
10498:     } else {
10499:         push(@{$changes{'searchby'}},@searchby);
10500:     }
10501: 
10502:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
10503:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
10504:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
10505:                 push(@{$changes{'searchtypes'}},$type);
10506:             }
10507:         }
10508:         foreach my $type (@searchtypes) {
10509:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
10510:                 push(@{$changes{'searchtypes'}},$type);
10511:             }
10512:         }
10513:     } else {
10514:         if (exists($currdirsrch{'searchtypes'})) {
10515:             foreach my $type (@searchtypes) {  
10516:                 if ($type ne $currdirsrch{'searchtypes'}) { 
10517:                     push(@{$changes{'searchtypes'}},$type);
10518:                 }
10519:             }
10520:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
10521:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
10522:             }   
10523:         } else {
10524:             push(@{$changes{'searchtypes'}},@searchtypes); 
10525:         }
10526:     }
10527: 
10528:     my %dirsrch_hash =  (
10529:             directorysrch => { available => $env{'form.dirsrch_available'},
10530:                                cansearch => \@cansearch,
10531:                                localonly => $env{'form.dirsrch_instlocalonly'},
10532:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
10533:                                lcavailable => $env{'form.dirsrch_domavailable'},
10534:                                searchby => \@searchby,
10535:                                searchtypes => \@searchtypes,
10536:                              }
10537:             );
10538:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
10539:                                              $dom);
10540:     if ($putresult eq 'ok') {
10541:         if (exists($currdirsrch{'available'})) {
10542:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
10543:                  $changes{'available'} = 1;
10544:              }
10545:         } else {
10546:             if ($env{'form.dirsrch_available'} eq '1') {
10547:                 $changes{'available'} = 1;
10548:             }
10549:         }
10550:         if (exists($currdirsrch{'lcavailable'})) {
10551:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
10552:                 $changes{'lcavailable'} = 1;
10553:             }
10554:         } else {
10555:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
10556:                 $changes{'lcavailable'} = 1;
10557:             }
10558:         }
10559:         if (exists($currdirsrch{'localonly'})) {
10560:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
10561:                 $changes{'localonly'} = 1;
10562:             }
10563:         } else {
10564:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
10565:                 $changes{'localonly'} = 1;
10566:             }
10567:         }
10568:         if (exists($currdirsrch{'lclocalonly'})) {
10569:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
10570:                 $changes{'lclocalonly'} = 1;
10571:             }
10572:         } else {
10573:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
10574:                 $changes{'lclocalonly'} = 1;
10575:             }
10576:         }
10577:         if (keys(%changes) > 0) {
10578:             $resulttext = &mt('Changes made:').'<ul>';
10579:             if ($changes{'available'}) {
10580:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
10581:             }
10582:             if ($changes{'lcavailable'}) {
10583:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
10584:             }
10585:             if ($changes{'localonly'}) {
10586:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
10587:             }
10588:             if ($changes{'lclocalonly'}) {
10589:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
10590:             }
10591:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
10592:                 my $chgtext;
10593:                 if (ref($usertypes) eq 'HASH') {
10594:                     if (keys(%{$usertypes}) > 0) {
10595:                         foreach my $type (@{$types}) {
10596:                             if (grep(/^\Q$type\E$/,@cansearch)) {
10597:                                 $chgtext .= $usertypes->{$type}.'; ';
10598:                             }
10599:                         }
10600:                         if (grep(/^default$/,@cansearch)) {
10601:                             $chgtext .= $othertitle;
10602:                         } else {
10603:                             $chgtext =~ s/\; $//;
10604:                         }
10605:                         $resulttext .=
10606:                             '<li>'.
10607:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
10608:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
10609:                             '</li>';
10610:                     }
10611:                 }
10612:             }
10613:             if (ref($changes{'searchby'}) eq 'ARRAY') {
10614:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
10615:                 my $chgtext;
10616:                 foreach my $type (@{$titleorder}) {
10617:                     if (grep(/^\Q$type\E$/,@searchby)) {
10618:                         if (defined($searchtitles->{$type})) {
10619:                             $chgtext .= $searchtitles->{$type}.'; ';
10620:                         }
10621:                     }
10622:                 }
10623:                 $chgtext =~ s/\; $//;
10624:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
10625:             }
10626:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
10627:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
10628:                 my $chgtext;
10629:                 foreach my $type (@{$srchtypeorder}) {
10630:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
10631:                         if (defined($srchtypes_desc->{$type})) {
10632:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
10633:                         }
10634:                     }
10635:                 }
10636:                 $chgtext =~ s/\; $//;
10637:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
10638:             }
10639:             $resulttext .= '</ul>';
10640:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
10641:             if (ref($lastactref) eq 'HASH') {
10642:                 $lastactref->{'directorysrch'} = 1;
10643:             }
10644:         } else {
10645:             $resulttext = &mt('No changes made to directory search settings');
10646:         }
10647:     } else {
10648:         $resulttext = '<span class="LC_error">'.
10649:                       &mt('An error occurred: [_1]',$putresult).'</span>';
10650:     }
10651:     return $resulttext;
10652: }
10653: 
10654: sub modify_contacts {
10655:     my ($dom,$lastactref,%domconfig) = @_;
10656:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
10657:     if (ref($domconfig{'contacts'}) eq 'HASH') {
10658:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
10659:             $currsetting{$key} = $domconfig{'contacts'}{$key};
10660:         }
10661:     }
10662:     my (%others,%to,%bcc,%includestr,%includeloc);
10663:     my @contacts = ('supportemail','adminemail');
10664:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
10665:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail');
10666:     my @toggles = ('reporterrors','reportupdates');
10667:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
10668:     foreach my $type (@mailings) {
10669:         @{$newsetting{$type}} = 
10670:             &Apache::loncommon::get_env_multiple('form.'.$type);
10671:         foreach my $item (@contacts) {
10672:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
10673:                 $contacts_hash{contacts}{$type}{$item} = 1;
10674:             } else {
10675:                 $contacts_hash{contacts}{$type}{$item} = 0;
10676:             }
10677:         }
10678:         $others{$type} = $env{'form.'.$type.'_others'};
10679:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
10680:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10681:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
10682:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
10683:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
10684:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
10685:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
10686:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
10687:             }
10688:         }
10689:     }
10690:     foreach my $item (@contacts) {
10691:         $to{$item} = $env{'form.'.$item};
10692:         $contacts_hash{'contacts'}{$item} = $to{$item};
10693:     }
10694:     foreach my $item (@toggles) {
10695:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
10696:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
10697:         }
10698:     }
10699:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
10700:         foreach my $field (@{$fields}) {
10701:             if (ref($possoptions->{$field}) eq 'ARRAY') {
10702:                 my $value = $env{'form.helpform_'.$field};
10703:                 $value =~ s/^\s+|\s+$//g;
10704:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
10705:                     $contacts_hash{contacts}{'helpform'}{$field} = $value;
10706:                     if ($field eq 'screenshot') {
10707:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
10708:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
10709:                             $contacts_hash{contacts}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
10710:                         }
10711:                     }
10712:                 }
10713:             }
10714:         }
10715:     }
10716:     if (keys(%currsetting) > 0) {
10717:         foreach my $item (@contacts) {
10718:             if ($to{$item} ne $currsetting{$item}) {
10719:                 $changes{$item} = 1;
10720:             }
10721:         }
10722:         foreach my $type (@mailings) {
10723:             foreach my $item (@contacts) {
10724:                 if (ref($currsetting{$type}) eq 'HASH') {
10725:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
10726:                         push(@{$changes{$type}},$item);
10727:                     }
10728:                 } else {
10729:                     push(@{$changes{$type}},@{$newsetting{$type}});
10730:                 }
10731:             }
10732:             if ($others{$type} ne $currsetting{$type}{'others'}) {
10733:                 push(@{$changes{$type}},'others');
10734:             }
10735:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10736:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
10737:                     push(@{$changes{$type}},'bcc'); 
10738:                 }
10739:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
10740:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
10741:                     push(@{$changes{$type}},'include');
10742:                 }
10743:             }
10744:         }
10745:         if (ref($fields) eq 'ARRAY') {
10746:             if (ref($currsetting{'helpform'}) eq 'HASH') {
10747:                 foreach my $field (@{$fields}) {
10748:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
10749:                         push(@{$changes{'helpform'}},$field);
10750:                     }
10751:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10752:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
10753:                             push(@{$changes{'helpform'}},'maxsize');
10754:                         }
10755:                     }
10756:                 }
10757:             } else {
10758:                 foreach my $field (@{$fields}) {
10759:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
10760:                         push(@{$changes{'helpform'}},$field);
10761:                     }
10762:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10763:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
10764:                             push(@{$changes{'helpform'}},'maxsize');
10765:                         }
10766:                     }
10767:                 }
10768:             }
10769:         }
10770:     } else {
10771:         my %default;
10772:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
10773:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
10774:         $default{'errormail'} = 'adminemail';
10775:         $default{'packagesmail'} = 'adminemail';
10776:         $default{'helpdeskmail'} = 'supportemail';
10777:         $default{'otherdomsmail'} = 'supportemail';
10778:         $default{'lonstatusmail'} = 'adminemail';
10779:         $default{'requestsmail'} = 'adminemail';
10780:         $default{'updatesmail'} = 'adminemail';
10781:         foreach my $item (@contacts) {
10782:            if ($to{$item} ne $default{$item}) {
10783:                $changes{$item} = 1;
10784:            }
10785:         }
10786:         foreach my $type (@mailings) {
10787:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
10788:                 push(@{$changes{$type}},@{$newsetting{$type}});
10789:             }
10790:             if ($others{$type} ne '') {
10791:                 push(@{$changes{$type}},'others');
10792:             }
10793:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10794:                 if ($bcc{$type} ne '') {
10795:                     push(@{$changes{$type}},'bcc');
10796:                 }
10797:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
10798:                     push(@{$changes{$type}},'include');
10799:                 }
10800:             }
10801:         }
10802:         if (ref($fields) eq 'ARRAY') {
10803:             foreach my $field (@{$fields}) {
10804:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
10805:                     push(@{$changes{'helpform'}},$field);
10806:                 }
10807:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10808:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
10809:                         push(@{$changes{'helpform'}},'maxsize');
10810:                     }
10811:                 }
10812:             }
10813:         }
10814:     }
10815:     foreach my $item (@toggles) {
10816:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
10817:             $changes{$item} = 1;
10818:         } elsif ((!$env{'form.'.$item}) &&
10819:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
10820:             $changes{$item} = 1;
10821:         }
10822:     }
10823:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
10824:                                              $dom);
10825:     if ($putresult eq 'ok') {
10826:         if (keys(%changes) > 0) {
10827:             &Apache::loncommon::devalidate_domconfig_cache($dom);
10828:             if (ref($lastactref) eq 'HASH') {
10829:                 $lastactref->{'domainconfig'} = 1;
10830:             }
10831:             my ($titles,$short_titles)  = &contact_titles();
10832:             $resulttext = &mt('Changes made:').'<ul>';
10833:             foreach my $item (@contacts) {
10834:                 if ($changes{$item}) {
10835:                     $resulttext .= '<li>'.$titles->{$item}.
10836:                                     &mt(' set to: ').
10837:                                     '<span class="LC_cusr_emph">'.
10838:                                     $to{$item}.'</span></li>';
10839:                 }
10840:             }
10841:             foreach my $type (@mailings) {
10842:                 if (ref($changes{$type}) eq 'ARRAY') {
10843:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10844:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
10845:                     } else {
10846:                         $resulttext .= '<li>'.$titles->{$type}.': ';
10847:                     }
10848:                     my @text;
10849:                     foreach my $item (@{$newsetting{$type}}) {
10850:                         push(@text,$short_titles->{$item});
10851:                     }
10852:                     if ($others{$type} ne '') {
10853:                         push(@text,$others{$type});
10854:                     }
10855:                     if (@text) {
10856:                         $resulttext .= '<span class="LC_cusr_emph">'.
10857:                                        join(', ',@text).'</span>';
10858:                     }
10859:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10860:                         if ($bcc{$type} ne '') {
10861:                             my $bcctext;
10862:                             if (@text) {
10863:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
10864:                             } else {
10865:                                 $bcctext = '(Bcc)';
10866:                             }
10867:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
10868:                         } elsif (!@text) {
10869:                             $resulttext .= &mt('No one');
10870:                         }   
10871:                         if ($includestr{$type} ne '') {
10872:                             if ($includeloc{$type} eq 'b') {
10873:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
10874:                             } elsif ($includeloc{$type} eq 's') {
10875:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
10876:                             }
10877:                         }
10878:                     } elsif (!@text) {
10879:                         $resulttext .= &mt('No recipients');
10880:                     }
10881:                     $resulttext .= '</li>';
10882:                 }
10883:             }
10884:             my @offon = ('off','on');
10885:             if ($changes{'reporterrors'}) {
10886:                 $resulttext .= '<li>'.
10887:                                &mt('E-mail error reports to [_1] set to "'.
10888:                                    $offon[$env{'form.reporterrors'}].'".',
10889:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
10890:                                        &mt('LON-CAPA core group - MSU'),600,500)).
10891:                                '</li>';
10892:             }
10893:             if ($changes{'reportupdates'}) {
10894:                 $resulttext .= '<li>'.
10895:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
10896:                                     $offon[$env{'form.reportupdates'}].'".',
10897:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
10898:                                         &mt('LON-CAPA core group - MSU'),600,500)).
10899:                                 '</li>';
10900:             }
10901:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
10902:                 my (@optional,@required,@unused,$maxsizechg);
10903:                 foreach my $field (@{$changes{'helpform'}}) {
10904:                     if ($field eq 'maxsize') {
10905:                         $maxsizechg = 1;
10906:                         next;
10907:                     }
10908:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
10909:                         push(@optional,$field);
10910:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
10911:                         push(@unused,$field);
10912:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
10913:                         push(@required,$field);
10914:                     }
10915:                 }
10916:                 if (@optional) {
10917:                     $resulttext .= '<li>'.
10918:                                    &mt('Help form fields changed to "Optional": [_1].',
10919:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
10920:                                    '</li>';
10921:                 }
10922:                 if (@required) {
10923:                     $resulttext .= '<li>'.
10924:                                    &mt('Help form fields changed to "Required": [_1].',
10925:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
10926:                                    '</li>';
10927:                 }
10928:                 if (@unused) {
10929:                     $resulttext .= '<li>'.
10930:                                    &mt('Help form fields changed to "Not shown": [_1].',
10931:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
10932:                                    '</li>';
10933:                 }
10934:                 if ($maxsizechg) {
10935:                     $resulttext .= '<li>'.
10936:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
10937:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
10938:                                    '</li>';
10939: 
10940:                 }
10941:             }
10942:             $resulttext .= '</ul>';
10943:         } else {
10944:             $resulttext = &mt('No changes made to contacts and form settings');
10945:         }
10946:     } else {
10947:         $resulttext = '<span class="LC_error">'.
10948:             &mt('An error occurred: [_1].',$putresult).'</span>';
10949:     }
10950:     return $resulttext;
10951: }
10952: 
10953: sub modify_usercreation {
10954:     my ($dom,%domconfig) = @_;
10955:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
10956:     my $warningmsg;
10957:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
10958:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
10959:             if ($key eq 'cancreate') {
10960:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
10961:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
10962:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
10963:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
10964:                             ($item eq 'recaptchaversion')) {
10965:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10966:                         } else {
10967:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10968:                         }
10969:                     }
10970:                 }
10971:             } elsif ($key eq 'email_rule') {
10972:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
10973:             } else {
10974:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
10975:             }
10976:         }
10977:     }
10978:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
10979:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
10980:     my @contexts = ('author','course','requestcrs');
10981:     foreach my $item(@contexts) {
10982:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
10983:     }
10984:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
10985:         foreach my $item (@contexts) {
10986:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
10987:                 push(@{$changes{'cancreate'}},$item);
10988:             }
10989:         }
10990:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
10991:         foreach my $item (@contexts) {
10992:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
10993:                 if ($cancreate{$item} ne 'any') {
10994:                     push(@{$changes{'cancreate'}},$item);
10995:                 }
10996:             } else {
10997:                 if ($cancreate{$item} ne 'none') {
10998:                     push(@{$changes{'cancreate'}},$item);
10999:                 }
11000:             }
11001:         }
11002:     } else {
11003:         foreach my $item (@contexts)  {
11004:             push(@{$changes{'cancreate'}},$item);
11005:         }
11006:     }
11007: 
11008:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
11009:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
11010:             if (!grep(/^\Q$type\E$/,@username_rule)) {
11011:                 push(@{$changes{'username_rule'}},$type);
11012:             }
11013:         }
11014:         foreach my $type (@username_rule) {
11015:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
11016:                 push(@{$changes{'username_rule'}},$type);
11017:             }
11018:         }
11019:     } else {
11020:         push(@{$changes{'username_rule'}},@username_rule);
11021:     }
11022: 
11023:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
11024:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
11025:             if (!grep(/^\Q$type\E$/,@id_rule)) {
11026:                 push(@{$changes{'id_rule'}},$type);
11027:             }
11028:         }
11029:         foreach my $type (@id_rule) {
11030:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
11031:                 push(@{$changes{'id_rule'}},$type);
11032:             }
11033:         }
11034:     } else {
11035:         push(@{$changes{'id_rule'}},@id_rule);
11036:     }
11037: 
11038:     my @authen_contexts = ('author','course','domain');
11039:     my @authtypes = ('int','krb4','krb5','loc');
11040:     my %authhash;
11041:     foreach my $item (@authen_contexts) {
11042:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
11043:         foreach my $auth (@authtypes) {
11044:             if (grep(/^\Q$auth\E$/,@authallowed)) {
11045:                 $authhash{$item}{$auth} = 1;
11046:             } else {
11047:                 $authhash{$item}{$auth} = 0;
11048:             }
11049:         }
11050:     }
11051:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
11052:         foreach my $item (@authen_contexts) {
11053:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
11054:                 foreach my $auth (@authtypes) {
11055:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
11056:                         push(@{$changes{'authtypes'}},$item);
11057:                         last;
11058:                     }
11059:                 }
11060:             }
11061:         }
11062:     } else {
11063:         foreach my $item (@authen_contexts) {
11064:             push(@{$changes{'authtypes'}},$item);
11065:         }
11066:     }
11067: 
11068:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
11069:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
11070:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
11071:     $save_usercreate{'id_rule'} = \@id_rule;
11072:     $save_usercreate{'username_rule'} = \@username_rule,
11073:     $save_usercreate{'authtypes'} = \%authhash;
11074: 
11075:     my %usercreation_hash =  (
11076:         usercreation     => \%save_usercreate,
11077:     );
11078: 
11079:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
11080:                                              $dom);
11081: 
11082:     if ($putresult eq 'ok') {
11083:         if (keys(%changes) > 0) {
11084:             $resulttext = &mt('Changes made:').'<ul>';
11085:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
11086:                 my %lt = &usercreation_types();
11087:                 foreach my $type (@{$changes{'cancreate'}}) {
11088:                     my $chgtext = $lt{$type}.', ';
11089:                     if ($cancreate{$type} eq 'none') {
11090:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
11091:                     } elsif ($cancreate{$type} eq 'any') {
11092:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
11093:                     } elsif ($cancreate{$type} eq 'official') {
11094:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
11095:                     } elsif ($cancreate{$type} eq 'unofficial') {
11096:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
11097:                     }
11098:                     $resulttext .= '<li>'.$chgtext.'</li>';
11099:                 }
11100:             }
11101:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
11102:                 my ($rules,$ruleorder) = 
11103:                     &Apache::lonnet::inst_userrules($dom,'username');
11104:                 my $chgtext = '<ul>';
11105:                 foreach my $type (@username_rule) {
11106:                     if (ref($rules->{$type}) eq 'HASH') {
11107:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
11108:                     }
11109:                 }
11110:                 $chgtext .= '</ul>';
11111:                 if (@username_rule > 0) {
11112:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
11113:                 } else {
11114:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
11115:                 }
11116:             }
11117:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
11118:                 my ($idrules,$idruleorder) = 
11119:                     &Apache::lonnet::inst_userrules($dom,'id');
11120:                 my $chgtext = '<ul>';
11121:                 foreach my $type (@id_rule) {
11122:                     if (ref($idrules->{$type}) eq 'HASH') {
11123:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
11124:                     }
11125:                 }
11126:                 $chgtext .= '</ul>';
11127:                 if (@id_rule > 0) {
11128:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
11129:                 } else {
11130:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
11131:                 }
11132:             }
11133:             my %authname = &authtype_names();
11134:             my %context_title = &context_names();
11135:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
11136:                 my $chgtext = '<ul>';
11137:                 foreach my $type (@{$changes{'authtypes'}}) {
11138:                     my @allowed;
11139:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
11140:                     foreach my $auth (@authtypes) {
11141:                         if ($authhash{$type}{$auth}) {
11142:                             push(@allowed,$authname{$auth});
11143:                         }
11144:                     }
11145:                     if (@allowed > 0) {
11146:                         $chgtext .= join(', ',@allowed).'</li>';
11147:                     } else {
11148:                         $chgtext .= &mt('none').'</li>';
11149:                     }
11150:                 }
11151:                 $chgtext .= '</ul>';
11152:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
11153:                 $resulttext .= '</li>';
11154:             }
11155:             $resulttext .= '</ul>';
11156:         } else {
11157:             $resulttext = &mt('No changes made to user creation settings');
11158:         }
11159:     } else {
11160:         $resulttext = '<span class="LC_error">'.
11161:             &mt('An error occurred: [_1]',$putresult).'</span>';
11162:     }
11163:     if ($warningmsg ne '') {
11164:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
11165:     }
11166:     return $resulttext;
11167: }
11168: 
11169: sub modify_selfcreation {
11170:     my ($dom,%domconfig) = @_;
11171:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
11172:     my (%save_usercreate,%save_usermodify);
11173:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11174:     if (ref($types) eq 'ARRAY') {
11175:         $usertypes->{'default'} = $othertitle;
11176:         push(@{$types},'default');
11177:     }
11178: #
11179: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
11180: #
11181:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
11182:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
11183:             if ($key eq 'cancreate') {
11184:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
11185:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
11186:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
11187:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
11188:                             ($item eq 'recaptchaversion') ||
11189:                             ($item eq 'emailusername') || ($item eq 'notify') ||
11190:                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
11191:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11192:                         } else {
11193:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11194:                         }
11195:                     }
11196:                 }
11197:             } elsif ($key eq 'email_rule') {
11198:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
11199:             } else {
11200:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
11201:             }
11202:         }
11203:     }
11204: #
11205: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
11206: #
11207:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
11208:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
11209:             if ($key eq 'selfcreate') {
11210:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
11211:             } else {
11212:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
11213:             }
11214:         }
11215:     }
11216: 
11217:     my @contexts = ('selfcreate');
11218:     @{$cancreate{'selfcreate'}} = ();
11219:     %{$cancreate{'emailusername'}} = ();
11220:     @{$cancreate{'statustocreate'}} = ();
11221:     %{$cancreate{'selfcreateprocessing'}} = ();
11222:     %{$cancreate{'shibenv'}} = ();
11223:     my %selfcreatetypes = (
11224:                              sso   => 'users authenticated by institutional single sign on',
11225:                              login => 'users authenticated by institutional log-in',
11226:                              email => 'users who provide a valid e-mail address for use as username',
11227:                           );
11228: #
11229: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
11230: # is permitted.
11231: #
11232: 
11233:     my @statuses;
11234:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
11235:         if (ref($domconfig{'inststatus'}{'inststatusguest'}) eq 'ARRAY') {
11236:             @statuses = @{$domconfig{'inststatus'}{'inststatusguest'}};
11237:         }
11238:     }
11239:     push(@statuses,'default');
11240: 
11241:     foreach my $item ('login','sso','email') {
11242:         if ($item eq 'email') {
11243:             if ($env{'form.cancreate_email'}) {
11244:                 push(@{$cancreate{'selfcreate'}},'email');
11245:                 push(@contexts,'selfcreateprocessing');
11246:                 foreach my $type (@statuses) {
11247:                     if ($type eq 'default') {
11248:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess'};
11249:                     } else { 
11250:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
11251:                     }
11252:                 }
11253:             }
11254:         } else {
11255:             if ($env{'form.cancreate_'.$item}) {
11256:                 push(@{$cancreate{'selfcreate'}},$item);
11257:             }
11258:         }
11259:     }
11260:     my (@email_rule,%userinfo,%savecaptcha);
11261:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
11262: #
11263: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
11264: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
11265: #
11266: 
11267:     if ($env{'form.cancreate_email'}) {
11268:         push(@contexts,'emailusername');
11269:         if (ref($types) eq 'ARRAY') {
11270:             foreach my $type (@{$types}) {
11271:                 if (ref($infofields) eq 'ARRAY') {
11272:                     foreach my $field (@{$infofields}) {
11273:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
11274:                             $cancreate{'emailusername'}{$type}{$field} = $1;
11275:                         }
11276:                     }
11277:                 }
11278:             }
11279:         }
11280: #
11281: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
11282: # queued requests for self-creation of account using e-mail address as username
11283: #
11284: 
11285:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
11286:         @approvalnotify = sort(@approvalnotify);
11287:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
11288:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11289:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
11290:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
11291:                     push(@{$changes{'cancreate'}},'notify');
11292:                 }
11293:             } else {
11294:                 if ($cancreate{'notify'}{'approval'}) {
11295:                     push(@{$changes{'cancreate'}},'notify');
11296:                 }
11297:             }
11298:         } elsif ($cancreate{'notify'}{'approval'}) {
11299:             push(@{$changes{'cancreate'}},'notify');
11300:         }
11301: 
11302: #
11303: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
11304: #
11305:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
11306:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
11307:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
11308:             if (@{$curr_usercreation{'email_rule'}} > 0) {
11309:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
11310:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
11311:                         push(@{$changes{'email_rule'}},$type);
11312:                     }
11313:                 }
11314:             }
11315:             if (@email_rule > 0) {
11316:                 foreach my $type (@email_rule) {
11317:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
11318:                         push(@{$changes{'email_rule'}},$type);
11319:                     }
11320:                 }
11321:             }
11322:         } elsif (@email_rule > 0) {
11323:             push(@{$changes{'email_rule'}},@email_rule);
11324:         }
11325:     }
11326: #  
11327: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
11328: # institutional log-in.
11329: #
11330:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
11331:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11332:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
11333:                ($domdefaults{'auth_def'} eq 'localauth'))) {
11334:             $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.').' '.
11335:                           &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.');
11336:         }
11337:     }
11338:     my @fields = ('lastname','firstname','middlename','generation',
11339:                   'permanentemail','id');
11340:     my @shibfields = (@fields,'inststatus');
11341:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11342: #
11343: # Where usernames may created for institutional log-in and/or institutional single sign on:
11344: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
11345: # may self-create accounts 
11346: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
11347: # which the user may supply, if institutional data is unavailable.
11348: #
11349:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
11350:         if (ref($types) eq 'ARRAY') {
11351:             if (@{$types} > 1) {
11352:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
11353:                 push(@contexts,'statustocreate');
11354:             } else {
11355:                 undef($cancreate{'statustocreate'});
11356:             } 
11357:             foreach my $type (@{$types}) {
11358:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
11359:                 foreach my $field (@fields) {
11360:                     if (grep(/^\Q$field\E$/,@modifiable)) {
11361:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
11362:                     } else {
11363:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
11364:                     }
11365:                 }
11366:             }
11367:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
11368:                 foreach my $type (@{$types}) {
11369:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
11370:                         foreach my $field (@fields) {
11371:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
11372:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
11373:                                 push(@{$changes{'selfcreate'}},$type);
11374:                                 last;
11375:                             }
11376:                         }
11377:                     }
11378:                 }
11379:             } else {
11380:                 foreach my $type (@{$types}) {
11381:                     push(@{$changes{'selfcreate'}},$type);
11382:                 }
11383:             }
11384:         }
11385:         foreach my $field (@shibfields) {
11386:             if ($env{'form.shibenv_'.$field} ne '') {
11387:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
11388:             }
11389:         }
11390:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11391:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
11392:                 foreach my $field (@shibfields) {
11393:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
11394:                         push(@{$changes{'cancreate'}},'shibenv');
11395:                     }
11396:                 }
11397:             } else {
11398:                 foreach my $field (@shibfields) {
11399:                     if ($env{'form.shibenv_'.$field}) {
11400:                         push(@{$changes{'cancreate'}},'shibenv');
11401:                         last;
11402:                     }
11403:                 }
11404:             }
11405:         }
11406:     }
11407:     foreach my $item (@contexts) {
11408:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
11409:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
11410:                 if (ref($cancreate{$item}) eq 'ARRAY') {
11411:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
11412:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11413:                             push(@{$changes{'cancreate'}},$item);
11414:                         }
11415:                     }
11416:                 }
11417:             }
11418:             if (ref($cancreate{$item}) eq 'ARRAY') {
11419:                 foreach my $type (@{$cancreate{$item}}) {
11420:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
11421:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11422:                             push(@{$changes{'cancreate'}},$item);
11423:                         }
11424:                     }
11425:                 }
11426:             }
11427:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
11428:             if (ref($cancreate{$item}) eq 'HASH') {
11429:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
11430:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
11431:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
11432:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
11433:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11434:                                     push(@{$changes{'cancreate'}},$item);
11435:                                 }
11436:                             }
11437:                         }
11438:                     } elsif ($item eq 'selfcreateprocessing') {
11439:                         if ($cancreate{$item}{$curr} ne $curr_usercreation{'cancreate'}{$item}{$curr}) {
11440:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11441:                                 push(@{$changes{'cancreate'}},$item);
11442:                             }
11443:                         }
11444:                     } else {
11445:                         if (!$cancreate{$item}{$curr}) {
11446:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11447:                                 push(@{$changes{'cancreate'}},$item);
11448:                             }
11449:                         }
11450:                     }
11451:                 }
11452:                 foreach my $field (keys(%{$cancreate{$item}})) {
11453:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
11454:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
11455:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
11456:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
11457:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11458:                                         push(@{$changes{'cancreate'}},$item);
11459:                                     }
11460:                                 }
11461:                             } else {
11462:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11463:                                     push(@{$changes{'cancreate'}},$item);
11464:                                 }
11465:                             }
11466:                         }
11467:                     } elsif ($item eq 'selfcreateprocessing') {
11468:                         if ($cancreate{$item}{$field} ne $curr_usercreation{'cancreate'}{$item}{$field}) {
11469:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11470:                                 push(@{$changes{'cancreate'}},$item);
11471:                             }
11472:                         }
11473:                     } else {
11474:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
11475:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11476:                                 push(@{$changes{'cancreate'}},$item);
11477:                             }
11478:                         }
11479:                     }
11480:                 }
11481:             }
11482:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
11483:             if (ref($cancreate{$item}) eq 'ARRAY') {
11484:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
11485:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11486:                         push(@{$changes{'cancreate'}},$item);
11487:                     }
11488:                 }
11489:             } elsif (ref($cancreate{$item}) eq 'HASH') {
11490:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
11491:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11492:                         push(@{$changes{'cancreate'}},$item);
11493:                     }
11494:                 }
11495:             }
11496:         } elsif ($item eq 'emailusername') {
11497:             if (ref($cancreate{$item}) eq 'HASH') {
11498:                 foreach my $type (keys(%{$cancreate{$item}})) {
11499:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
11500:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
11501:                             if ($cancreate{$item}{$type}{$field}) {
11502:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11503:                                     push(@{$changes{'cancreate'}},$item);
11504:                                 }
11505:                                 last;
11506:                             }
11507:                         }
11508:                     }
11509:                 }
11510:             }
11511:         }
11512:     }
11513: #
11514: # Populate %save_usercreate hash with updates to self-creation configuration.
11515: #
11516:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
11517:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
11518:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
11519:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
11520:     if (ref($cancreate{'notify'}) eq 'HASH') {
11521:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
11522:     }
11523:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
11524:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
11525:     }
11526:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
11527:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
11528:     }
11529:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
11530:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
11531:     }
11532:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
11533:     $save_usercreate{'emailrule'} = \@email_rule;
11534: 
11535:     my %userconfig_hash = (
11536:             usercreation     => \%save_usercreate,
11537:             usermodification => \%save_usermodify,
11538:     );
11539:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
11540:                                              $dom);
11541: #
11542: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
11543: #
11544:     if ($putresult eq 'ok') {
11545:         if (keys(%changes) > 0) {
11546:             $resulttext = &mt('Changes made:').'<ul>';
11547:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
11548:                 my %lt = &selfcreation_types();
11549:                 foreach my $type (@{$changes{'cancreate'}}) {
11550:                     my $chgtext;
11551:                     if ($type eq 'selfcreate') {
11552:                         if (@{$cancreate{$type}} == 0) {
11553:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
11554:                         } else {
11555:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
11556:                                         '<ul>';
11557:                             foreach my $case (@{$cancreate{$type}}) {
11558:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
11559:                             }
11560:                             $chgtext .= '</ul>';
11561:                             if (ref($cancreate{$type}) eq 'ARRAY') {
11562:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
11563:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
11564:                                         if (@{$cancreate{'statustocreate'}} == 0) {
11565:                                             $chgtext .= '<br />'.
11566:                                                         '<span class="LC_warning">'.
11567:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
11568:                                                         '</span>';
11569:                                         }
11570:                                     }
11571:                                 }
11572:                             }
11573:                         }
11574:                     } elsif ($type eq 'shibenv') {
11575:                         if (keys(%{$cancreate{$type}}) == 0) {
11576:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information'); 
11577:                         } else {
11578:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
11579:                                         '<ul>';
11580:                             foreach my $field (@shibfields) {
11581:                                 next if ($cancreate{$type}{$field} eq '');
11582:                                 if ($field eq 'inststatus') {
11583:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
11584:                                 } else {
11585:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
11586:                                 }
11587:                             }
11588:                             $chgtext .= '</ul>';
11589:                         }  
11590:                     } elsif ($type eq 'statustocreate') {
11591:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
11592:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
11593:                             if (@{$cancreate{'selfcreate'}} > 0) {
11594:                                 if (@{$cancreate{'statustocreate'}} == 0) {
11595:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
11596:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
11597:                                         $chgtext .= '<br />'.
11598:                                                     '<span class="LC_warning">'.
11599:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
11600:                                                     '</span>';
11601:                                     }
11602:                                 } elsif (ref($usertypes) eq 'HASH') {
11603:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
11604:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
11605:                                     } else {
11606:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
11607:                                     }
11608:                                     $chgtext .= '<ul>';
11609:                                     foreach my $case (@{$cancreate{$type}}) {
11610:                                         if ($case eq 'default') {
11611:                                             $chgtext .= '<li>'.$othertitle.'</li>';
11612:                                         } else {
11613:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
11614:                                         }
11615:                                     }
11616:                                     $chgtext .= '</ul>';
11617:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
11618:                                         $chgtext .= '<br /><span class="LC_warning">'.
11619:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
11620:                                                     '</span>';
11621:                                     }
11622:                                 }
11623:                             } else {
11624:                                 if (@{$cancreate{$type}} == 0) {
11625:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
11626:                                 } else {
11627:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
11628:                                 }
11629:                             }
11630:                         }
11631:                     } elsif ($type eq 'selfcreateprocessing') {
11632:                         my %choices = &Apache::lonlocal::texthash (
11633:                                                                     automatic => 'Automatic approval',
11634:                                                                     approval  => 'Queued for approval',
11635:                                                                   );
11636:                         if (@statuses > 1) {
11637:                             $chgtext .= &mt('Processing of requests to create account with e-mail address as username set as follows:'). 
11638:                                         '<ul>';
11639:                            foreach my $type (@statuses) {
11640:                                if ($type eq 'default') {
11641:                                    $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
11642:                                } else {
11643:                                    $chgtext .= '<li>'.$usertypes->{$type}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
11644:                                }
11645:                            }
11646:                            $chgtext .= '</ul>';
11647:                         } else {
11648:                            $chgtext .= &mt('Processing of requests to create account with e-mail address as username set to: "[_1]"',
11649:                                          $choices{$cancreate{'selfcreateprocessing'}{'default'}});
11650:                         }
11651:                     } elsif ($type eq 'captcha') {
11652:                         if ($savecaptcha{$type} eq 'notused') {
11653:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
11654:                         } else {
11655:                             my %captchas = &captcha_phrases();
11656:                             if ($captchas{$savecaptcha{$type}}) {
11657:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
11658:                             } else {
11659:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
11660:                             }
11661:                         }
11662:                     } elsif ($type eq 'recaptchakeys') {
11663:                         my ($privkey,$pubkey);
11664:                         if (ref($savecaptcha{$type}) eq 'HASH') {
11665:                             $pubkey = $savecaptcha{$type}{'public'};
11666:                             $privkey = $savecaptcha{$type}{'private'};
11667:                         }
11668:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
11669:                         if (!$pubkey) {
11670:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
11671:                         } else {
11672:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
11673:                         }
11674:                         if (!$privkey) {
11675:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
11676:                         } else {
11677:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
11678:                         }
11679:                         $chgtext .= '</ul>';
11680:                     } elsif ($type eq 'recaptchaversion') {
11681:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
11682:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
11683:                         }
11684:                     } elsif ($type eq 'emailusername') {
11685:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
11686:                             if (ref($types) eq 'ARRAY') {
11687:                                 foreach my $type (@{$types}) {
11688:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
11689:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
11690:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',"'$usertypes->{$type}'").
11691:                                                     '<ul>';
11692:                                             foreach my $field (@{$infofields}) {
11693:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
11694:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
11695:                                                 }
11696:                                             }
11697:                                             $chgtext .= '</ul>';
11698:                                         } else {
11699:                                             $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 />';
11700:                                         }
11701:                                     } else {
11702:                                         $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 />';
11703:                                     }
11704:                                 }
11705:                             }
11706:                         }
11707:                     } elsif ($type eq 'notify') {
11708:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
11709:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
11710:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
11711:                                 if ($cancreate{'notify'}{'approval'}) {
11712:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
11713:                                 }
11714:                             }
11715:                         }
11716:                     }
11717:                     if ($chgtext) {
11718:                         $resulttext .= '<li>'.$chgtext.'</li>';
11719:                     }
11720:                 }
11721:             }
11722:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
11723:                 my ($emailrules,$emailruleorder) =
11724:                     &Apache::lonnet::inst_userrules($dom,'email');
11725:                 my $chgtext = '<ul>';
11726:                 foreach my $type (@email_rule) {
11727:                     if (ref($emailrules->{$type}) eq 'HASH') {
11728:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
11729:                     }
11730:                 }
11731:                 $chgtext .= '</ul>';
11732:                 if (@email_rule > 0) {
11733:                     $resulttext .= '<li>'.
11734:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
11735:                                        $chgtext.
11736:                                    '</li>';
11737:                 } else {
11738:                     $resulttext .= '<li>'.
11739:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
11740:                                    '</li>';
11741:                 }
11742:             }
11743:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
11744:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
11745:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11746:                 foreach my $type (@{$changes{'selfcreate'}}) {
11747:                     my $typename = $type;
11748:                     if (ref($usertypes) eq 'HASH') {
11749:                         if ($usertypes->{$type} ne '') {
11750:                             $typename = $usertypes->{$type};
11751:                         }
11752:                     }
11753:                     my @modifiable;
11754:                     $resulttext .= '<li>'.
11755:                                     &mt('Self-creation of account by users with status: [_1]',
11756:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
11757:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
11758:                     foreach my $field (@fields) {
11759:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
11760:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
11761:                         }
11762:                     }
11763:                     if (@modifiable > 0) {
11764:                         $resulttext .= join(', ',@modifiable);
11765:                     } else {
11766:                         $resulttext .= &mt('none');
11767:                     }
11768:                     $resulttext .= '</li>';
11769:                 }
11770:                 $resulttext .= '</ul></li>';
11771:             }
11772:             $resulttext .= '</ul>';
11773:         } else {
11774:             $resulttext = &mt('No changes made to self-creation settings');
11775:         }
11776:     } else {
11777:         $resulttext = '<span class="LC_error">'.
11778:             &mt('An error occurred: [_1]',$putresult).'</span>';
11779:     }
11780:     if ($warningmsg ne '') {
11781:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
11782:     }
11783:     return $resulttext;
11784: }
11785: 
11786: sub process_captcha {
11787:     my ($container,$changes,$newsettings,$current) = @_;
11788:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
11789:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
11790:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
11791:         $newsettings->{'captcha'} = 'original';
11792:     }
11793:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
11794:         if ($container eq 'cancreate') {
11795:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11796:                 push(@{$changes->{'cancreate'}},'captcha');
11797:             } elsif (!defined($changes->{'cancreate'})) {
11798:                 $changes->{'cancreate'} = ['captcha'];
11799:             }
11800:         } else {
11801:             $changes->{'captcha'} = 1;
11802:         }
11803:     }
11804:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
11805:     if ($newsettings->{'captcha'} eq 'recaptcha') {
11806:         $newpub = $env{'form.'.$container.'_recaptchapub'};
11807:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
11808:         $newpub =~ s/[^\w\-]//g;
11809:         $newpriv =~ s/[^\w\-]//g;
11810:         $newsettings->{'recaptchakeys'} = {
11811:                                              public  => $newpub,
11812:                                              private => $newpriv,
11813:                                           };
11814:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
11815:         $newversion =~ s/\D//g;
11816:         if ($newversion ne '2') {
11817:             $newversion = 1;
11818:         }
11819:         $newsettings->{'recaptchaversion'} = $newversion;
11820:     }
11821:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
11822:         $currpub = $current->{'recaptchakeys'}{'public'};
11823:         $currpriv = $current->{'recaptchakeys'}{'private'};
11824:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
11825:             $newsettings->{'recaptchakeys'} = {
11826:                                                  public  => '',
11827:                                                  private => '',
11828:                                               }
11829:         }
11830:     }
11831:     if ($current->{'captcha'} eq 'recaptcha') {
11832:         $currversion = $current->{'recaptchaversion'};
11833:         if ($currversion ne '2') {
11834:             $currversion = 1;
11835:         }
11836:     }
11837:     if ($currversion ne $newversion) {
11838:         if ($container eq 'cancreate') {
11839:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11840:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
11841:             } elsif (!defined($changes->{'cancreate'})) {
11842:                 $changes->{'cancreate'} = ['recaptchaversion'];
11843:             }
11844:         } else {
11845:             $changes->{'recaptchaversion'} = 1;
11846:         }
11847:     }
11848:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
11849:         if ($container eq 'cancreate') {
11850:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11851:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
11852:             } elsif (!defined($changes->{'cancreate'})) {
11853:                 $changes->{'cancreate'} = ['recaptchakeys'];
11854:             }
11855:         } else {
11856:             $changes->{'recaptchakeys'} = 1;
11857:         }
11858:     }
11859:     return;
11860: }
11861: 
11862: sub modify_usermodification {
11863:     my ($dom,%domconfig) = @_;
11864:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
11865:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
11866:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
11867:             if ($key eq 'selfcreate') {
11868:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
11869:             } else {  
11870:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
11871:             }
11872:         }
11873:     }
11874:     my @contexts = ('author','course');
11875:     my %context_title = (
11876:                            author => 'In author context',
11877:                            course => 'In course context',
11878:                         );
11879:     my @fields = ('lastname','firstname','middlename','generation',
11880:                   'permanentemail','id');
11881:     my %roles = (
11882:                   author => ['ca','aa'],
11883:                   course => ['st','ep','ta','in','cr'],
11884:                 );
11885:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11886:     foreach my $context (@contexts) {
11887:         foreach my $role (@{$roles{$context}}) {
11888:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
11889:             foreach my $item (@fields) {
11890:                 if (grep(/^\Q$item\E$/,@modifiable)) {
11891:                     $modifyhash{$context}{$role}{$item} = 1;
11892:                 } else {
11893:                     $modifyhash{$context}{$role}{$item} = 0;
11894:                 }
11895:             }
11896:         }
11897:         if (ref($curr_usermodification{$context}) eq 'HASH') {
11898:             foreach my $role (@{$roles{$context}}) {
11899:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
11900:                     foreach my $field (@fields) {
11901:                         if ($modifyhash{$context}{$role}{$field} ne 
11902:                                 $curr_usermodification{$context}{$role}{$field}) {
11903:                             push(@{$changes{$context}},$role);
11904:                             last;
11905:                         }
11906:                     }
11907:                 }
11908:             }
11909:         } else {
11910:             foreach my $context (@contexts) {
11911:                 foreach my $role (@{$roles{$context}}) {
11912:                     push(@{$changes{$context}},$role);
11913:                 }
11914:             }
11915:         }
11916:     }
11917:     my %usermodification_hash =  (
11918:                                    usermodification => \%modifyhash,
11919:                                  );
11920:     my $putresult = &Apache::lonnet::put_dom('configuration',
11921:                                              \%usermodification_hash,$dom);
11922:     if ($putresult eq 'ok') {
11923:         if (keys(%changes) > 0) {
11924:             $resulttext = &mt('Changes made: ').'<ul>';
11925:             foreach my $context (@contexts) {
11926:                 if (ref($changes{$context}) eq 'ARRAY') {
11927:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
11928:                     if (ref($changes{$context}) eq 'ARRAY') {
11929:                         foreach my $role (@{$changes{$context}}) {
11930:                             my $rolename;
11931:                             if ($role eq 'cr') {
11932:                                 $rolename = &mt('Custom');
11933:                             } else {
11934:                                 $rolename = &Apache::lonnet::plaintext($role);
11935:                             }
11936:                             my @modifiable;
11937:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
11938:                             foreach my $field (@fields) {
11939:                                 if ($modifyhash{$context}{$role}{$field}) {
11940:                                     push(@modifiable,$fieldtitles{$field});
11941:                                 }
11942:                             }
11943:                             if (@modifiable > 0) {
11944:                                 $resulttext .= join(', ',@modifiable);
11945:                             } else {
11946:                                 $resulttext .= &mt('none'); 
11947:                             }
11948:                             $resulttext .= '</li>';
11949:                         }
11950:                         $resulttext .= '</ul></li>';
11951:                     }
11952:                 }
11953:             }
11954:             $resulttext .= '</ul>';
11955:         } else {
11956:             $resulttext = &mt('No changes made to user modification settings');
11957:         }
11958:     } else {
11959:         $resulttext = '<span class="LC_error">'.
11960:             &mt('An error occurred: [_1]',$putresult).'</span>';
11961:     }
11962:     return $resulttext;
11963: }
11964: 
11965: sub modify_defaults {
11966:     my ($dom,$lastactref,%domconfig) = @_;
11967:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
11968:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11969:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
11970:                  'portal_def','intauth_cost','intauth_check','intauth_switch');
11971:     my @authtypes = ('internal','krb4','krb5','localauth');
11972:     foreach my $item (@items) {
11973:         $newvalues{$item} = $env{'form.'.$item};
11974:         if ($item eq 'auth_def') {
11975:             if ($newvalues{$item} ne '') {
11976:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
11977:                     push(@errors,$item);
11978:                 }
11979:             }
11980:         } elsif ($item eq 'lang_def') {
11981:             if ($newvalues{$item} ne '') {
11982:                 if ($newvalues{$item} =~ /^(\w+)/) {
11983:                     my $langcode = $1;
11984:                     if ($langcode ne 'x_chef') {
11985:                         if (code2language($langcode) eq '') {
11986:                             push(@errors,$item);
11987:                         }
11988:                     }
11989:                 } else {
11990:                     push(@errors,$item);
11991:                 }
11992:             }
11993:         } elsif ($item eq 'timezone_def') {
11994:             if ($newvalues{$item} ne '') {
11995:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
11996:                     push(@errors,$item);   
11997:                 }
11998:             }
11999:         } elsif ($item eq 'datelocale_def') {
12000:             if ($newvalues{$item} ne '') {
12001:                 my @datelocale_ids = DateTime::Locale->ids();
12002:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
12003:                     push(@errors,$item);
12004:                 }
12005:             }
12006:         } elsif ($item eq 'portal_def') {
12007:             if ($newvalues{$item} ne '') {
12008:                 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])\/?$/) {
12009:                     push(@errors,$item);
12010:                 }
12011:             }
12012:         } elsif ($item eq 'intauth_cost') {
12013:             if ($newvalues{$item} ne '') {
12014:                 if ($newvalues{$item} =~ /\D/) {
12015:                     push(@errors,$item);
12016:                 }
12017:             }
12018:         } elsif ($item eq 'intauth_check') {
12019:             if ($newvalues{$item} ne '') {
12020:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
12021:                     push(@errors,$item);
12022:                 }
12023:             }
12024:         } elsif ($item eq 'intauth_switch') {
12025:             if ($newvalues{$item} ne '') {
12026:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
12027:                     push(@errors,$item);
12028:                 }
12029:             }
12030:         }
12031:         if (grep(/^\Q$item\E$/,@errors)) {
12032:             $newvalues{$item} = $domdefaults{$item};
12033:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
12034:             $changes{$item} = 1;
12035:         }
12036:         $domdefaults{$item} = $newvalues{$item};
12037:     }
12038:     my %defaults_hash = (
12039:                          defaults => \%newvalues,
12040:                         );
12041:     my $title = &defaults_titles();
12042: 
12043:     my $currinststatus;
12044:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
12045:         $currinststatus = $domconfig{'inststatus'};
12046:     } else {
12047:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12048:         $currinststatus = {
12049:                              inststatustypes => $usertypes,
12050:                              inststatusorder => $types,
12051:                              inststatusguest => [],
12052:                           };
12053:     }
12054:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
12055:     my @allpos;
12056:     my %guests;
12057:     my %alltypes;
12058:     my ($currtitles,$currguests,$currorder);
12059:     if (ref($currinststatus) eq 'HASH') {
12060:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
12061:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
12062:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
12063:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
12064:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
12065:                     }
12066:                 }
12067:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
12068:                     my $position = $env{'form.inststatus_pos_'.$type};
12069:                     $position =~ s/\D+//g;
12070:                     $allpos[$position] = $type;
12071:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
12072:                     $alltypes{$type} =~ s/`//g;
12073:                     if ($env{'form.inststatus_guest_'.$type}) {
12074:                         $guests{$type} = 1;
12075:                     }
12076:                 }
12077:             }
12078:             if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
12079:                 $currguests = join(',',@{$currinststatus->{'inststatusguest'}});
12080:             }
12081:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
12082:             $currtitles =~ s/,$//;
12083:         }
12084:     }
12085:     if ($env{'form.addinststatus'}) {
12086:         my $newtype = $env{'form.addinststatus'};
12087:         $newtype =~ s/\W//g;
12088:         unless (exists($alltypes{$newtype})) {
12089:             if ($env{'form.addinststatus_guest'}) {
12090:                 $guests{$newtype} = 1;
12091:             }
12092:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
12093:             $alltypes{$newtype} =~ s/`//g; 
12094:             my $position = $env{'form.addinststatus_pos'};
12095:             $position =~ s/\D+//g;
12096:             if ($position ne '') {
12097:                 $allpos[$position] = $newtype;
12098:             }
12099:         }
12100:     }
12101:     my (@orderedstatus,@orderedguests);
12102:     foreach my $type (@allpos) {
12103:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
12104:             push(@orderedstatus,$type);
12105:             if ($guests{$type}) {
12106:                 push(@orderedguests,$type);
12107:             }
12108:         }
12109:     }
12110:     foreach my $type (keys(%alltypes)) {
12111:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
12112:             delete($alltypes{$type});
12113:         }
12114:     }
12115:     $defaults_hash{'inststatus'} = {
12116:                                      inststatustypes => \%alltypes,
12117:                                      inststatusorder => \@orderedstatus,
12118:                                      inststatusguest => \@orderedguests,
12119:                                    };
12120:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
12121:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
12122:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
12123:         }
12124:     }
12125:     if ($currorder ne join(',',@orderedstatus)) {
12126:         $changes{'inststatus'}{'inststatusorder'} = 1;
12127:     }
12128:     if ($currguests ne join(',',@orderedguests)) {
12129:         $changes{'inststatus'}{'inststatusguest'} = 1;
12130:     }
12131:     my $newtitles;
12132:     foreach my $item (@orderedstatus) {
12133:         $newtitles .= $alltypes{$item}.',';
12134:     }
12135:     $newtitles =~ s/,$//;
12136:     if ($currtitles ne $newtitles) {
12137:         $changes{'inststatus'}{'inststatustypes'} = 1;
12138:     }
12139:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
12140:                                              $dom);
12141:     if ($putresult eq 'ok') {
12142:         if (keys(%changes) > 0) {
12143:             $resulttext = &mt('Changes made:').'<ul>';
12144:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
12145:             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";
12146:             foreach my $item (sort(keys(%changes))) {
12147:                 if ($item eq 'inststatus') {
12148:                     if (ref($changes{'inststatus'}) eq 'HASH') {
12149:                         if (($changes{'inststatus'}{'inststatustypes'}) || $changes{'inststatus'}{'inststatusorder'}) {
12150:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
12151:                             foreach my $type (@orderedstatus) { 
12152:                                 $resulttext .= $alltypes{$type}.', ';
12153:                             }
12154:                             $resulttext =~ s/, $//;
12155:                             $resulttext .= '</li>';
12156:                         }
12157:                         if ($changes{'inststatus'}{'inststatusguest'}) {
12158:                             $resulttext .= '<li>'; 
12159:                             if (@orderedguests) {
12160:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to:').' ';
12161:                                 foreach my $type (@orderedguests) {
12162:                                     $resulttext .= $alltypes{$type}.', ';
12163:                                 }
12164:                                 $resulttext =~ s/, $//;
12165:                             } else {
12166:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to none.');
12167:                             }
12168:                             $resulttext .= '</li>';
12169:                         }
12170:                     }
12171:                 } else {
12172:                     my $value = $env{'form.'.$item};
12173:                     if ($value eq '') {
12174:                         $value = &mt('none');
12175:                     } elsif ($item eq 'auth_def') {
12176:                         my %authnames = &authtype_names();
12177:                         my %shortauth = (
12178:                                           internal   => 'int',
12179:                                           krb4       => 'krb4',
12180:                                           krb5       => 'krb5',
12181:                                           localauth  => 'loc',
12182:                         );
12183:                         $value = $authnames{$shortauth{$value}};
12184:                     } elsif ($item eq 'intauth_switch') {
12185:                         my %optiondesc = &Apache::lonlocal::texthash (
12186:                                             0 => 'No',
12187:                                             1 => 'Yes',
12188:                                             2 => 'Yes, and copy existing passwd file to passwd.bak file',
12189:                                          );
12190:                         if ($value =~ /^(0|1|2)$/) {
12191:                             $value = $optiondesc{$value};
12192:                         } else {
12193:                             $value = &mt('none -- defaults to No');
12194:                         }
12195:                     } elsif ($item eq 'intauth_check') {
12196:                         my %optiondesc = &Apache::lonlocal::texthash (
12197:                                              0 => 'No',
12198:                                              1 => 'Yes, allow login then update passwd file using default cost (if higher)',
12199:                                              2 => 'Yes, disallow login if stored cost is less than domain default',
12200:                                          );
12201:                         if ($value =~ /^(0|1|2)$/) {
12202:                             $value = $optiondesc{$value};
12203:                         } else {
12204:                             $value = &mt('none -- defaults to No');
12205:                         }
12206:                     }
12207:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
12208:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
12209:                 }
12210:             }
12211:             $resulttext .= '</ul>';
12212:             $mailmsgtext .= "\n";
12213:             my $cachetime = 24*60*60;
12214:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12215:             if (ref($lastactref) eq 'HASH') {
12216:                 $lastactref->{'domdefaults'} = 1;
12217:             }
12218:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
12219:                 my $notify = 1;
12220:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
12221:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
12222:                         $notify = 0;
12223:                     }
12224:                 }
12225:                 if ($notify) {
12226:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
12227:                                                "LON-CAPA Domain Settings Change - $dom",
12228:                                                $mailmsgtext);
12229:                 }
12230:             }
12231:         } else {
12232:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
12233:         }
12234:     } else {
12235:         $resulttext = '<span class="LC_error">'.
12236:             &mt('An error occurred: [_1]',$putresult).'</span>';
12237:     }
12238:     if (@errors > 0) {
12239:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
12240:         foreach my $item (@errors) {
12241:             $resulttext .= ' "'.$title->{$item}.'",';
12242:         }
12243:         $resulttext =~ s/,$//;
12244:     }
12245:     return $resulttext;
12246: }
12247: 
12248: sub modify_scantron {
12249:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12250:     my ($resulttext,%confhash,%changes,$errors);
12251:     my $custom = 'custom.tab';
12252:     my $default = 'default.tab';
12253:     my $servadm = $r->dir_config('lonAdmEMail');
12254:     my ($configuserok,$author_ok,$switchserver) = 
12255:         &config_check($dom,$confname,$servadm);
12256:     if ($env{'form.scantronformat.filename'} ne '') {
12257:         my $error;
12258:         if ($configuserok eq 'ok') {
12259:             if ($switchserver) {
12260:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
12261:             } else {
12262:                 if ($author_ok eq 'ok') {
12263:                     my ($result,$scantronurl) =
12264:                         &publishlogo($r,'upload','scantronformat',$dom,
12265:                                      $confname,'scantron','','',$custom);
12266:                     if ($result eq 'ok') {
12267:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
12268:                         $changes{'scantronformat'} = 1;
12269:                     } else {
12270:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
12271:                     }
12272:                 } else {
12273:                     $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);
12274:                 }
12275:             }
12276:         } else {
12277:             $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);
12278:         }
12279:         if ($error) {
12280:             &Apache::lonnet::logthis($error);
12281:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12282:         }
12283:     }
12284:     if (ref($domconfig{'scantron'}) eq 'HASH') {
12285:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
12286:             if ($env{'form.scantronformat_del'}) {
12287:                 $confhash{'scantron'}{'scantronformat'} = '';
12288:                 $changes{'scantronformat'} = 1;
12289:             }
12290:         }
12291:     }
12292:     if (keys(%confhash) > 0) {
12293:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
12294:                                                  $dom);
12295:         if ($putresult eq 'ok') {
12296:             if (keys(%changes) > 0) {
12297:                 if (ref($confhash{'scantron'}) eq 'HASH') {
12298:                     $resulttext = &mt('Changes made:').'<ul>';
12299:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
12300:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
12301:                     } else {
12302:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
12303:                     }
12304:                     $resulttext .= '</ul>';
12305:                 } else {
12306:                     $resulttext = &mt('Changes made to bubblesheet format file.');
12307:                 }
12308:                 $resulttext .= '</ul>';
12309:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
12310:                 if (ref($lastactref) eq 'HASH') {
12311:                     $lastactref->{'domainconfig'} = 1;
12312:                 }
12313:             } else {
12314:                 $resulttext = &mt('No changes made to bubblesheet format file');
12315:             }
12316:         } else {
12317:             $resulttext = '<span class="LC_error">'.
12318:                 &mt('An error occurred: [_1]',$putresult).'</span>';
12319:         }
12320:     } else {
12321:         $resulttext = &mt('No changes made to bubblesheet format file'); 
12322:     }
12323:     if ($errors) {
12324:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
12325:                        $errors.'</ul>';
12326:     }
12327:     return $resulttext;
12328: }
12329: 
12330: sub modify_coursecategories {
12331:     my ($dom,$lastactref,%domconfig) = @_;
12332:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
12333:         $cathash);
12334:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
12335:     my @catitems = ('unauth','auth');
12336:     my @cattypes = ('std','domonly','codesrch','none');
12337:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
12338:         $cathash = $domconfig{'coursecategories'}{'cats'};
12339:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
12340:             $changes{'togglecats'} = 1;
12341:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
12342:         }
12343:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
12344:             $changes{'categorize'} = 1;
12345:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
12346:         }
12347:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
12348:             $changes{'togglecatscomm'} = 1;
12349:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
12350:         }
12351:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
12352:             $changes{'categorizecomm'} = 1;
12353:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
12354: 
12355:         }
12356:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
12357:             $changes{'togglecatsplace'} = 1;
12358:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
12359:         }
12360:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
12361:             $changes{'categorizeplace'} = 1;
12362:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
12363:         }
12364:         foreach my $item (@catitems) {
12365:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
12366:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
12367:                     $changes{$item} = 1;
12368:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
12369:                 }
12370:             }
12371:         }
12372:     } else {
12373:         $changes{'togglecats'} = 1;
12374:         $changes{'categorize'} = 1;
12375:         $changes{'togglecatscomm'} = 1;
12376:         $changes{'categorizecomm'} = 1;
12377:         $changes{'togglecatsplace'} = 1;
12378:         $changes{'categorizeplace'} = 1;
12379:         $domconfig{'coursecategories'} = {
12380:                                              togglecats => $env{'form.togglecats'},
12381:                                              categorize => $env{'form.categorize'},
12382:                                              togglecatscomm => $env{'form.togglecatscomm'},
12383:                                              categorizecomm => $env{'form.categorizecomm'},
12384:                                              togglecatsplace => $env{'form.togglecatsplace'},
12385:                                              categorizeplace => $env{'form.categorizeplace'},
12386:                                          };
12387:         foreach my $item (@catitems) {
12388:             if ($env{'form.coursecat_'.$item} ne 'std') {
12389:                 $changes{$item} = 1;
12390:             }
12391:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
12392:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
12393:             }
12394:         }
12395:     }
12396:     if (ref($cathash) eq 'HASH') {
12397:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
12398:             push (@deletecategory,'instcode::0');
12399:         }
12400:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
12401:             push(@deletecategory,'communities::0');
12402:         }
12403:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
12404:             push(@deletecategory,'placement::0');
12405:         }
12406:     }
12407:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
12408:     if (ref($cathash) eq 'HASH') {
12409:         if (@deletecategory > 0) {
12410:             #FIXME Need to remove category from all courses using a deleted category 
12411:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
12412:             foreach my $item (@deletecategory) {
12413:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
12414:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
12415:                     $deletions{$item} = 1;
12416:                     &recurse_cat_deletes($item,$cathash,\%deletions);
12417:                 }
12418:             }
12419:         }
12420:         foreach my $item (keys(%{$cathash})) {
12421:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
12422:             if ($cathash->{$item} ne $env{'form.'.$item}) {
12423:                 $reorderings{$item} = 1;
12424:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
12425:             }
12426:             if ($env{'form.addcategory_name_'.$item} ne '') {
12427:                 my $newcat = $env{'form.addcategory_name_'.$item};
12428:                 my $newdepth = $depth+1;
12429:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
12430:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
12431:                 $adds{$newitem} = 1; 
12432:             }
12433:             if ($env{'form.subcat_'.$item} ne '') {
12434:                 my $newcat = $env{'form.subcat_'.$item};
12435:                 my $newdepth = $depth+1;
12436:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
12437:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
12438:                 $adds{$newitem} = 1;
12439:             }
12440:         }
12441:     }
12442:     if ($env{'form.instcode'} eq '1') {
12443:         if (ref($cathash) eq 'HASH') {
12444:             my $newitem = 'instcode::0';
12445:             if ($cathash->{$newitem} eq '') {  
12446:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
12447:                 $adds{$newitem} = 1;
12448:             }
12449:         } else {
12450:             my $newitem = 'instcode::0';
12451:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
12452:             $adds{$newitem} = 1;
12453:         }
12454:     }
12455:     if ($env{'form.communities'} eq '1') {
12456:         if (ref($cathash) eq 'HASH') {
12457:             my $newitem = 'communities::0';
12458:             if ($cathash->{$newitem} eq '') {
12459:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
12460:                 $adds{$newitem} = 1;
12461:             }
12462:         } else {
12463:             my $newitem = 'communities::0';
12464:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
12465:             $adds{$newitem} = 1;
12466:         }
12467:     }
12468:     if ($env{'form.placement'} eq '1') {
12469:         if (ref($cathash) eq 'HASH') {
12470:             my $newitem = 'placement::0';
12471:             if ($cathash->{$newitem} eq '') {
12472:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
12473:                 $adds{$newitem} = 1;
12474:             }
12475:         } else {
12476:             my $newitem = 'placement::0';
12477:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
12478:             $adds{$newitem} = 1;
12479:         }
12480:     }
12481:     if ($env{'form.addcategory_name'} ne '') {
12482:         if (($env{'form.addcategory_name'} ne 'instcode') &&
12483:             ($env{'form.addcategory_name'} ne 'communities') &&
12484:             ($env{'form.addcategory_name'} ne 'placement')) {
12485:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
12486:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
12487:             $adds{$newitem} = 1;
12488:         }
12489:     }
12490:     my $putresult;
12491:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12492:         if (keys(%deletions) > 0) {
12493:             foreach my $key (keys(%deletions)) {
12494:                 if ($predelallitems{$key} ne '') {
12495:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
12496:                 }
12497:             }
12498:         }
12499:         my (@chkcats,@chktrails,%chkallitems);
12500:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
12501:         if (ref($chkcats[0]) eq 'ARRAY') {
12502:             my $depth = 0;
12503:             my $chg = 0;
12504:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
12505:                 my $name = $chkcats[0][$i];
12506:                 my $item;
12507:                 if ($name eq '') {
12508:                     $chg ++;
12509:                 } else {
12510:                     $item = &escape($name).'::0';
12511:                     if ($chg) {
12512:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
12513:                     }
12514:                     $depth ++; 
12515:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
12516:                     $depth --;
12517:                 }
12518:             }
12519:         }
12520:     }
12521:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12522:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
12523:         if ($putresult eq 'ok') {
12524:             my %title = (
12525:                          togglecats     => 'Show/Hide a course in catalog',
12526:                          categorize     => 'Assign a category to a course',
12527:                          togglecatscomm => 'Show/Hide a community in catalog',
12528:                          categorizecomm => 'Assign a category to a community',
12529:                         );
12530:             my %level = (
12531:                          dom  => 'set in Domain ("Modify Course/Community")',
12532:                          crs  => 'set in Course ("Course Configuration")',
12533:                          comm => 'set in Community ("Community Configuration")',
12534:                          none     => 'No catalog',
12535:                          std      => 'Standard catalog',
12536:                          domonly  => 'Domain-only catalog',
12537:                          codesrch => 'Code search form',
12538:                         );
12539:             $resulttext = &mt('Changes made:').'<ul>';
12540:             if ($changes{'togglecats'}) {
12541:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
12542:             }
12543:             if ($changes{'categorize'}) {
12544:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
12545:             }
12546:             if ($changes{'togglecatscomm'}) {
12547:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
12548:             }
12549:             if ($changes{'categorizecomm'}) {
12550:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
12551:             }
12552:             if ($changes{'unauth'}) {
12553:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
12554:             }
12555:             if ($changes{'auth'}) {
12556:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
12557:             }
12558:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12559:                 my $cathash;
12560:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
12561:                     $cathash = $domconfig{'coursecategories'}{'cats'};
12562:                 } else {
12563:                     $cathash = {};
12564:                 } 
12565:                 my (@cats,@trails,%allitems);
12566:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
12567:                 if (keys(%deletions) > 0) {
12568:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
12569:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
12570:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
12571:                     }
12572:                     $resulttext .= '</ul></li>';
12573:                 }
12574:                 if (keys(%reorderings) > 0) {
12575:                     my %sort_by_trail;
12576:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
12577:                     foreach my $key (keys(%reorderings)) {
12578:                         if ($allitems{$key} ne '') {
12579:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
12580:                         }
12581:                     }
12582:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
12583:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
12584:                     }
12585:                     $resulttext .= '</ul></li>';
12586:                 }
12587:                 if (keys(%adds) > 0) {
12588:                     my %sort_by_trail;
12589:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
12590:                     foreach my $key (keys(%adds)) {
12591:                         if ($allitems{$key} ne '') {
12592:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
12593:                         }
12594:                     }
12595:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
12596:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
12597:                     }
12598:                     $resulttext .= '</ul></li>';
12599:                 }
12600:             }
12601:             $resulttext .= '</ul>';
12602:             if ($changes{'unauth'} || $changes{'auth'}) {
12603:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
12604:                 if ($changes{'auth'}) {
12605:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
12606:                 }
12607:                 if ($changes{'unauth'}) {
12608:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
12609:                 }
12610:                 my $cachetime = 24*60*60;
12611:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12612:                 if (ref($lastactref) eq 'HASH') {
12613:                     $lastactref->{'domdefaults'} = 1;
12614:                 }
12615:             }
12616:         } else {
12617:             $resulttext = '<span class="LC_error">'.
12618:                           &mt('An error occurred: [_1]',$putresult).'</span>';
12619:         }
12620:     } else {
12621:         $resulttext = &mt('No changes made to course and community categories');
12622:     }
12623:     return $resulttext;
12624: }
12625: 
12626: sub modify_serverstatuses {
12627:     my ($dom,%domconfig) = @_;
12628:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
12629:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
12630:         %currserverstatus = %{$domconfig{'serverstatuses'}};
12631:     }
12632:     my @pages = &serverstatus_pages();
12633:     foreach my $type (@pages) {
12634:         $newserverstatus{$type}{'namedusers'} = '';
12635:         $newserverstatus{$type}{'machines'} = '';
12636:         if (defined($env{'form.'.$type.'_namedusers'})) {
12637:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
12638:             my @okusers;
12639:             foreach my $user (@users) {
12640:                 my ($uname,$udom) = split(/:/,$user);
12641:                 if (($udom =~ /^$match_domain$/) &&   
12642:                     (&Apache::lonnet::domain($udom)) &&
12643:                     ($uname =~ /^$match_username$/)) {
12644:                     if (!grep(/^\Q$user\E/,@okusers)) {
12645:                         push(@okusers,$user);
12646:                     }
12647:                 }
12648:             }
12649:             if (@okusers > 0) {
12650:                  @okusers = sort(@okusers);
12651:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
12652:             }
12653:         }
12654:         if (defined($env{'form.'.$type.'_machines'})) {
12655:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
12656:             my @okmachines;
12657:             foreach my $ip (@machines) {
12658:                 my @parts = split(/\./,$ip);
12659:                 next if (@parts < 4);
12660:                 my $badip = 0;
12661:                 for (my $i=0; $i<4; $i++) {
12662:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
12663:                         $badip = 1;
12664:                         last;
12665:                     }
12666:                 }
12667:                 if (!$badip) {
12668:                     push(@okmachines,$ip);     
12669:                 }
12670:             }
12671:             @okmachines = sort(@okmachines);
12672:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
12673:         }
12674:     }
12675:     my %serverstatushash =  (
12676:                                 serverstatuses => \%newserverstatus,
12677:                             );
12678:     foreach my $type (@pages) {
12679:         foreach my $setting ('namedusers','machines') {
12680:             my (@current,@new);
12681:             if (ref($currserverstatus{$type}) eq 'HASH') {
12682:                 if ($currserverstatus{$type}{$setting} ne '') { 
12683:                     @current = split(/,/,$currserverstatus{$type}{$setting});
12684:                 }
12685:             }
12686:             if ($newserverstatus{$type}{$setting} ne '') {
12687:                 @new = split(/,/,$newserverstatus{$type}{$setting});
12688:             }
12689:             if (@current > 0) {
12690:                 if (@new > 0) {
12691:                     foreach my $item (@current) {
12692:                         if (!grep(/^\Q$item\E$/,@new)) {
12693:                             $changes{$type}{$setting} = 1;
12694:                             last;
12695:                         }
12696:                     }
12697:                     foreach my $item (@new) {
12698:                         if (!grep(/^\Q$item\E$/,@current)) {
12699:                             $changes{$type}{$setting} = 1;
12700:                             last;
12701:                         }
12702:                     }
12703:                 } else {
12704:                     $changes{$type}{$setting} = 1;
12705:                 }
12706:             } elsif (@new > 0) {
12707:                 $changes{$type}{$setting} = 1;
12708:             }
12709:         }
12710:     }
12711:     if (keys(%changes) > 0) {
12712:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
12713:         my $putresult = &Apache::lonnet::put_dom('configuration',
12714:                                                  \%serverstatushash,$dom);
12715:         if ($putresult eq 'ok') {
12716:             $resulttext .= &mt('Changes made:').'<ul>';
12717:             foreach my $type (@pages) {
12718:                 if (ref($changes{$type}) eq 'HASH') {
12719:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
12720:                     if ($changes{$type}{'namedusers'}) {
12721:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
12722:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
12723:                         } else {
12724:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
12725:                         }
12726:                     }
12727:                     if ($changes{$type}{'machines'}) {
12728:                         if ($newserverstatus{$type}{'machines'} eq '') {
12729:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
12730:                         } else {
12731:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
12732:                         }
12733: 
12734:                     }
12735:                     $resulttext .= '</ul></li>';
12736:                 }
12737:             }
12738:             $resulttext .= '</ul>';
12739:         } else {
12740:             $resulttext = '<span class="LC_error">'.
12741:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
12742: 
12743:         }
12744:     } else {
12745:         $resulttext = &mt('No changes made to access to server status pages');
12746:     }
12747:     return $resulttext;
12748: }
12749: 
12750: sub modify_helpsettings {
12751:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12752:     my ($resulttext,$errors,%changes,%helphash);
12753:     my %defaultchecked = ('submitbugs' => 'on');
12754:     my @offon = ('off','on');
12755:     my @toggles = ('submitbugs');
12756:     my %current = ('submitbugs' => '',
12757:                    'adhoc'      => {},
12758:                   );
12759:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
12760:         %current = %{$domconfig{'helpsettings'}};
12761:     }
12762:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12763:     foreach my $item (@toggles) {
12764:         if ($defaultchecked{$item} eq 'on') { 
12765:             if ($current{$item} eq '') {
12766:                 if ($env{'form.'.$item} eq '0') {
12767:                     $changes{$item} = 1;
12768:                 }
12769:             } elsif ($current{$item} ne $env{'form.'.$item}) {
12770:                 $changes{$item} = 1;
12771:             }
12772:         } elsif ($defaultchecked{$item} eq 'off') {
12773:             if ($current{$item} eq '') {
12774:                 if ($env{'form.'.$item} eq '1') {
12775:                     $changes{$item} = 1;
12776:                 }
12777:             } elsif ($current{$item} ne $env{'form.'.$item}) {
12778:                 $changes{$item} = 1;
12779:             }
12780:         }
12781:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
12782:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
12783:         }
12784:     }
12785:     my $maxnum = $env{'form.helproles_maxnum'};
12786:     my $confname = $dom.'-domainconfig';
12787:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
12788:     my (@allpos,%newsettings,%changedprivs,$newrole);
12789:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12790:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
12791:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
12792:     my %lt = &Apache::lonlocal::texthash(
12793:                     s      => 'system',
12794:                     d      => 'domain',
12795:                     order  => 'Display order',
12796:                     access => 'Role usage',
12797:                     all    => 'All with domain helpdesk or helpdesk assistant role',
12798:                     dh     => 'All with domain helpdesk role',
12799:                     da     => 'All with domain helpdesk assistant role',
12800:                     none   => 'None',
12801:                     status => 'Determined based on institutional status',
12802:                     inc    => 'Include all, but exclude specific personnel',
12803:                     exc    => 'Exclude all, but include specific personnel',
12804:     );
12805:     for (my $num=0; $num<=$maxnum; $num++) {
12806:         my ($prefix,$identifier,$rolename,%curr);
12807:         if ($num == $maxnum) {
12808:             next unless ($env{'form.newcusthelp'} == $maxnum);
12809:             $identifier = 'custhelp'.$num;
12810:             $prefix = 'helproles_'.$num;
12811:             $rolename = $env{'form.custhelpname'.$num};
12812:             $rolename=~s/[^A-Za-z0-9]//gs;
12813:             next if ($rolename eq '');
12814:             next if (exists($existing{'rolesdef_'.$rolename}));
12815:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
12816:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
12817:                                                      $newprivs{'c'},$confname,$dom);
12818:             if ($result ne 'ok') {
12819:                 $errors .= '<li><span class="LC_error">'.
12820:                            &mt('An error occurred storing the new custom role: [_1]',
12821:                            $result).'</span></li>';
12822:                 next;
12823:             } else {
12824:                 $changedprivs{$rolename} = \%newprivs;
12825:                 $newrole = $rolename;
12826:             }
12827:         } else {
12828:             $prefix = 'helproles_'.$num;
12829:             $rolename = $env{'form.'.$prefix};
12830:             next if ($rolename eq '');
12831:             next unless (exists($existing{'rolesdef_'.$rolename}));
12832:             $identifier = 'custhelp'.$num;
12833:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
12834:             my %currprivs;
12835:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
12836:                 split(/\_/,$existing{'rolesdef_'.$rolename});
12837:             foreach my $level ('c','d','s') {
12838:                 if ($newprivs{$level} ne $currprivs{$level}) {
12839:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
12840:                                                              $newprivs{'c'},$confname,$dom);
12841:                     if ($result ne 'ok') {
12842:                         $errors .= '<li><span class="LC_error">'.
12843:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
12844:                                        $rolename,$result).'</span></li>';
12845:                     } else {
12846:                         $changedprivs{$rolename} = \%newprivs;
12847:                     }
12848:                     last;
12849:                 }
12850:             }
12851:             if (ref($current{'adhoc'}) eq 'HASH') {
12852:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
12853:                     %curr = %{$current{'adhoc'}{$rolename}};
12854:                 }
12855:             }
12856:         }
12857:         my $newpos = $env{'form.'.$prefix.'_pos'};
12858:         $newpos =~ s/\D+//g;
12859:         $allpos[$newpos] = $rolename;
12860:         my $newdesc = $env{'form.'.$prefix.'_desc'};
12861:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
12862:         if ($curr{'desc'}) {
12863:             if ($curr{'desc'} ne $newdesc) {
12864:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
12865:                 $newsettings{$rolename}{'desc'} = $newdesc;
12866:             }
12867:         } elsif ($newdesc ne '') {
12868:             $changes{'customrole'}{$rolename}{'desc'} = 1;
12869:             $newsettings{$rolename}{'desc'} = $newdesc;
12870:         }
12871:         my $access = $env{'form.'.$prefix.'_access'};
12872:         if (grep(/^\Q$access\E$/,@accesstypes)) {
12873:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
12874:             if ($access eq 'status') {
12875:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
12876:                 if (scalar(@statuses) == 0) {
12877:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
12878:                 } else {
12879:                     my (@shownstatus,$numtypes);
12880:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
12881:                     if (ref($types) eq 'ARRAY') {
12882:                         $numtypes = scalar(@{$types});
12883:                         foreach my $type (sort(@statuses)) {
12884:                             if ($type eq 'default') {
12885:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
12886:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
12887:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
12888:                                 push(@shownstatus,$usertypes->{$type});
12889:                             }
12890:                         }
12891:                     }
12892:                     if (grep(/^default$/,@statuses)) {
12893:                         push(@shownstatus,$othertitle);
12894:                     }
12895:                     if (scalar(@shownstatus) == 1+$numtypes) {
12896:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
12897:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
12898:                     } else {
12899:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
12900:                         if (ref($curr{'status'}) eq 'ARRAY') {
12901:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
12902:                             if (@diffs) {
12903:                                 $changes{'customrole'}{$rolename}{$access} = 1;
12904:                             }
12905:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12906:                             $changes{'customrole'}{$rolename}{$access} = 1;
12907:                         }
12908:                     }
12909:                 }
12910:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
12911:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
12912:                 my @newspecstaff;
12913:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
12914:                 foreach my $person (sort(@personnel)) {
12915:                     if ($domhelpdesk{$person}) {
12916:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
12917:                     }
12918:                 }
12919:                 if (ref($curr{$access}) eq 'ARRAY') {
12920:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
12921:                     if (@diffs) {
12922:                         $changes{'customrole'}{$rolename}{$access} = 1;
12923:                     }
12924:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12925:                     $changes{'customrole'}{$rolename}{$access} = 1;
12926:                 }
12927:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12928:                     my ($uname,$udom) = split(/:/,$person);
12929:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
12930:                 }
12931:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
12932:             }
12933:         } else {
12934:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
12935:         }
12936:         unless ($curr{'access'} eq $access) {
12937:             $changes{'customrole'}{$rolename}{'access'} = 1;
12938:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
12939:         }
12940:     }
12941:     if (@allpos > 0) {
12942:         my $idx = 0;
12943:         foreach my $rolename (@allpos) {
12944:             if ($rolename ne '') {
12945:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
12946:                 if (ref($current{'adhoc'}) eq 'HASH') {
12947:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
12948:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
12949:                             $changes{'customrole'}{$rolename}{'order'} = 1;
12950:                             $newsettings{$rolename}{'order'} = $idx+1;
12951:                         }
12952:                     }
12953:                 }
12954:                 $idx ++;
12955:             }
12956:         }
12957:     }
12958:     my $putresult;
12959:     if (keys(%changes) > 0) {
12960:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
12961:         if ($putresult eq 'ok') {
12962:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
12963:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
12964:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
12965:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
12966:                 }
12967:             }
12968:             my $cachetime = 24*60*60;
12969:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12970:             if (ref($lastactref) eq 'HASH') {
12971:                 $lastactref->{'domdefaults'} = 1;
12972:             }
12973:         } else {
12974:             $errors .= '<li><span class="LC_error">'.
12975:                        &mt('An error occurred storing the settings: [_1]',
12976:                            $putresult).'</span></li>';
12977:         }
12978:     }
12979:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
12980:         $resulttext = &mt('Changes made:').'<ul>';
12981:         my (%shownprivs,@levelorder);
12982:         @levelorder = ('c','d','s');
12983:         if ((keys(%changes)) && ($putresult eq 'ok')) {
12984:             foreach my $item (sort(keys(%changes))) {
12985:                 if ($item eq 'submitbugs') {
12986:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
12987:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
12988:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
12989:                 } elsif ($item eq 'customrole') {
12990:                     if (ref($changes{'customrole'}) eq 'HASH') {
12991:                         my @keyorder = ('order','desc','access','status','exc','inc');
12992:                         my %keytext = &Apache::lonlocal::texthash(
12993:                                                                    order  => 'Order',
12994:                                                                    desc   => 'Role description',
12995:                                                                    access => 'Role usage',
12996:                                                                    status => 'Allowed institutional types',
12997:                                                                    exc    => 'Allowed personnel',
12998:                                                                    inc    => 'Disallowed personnel',
12999:                         );
13000:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
13001:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
13002:                                 if ($role eq $newrole) {
13003:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
13004:                                                               $role).'<ul>';
13005:                                 } else {
13006:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
13007:                                                               $role).'<ul>';
13008:                                 }
13009:                                 foreach my $key (@keyorder) {
13010:                                     if ($changes{'customrole'}{$role}{$key}) {
13011:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
13012:                                                                   $keytext{$key},$newsettings{$role}{$key}).
13013:                                                        '</li>';
13014:                                     }
13015:                                 }
13016:                                 if (ref($changedprivs{$role}) eq 'HASH') {
13017:                                     $shownprivs{$role} = 1;
13018:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
13019:                                     foreach my $level (@levelorder) {
13020:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
13021:                                             next if ($item eq '');
13022:                                             my ($priv) = split(/\&/,$item,2);
13023:                                             if (&Apache::lonnet::plaintext($priv)) {
13024:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
13025:                                                 unless ($level eq 'c') {
13026:                                                     $resulttext .= ' ('.$lt{$level}.')';
13027:                                                 }
13028:                                                 $resulttext .= '</li>';
13029:                                             }
13030:                                         }
13031:                                     }
13032:                                     $resulttext .= '</ul>';
13033:                                 }
13034:                                 $resulttext .= '</ul></li>';
13035:                             }
13036:                         }
13037:                     }
13038:                 }
13039:             }
13040:         }
13041:         if (keys(%changedprivs)) {
13042:             foreach my $role (sort(keys(%changedprivs))) {
13043:                 unless ($shownprivs{$role}) {
13044:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
13045:                                               $role).'<ul>'.
13046:                                    '<li>'.&mt('Privileges set to :').'<ul>';
13047:                     foreach my $level (@levelorder) {
13048:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
13049:                             next if ($item eq '');
13050:                             my ($priv) = split(/\&/,$item,2);
13051:                             if (&Apache::lonnet::plaintext($priv)) {
13052:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
13053:                                 unless ($level eq 'c') {
13054:                                     $resulttext .= ' ('.$lt{$level}.')';
13055:                                 }
13056:                                 $resulttext .= '</li>';
13057:                             }
13058:                         }
13059:                     }
13060:                     $resulttext .= '</ul></li></ul></li>';
13061:                 }
13062:             }
13063:         }
13064:         $resulttext .= '</ul>';
13065:     } else {
13066:         $resulttext = &mt('No changes made to help settings');
13067:     }
13068:     if ($errors) {
13069:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
13070:                                     $errors.'</ul>';
13071:     }
13072:     return $resulttext;
13073: }
13074: 
13075: sub modify_coursedefaults {
13076:     my ($dom,$lastactref,%domconfig) = @_;
13077:     my ($resulttext,$errors,%changes,%defaultshash);
13078:     my %defaultchecked = (
13079:                            'canuse_pdfforms' => 'off',
13080:                            'uselcmath'       => 'on',
13081:                            'usejsme'         => 'on'
13082:                          );
13083:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme');
13084:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
13085:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
13086:                    'mysqltables_official','mysqltables_unofficial','mysqltables_community',
13087:                    'mysqltables_textbook','mysqltables_placement');
13088:     my @types = ('official','unofficial','community','textbook','placement');
13089:     my %staticdefaults = (
13090:                            anonsurvey_threshold => 10,
13091:                            uploadquota          => 500,
13092:                            postsubmit           => 60,
13093:                            mysqltables          => 172800,
13094:                          );
13095: 
13096:     $defaultshash{'coursedefaults'} = {};
13097: 
13098:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
13099:         if ($domconfig{'coursedefaults'} eq '') {
13100:             $domconfig{'coursedefaults'} = {};
13101:         }
13102:     }
13103: 
13104:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
13105:         foreach my $item (@toggles) {
13106:             if ($defaultchecked{$item} eq 'on') {
13107:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
13108:                     ($env{'form.'.$item} eq '0')) {
13109:                     $changes{$item} = 1;
13110:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
13111:                     $changes{$item} = 1;
13112:                 }
13113:             } elsif ($defaultchecked{$item} eq 'off') {
13114:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
13115:                     ($env{'form.'.$item} eq '1')) {
13116:                     $changes{$item} = 1;
13117:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
13118:                     $changes{$item} = 1;
13119:                 }
13120:             }
13121:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
13122:         }
13123:         foreach my $item (@numbers) {
13124:             my ($currdef,$newdef);
13125:             $newdef = $env{'form.'.$item};
13126:             if ($item eq 'anonsurvey_threshold') {
13127:                 $currdef = $domconfig{'coursedefaults'}{$item};
13128:                 $newdef =~ s/\D//g;
13129:                 if ($newdef eq '' || $newdef < 1) {
13130:                     $newdef = 1;
13131:                 }
13132:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
13133:             } else {
13134:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
13135:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
13136:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
13137:                 }
13138:                 $newdef =~ s/[^\w.\-]//g;
13139:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
13140:             }
13141:             if ($currdef ne $newdef) {
13142:                 my $staticdef;
13143:                 if ($item eq 'anonsurvey_threshold') {
13144:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
13145:                         $changes{$item} = 1;
13146:                     }
13147:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
13148:                     my $setting = $1;
13149:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
13150:                         $changes{$setting} = 1;
13151:                     }
13152:                 }
13153:             }
13154:         }
13155:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
13156:         my @currclonecode;
13157:         if (ref($currclone) eq 'HASH') {
13158:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
13159:                 @currclonecode = @{$currclone->{'instcode'}};
13160:             }
13161:         }
13162:         my $newclone;
13163:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
13164:             $newclone = $env{'form.canclone'};
13165:         }
13166:         if ($newclone eq 'instcode') {
13167:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
13168:             my (%codedefaults,@code_order,@clonecode);
13169:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
13170:                                                     \@code_order);
13171:             foreach my $item (@code_order) {
13172:                 if (grep(/^\Q$item\E$/,@newcodes)) {
13173:                     push(@clonecode,$item);
13174:                 }
13175:             }
13176:             if (@clonecode) {
13177:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
13178:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
13179:                 if (@diffs) {
13180:                     $changes{'canclone'} = 1;
13181:                 }
13182:             } else {
13183:                 $newclone eq '';
13184:             }
13185:         } elsif ($newclone ne '') {
13186:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
13187:         }
13188:         if ($newclone ne $currclone) {
13189:             $changes{'canclone'} = 1;
13190:         }
13191:         my %credits;
13192:         foreach my $type (@types) {
13193:             unless ($type eq 'community') {
13194:                 $credits{$type} = $env{'form.'.$type.'_credits'};
13195:                 $credits{$type} =~ s/[^\d.]+//g;
13196:             }
13197:         }
13198:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
13199:             ($env{'form.coursecredits'} eq '1')) {
13200:             $changes{'coursecredits'} = 1;
13201:             foreach my $type (keys(%credits)) {
13202:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
13203:             }
13204:         } else {
13205:             if ($env{'form.coursecredits'} eq '1') {
13206:                 foreach my $type (@types) {
13207:                     unless ($type eq 'community') {
13208:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
13209:                             $changes{'coursecredits'} = 1;
13210:                         }
13211:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
13212:                     }
13213:                 }
13214:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13215:                 foreach my $type (@types) {
13216:                     unless ($type eq 'community') {
13217:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
13218:                             $changes{'coursecredits'} = 1;
13219:                             last;
13220:                         }
13221:                     }
13222:                 }
13223:             }
13224:         }
13225:         if ($env{'form.postsubmit'} eq '1') {
13226:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
13227:             my %currtimeout;
13228:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13229:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
13230:                     $changes{'postsubmit'} = 1;
13231:                 }
13232:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13233:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
13234:                 }
13235:             } else {
13236:                 $changes{'postsubmit'} = 1;
13237:             }
13238:             foreach my $type (@types) {
13239:                 my $timeout = $env{'form.'.$type.'_timeout'};
13240:                 $timeout =~ s/\D//g;
13241:                 if ($timeout == $staticdefaults{'postsubmit'}) {
13242:                     $timeout = '';
13243:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
13244:                     $timeout = '0';
13245:                 }
13246:                 unless ($timeout eq '') {
13247:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
13248:                 }
13249:                 if (exists($currtimeout{$type})) {
13250:                     if ($timeout ne $currtimeout{$type}) {
13251:                         $changes{'postsubmit'} = 1;
13252:                     }
13253:                 } elsif ($timeout ne '') {
13254:                     $changes{'postsubmit'} = 1;
13255:                 }
13256:             }
13257:         } else {
13258:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
13259:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13260:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
13261:                     $changes{'postsubmit'} = 1;
13262:                 }
13263:             } else {
13264:                 $changes{'postsubmit'} = 1;
13265:             }
13266:         }
13267:     }
13268:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13269:                                              $dom);
13270:     if ($putresult eq 'ok') {
13271:         if (keys(%changes) > 0) {
13272:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13273:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
13274:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
13275:                 ($changes{'canclone'}) || ($changes{'mysqltables'})) {
13276:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme') { 
13277:                     if ($changes{$item}) {
13278:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
13279:                     }
13280:                 }
13281:                 if ($changes{'coursecredits'}) {
13282:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13283:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
13284:                             $domdefaults{$type.'credits'} =
13285:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
13286:                         }
13287:                     }
13288:                 }
13289:                 if ($changes{'postsubmit'}) {
13290:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13291:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
13292:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13293:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
13294:                                 $domdefaults{$type.'postsubtimeout'} =
13295:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
13296:                             }
13297:                         }
13298:                     }
13299:                 }
13300:                 if ($changes{'uploadquota'}) {
13301:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
13302:                         foreach my $type (@types) {
13303:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
13304:                         }
13305:                     }
13306:                 }
13307:                 if ($changes{'canclone'}) {
13308:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
13309:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
13310:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
13311:                             if (@clonecodes) {
13312:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
13313:                             }
13314:                         }
13315:                     } else {
13316:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
13317:                     }
13318:                 }
13319:                 my $cachetime = 24*60*60;
13320:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13321:                 if (ref($lastactref) eq 'HASH') {
13322:                     $lastactref->{'domdefaults'} = 1;
13323:                 }
13324:             }
13325:             $resulttext = &mt('Changes made:').'<ul>';
13326:             foreach my $item (sort(keys(%changes))) {
13327:                 if ($item eq 'canuse_pdfforms') {
13328:                     if ($env{'form.'.$item} eq '1') {
13329:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
13330:                     } else {
13331:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
13332:                     }
13333:                 } elsif ($item eq 'uselcmath') {
13334:                     if ($env{'form.'.$item} eq '1') {
13335:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
13336:                     } else {
13337:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
13338:                     }
13339:                 } elsif ($item eq 'usejsme') {
13340:                     if ($env{'form.'.$item} eq '1') {
13341:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
13342:                     } else {
13343:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
13344:                     }
13345:                 } elsif ($item eq 'anonsurvey_threshold') {
13346:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
13347:                 } elsif ($item eq 'uploadquota') {
13348:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
13349:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
13350:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
13351:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
13352:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
13353:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
13354:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
13355:                                        '</ul>'.
13356:                                        '</li>';
13357:                     } else {
13358:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
13359:                     }
13360:                 } elsif ($item eq 'mysqltables') {
13361:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
13362:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
13363:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
13364:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
13365:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
13366:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
13367:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
13368:                                        '</ul>'.
13369:                                        '</li>';
13370:                     } else {
13371:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
13372:                     }
13373:                 } elsif ($item eq 'postsubmit') {
13374:                     if ($domdefaults{'postsubmit'} eq 'off') {
13375:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
13376:                     } else {
13377:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
13378:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13379:                             $resulttext .= &mt('durations:').'<ul>';
13380:                             foreach my $type (@types) {
13381:                                 $resulttext .= '<li>';
13382:                                 my $timeout;
13383:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13384:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
13385:                                 }
13386:                                 my $display;
13387:                                 if ($timeout eq '0') {
13388:                                     $display = &mt('unlimited');
13389:                                 } elsif ($timeout eq '') {
13390:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
13391:                                 } else {
13392:                                     $display = &mt('[quant,_1,second]',$timeout);
13393:                                 }
13394:                                 if ($type eq 'community') {
13395:                                     $resulttext .= &mt('Communities');
13396:                                 } elsif ($type eq 'official') {
13397:                                     $resulttext .= &mt('Official courses');
13398:                                 } elsif ($type eq 'unofficial') {
13399:                                     $resulttext .= &mt('Unofficial courses');
13400:                                 } elsif ($type eq 'textbook') {
13401:                                     $resulttext .= &mt('Textbook courses');
13402:                                 } elsif ($type eq 'placement') {
13403:                                     $resulttext .= &mt('Placement tests');
13404:                                 }
13405:                                 $resulttext .= ' -- '.$display.'</li>';
13406:                             }
13407:                             $resulttext .= '</ul>';
13408:                         }
13409:                         $resulttext .= '</li>';
13410:                     }
13411:                 } elsif ($item eq 'coursecredits') {
13412:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13413:                         if (($domdefaults{'officialcredits'} eq '') &&
13414:                             ($domdefaults{'unofficialcredits'} eq '') &&
13415:                             ($domdefaults{'textbookcredits'} eq '')) {
13416:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
13417:                         } else {
13418:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
13419:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
13420:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
13421:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
13422:                                            '</ul>'.
13423:                                            '</li>';
13424:                         }
13425:                     } else {
13426:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
13427:                     }
13428:                 } elsif ($item eq 'canclone') {
13429:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
13430:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
13431:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
13432:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
13433:                         }
13434:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
13435:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
13436:                     } else {
13437:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
13438:                     }
13439:                 }
13440:             }
13441:             $resulttext .= '</ul>';
13442:         } else {
13443:             $resulttext = &mt('No changes made to course defaults');
13444:         }
13445:     } else {
13446:         $resulttext = '<span class="LC_error">'.
13447:             &mt('An error occurred: [_1]',$putresult).'</span>';
13448:     }
13449:     return $resulttext;
13450: }
13451: 
13452: sub modify_selfenrollment {
13453:     my ($dom,$lastactref,%domconfig) = @_;
13454:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
13455:     my @types = ('official','unofficial','community','textbook','placement');
13456:     my %titles = &tool_titles();
13457:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
13458:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
13459:     $ordered{'default'} = ['types','registered','approval','limit'];
13460: 
13461:     my (%roles,%shown,%toplevel);
13462:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
13463: 
13464:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
13465:         if ($domconfig{'selfenrollment'} eq '') {
13466:             $domconfig{'selfenrollment'} = {};
13467:         }
13468:     }
13469:     %toplevel = (
13470:                   admin      => 'Configuration Rights',
13471:                   default    => 'Default settings',
13472:                   validation => 'Validation of self-enrollment requests',
13473:                 );
13474:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
13475: 
13476:     if (ref($ordered{'admin'}) eq 'ARRAY') {
13477:         foreach my $item (@{$ordered{'admin'}}) {
13478:             foreach my $type (@types) {
13479:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
13480:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
13481:                 } else {
13482:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
13483:                 }
13484:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
13485:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
13486:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
13487:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
13488:                             push(@{$changes{'admin'}{$type}},$item);
13489:                         }
13490:                     } else {
13491:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
13492:                             push(@{$changes{'admin'}{$type}},$item);
13493:                         }
13494:                     }
13495:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
13496:                     push(@{$changes{'admin'}{$type}},$item);
13497:                 }
13498:             }
13499:         }
13500:     }
13501: 
13502:     foreach my $item (@{$ordered{'default'}}) {
13503:         foreach my $type (@types) {
13504:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
13505:             if ($item eq 'types') {
13506:                 unless (($value eq 'all') || ($value eq 'dom')) {
13507:                     $value = '';
13508:                 }
13509:             } elsif ($item eq 'registered') {
13510:                 unless ($value eq '1') {
13511:                     $value = 0;
13512:                 }
13513:             } elsif ($item eq 'approval') {
13514:                 unless ($value =~ /^[012]$/) {
13515:                     $value = 0;
13516:                 }
13517:             } else {
13518:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
13519:                     $value = 'none';
13520:                 }
13521:             }
13522:             $selfenrollhash{'default'}{$type}{$item} = $value;
13523:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
13524:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
13525:                     if ($selfenrollhash{'default'}{$type}{$item} ne
13526:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
13527:                          push(@{$changes{'default'}{$type}},$item);
13528:                     }
13529:                 } else {
13530:                     push(@{$changes{'default'}{$type}},$item);
13531:                 }
13532:             } else {
13533:                 push(@{$changes{'default'}{$type}},$item);
13534:             }
13535:             if ($item eq 'limit') {
13536:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
13537:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
13538:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
13539:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
13540:                     }
13541:                 } else {
13542:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
13543:                 }
13544:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
13545:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
13546:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
13547:                          push(@{$changes{'default'}{$type}},'cap');
13548:                     }
13549:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
13550:                     push(@{$changes{'default'}{$type}},'cap');
13551:                 }
13552:             }
13553:         }
13554:     }
13555: 
13556:     foreach my $item (@{$itemsref}) {
13557:         if ($item eq 'fields') {
13558:             my @changed;
13559:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
13560:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
13561:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
13562:             }
13563:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
13564:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
13565:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
13566:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
13567:                 } else {
13568:                     @changed = @{$selfenrollhash{'validation'}{$item}};
13569:                 }
13570:             } else {
13571:                 @changed = @{$selfenrollhash{'validation'}{$item}};
13572:             }
13573:             if (@changed) {
13574:                 if ($selfenrollhash{'validation'}{$item}) { 
13575:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
13576:                 } else {
13577:                     $changes{'validation'}{$item} = &mt('None');
13578:                 }
13579:             }
13580:         } else {
13581:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
13582:             if ($item eq 'markup') {
13583:                if ($env{'form.selfenroll_validation_'.$item}) {
13584:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
13585:                }
13586:             }
13587:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
13588:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
13589:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
13590:                 }
13591:             }
13592:         }
13593:     }
13594: 
13595:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
13596:                                              $dom);
13597:     if ($putresult eq 'ok') {
13598:         if (keys(%changes) > 0) {
13599:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13600:             $resulttext = &mt('Changes made:').'<ul>';
13601:             foreach my $key ('admin','default','validation') {
13602:                 if (ref($changes{$key}) eq 'HASH') {
13603:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
13604:                     if ($key eq 'validation') {
13605:                         foreach my $item (@{$itemsref}) {
13606:                             if (exists($changes{$key}{$item})) {
13607:                                 if ($item eq 'markup') {
13608:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
13609:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
13610:                                 } else {  
13611:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
13612:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
13613:                                 }
13614:                             }
13615:                         }
13616:                     } else {
13617:                         foreach my $type (@types) {
13618:                             if ($type eq 'community') {
13619:                                 $roles{'1'} = &mt('Community personnel');
13620:                             } else {
13621:                                 $roles{'1'} = &mt('Course personnel');
13622:                             }
13623:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
13624:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
13625:                                     if ($key eq 'admin') {
13626:                                         my @mgrdc = ();
13627:                                         if (ref($ordered{$key}) eq 'ARRAY') {
13628:                                             foreach my $item (@{$ordered{'admin'}}) {
13629:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
13630:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
13631:                                                         push(@mgrdc,$item);
13632:                                                     }
13633:                                                 }
13634:                                             }
13635:                                             if (@mgrdc) {
13636:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
13637:                                             } else {
13638:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
13639:                                             }
13640:                                         }
13641:                                     } else {
13642:                                         if (ref($ordered{$key}) eq 'ARRAY') {
13643:                                             foreach my $item (@{$ordered{$key}}) {
13644:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
13645:                                                     $domdefaults{$type.'selfenroll'.$item} =
13646:                                                         $selfenrollhash{$key}{$type}{$item};
13647:                                                 }
13648:                                             }
13649:                                         }
13650:                                     }
13651:                                 }
13652:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
13653:                                 foreach my $item (@{$ordered{$key}}) {
13654:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
13655:                                         $resulttext .= '<li>';
13656:                                         if ($key eq 'admin') {
13657:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
13658:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
13659:                                         } else {
13660:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
13661:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
13662:                                         }
13663:                                         $resulttext .= '</li>';
13664:                                     }
13665:                                 }
13666:                                 $resulttext .= '</ul></li>';
13667:                             }
13668:                         }
13669:                         $resulttext .= '</ul></li>'; 
13670:                     }
13671:                 }
13672:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
13673:                     my $cachetime = 24*60*60;
13674:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13675:                     if (ref($lastactref) eq 'HASH') {
13676:                         $lastactref->{'domdefaults'} = 1;
13677:                     }
13678:                 }
13679:             }
13680:             $resulttext .= '</ul>';
13681:         } else {
13682:             $resulttext = &mt('No changes made to self-enrollment settings');
13683:         }
13684:     } else {
13685:         $resulttext = '<span class="LC_error">'.
13686:             &mt('An error occurred: [_1]',$putresult).'</span>';
13687:     }
13688:     return $resulttext;
13689: }
13690: 
13691: sub modify_usersessions {
13692:     my ($dom,$lastactref,%domconfig) = @_;
13693:     my @hostingtypes = ('version','excludedomain','includedomain');
13694:     my @offloadtypes = ('primary','default');
13695:     my %types = (
13696:                   remote => \@hostingtypes,
13697:                   hosted => \@hostingtypes,
13698:                   spares => \@offloadtypes,
13699:                 );
13700:     my @prefixes = ('remote','hosted','spares');
13701:     my @lcversions = &Apache::lonnet::all_loncaparevs();
13702:     my (%by_ip,%by_location,@intdoms,@instdoms);
13703:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
13704:     my @locations = sort(keys(%by_location));
13705:     my (%defaultshash,%changes);
13706:     foreach my $prefix (@prefixes) {
13707:         $defaultshash{'usersessions'}{$prefix} = {};
13708:     }
13709:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13710:     my $resulttext;
13711:     my %iphost = &Apache::lonnet::get_iphost();
13712:     foreach my $prefix (@prefixes) {
13713:         next if ($prefix eq 'spares');
13714:         foreach my $type (@{$types{$prefix}}) {
13715:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
13716:             if ($type eq 'version') {
13717:                 my $value = $env{'form.'.$prefix.'_'.$type};
13718:                 my $okvalue;
13719:                 if ($value ne '') {
13720:                     if (grep(/^\Q$value\E$/,@lcversions)) {
13721:                         $okvalue = $value;
13722:                     }
13723:                 }
13724:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
13725:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
13726:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
13727:                             if ($inuse == 0) {
13728:                                 $changes{$prefix}{$type} = 1;
13729:                             } else {
13730:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
13731:                                     $changes{$prefix}{$type} = 1;
13732:                                 }
13733:                                 if ($okvalue ne '') {
13734:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13735:                                 } 
13736:                             }
13737:                         } else {
13738:                             if (($inuse == 1) && ($okvalue ne '')) {
13739:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13740:                                 $changes{$prefix}{$type} = 1;
13741:                             }
13742:                         }
13743:                     } else {
13744:                         if (($inuse == 1) && ($okvalue ne '')) {
13745:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13746:                             $changes{$prefix}{$type} = 1;
13747:                         }
13748:                     }
13749:                 } else {
13750:                     if (($inuse == 1) && ($okvalue ne '')) {
13751:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13752:                         $changes{$prefix}{$type} = 1;
13753:                     }
13754:                 }
13755:             } else {
13756:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
13757:                 my @okvals;
13758:                 foreach my $val (@vals) {
13759:                     if ($val =~ /:/) {
13760:                         my @items = split(/:/,$val);
13761:                         foreach my $item (@items) {
13762:                             if (ref($by_location{$item}) eq 'ARRAY') {
13763:                                 push(@okvals,$item);
13764:                             }
13765:                         }
13766:                     } else {
13767:                         if (ref($by_location{$val}) eq 'ARRAY') {
13768:                             push(@okvals,$val);
13769:                         }
13770:                     }
13771:                 }
13772:                 @okvals = sort(@okvals);
13773:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
13774:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
13775:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
13776:                             if ($inuse == 0) {
13777:                                 $changes{$prefix}{$type} = 1; 
13778:                             } else {
13779:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13780:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
13781:                                 if (@changed > 0) {
13782:                                     $changes{$prefix}{$type} = 1;
13783:                                 }
13784:                             }
13785:                         } else {
13786:                             if ($inuse == 1) {
13787:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13788:                                 $changes{$prefix}{$type} = 1;
13789:                             }
13790:                         } 
13791:                     } else {
13792:                         if ($inuse == 1) {
13793:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13794:                             $changes{$prefix}{$type} = 1;
13795:                         }
13796:                     }
13797:                 } else {
13798:                     if ($inuse == 1) {
13799:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13800:                         $changes{$prefix}{$type} = 1;
13801:                     }
13802:                 }
13803:             }
13804:         }
13805:     }
13806: 
13807:     my @alldoms = &Apache::lonnet::all_domains();
13808:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
13809:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
13810:     my $savespares;
13811: 
13812:     foreach my $lonhost (sort(keys(%servers))) {
13813:         my $serverhomeID =
13814:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
13815:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
13816:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
13817:         my %spareschg;
13818:         foreach my $type (@{$types{'spares'}}) {
13819:             my @okspares;
13820:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
13821:             foreach my $server (@checked) {
13822:                 if (&Apache::lonnet::hostname($server) ne '') {
13823:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
13824:                         unless (grep(/^\Q$server\E$/,@okspares)) {
13825:                             push(@okspares,$server);
13826:                         }
13827:                     }
13828:                 }
13829:             }
13830:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
13831:             my $newspare;
13832:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
13833:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
13834:                     $newspare = $new;
13835:                 }
13836:             }
13837:             my @spares;
13838:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
13839:                 @spares = sort(@okspares,$newspare);
13840:             } else {
13841:                 @spares = sort(@okspares);
13842:             }
13843:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
13844:             if (ref($spareid{$lonhost}) eq 'HASH') {
13845:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
13846:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
13847:                     if (@diffs > 0) {
13848:                         $spareschg{$type} = 1;
13849:                     }
13850:                 }
13851:             }
13852:         }
13853:         if (keys(%spareschg) > 0) {
13854:             $changes{'spares'}{$lonhost} = \%spareschg;
13855:         }
13856:     }
13857:     $defaultshash{'usersessions'}{'offloadnow'} = {};
13858:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
13859:     my @okoffload;
13860:     if (@offloadnow) {
13861:         foreach my $server (@offloadnow) {
13862:             if (&Apache::lonnet::hostname($server) ne '') {
13863:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
13864:                     push(@okoffload,$server);
13865:                 }
13866:             }
13867:         }
13868:         if (@okoffload) {
13869:             foreach my $lonhost (@okoffload) {
13870:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
13871:             }
13872:         }
13873:     }
13874:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
13875:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
13876:             if (ref($changes{'spares'}) eq 'HASH') {
13877:                 if (keys(%{$changes{'spares'}}) > 0) {
13878:                     $savespares = 1;
13879:                 }
13880:             }
13881:         } else {
13882:             $savespares = 1;
13883:         }
13884:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
13885:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
13886:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
13887:                     $changes{'offloadnow'} = 1;
13888:                     last;
13889:                 }
13890:             }
13891:             unless ($changes{'offloadnow'}) {
13892:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
13893:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
13894:                         $changes{'offloadnow'} = 1;
13895:                         last;
13896:                     }
13897:                 }
13898:             }
13899:         } elsif (@okoffload) {
13900:             $changes{'offloadnow'} = 1;
13901:         }
13902:     } elsif (@okoffload) {
13903:         $changes{'offloadnow'} = 1;
13904:     }
13905:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
13906:     if ((keys(%changes) > 0) || ($savespares)) {
13907:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13908:                                                  $dom);
13909:         if ($putresult eq 'ok') {
13910:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
13911:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
13912:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
13913:                 }
13914:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
13915:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
13916:                 }
13917:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
13918:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
13919:                 }
13920:             }
13921:             my $cachetime = 24*60*60;
13922:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13923:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
13924:             if (ref($lastactref) eq 'HASH') {
13925:                 $lastactref->{'domdefaults'} = 1;
13926:                 $lastactref->{'usersessions'} = 1;
13927:             }
13928:             if (keys(%changes) > 0) {
13929:                 my %lt = &usersession_titles();
13930:                 $resulttext = &mt('Changes made:').'<ul>';
13931:                 foreach my $prefix (@prefixes) {
13932:                     if (ref($changes{$prefix}) eq 'HASH') {
13933:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
13934:                         if ($prefix eq 'spares') {
13935:                             if (ref($changes{$prefix}) eq 'HASH') {
13936:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
13937:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
13938:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
13939:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
13940:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
13941:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
13942:                                         foreach my $type (@{$types{$prefix}}) {
13943:                                             if ($changes{$prefix}{$lonhost}{$type}) {
13944:                                                 my $offloadto = &mt('None');
13945:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
13946:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
13947:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
13948:                                                     }
13949:                                                 }
13950:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
13951:                                             }
13952:                                         }
13953:                                     }
13954:                                     $resulttext .= '</li>';
13955:                                 }
13956:                             }
13957:                         } else {
13958:                             foreach my $type (@{$types{$prefix}}) {
13959:                                 if (defined($changes{$prefix}{$type})) {
13960:                                     my $newvalue;
13961:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
13962:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
13963:                                             if ($type eq 'version') {
13964:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
13965:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
13966:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
13967:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
13968:                                                 }
13969:                                             }
13970:                                         }
13971:                                     }
13972:                                     if ($newvalue eq '') {
13973:                                         if ($type eq 'version') {
13974:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
13975:                                         } else {
13976:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
13977:                                         }
13978:                                     } else {
13979:                                         if ($type eq 'version') {
13980:                                             $newvalue .= ' '.&mt('(or later)'); 
13981:                                         }
13982:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
13983:                                     }
13984:                                 }
13985:                             }
13986:                         }
13987:                         $resulttext .= '</ul>';
13988:                     }
13989:                 }
13990:                 if ($changes{'offloadnow'}) {
13991:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
13992:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
13993:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
13994:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
13995:                                 $resulttext .= '<li>'.$lonhost.'</li>';
13996:                             }
13997:                             $resulttext .= '</ul>';
13998:                         } else {
13999:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
14000:                         }
14001:                     } else {
14002:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
14003:                     }
14004:                 }
14005:                 $resulttext .= '</ul>';
14006:             } else {
14007:                 $resulttext = $nochgmsg;
14008:             }
14009:         } else {
14010:             $resulttext = '<span class="LC_error">'.
14011:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14012:         }
14013:     } else {
14014:         $resulttext = $nochgmsg;
14015:     }
14016:     return $resulttext;
14017: }
14018: 
14019: sub modify_ssl {
14020:     my ($dom,$lastactref,%domconfig) = @_;
14021:     my (%by_ip,%by_location,@intdoms,@instdoms);
14022:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
14023:     my @locations = sort(keys(%by_location));
14024:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14025:     my (%defaultshash,%changes);
14026:     my $action = 'ssl';
14027:     my @prefixes = ('connto','connfrom','replication');
14028:     foreach my $prefix (@prefixes) {
14029:         $defaultshash{$action}{$prefix} = {};
14030:     }
14031:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14032:     my $resulttext;
14033:     my %iphost = &Apache::lonnet::get_iphost();
14034:     my @reptypes = ('certreq','nocertreq');
14035:     my @connecttypes = ('dom','intdom','other');
14036:     my %types = (
14037:                   connto      => \@connecttypes,
14038:                   connfrom    => \@connecttypes,
14039:                   replication => \@reptypes,
14040:                 );
14041:     foreach my $prefix (sort(keys(%types))) {
14042:         foreach my $type (@{$types{$prefix}}) {
14043:             if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
14044:                 my $value = 'yes';
14045:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
14046:                     $value = $env{'form.'.$prefix.'_'.$type};
14047:                 }
14048:                 if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
14049:                     if ($domconfig{$action}{$prefix}{$type} ne '') {
14050:                         if ($value ne $domconfig{$action}{$prefix}{$type}) {
14051:                             $changes{$prefix}{$type} = 1;
14052:                         }
14053:                         $defaultshash{$action}{$prefix}{$type} = $value;
14054:                     } else {
14055:                         $defaultshash{$action}{$prefix}{$type} = $value;
14056:                         $changes{$prefix}{$type} = 1;
14057:                     }
14058:                 } else {
14059:                     $defaultshash{$action}{$prefix}{$type} = $value;
14060:                     $changes{$prefix}{$type} = 1;
14061:                 }
14062:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
14063:                     delete($changes{$prefix}{$type});
14064:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
14065:                     delete($changes{$prefix}{$type});
14066:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
14067:                     delete($changes{$prefix}{$type});
14068:                 }
14069:             } elsif ($prefix eq 'replication') {
14070:                 if (@locations > 0) {
14071:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
14072:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
14073:                     my @okvals;
14074:                     foreach my $val (@vals) {
14075:                         if ($val =~ /:/) {
14076:                             my @items = split(/:/,$val);
14077:                             foreach my $item (@items) {
14078:                                 if (ref($by_location{$item}) eq 'ARRAY') {
14079:                                     push(@okvals,$item);
14080:                                 }
14081:                             }
14082:                         } else {
14083:                             if (ref($by_location{$val}) eq 'ARRAY') {
14084:                                 push(@okvals,$val);
14085:                             }
14086:                         }
14087:                     }
14088:                     @okvals = sort(@okvals);
14089:                     if (ref($domconfig{$action}) eq 'HASH') {
14090:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
14091:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
14092:                                 if ($inuse == 0) {
14093:                                     $changes{$prefix}{$type} = 1;
14094:                                 } else {
14095:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
14096:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
14097:                                     if (@changed > 0) {
14098:                                         $changes{$prefix}{$type} = 1;
14099:                                     }
14100:                                 }
14101:                             } else {
14102:                                 if ($inuse == 1) {
14103:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
14104:                                     $changes{$prefix}{$type} = 1;
14105:                                 }
14106:                             }
14107:                         } else {
14108:                             if ($inuse == 1) {
14109:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
14110:                                 $changes{$prefix}{$type} = 1;
14111:                             }
14112:                         }
14113:                     } else {
14114:                         if ($inuse == 1) {
14115:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
14116:                             $changes{$prefix}{$type} = 1;
14117:                         }
14118:                     }
14119:                 }
14120:             }
14121:         }
14122:     }
14123:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
14124:     if (keys(%changes) > 0) {
14125:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
14126:                                                  $dom);
14127:         if ($putresult eq 'ok') {
14128:             if (ref($defaultshash{$action}) eq 'HASH') {
14129:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
14130:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
14131:                 }
14132:                 if (ref($defaultshash{$action}{'connto'}) eq 'HASH') {
14133:                     $domdefaults{'connto'} = $domconfig{$action}{'connto'};
14134:                 }
14135:                 if (ref($defaultshash{$action}{'connfrom'}) eq 'HASH') {
14136:                     $domdefaults{'connfrom'} = $domconfig{$action}{'connfrom'};
14137:                 }
14138:             }
14139:             my $cachetime = 24*60*60;
14140:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14141:             if (ref($lastactref) eq 'HASH') {
14142:                 $lastactref->{'domdefaults'} = 1;
14143:             }
14144:             if (keys(%changes) > 0) {
14145:                 my %titles = &ssl_titles();
14146:                 $resulttext = &mt('Changes made:').'<ul>';
14147:                 foreach my $prefix (@prefixes) {
14148:                     if (ref($changes{$prefix}) eq 'HASH') {
14149:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
14150:                         foreach my $type (@{$types{$prefix}}) {
14151:                             if (defined($changes{$prefix}{$type})) {
14152:                                 my $newvalue;
14153:                                 if (ref($defaultshash{$action}) eq 'HASH') {
14154:                                     if (ref($defaultshash{$action}{$prefix})) {
14155:                                         if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
14156:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
14157:                                         } elsif (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
14158:                                             if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
14159:                                                 $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
14160:                                             }
14161:                                         }
14162:                                     }
14163:                                     if ($newvalue eq '') {
14164:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
14165:                                     } else {
14166:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
14167:                                     }
14168:                                 }
14169:                             }
14170:                         }
14171:                         $resulttext .= '</ul>';
14172:                     }
14173:                 }
14174:             } else {
14175:                 $resulttext = $nochgmsg;
14176:             }
14177:         } else {
14178:             $resulttext = '<span class="LC_error">'.
14179:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14180:         }
14181:     } else {
14182:         $resulttext = $nochgmsg;
14183:     }
14184:     return $resulttext;
14185: }
14186: 
14187: sub modify_trust {
14188:     my ($dom,$lastactref,%domconfig) = @_;
14189:     my (%by_ip,%by_location,@intdoms,@instdoms);
14190:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
14191:     my @locations = sort(keys(%by_location));
14192:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
14193:     my @types = ('exc','inc');
14194:     my (%defaultshash,%changes);
14195:     foreach my $prefix (@prefixes) {
14196:         $defaultshash{'trust'}{$prefix} = {};
14197:     }
14198:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14199:     my $resulttext;
14200:     foreach my $prefix (@prefixes) {
14201:         foreach my $type (@types) {
14202:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
14203:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
14204:             my @okvals;
14205:             foreach my $val (@vals) {
14206:                 if ($val =~ /:/) {
14207:                     my @items = split(/:/,$val);
14208:                     foreach my $item (@items) {
14209:                         if (ref($by_location{$item}) eq 'ARRAY') {
14210:                             push(@okvals,$item);
14211:                         }
14212:                     }
14213:                 } else {
14214:                     if (ref($by_location{$val}) eq 'ARRAY') {
14215:                         push(@okvals,$val);
14216:                     }
14217:                 }
14218:             }
14219:             @okvals = sort(@okvals);
14220:             if (ref($domconfig{'trust'}) eq 'HASH') {
14221:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
14222:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
14223:                         if ($inuse == 0) {
14224:                             $changes{$prefix}{$type} = 1;
14225:                         } else {
14226:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
14227:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
14228:                             if (@changed > 0) {
14229:                                 $changes{$prefix}{$type} = 1;
14230:                             }
14231:                         }
14232:                     } else {
14233:                         if ($inuse == 1) {
14234:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
14235:                             $changes{$prefix}{$type} = 1;
14236:                         }
14237:                     }
14238:                 } else {
14239:                     if ($inuse == 1) {
14240:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
14241:                         $changes{$prefix}{$type} = 1;
14242:                     }
14243:                 }
14244:             } else {
14245:                 if ($inuse == 1) {
14246:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
14247:                     $changes{$prefix}{$type} = 1;
14248:                 }
14249:             }
14250:         }
14251:     }
14252:     my $nochgmsg = &mt('No changes made to trust settings.');
14253:     if (keys(%changes) > 0) {
14254:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
14255:                                                  $dom);
14256:         if ($putresult eq 'ok') {
14257:             if (ref($defaultshash{'trust'}) eq 'HASH') {
14258:                 foreach my $prefix (@prefixes) {
14259:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
14260:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
14261:                     }
14262:                 }
14263:             }
14264:             my $cachetime = 24*60*60;
14265:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14266:             if (ref($lastactref) eq 'HASH') {
14267:                 $lastactref->{'domdefaults'} = 1;
14268:             }
14269:             if (keys(%changes) > 0) {
14270:                 my %lt = &trust_titles();
14271:                 $resulttext = &mt('Changes made:').'<ul>';
14272:                 foreach my $prefix (@prefixes) {
14273:                     if (ref($changes{$prefix}) eq 'HASH') {
14274:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
14275:                         foreach my $type (@types) {
14276:                             if (defined($changes{$prefix}{$type})) {
14277:                                 my $newvalue;
14278:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
14279:                                     if (ref($defaultshash{'trust'}{$prefix})) {
14280:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
14281:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
14282:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
14283:                                             }
14284:                                         }
14285:                                     }
14286:                                 }
14287:                                 if ($newvalue eq '') {
14288:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
14289:                                 } else {
14290:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
14291:                                 }
14292:                             }
14293:                         }
14294:                         $resulttext .= '</ul>';
14295:                     }
14296:                 }
14297:                 $resulttext .= '</ul>';
14298:             } else {
14299:                 $resulttext = $nochgmsg;
14300:             }
14301:         } else {
14302:             $resulttext = '<span class="LC_error">'.
14303:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14304:         }
14305:     } else {
14306:         $resulttext = $nochgmsg;
14307:     }
14308:     return $resulttext;
14309: }
14310: 
14311: sub modify_loadbalancing {
14312:     my ($dom,%domconfig) = @_;
14313:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
14314:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
14315:     my ($othertitle,$usertypes,$types) =
14316:         &Apache::loncommon::sorted_inst_types($dom);
14317:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14318:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
14319:     my @sparestypes = ('primary','default');
14320:     my %typetitles = &sparestype_titles();
14321:     my $resulttext;
14322:     my (%currbalancer,%currtargets,%currrules,%existing);
14323:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
14324:         %existing = %{$domconfig{'loadbalancing'}};
14325:     }
14326:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
14327:                               \%currtargets,\%currrules);
14328:     my ($saveloadbalancing,%defaultshash,%changes);
14329:     my ($alltypes,$othertypes,$titles) =
14330:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
14331:     my %ruletitles = &offloadtype_text();
14332:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
14333:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
14334:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
14335:         if ($balancer eq '') {
14336:             next;
14337:         }
14338:         if (!exists($servers{$balancer})) {
14339:             if (exists($currbalancer{$balancer})) {
14340:                 push(@{$changes{'delete'}},$balancer);
14341:             }
14342:             next;
14343:         }
14344:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
14345:             push(@{$changes{'delete'}},$balancer);
14346:             next;
14347:         }
14348:         if (!exists($currbalancer{$balancer})) {
14349:             push(@{$changes{'add'}},$balancer);
14350:         }
14351:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
14352:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
14353:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
14354:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
14355:             $saveloadbalancing = 1;
14356:         }
14357:         foreach my $sparetype (@sparestypes) {
14358:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
14359:             my @offloadto;
14360:             foreach my $target (@targets) {
14361:                 if (($servers{$target}) && ($target ne $balancer)) {
14362:                     if ($sparetype eq 'default') {
14363:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
14364:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
14365:                         }
14366:                     }
14367:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
14368:                         push(@offloadto,$target);
14369:                     }
14370:                 }
14371:             }
14372:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
14373:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
14374:                     push(@offloadto,$balancer);
14375:                 }
14376:             }
14377:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
14378:         }
14379:         if (ref($currtargets{$balancer}) eq 'HASH') {
14380:             foreach my $sparetype (@sparestypes) {
14381:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
14382:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
14383:                     if (@targetdiffs > 0) {
14384:                         $changes{'curr'}{$balancer}{'targets'} = 1;
14385:                     }
14386:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14387:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14388:                         $changes{'curr'}{$balancer}{'targets'} = 1;
14389:                     }
14390:                 }
14391:             }
14392:         } else {
14393:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
14394:                 foreach my $sparetype (@sparestypes) {
14395:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14396:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14397:                             $changes{'curr'}{$balancer}{'targets'} = 1;
14398:                         }
14399:                     }
14400:                 }
14401:             }
14402:         }
14403:         my $ishomedom;
14404:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
14405:             $ishomedom = 1;
14406:         }
14407:         if (ref($alltypes) eq 'ARRAY') {
14408:             foreach my $type (@{$alltypes}) {
14409:                 my $rule;
14410:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
14411:                          (!$ishomedom)) {
14412:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
14413:                 }
14414:                 if ($rule eq 'specific') {
14415:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
14416:                     if (exists($servers{$specifiedhost})) {
14417:                         $rule = $specifiedhost;
14418:                     }
14419:                 }
14420:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
14421:                 if (ref($currrules{$balancer}) eq 'HASH') {
14422:                     if ($rule ne $currrules{$balancer}{$type}) {
14423:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
14424:                     }
14425:                 } elsif ($rule ne '') {
14426:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
14427:                 }
14428:             }
14429:         }
14430:     }
14431:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
14432:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
14433:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
14434:             $defaultshash{'loadbalancing'} = {};
14435:         }
14436:         my $putresult = &Apache::lonnet::put_dom('configuration',
14437:                                                  \%defaultshash,$dom);
14438:         if ($putresult eq 'ok') {
14439:             if (keys(%changes) > 0) {
14440:                 my %toupdate;
14441:                 if (ref($changes{'delete'}) eq 'ARRAY') {
14442:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
14443:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
14444:                         $toupdate{$balancer} = 1;
14445:                     }
14446:                 }
14447:                 if (ref($changes{'add'}) eq 'ARRAY') {
14448:                     foreach my $balancer (sort(@{$changes{'add'}})) {
14449:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
14450:                         $toupdate{$balancer} = 1;
14451:                     }
14452:                 }
14453:                 if (ref($changes{'curr'}) eq 'HASH') {
14454:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
14455:                         $toupdate{$balancer} = 1;
14456:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
14457:                             if ($changes{'curr'}{$balancer}{'targets'}) {
14458:                                 my %offloadstr;
14459:                                 foreach my $sparetype (@sparestypes) {
14460:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14461:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14462:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
14463:                                         }
14464:                                     }
14465:                                 }
14466:                                 if (keys(%offloadstr) == 0) {
14467:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
14468:                                 } else {
14469:                                     my $showoffload;
14470:                                     foreach my $sparetype (@sparestypes) {
14471:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
14472:                                         if (defined($offloadstr{$sparetype})) {
14473:                                             $showoffload .= $offloadstr{$sparetype};
14474:                                         } else {
14475:                                             $showoffload .= &mt('None');
14476:                                         }
14477:                                         $showoffload .= ('&nbsp;'x3);
14478:                                     }
14479:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
14480:                                 }
14481:                             }
14482:                         }
14483:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
14484:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
14485:                                 foreach my $type (@{$alltypes}) {
14486:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
14487:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
14488:                                         my $balancetext;
14489:                                         if ($rule eq '') {
14490:                                             $balancetext =  $ruletitles{'default'};
14491:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
14492:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
14493:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
14494:                                                 foreach my $sparetype (@sparestypes) {
14495:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14496:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
14497:                                                     }
14498:                                                 }
14499:                                                 foreach my $item (@{$alltypes}) {
14500:                                                     next if ($item =~  /^_LC_ipchange/);
14501:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
14502:                                                     if ($hasrule eq 'homeserver') {
14503:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
14504:                                                     } else {
14505:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
14506:                                                             if ($servers{$hasrule}) {
14507:                                                                 $toupdate{$hasrule} = 1;
14508:                                                             }
14509:                                                         }
14510:                                                     }
14511:                                                 }
14512:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
14513:                                                     $balancetext =  $ruletitles{$rule};
14514:                                                 } else {
14515:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
14516:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
14517:                                                     if ($receiver) {
14518:                                                         $toupdate{$receiver};
14519:                                                     }
14520:                                                 }
14521:                                             } else {
14522:                                                 $balancetext =  $ruletitles{$rule};
14523:                                             }
14524:                                         } else {
14525:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
14526:                                         }
14527:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
14528:                                     }
14529:                                 }
14530:                             }
14531:                         }
14532:                         if (keys(%toupdate)) {
14533:                             my %thismachine;
14534:                             my $updatedhere;
14535:                             my $cachetime = 60*60*24;
14536:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
14537:                             foreach my $lonhost (keys(%toupdate)) {
14538:                                 if ($thismachine{$lonhost}) {
14539:                                     unless ($updatedhere) {
14540:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
14541:                                                                       $defaultshash{'loadbalancing'},
14542:                                                                       $cachetime);
14543:                                         $updatedhere = 1;
14544:                                     }
14545:                                 } else {
14546:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
14547:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
14548:                                 }
14549:                             }
14550:                         }
14551:                     }
14552:                 }
14553:                 if ($resulttext ne '') {
14554:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
14555:                 } else {
14556:                     $resulttext = $nochgmsg;
14557:                 }
14558:             } else {
14559:                 $resulttext = $nochgmsg;
14560:             }
14561:         } else {
14562:             $resulttext = '<span class="LC_error">'.
14563:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14564:         }
14565:     } else {
14566:         $resulttext = $nochgmsg;
14567:     }
14568:     return $resulttext;
14569: }
14570: 
14571: sub recurse_check {
14572:     my ($chkcats,$categories,$depth,$name) = @_;
14573:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
14574:         my $chg = 0;
14575:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
14576:             my $category = $chkcats->[$depth]{$name}[$j];
14577:             my $item;
14578:             if ($category eq '') {
14579:                 $chg ++;
14580:             } else {
14581:                 my $deeper = $depth + 1;
14582:                 $item = &escape($category).':'.&escape($name).':'.$depth;
14583:                 if ($chg) {
14584:                     $categories->{$item} -= $chg;
14585:                 }
14586:                 &recurse_check($chkcats,$categories,$deeper,$category);
14587:                 $deeper --;
14588:             }
14589:         }
14590:     }
14591:     return;
14592: }
14593: 
14594: sub recurse_cat_deletes {
14595:     my ($item,$coursecategories,$deletions) = @_;
14596:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
14597:     my $subdepth = $depth + 1;
14598:     if (ref($coursecategories) eq 'HASH') {
14599:         foreach my $subitem (keys(%{$coursecategories})) {
14600:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
14601:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
14602:                 delete($coursecategories->{$subitem});
14603:                 $deletions->{$subitem} = 1;
14604:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
14605:             }
14606:         }
14607:     }
14608:     return;
14609: }
14610: 
14611: sub active_dc_picker {
14612:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
14613:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
14614:     my @domcoord = keys(%domcoords);
14615:     if (keys(%currhash)) {
14616:         foreach my $dc (keys(%currhash)) {
14617:             unless (exists($domcoords{$dc})) {
14618:                 push(@domcoord,$dc);
14619:             }
14620:         }
14621:     }
14622:     @domcoord = sort(@domcoord);
14623:     my $numdcs = scalar(@domcoord);
14624:     my $rows = 0;
14625:     my $table;
14626:     if ($numdcs > 1) {
14627:         $table = '<table>';
14628:         for (my $i=0; $i<@domcoord; $i++) {
14629:             my $rem = $i%($numinrow);
14630:             if ($rem == 0) {
14631:                 if ($i > 0) {
14632:                     $table .= '</tr>';
14633:                 }
14634:                 $table .= '<tr>';
14635:                 $rows ++;
14636:             }
14637:             my $check = '';
14638:             if ($inputtype eq 'radio') {
14639:                 if (keys(%currhash) == 0) {
14640:                     if (!$i) {
14641:                         $check = ' checked="checked"';
14642:                     }
14643:                 } elsif (exists($currhash{$domcoord[$i]})) {
14644:                     $check = ' checked="checked"';
14645:                 }
14646:             } else {
14647:                 if (exists($currhash{$domcoord[$i]})) {
14648:                     $check = ' checked="checked"';
14649:                 }
14650:             }
14651:             if ($i == @domcoord - 1) {
14652:                 my $colsleft = $numinrow - $rem;
14653:                 if ($colsleft > 1) {
14654:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
14655:                 } else {
14656:                     $table .= '<td class="LC_left_item">';
14657:                 }
14658:             } else {
14659:                 $table .= '<td class="LC_left_item">';
14660:             }
14661:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
14662:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
14663:             $table .= '<span class="LC_nobreak"><label>'.
14664:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
14665:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
14666:             if ($user ne $dcname.':'.$dcdom) {
14667:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14668:             }
14669:             $table .= '</label></span></td>';
14670:         }
14671:         $table .= '</tr></table>';
14672:     } elsif ($numdcs == 1) {
14673:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
14674:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
14675:         if ($inputtype eq 'radio') {
14676:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
14677:             if ($user ne $dcname.':'.$dcdom) {
14678:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14679:             }
14680:         } else {
14681:             my $check;
14682:             if (exists($currhash{$domcoord[0]})) {
14683:                 $check = ' checked="checked"';
14684:             }
14685:             $table = '<span class="LC_nobreak"><label>'.
14686:                      '<input type="checkbox" name="'.$name.'" '.
14687:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
14688:             if ($user ne $dcname.':'.$dcdom) {
14689:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14690:             }
14691:             $table .= '</label></span>';
14692:             $rows ++;
14693:         }
14694:     }
14695:     return ($numdcs,$table,$rows);
14696: }
14697: 
14698: sub usersession_titles {
14699:     return &Apache::lonlocal::texthash(
14700:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
14701:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
14702:                spares => 'Servers offloaded to, when busy',
14703:                version => 'LON-CAPA version requirement',
14704:                excludedomain => 'Allow all, but exclude specific domains',
14705:                includedomain => 'Deny all, but include specific domains',
14706:                primary => 'Primary (checked first)',
14707:                default => 'Default',
14708:            );
14709: }
14710: 
14711: sub id_for_thisdom {
14712:     my (%servers) = @_;
14713:     my %altids;
14714:     foreach my $server (keys(%servers)) {
14715:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
14716:         if ($serverhome ne $server) {
14717:             $altids{$serverhome} = $server;
14718:         }
14719:     }
14720:     return %altids;
14721: }
14722: 
14723: sub count_servers {
14724:     my ($currbalancer,%servers) = @_;
14725:     my (@spares,$numspares);
14726:     foreach my $lonhost (sort(keys(%servers))) {
14727:         next if ($currbalancer eq $lonhost);
14728:         push(@spares,$lonhost);
14729:     }
14730:     if ($currbalancer) {
14731:         $numspares = scalar(@spares);
14732:     } else {
14733:         $numspares = scalar(@spares) - 1;
14734:     }
14735:     return ($numspares,@spares);
14736: }
14737: 
14738: sub lonbalance_targets_js {
14739:     my ($dom,$types,$servers,$settings) = @_;
14740:     my $select = &mt('Select');
14741:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
14742:     if (ref($servers) eq 'HASH') {
14743:         $alltargets = join("','",sort(keys(%{$servers})));
14744:         my @homedoms;
14745:         foreach my $server (sort(keys(%{$servers}))) {
14746:             if (&Apache::lonnet::host_domain($server) eq $dom) {
14747:                 push(@homedoms,'1');
14748:             } else {
14749:                 push(@homedoms,'0');
14750:             }
14751:         }
14752:         $allishome = join("','",@homedoms);
14753:     }
14754:     if (ref($types) eq 'ARRAY') {
14755:         if (@{$types} > 0) {
14756:             @alltypes = @{$types};
14757:         }
14758:     }
14759:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
14760:     $allinsttypes = join("','",@alltypes);
14761:     my (%currbalancer,%currtargets,%currrules,%existing);
14762:     if (ref($settings) eq 'HASH') {
14763:         %existing = %{$settings};
14764:     }
14765:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
14766:                               \%currtargets,\%currrules);
14767:     my $balancers = join("','",sort(keys(%currbalancer)));
14768:     return <<"END";
14769: 
14770: <script type="text/javascript">
14771: // <![CDATA[
14772: 
14773: currBalancers = new Array('$balancers');
14774: 
14775: function toggleTargets(balnum) {
14776:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
14777:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
14778:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
14779:     var prevbalancer = prevhostitem.value;
14780:     var baltotal = document.getElementById('loadbalancing_total').value;
14781:     prevhostitem.value = balancer;
14782:     if (prevbalancer != '') {
14783:         var prevIdx = currBalancers.indexOf(prevbalancer);
14784:         if (prevIdx != -1) {
14785:             currBalancers.splice(prevIdx,1);
14786:         }
14787:     }
14788:     if (balancer == '') {
14789:         hideSpares(balnum);
14790:     } else {
14791:         var currIdx = currBalancers.indexOf(balancer);
14792:         if (currIdx == -1) {
14793:             currBalancers.push(balancer);
14794:         }
14795:         var homedoms = new Array('$allishome');
14796:         var ishomedom = homedoms[lonhostitem.selectedIndex];
14797:         showSpares(balancer,ishomedom,balnum);
14798:     }
14799:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
14800:     return;
14801: }
14802: 
14803: function showSpares(balancer,ishomedom,balnum) {
14804:     var alltargets = new Array('$alltargets');
14805:     var insttypes = new Array('$allinsttypes');
14806:     var offloadtypes = new Array('primary','default');
14807: 
14808:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
14809:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
14810:  
14811:     for (var i=0; i<offloadtypes.length; i++) {
14812:         var count = 0;
14813:         for (var j=0; j<alltargets.length; j++) {
14814:             if (alltargets[j] != balancer) {
14815:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
14816:                 item.value = alltargets[j];
14817:                 item.style.textAlign='left';
14818:                 item.style.textFace='normal';
14819:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
14820:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
14821:                     item.disabled = '';
14822:                 } else {
14823:                     item.disabled = 'disabled';
14824:                     item.checked = false;
14825:                 }
14826:                 count ++;
14827:             }
14828:         }
14829:     }
14830:     for (var k=0; k<insttypes.length; k++) {
14831:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
14832:             if (ishomedom == 1) {
14833:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
14834:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
14835:             } else {
14836:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
14837:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
14838:             }
14839:         } else {
14840:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
14841:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
14842:         }
14843:         if ((insttypes[k] != '_LC_external') && 
14844:             ((insttypes[k] != '_LC_internetdom') ||
14845:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
14846:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
14847:             item.options.length = 0;
14848:             item.options[0] = new Option("","",true,true);
14849:             var idx = 0;
14850:             for (var m=0; m<alltargets.length; m++) {
14851:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
14852:                     idx ++;
14853:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
14854:                 }
14855:             }
14856:         }
14857:     }
14858:     return;
14859: }
14860: 
14861: function hideSpares(balnum) {
14862:     var alltargets = new Array('$alltargets');
14863:     var insttypes = new Array('$allinsttypes');
14864:     var offloadtypes = new Array('primary','default');
14865: 
14866:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
14867:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
14868: 
14869:     var total = alltargets.length - 1;
14870:     for (var i=0; i<offloadtypes; i++) {
14871:         for (var j=0; j<total; j++) {
14872:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
14873:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
14874:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
14875:         }
14876:     }
14877:     for (var k=0; k<insttypes.length; k++) {
14878:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
14879:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
14880:         if (insttypes[k] != '_LC_external') {
14881:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
14882:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
14883:         }
14884:     }
14885:     return;
14886: }
14887: 
14888: function checkOffloads(item,balnum,type) {
14889:     var alltargets = new Array('$alltargets');
14890:     var offloadtypes = new Array('primary','default');
14891:     if (item.checked) {
14892:         var total = alltargets.length - 1;
14893:         var other;
14894:         if (type == offloadtypes[0]) {
14895:             other = offloadtypes[1];
14896:         } else {
14897:             other = offloadtypes[0];
14898:         }
14899:         for (var i=0; i<total; i++) {
14900:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
14901:             if (server == item.value) {
14902:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
14903:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
14904:                 }
14905:             }
14906:         }
14907:     }
14908:     return;
14909: }
14910: 
14911: function singleServerToggle(balnum,type) {
14912:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
14913:     if (offloadtoSelIdx == 0) {
14914:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
14915:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
14916: 
14917:     } else {
14918:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
14919:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
14920:     }
14921:     return;
14922: }
14923: 
14924: function balanceruleChange(formname,balnum,type) {
14925:     if (type == '_LC_external') {
14926:         return;
14927:     }
14928:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
14929:     for (var i=0; i<typesRules.length; i++) {
14930:         if (formname.elements[typesRules[i]].checked) {
14931:             if (formname.elements[typesRules[i]].value != 'specific') {
14932:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
14933:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
14934:             } else {
14935:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
14936:             }
14937:         }
14938:     }
14939:     return;
14940: }
14941: 
14942: function balancerDeleteChange(balnum) {
14943:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
14944:     var baltotal = document.getElementById('loadbalancing_total').value;
14945:     var addtarget;
14946:     var removetarget;
14947:     var action = 'delete';
14948:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
14949:         var lonhost = hostitem.value;
14950:         var currIdx = currBalancers.indexOf(lonhost);
14951:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
14952:             if (currIdx != -1) {
14953:                 currBalancers.splice(currIdx,1);
14954:             }
14955:             addtarget = lonhost;
14956:         } else {
14957:             if (currIdx == -1) {
14958:                 currBalancers.push(lonhost);
14959:             }
14960:             removetarget = lonhost;
14961:             action = 'undelete';
14962:         }
14963:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
14964:     }
14965:     return;
14966: }
14967: 
14968: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
14969:     if (baltotal > 1) {
14970:         var offloadtypes = new Array('primary','default');
14971:         var alltargets = new Array('$alltargets');
14972:         var insttypes = new Array('$allinsttypes');
14973:         for (var i=0; i<baltotal; i++) {
14974:             if (i != balnum) {
14975:                 for (var j=0; j<offloadtypes.length; j++) {
14976:                     var total = alltargets.length - 1;
14977:                     for (var k=0; k<total; k++) {
14978:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
14979:                         var server = serveritem.value;
14980:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
14981:                             if (server == addtarget) {
14982:                                 serveritem.disabled = '';
14983:                             }
14984:                         }
14985:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
14986:                             if (server == removetarget) {
14987:                                 serveritem.disabled = 'disabled';
14988:                                 serveritem.checked = false;
14989:                             }
14990:                         }
14991:                     }
14992:                 }
14993:                 for (var j=0; j<insttypes.length; j++) {
14994:                     if (insttypes[j] != '_LC_external') {
14995:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
14996:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
14997:                             var currSel = singleserver.selectedIndex;
14998:                             var currVal = singleserver.options[currSel].value;
14999:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
15000:                                 var numoptions = singleserver.options.length;
15001:                                 var needsnew = 1;
15002:                                 for (var k=0; k<numoptions; k++) {
15003:                                     if (singleserver.options[k] == addtarget) {
15004:                                         needsnew = 0;
15005:                                         break;
15006:                                     }
15007:                                 }
15008:                                 if (needsnew == 1) {
15009:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
15010:                                 }
15011:                             }
15012:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
15013:                                 singleserver.options.length = 0;
15014:                                 if ((currVal) && (currVal != removetarget)) {
15015:                                     singleserver.options[0] = new Option("","",false,false);
15016:                                 } else {
15017:                                     singleserver.options[0] = new Option("","",true,true);
15018:                                 }
15019:                                 var idx = 0;
15020:                                 for (var m=0; m<alltargets.length; m++) {
15021:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
15022:                                         idx ++;
15023:                                         if (currVal == alltargets[m]) {
15024:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
15025:                                         } else {
15026:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
15027:                                         }
15028:                                     }
15029:                                 }
15030:                             }
15031:                         }
15032:                     }
15033:                 }
15034:             }
15035:         }
15036:     }
15037:     return;
15038: }
15039: 
15040: // ]]>
15041: </script>
15042: 
15043: END
15044: }
15045: 
15046: sub new_spares_js {
15047:     my @sparestypes = ('primary','default');
15048:     my $types = join("','",@sparestypes);
15049:     my $select = &mt('Select');
15050:     return <<"END";
15051: 
15052: <script type="text/javascript">
15053: // <![CDATA[
15054: 
15055: function updateNewSpares(formname,lonhost) {
15056:     var types = new Array('$types');
15057:     var include = new Array();
15058:     var exclude = new Array();
15059:     for (var i=0; i<types.length; i++) {
15060:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
15061:         for (var j=0; j<spareboxes.length; j++) {
15062:             if (formname.elements[spareboxes[j]].checked) {
15063:                 exclude.push(formname.elements[spareboxes[j]].value);
15064:             } else {
15065:                 include.push(formname.elements[spareboxes[j]].value);
15066:             }
15067:         }
15068:     }
15069:     for (var i=0; i<types.length; i++) {
15070:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
15071:         var selIdx = newSpare.selectedIndex;
15072:         var currnew = newSpare.options[selIdx].value;
15073:         var okSpares = new Array();
15074:         for (var j=0; j<newSpare.options.length; j++) {
15075:             var possible = newSpare.options[j].value;
15076:             if (possible != '') {
15077:                 if (exclude.indexOf(possible) == -1) {
15078:                     okSpares.push(possible);
15079:                 } else {
15080:                     if (currnew == possible) {
15081:                         selIdx = 0;
15082:                     }
15083:                 }
15084:             }
15085:         }
15086:         for (var k=0; k<include.length; k++) {
15087:             if (okSpares.indexOf(include[k]) == -1) {
15088:                 okSpares.push(include[k]);
15089:             }
15090:         }
15091:         okSpares.sort();
15092:         newSpare.options.length = 0;
15093:         if (selIdx == 0) {
15094:             newSpare.options[0] = new Option("$select","",true,true);
15095:         } else {
15096:             newSpare.options[0] = new Option("$select","",false,false);
15097:         }
15098:         for (var m=0; m<okSpares.length; m++) {
15099:             var idx = m+1;
15100:             var selThis = 0;
15101:             if (selIdx != 0) {
15102:                 if (okSpares[m] == currnew) {
15103:                     selThis = 1;
15104:                 }
15105:             }
15106:             if (selThis == 1) {
15107:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
15108:             } else {
15109:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
15110:             }
15111:         }
15112:     }
15113:     return;
15114: }
15115: 
15116: function checkNewSpares(lonhost,type) {
15117:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
15118:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
15119:     if (chosen != '') { 
15120:         var othertype;
15121:         var othernewSpare;
15122:         if (type == 'primary') {
15123:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
15124:         }
15125:         if (type == 'default') {
15126:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
15127:         }
15128:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
15129:             othernewSpare.selectedIndex = 0;
15130:         }
15131:     }
15132:     return;
15133: }
15134: 
15135: // ]]>
15136: </script>
15137: 
15138: END
15139: 
15140: }
15141: 
15142: sub common_domprefs_js {
15143:     return <<"END";
15144: 
15145: <script type="text/javascript">
15146: // <![CDATA[
15147: 
15148: function getIndicesByName(formname,item) {
15149:     var group = new Array();
15150:     for (var i=0;i<formname.elements.length;i++) {
15151:         if (formname.elements[i].name == item) {
15152:             group.push(formname.elements[i].id);
15153:         }
15154:     }
15155:     return group;
15156: }
15157: 
15158: // ]]>
15159: </script>
15160: 
15161: END
15162: 
15163: }
15164: 
15165: sub recaptcha_js {
15166:     my %lt = &captcha_phrases();
15167:     return <<"END";
15168: 
15169: <script type="text/javascript">
15170: // <![CDATA[
15171: 
15172: function updateCaptcha(caller,context) {
15173:     var privitem;
15174:     var pubitem;
15175:     var privtext;
15176:     var pubtext;
15177:     var versionitem;
15178:     var versiontext;
15179:     if (document.getElementById(context+'_recaptchapub')) {
15180:         pubitem = document.getElementById(context+'_recaptchapub');
15181:     } else {
15182:         return;
15183:     }
15184:     if (document.getElementById(context+'_recaptchapriv')) {
15185:         privitem = document.getElementById(context+'_recaptchapriv');
15186:     } else {
15187:         return;
15188:     }
15189:     if (document.getElementById(context+'_recaptchapubtxt')) {
15190:         pubtext = document.getElementById(context+'_recaptchapubtxt');
15191:     } else {
15192:         return;
15193:     }
15194:     if (document.getElementById(context+'_recaptchaprivtxt')) {
15195:         privtext = document.getElementById(context+'_recaptchaprivtxt');
15196:     } else {
15197:         return;
15198:     }
15199:     if (document.getElementById(context+'_recaptchaversion')) {
15200:         versionitem = document.getElementById(context+'_recaptchaversion');
15201:     } else {
15202:         return;
15203:     }
15204:     if (document.getElementById(context+'_recaptchavertxt')) {
15205:         versiontext = document.getElementById(context+'_recaptchavertxt');
15206:     } else {
15207:         return;
15208:     }
15209:     if (caller.checked) {
15210:         if (caller.value == 'recaptcha') {
15211:             pubitem.type = 'text';
15212:             privitem.type = 'text';
15213:             pubitem.size = '40';
15214:             privitem.size = '40';
15215:             pubtext.innerHTML = "$lt{'pub'}";
15216:             privtext.innerHTML = "$lt{'priv'}";
15217:             versionitem.type = 'text';
15218:             versionitem.size = '3';
15219:             versiontext.innerHTML = "$lt{'ver'}";
15220:         } else {
15221:             pubitem.type = 'hidden';
15222:             privitem.type = 'hidden';
15223:             versionitem.type = 'hidden';
15224:             pubtext.innerHTML = '';
15225:             privtext.innerHTML = '';
15226:             versiontext.innerHTML = '';
15227:         }
15228:     }
15229:     return;
15230: }
15231: 
15232: // ]]>
15233: </script>
15234: 
15235: END
15236: 
15237: }
15238: 
15239: sub toggle_display_js {
15240:     return <<"END";
15241: 
15242: <script type="text/javascript">
15243: // <![CDATA[
15244: 
15245: function toggleDisplay(domForm,caller) {
15246:     if (document.getElementById(caller)) {
15247:         var divitem = document.getElementById(caller);
15248:         var optionsElement = domForm.coursecredits;
15249:         var checkval = 1;
15250:         var dispval = 'block';
15251:         if (caller == 'emailoptions') {
15252:             optionsElement = domForm.cancreate_email; 
15253:         }
15254:         if (caller == 'studentsubmission') {
15255:             optionsElement = domForm.postsubmit;
15256:         }
15257:         if (caller == 'cloneinstcode') {
15258:             optionsElement = domForm.canclone;
15259:             checkval = 'instcode';
15260:         }
15261:         if (optionsElement.length) {
15262:             var currval;
15263:             for (var i=0; i<optionsElement.length; i++) {
15264:                 if (optionsElement[i].checked) {
15265:                    currval = optionsElement[i].value;
15266:                 }
15267:             }
15268:             if (currval == checkval) {
15269:                 divitem.style.display = dispval;
15270:             } else {
15271:                 divitem.style.display = 'none';
15272:             }
15273:         }
15274:     }
15275:     return;
15276: }
15277: 
15278: // ]]>
15279: </script>
15280: 
15281: END
15282: 
15283: }
15284: 
15285: sub captcha_phrases {
15286:     return &Apache::lonlocal::texthash (
15287:                  priv => 'Private key',
15288:                  pub  => 'Public key',
15289:                  original  => 'original (CAPTCHA)',
15290:                  recaptcha => 'successor (ReCAPTCHA)',
15291:                  notused   => 'unused',
15292:                  ver => 'ReCAPTCHA version (1 or 2)',
15293:     );
15294: }
15295: 
15296: sub devalidate_remote_domconfs {
15297:     my ($dom,$cachekeys) = @_;
15298:     return unless (ref($cachekeys) eq 'HASH');
15299:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
15300:     my %thismachine;
15301:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
15302:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions','directorysrch');
15303:     if (keys(%servers)) {
15304:         foreach my $server (keys(%servers)) {
15305:             next if ($thismachine{$server});
15306:             my @cached;
15307:             foreach my $name (@posscached) {
15308:                 if ($cachekeys->{$name}) {
15309:                     push(@cached,&escape($name).':'.&escape($dom));
15310:                 }
15311:             }
15312:             if (@cached) {
15313:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
15314:             }
15315:         }
15316:     }
15317:     return;
15318: }
15319: 
15320: 1;

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