File:  [LON-CAPA] / loncom / interface / domainprefs.pm
Revision 1.299: download - view: text, annotated - select for diffs
Fri May 19 19:18:22 2017 UTC (7 years, 1 month ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Facilitate localization.

    1: # The LearningOnline Network with CAPA
    2: # Handler to set domain-wide configuration settings
    3: #
    4: # $Id: domainprefs.pm,v 1.299 2017/05/19 19:18:22 raeburn Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: #
   29: ###############################################################
   30: ###############################################################
   31: 
   32: =pod
   33: 
   34: =head1 NAME
   35: 
   36: Apache::domainprefs.pm
   37: 
   38: =head1 SYNOPSIS
   39: 
   40: Handles configuration of a LON-CAPA domain.  
   41: 
   42: This is part of the LearningOnline Network with CAPA project
   43: described at http://www.lon-capa.org.
   44: 
   45: 
   46: =head1 OVERVIEW
   47: 
   48: Each institution using LON-CAPA will typically have a single domain designated 
   49: for use by individuals affiliated with the institution.  Accordingly, each domain
   50: may define a default set of logos and a color scheme which can be used to "brand"
   51: the LON-CAPA instance. In addition, an institution will typically have a language
   52: and timezone which are used for the majority of courses.
   53: 
   54: LON-CAPA provides a mechanism to display and modify these defaults, as well as a 
   55: host of other domain-wide settings which determine the types of functionality
   56: available to users and courses in the domain.
   57: 
   58: There is also a mechanism to configure cataloging of courses in the domain, and
   59: controls on the operation of automated processes which govern such things as
   60: roster updates, user directory updates and processing of course requests.
   61: 
   62: The domain coordination manual which is built dynamically on install/update of 
   63: LON-CAPA from the relevant help items provides more information about domain 
   64: configuration.
   65: 
   66: Most of the domain settings are stored in the configuration.db GDBM file which is
   67: housed on the primary library server for the domain in /home/httpd/lonUsers/$dom,
   68: where $dom is the domain.  The configuration.db stores settings in a number of 
   69: frozen hashes of hashes.  In a few cases, domain information must be uploaded to
   70: the domain as files (e.g., image files for logos etc., or plain text files for
   71: bubblesheet formats).  In this case the domainprefs.pm must be running in a user
   72: session hosted on the primary library server in the domain, as these files are 
   73: stored in author space belonging to a special $dom-domainconfig user.   
   74: 
   75: domainprefs.pm in combination with lonconfigsettings.pm will retrieve and display
   76: the current settings, and provides an interface to make modifications.
   77: 
   78: =head1 SUBROUTINES
   79: 
   80: =over
   81: 
   82: =item print_quotas()
   83: 
   84: Inputs: 4 
   85: 
   86: $dom,$settings,$rowtotal,$action.
   87: 
   88: $dom is the domain, $settings is a reference to a hash of current settings for
   89: the current context, $rowtotal is a reference to the scalar used to record the 
   90: number of rows displayed on the page, and $action is the context (quotas, 
   91: requestcourses or requestauthor).
   92: 
   93: The print_quotas routine was orginally created to display/store information
   94: about default quota sizes for portfolio spaces for the different types of 
   95: institutional affiliation in the domain (e.g., Faculty, Staff, Student etc.), 
   96: but is now also used to manage availability of user tools: 
   97: i.e., blogs, aboutme page, and portfolios, and the course request tool,
   98: used by course owners to request creation of a course, and to display/store
   99: default quota sizes for Authoring Spaces.
  100: 
  101: Outputs: 1
  102: 
  103: $datatable  - HTML containing form elements which allow settings to be changed. 
  104: 
  105: In the case of course requests, radio buttons are displayed for each institutional
  106: affiliate type (and also default, and _LC_adv) for each of the course types 
  107: (official, unofficial, community, textbook, and placement).  
  108: In each case the radio buttons allow the selection of one of four values:  
  109: 
  110: 0, approval, validate, autolimit=N (where N is blank, or a positive integer).
  111: which have the following effects:
  112: 
  113: 0
  114: 
  115: =over
  116: 
  117: - course requests are not allowed for this course types/affiliation
  118: 
  119: =back
  120: 
  121: approval 
  122: 
  123: =over 
  124: 
  125: - course requests must be approved by a Doman Coordinator in the 
  126: course's domain
  127: 
  128: =back
  129: 
  130: validate 
  131: 
  132: =over
  133: 
  134: - an institutional validation (e.g., check requestor is instructor
  135: of record) needs to be passed before the course will be created.  The required
  136: validation is in localenroll.pm on the primary library server for the course 
  137: domain.
  138: 
  139: =back
  140: 
  141: autolimit 
  142: 
  143: =over
  144:  
  145: - course requests will be processed automatically up to a limit of
  146: N requests for the course type for the particular requestor.
  147: If N is undefined, there is no limit to the number of course requests
  148: which a course owner may submit and have processed automatically. 
  149: 
  150: =back
  151: 
  152: =item modify_quotas() 
  153: 
  154: =back
  155: 
  156: =cut
  157: 
  158: package Apache::domainprefs;
  159: 
  160: use strict;
  161: use Apache::Constants qw(:common :http);
  162: use Apache::lonnet;
  163: use Apache::loncommon();
  164: use Apache::lonhtmlcommon();
  165: use Apache::lonlocal;
  166: use Apache::lonmsg();
  167: use Apache::lonconfigsettings;
  168: use Apache::lonuserutils();
  169: use Apache::loncoursequeueadmin();
  170: use LONCAPA qw(:DEFAULT :match);
  171: use LONCAPA::Enrollment;
  172: use LONCAPA::lonauthcgi();
  173: use LONCAPA::SSL;
  174: use File::Copy;
  175: use Locale::Language;
  176: use DateTime::TimeZone;
  177: use DateTime::Locale;
  178: use Time::HiRes qw( sleep );
  179: 
  180: my $registered_cleanup;
  181: my $modified_urls;
  182: 
  183: sub handler {
  184:     my $r=shift;
  185:     if ($r->header_only) {
  186:         &Apache::loncommon::content_type($r,'text/html');
  187:         $r->send_http_header;
  188:         return OK;
  189:     }
  190: 
  191:     my $context = 'domain';
  192:     my $dom = $env{'request.role.domain'};
  193:     my $domdesc = &Apache::lonnet::domain($dom,'description');
  194:     if (&Apache::lonnet::allowed('mau',$dom)) {
  195:         &Apache::loncommon::content_type($r,'text/html');
  196:         $r->send_http_header;
  197:     } else {
  198:         $env{'user.error.msg'}=
  199:         "/adm/domainprefs:mau:0:0:Cannot modify domain settings";
  200:         return HTTP_NOT_ACCEPTABLE;
  201:     }
  202: 
  203:     $registered_cleanup=0;
  204:     @{$modified_urls}=();
  205: 
  206:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  207:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
  208:                                             ['phase','actions']);
  209:     my $phase = 'pickactions';
  210:     if ( exists($env{'form.phase'}) ) {
  211:         $phase = $env{'form.phase'};
  212:     }
  213:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
  214:     my %domconfig =
  215:       &Apache::lonnet::get_dom('configuration',['login','rolecolors',
  216:                 'quotas','autoenroll','autoupdate','autocreate',
  217:                 'directorysrch','usercreation','usermodification',
  218:                 'contacts','defaults','scantron','coursecategories',
  219:                 'serverstatuses','requestcourses','helpsettings',
  220:                 'coursedefaults','usersessions','loadbalancing',
  221:                 'requestauthor','selfenrollment','inststatus',
  222:                 'ltitools','ssl','trust'],$dom);
  223:     if (ref($domconfig{'ltitools'}) eq 'HASH') {
  224:         my %encconfig =
  225:             &Apache::lonnet::get_dom('encconfig',['ltitools'],$dom);
  226:         if (ref($encconfig{'ltitools'}) eq 'HASH') {
  227:             foreach my $id (keys(%{$domconfig{'ltitools'}})) {
  228:                 if (ref($domconfig{'ltitools'}{$id}) eq 'HASH') {
  229:                     foreach my $item ('key','secret') {
  230:                         $domconfig{'ltitools'}{$id}{$item} = $encconfig{'ltitools'}{$id}{$item};
  231:                     }
  232:                 }
  233:             }
  234:         }
  235:     }
  236:     my @prefs_order = ('rolecolors','login','defaults','quotas','autoenroll',
  237:                        'autoupdate','autocreate','directorysrch','contacts',
  238:                        'usercreation','selfcreation','usermodification','scantron',
  239:                        'requestcourses','requestauthor','coursecategories',
  240:                        'serverstatuses','helpsettings','coursedefaults',
  241:                        'ltitools','selfenrollment','usersessions','ssl','trust');
  242:     my %existing;
  243:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
  244:         %existing = %{$domconfig{'loadbalancing'}};
  245:     }
  246:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  247:         push(@prefs_order,'loadbalancing');
  248:     }
  249:     my %prefs = (
  250:         'rolecolors' =>
  251:                    { text => 'Default color schemes',
  252:                      help => 'Domain_Configuration_Color_Schemes',
  253:                      header => [{col1 => 'Student Settings',
  254:                                  col2 => '',},
  255:                                 {col1 => 'Coordinator Settings',
  256:                                  col2 => '',},
  257:                                 {col1 => 'Author Settings',
  258:                                  col2 => '',},
  259:                                 {col1 => 'Administrator Settings',
  260:                                  col2 => '',}],
  261:                       print => \&print_rolecolors,
  262:                       modify => \&modify_rolecolors,
  263:                     },
  264:         'login' =>
  265:                     { text => 'Log-in page options',
  266:                       help => 'Domain_Configuration_Login_Page',
  267:                       header => [{col1 => 'Log-in Page Items',
  268:                                   col2 => '',},
  269:                                  {col1 => 'Log-in Help',
  270:                                   col2 => 'Value'},
  271:                                  {col1 => 'Custom HTML in document head',
  272:                                   col2 => 'Value'}],
  273:                       print => \&print_login,
  274:                       modify => \&modify_login,
  275:                     },
  276:         'defaults' => 
  277:                     { text => 'Default authentication/language/timezone/portal/types',
  278:                       help => 'Domain_Configuration_LangTZAuth',
  279:                       header => [{col1 => 'Setting',
  280:                                   col2 => 'Value'},
  281:                                  {col1 => 'Internal Authentication',
  282:                                   col2 => 'Value'},
  283:                                  {col1 => 'Institutional user types',
  284:                                   col2 => 'Assignable to e-mail usernames'}],
  285:                       print => \&print_defaults,
  286:                       modify => \&modify_defaults,
  287:                     },
  288:         'quotas' => 
  289:                     { text => 'Blogs, personal web pages, webDAV/quotas, portfolios',
  290:                       help => 'Domain_Configuration_Quotas',
  291:                       header => [{col1 => 'User affiliation',
  292:                                   col2 => 'Available tools',
  293:                                   col3 => 'Quotas, MB; (Authoring requires role)',}],
  294:                       print => \&print_quotas,
  295:                       modify => \&modify_quotas,
  296:                     },
  297:         'autoenroll' =>
  298:                    { text => 'Auto-enrollment settings',
  299:                      help => 'Domain_Configuration_Auto_Enrollment',
  300:                      header => [{col1 => 'Configuration setting',
  301:                                  col2 => 'Value(s)'}],
  302:                      print => \&print_autoenroll,
  303:                      modify => \&modify_autoenroll,
  304:                    },
  305:         'autoupdate' => 
  306:                    { text => 'Auto-update settings',
  307:                      help => 'Domain_Configuration_Auto_Updates',
  308:                      header => [{col1 => 'Setting',
  309:                                  col2 => 'Value',},
  310:                                 {col1 => 'Setting',
  311:                                  col2 => 'Affiliation'},
  312:                                 {col1 => 'User population',
  313:                                  col2 => 'Updatable user data'}],
  314:                      print => \&print_autoupdate,
  315:                      modify => \&modify_autoupdate,
  316:                   },
  317:         'autocreate' => 
  318:                   { text => 'Auto-course creation settings',
  319:                      help => 'Domain_Configuration_Auto_Creation',
  320:                      header => [{col1 => 'Configuration Setting',
  321:                                  col2 => 'Value',}],
  322:                      print => \&print_autocreate,
  323:                      modify => \&modify_autocreate,
  324:                   },
  325:         'directorysrch' => 
  326:                   { text => 'Directory searches',
  327:                     help => 'Domain_Configuration_InstDirectory_Search',
  328:                     header => [{col1 => 'Institutional Directory Setting',
  329:                                 col2 => 'Value',},
  330:                                {col1 => 'LON-CAPA Directory Setting',
  331:                                 col2 => 'Value',}],
  332:                     print => \&print_directorysrch,
  333:                     modify => \&modify_directorysrch,
  334:                   },
  335:         'contacts' =>
  336:                   { text => 'E-mail addresses and helpform',
  337:                     help => 'Domain_Configuration_Contact_Info',
  338:                     header => [{col1 => 'Default e-mail addresses',
  339:                                 col2 => 'Value',},
  340:                                {col1 => 'Recipient(s) for notifications',
  341:                                 col2 => 'Value',},
  342:                                {col1 => 'Ask helpdesk form settings',
  343:                                 col2 => 'Value',},],
  344:                     print => \&print_contacts,
  345:                     modify => \&modify_contacts,
  346:                   },
  347:         'usercreation' => 
  348:                   { text => 'User creation',
  349:                     help => 'Domain_Configuration_User_Creation',
  350:                     header => [{col1 => 'Format rule type',
  351:                                 col2 => 'Format rules in force'},
  352:                                {col1 => 'User account creation',
  353:                                 col2 => 'Usernames which may be created',},
  354:                                {col1 => 'Context',
  355:                                 col2 => 'Assignable authentication types'}],
  356:                     print => \&print_usercreation,
  357:                     modify => \&modify_usercreation,
  358:                   },
  359:         'selfcreation' => 
  360:                   { text => 'Users self-creating accounts',
  361:                     help => 'Domain_Configuration_Self_Creation', 
  362:                     header => [{col1 => 'Self-creation with institutional username',
  363:                                 col2 => 'Enabled?'},
  364:                                {col1 => 'Institutional user type (login/SSO self-creation)',
  365:                                 col2 => 'Information user can enter'},
  366:                                {col1 => 'Self-creation with e-mail as username',
  367:                                 col2 => 'Settings'}],
  368:                     print => \&print_selfcreation,
  369:                     modify => \&modify_selfcreation,
  370:                   },
  371:         'usermodification' =>
  372:                   { text => 'User modification',
  373:                     help => 'Domain_Configuration_User_Modification',
  374:                     header => [{col1 => 'Target user has role',
  375:                                 col2 => 'User information updatable in author context'},
  376:                                {col1 => 'Target user has role',
  377:                                 col2 => 'User information updatable in course context'}],
  378:                     print => \&print_usermodification,
  379:                     modify => \&modify_usermodification,
  380:                   },
  381:         'scantron' =>
  382:                   { text => 'Bubblesheet format file',
  383:                     help => 'Domain_Configuration_Scantron_Format',
  384:                     header => [ {col1 => 'Item',
  385:                                  col2 => '',
  386:                               }],
  387:                     print => \&print_scantron,
  388:                     modify => \&modify_scantron,
  389:                   },
  390:         'requestcourses' => 
  391:                  {text => 'Request creation of courses',
  392:                   help => 'Domain_Configuration_Request_Courses',
  393:                   header => [{col1 => 'User affiliation',
  394:                               col2 => 'Availability/Processing of requests',},
  395:                              {col1 => 'Setting',
  396:                               col2 => 'Value'},
  397:                              {col1 => 'Available textbooks',
  398:                               col2 => ''},
  399:                              {col1 => 'Available templates',
  400:                               col2 => ''},
  401:                              {col1 => 'Validation (not official courses)',
  402:                               col2 => 'Value'},],
  403:                   print => \&print_quotas,
  404:                   modify => \&modify_quotas,
  405:                  },
  406:         'requestauthor' =>
  407:                  {text => 'Request Authoring Space',
  408:                   help => 'Domain_Configuration_Request_Author',
  409:                   header => [{col1 => 'User affiliation',
  410:                               col2 => 'Availability/Processing of requests',},
  411:                              {col1 => 'Setting',
  412:                               col2 => 'Value'}],
  413:                   print => \&print_quotas,
  414:                   modify => \&modify_quotas,
  415:                  },
  416:         'coursecategories' =>
  417:                   { text => 'Cataloging of courses/communities',
  418:                     help => 'Domain_Configuration_Cataloging_Courses',
  419:                     header => [{col1 => 'Catalog type/availability',
  420:                                 col2 => '',},
  421:                                {col1 => 'Category settings for standard catalog',
  422:                                 col2 => '',},
  423:                                {col1 => 'Categories',
  424:                                 col2 => '',
  425:                                }],
  426:                     print => \&print_coursecategories,
  427:                     modify => \&modify_coursecategories,
  428:                   },
  429:         'serverstatuses' =>
  430:                  {text   => 'Access to server status pages',
  431:                   help   => 'Domain_Configuration_Server_Status',
  432:                   header => [{col1 => 'Status Page',
  433:                               col2 => 'Other named users',
  434:                               col3 => 'Specific IPs',
  435:                             }],
  436:                   print => \&print_serverstatuses,
  437:                   modify => \&modify_serverstatuses,
  438:                  },
  439:         'helpsettings' =>
  440:                  {text   => 'Support settings',
  441:                   help   => 'Domain_Configuration_Help_Settings',
  442:                   header => [{col1 => 'Help Page Settings (logged-in users)',
  443:                               col2 => 'Value'},
  444:                              {col1 => 'Helpdesk Roles',
  445:                               col2 => 'Settings'},],
  446:                   print  => \&print_helpsettings,
  447:                   modify => \&modify_helpsettings,
  448:                  },
  449:         'coursedefaults' => 
  450:                  {text => 'Course/Community defaults',
  451:                   help => 'Domain_Configuration_Course_Defaults',
  452:                   header => [{col1 => 'Defaults which can be overridden in each course by a CC',
  453:                               col2 => 'Value',},
  454:                              {col1 => 'Defaults which can be overridden for each course by a DC',
  455:                               col2 => 'Value',},],
  456:                   print => \&print_coursedefaults,
  457:                   modify => \&modify_coursedefaults,
  458:                  },
  459:         'selfenrollment' => 
  460:                  {text   => 'Self-enrollment in Course/Community',
  461:                   help   => 'Domain_Configuration_Selfenrollment',
  462:                   header => [{col1 => 'Configuration Rights',
  463:                               col2 => 'Configured by Course Personnel or Domain Coordinator?'},
  464:                              {col1 => 'Defaults',
  465:                               col2 => 'Value'},
  466:                              {col1 => 'Self-enrollment validation (optional)',
  467:                               col2 => 'Value'},],
  468:                   print => \&print_selfenrollment,
  469:                   modify => \&modify_selfenrollment,
  470:                  },
  471:         'privacy' => 
  472:                  {text   => 'User Privacy',
  473:                   help   => 'Domain_Configuration_User_Privacy',
  474:                   header => [{col1 => 'Setting',
  475:                               col2 => 'Value',}],
  476:                   print => \&print_privacy,
  477:                   modify => \&modify_privacy,
  478:                  },
  479:         'usersessions' =>
  480:                  {text  => 'User session hosting/offloading',
  481:                   help  => 'Domain_Configuration_User_Sessions',
  482:                   header => [{col1 => 'Domain server',
  483:                               col2 => 'Servers to offload sessions to when busy'},
  484:                              {col1 => 'Hosting of users from other domains',
  485:                               col2 => 'Rules'},
  486:                              {col1 => "Hosting domain's own users elsewhere",
  487:                               col2 => 'Rules'}],
  488:                   print => \&print_usersessions,
  489:                   modify => \&modify_usersessions,
  490:                  },
  491:         'loadbalancing' =>
  492:                  {text  => 'Dedicated Load Balancer(s)',
  493:                   help  => 'Domain_Configuration_Load_Balancing',
  494:                   header => [{col1 => 'Balancers',
  495:                               col2 => 'Default destinations',
  496:                               col3 => 'User affiliation',
  497:                               col4 => 'Overrides'},
  498:                             ],
  499:                   print => \&print_loadbalancing,
  500:                   modify => \&modify_loadbalancing,
  501:                  },
  502:         'ltitools' => 
  503:                  {text => 'External Tools (LTI)',
  504:                   help => 'Domain_Configuration_LTI_Tools',
  505:                   header => [{col1 => 'Setting',
  506:                               col2 => 'Value',}],
  507:                   print => \&print_ltitools,
  508:                   modify => \&modify_ltitools,
  509:                  },
  510:         'ssl' =>
  511:                  {text  => 'LON-CAPA Network (SSL)',
  512:                   help  => 'Domain_Configuration_Network_SSL',
  513:                   header => [{col1 => 'Server',
  514:                               col2 => 'Certificate Status'},
  515:                              {col1 => 'Connections to other servers',
  516:                               col2 => 'Rules'},
  517:                              {col1 => 'Connections from other servers',
  518:                               col2 => 'Rules'},
  519:                              {col1 => "Replicating domain's published content",
  520:                               col2 => 'Rules'}],
  521:                   print => \&print_ssl,
  522:                   modify => \&modify_ssl,
  523:                  },
  524:         'trust' =>
  525:                  {text   => 'Trust Settings',
  526:                   help   => 'Domain_Configuration_Trust',
  527:                   header => [{col1 => "Access to this domain's content by others",
  528:                               col2 => 'Rules'},
  529:                              {col1 => "Access to other domain's content by this domain",
  530:                               col2 => 'Rules'},
  531:                              {col1 => "Enrollment in this domain's courses by others",
  532:                               col2 => 'Rules',},
  533:                              {col1 => "Co-author roles in this domain for others",
  534:                               col2 => 'Rules',},
  535:                              {col1 => "Co-author roles for this domain's users elsewhere",
  536:                               col2 => 'Rules',},
  537:                              {col1 => "Domain roles in this domain assignable to others",
  538:                               col2 => 'Rules'},
  539:                              {col1 => "Course catalog for this domain displayed elsewhere",
  540:                               col2 => 'Rules'},
  541:                              {col1 => "Requests for creation of courses in this domain by others",
  542:                               col2 => 'Rules'},
  543:                              {col1 => "Users in other domains can send messages to this domain",
  544:                               col2 => 'Rules'},],
  545:                   print => \&print_trust,
  546:                   modify => \&modify_trust,
  547:                  },
  548:     );
  549:     if (keys(%servers) > 1) {
  550:         $prefs{'login'}  = { text   => 'Log-in page options',
  551:                              help   => 'Domain_Configuration_Login_Page',
  552:                             header => [{col1 => 'Log-in Service',
  553:                                         col2 => 'Server Setting',},
  554:                                        {col1 => 'Log-in Page Items',
  555:                                         col2 => ''},
  556:                                        {col1 => 'Log-in Help',
  557:                                         col2 => 'Value'},
  558:                                        {col1 => 'Custom HTML in document head',
  559:                                         col2 => 'Value'}],
  560:                             print => \&print_login,
  561:                             modify => \&modify_login,
  562:                            };
  563:     }
  564: 
  565:     my @roles = ('student','coordinator','author','admin');
  566:     my @actions = &Apache::loncommon::get_env_multiple('form.actions');
  567:     &Apache::lonhtmlcommon::add_breadcrumb
  568:     ({href=>"javascript:changePage(document.$phase,'pickactions')",
  569:       text=>"Settings to display/modify"});
  570:     my $confname = $dom.'-domainconfig';
  571: 
  572:     if ($phase eq 'process') {
  573:         my $result = &Apache::lonconfigsettings::make_changes($r,$dom,$phase,$context,\@prefs_order,
  574:                                                               \%prefs,\%domconfig,$confname,\@roles);
  575:         if ((ref($result) eq 'HASH') && (keys(%{$result}))) {
  576:             $r->rflush();
  577:             &devalidate_remote_domconfs($dom,$result);
  578:         }
  579:     } elsif ($phase eq 'display') {
  580:         my $js = &recaptcha_js().
  581:                  &toggle_display_js();
  582:         if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
  583:             my ($othertitle,$usertypes,$types) =
  584:                 &Apache::loncommon::sorted_inst_types($dom);
  585:             $js .= &lonbalance_targets_js($dom,$types,\%servers,
  586:                                           $domconfig{'loadbalancing'}).
  587:                    &new_spares_js().
  588:                    &common_domprefs_js().
  589:                    &Apache::loncommon::javascript_array_indexof();
  590:         }
  591:         if (grep(/^requestcourses$/,@actions)) {
  592:             my $javascript_validations;
  593:             my $coursebrowserjs=&Apache::loncommon::coursebrowser_javascript($env{'request.role.domain'}); 
  594:             $js .= <<END;
  595: <script type="text/javascript">
  596: $javascript_validations
  597: </script>
  598: $coursebrowserjs
  599: END
  600:         }
  601:         if (grep(/^contacts$/,@actions)) {
  602:             $js .= &contacts_javascript();
  603:         }
  604:         &Apache::lonconfigsettings::display_settings($r,$dom,$phase,$context,\@prefs_order,\%prefs,\%domconfig,$confname,$js);
  605:     } else {
  606: # check if domconfig user exists for the domain.
  607:         my $servadm = $r->dir_config('lonAdmEMail');
  608:         my ($configuserok,$author_ok,$switchserver) =
  609:             &config_check($dom,$confname,$servadm);
  610:         unless ($configuserok eq 'ok') {
  611:             &Apache::lonconfigsettings::print_header($r,$phase,$context);
  612:             $r->print(&mt('The domain configuration user "[_1]" has yet to be created.',
  613:                           $confname).
  614:                       '<br />'
  615:             );
  616:             if ($switchserver) {
  617:                 $r->print(&mt('Ordinarily, that domain configuration user is created when the ./UPDATE script is run to install LON-CAPA for the first time.').
  618:                           '<br />'.
  619:                           &mt('However, that does not apply when new domains are added to a multi-domain server, and ./UPDATE has not been run recently.').
  620:                           '<br />'.
  621:                           &mt('The "[_1]" user can be created automatically when a Domain Coordinator visits the web-based "Set domain configuration" screen, in a session hosted on the primary library server.',$confname).
  622:                           '<br />'.
  623:                           &mt('To do that now, use the following link: [_1]',$switchserver)
  624:                 );
  625:             } else {
  626:                 $r->print(&mt('To create that user from the command line run the ./UPDATE script found in the top level directory of the extracted LON-CAPA tarball.').
  627:                           '<br />'.
  628:                           &mt('Once that is done, you will be able to use the web-based "Set domain configuration" to configure the domain')
  629:                 );
  630:             }
  631:             $r->print(&Apache::loncommon::end_page());
  632:             return OK;
  633:         }
  634:         if (keys(%domconfig) == 0) {
  635:             my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
  636:             my @ids=&Apache::lonnet::current_machine_ids();
  637:             if (!grep(/^\Q$primarylibserv\E$/,@ids)) {
  638:                 my %designhash = &Apache::loncommon::get_domainconf($dom);
  639:                 my @loginimages = ('img','logo','domlogo','login');
  640:                 my $custom_img_count = 0;
  641:                 foreach my $img (@loginimages) {
  642:                     if ($designhash{$dom.'.login.'.$img} ne '') {
  643:                         $custom_img_count ++;
  644:                     }
  645:                 }
  646:                 foreach my $role (@roles) {
  647:                     if ($designhash{$dom.'.'.$role.'.img'} ne '') {
  648:                         $custom_img_count ++;
  649:                     }
  650:                 }
  651:                 if ($custom_img_count > 0) {
  652:                     &Apache::lonconfigsettings::print_header($r,$phase,$context);
  653:                     my $switch_server = &check_switchserver($dom,$confname);
  654:                     $r->print(
  655:     &mt('Domain configuration settings have yet to be saved for this domain via the web-based domain preferences interface.').'<br />'.
  656:     &mt("While this remains so, you must switch to the domain's primary library server in order to update settings.").'<br /><br />'.
  657:     &mt("Thereafter, (with a Domain Coordinator role selected in the domain) you will be able to update settings when logged in to any server in the LON-CAPA network.").'<br />'.
  658:     &mt("However, you will still need to switch to the domain's primary library server to upload new images or logos.").'<br /><br />');
  659:                     if ($switch_server) {
  660:                         $r->print($switch_server.' '.&mt('to primary library server for domain: [_1]',$dom));
  661:                     }
  662:                     $r->print(&Apache::loncommon::end_page());
  663:                     return OK;
  664:                 }
  665:             }
  666:         }
  667:         &Apache::lonconfigsettings::display_choices($r,$phase,$context,\@prefs_order,\%prefs);
  668:     }
  669:     return OK;
  670: }
  671: 
  672: sub process_changes {
  673:     my ($r,$dom,$confname,$action,$roles,$values,$lastactref) = @_;
  674:     my %domconfig;
  675:     if (ref($values) eq 'HASH') {
  676:         %domconfig = %{$values};
  677:     }
  678:     my $output;
  679:     if ($action eq 'login') {
  680:         $output = &modify_login($r,$dom,$confname,$lastactref,%domconfig);
  681:     } elsif ($action eq 'rolecolors') {
  682:         $output = &modify_rolecolors($r,$dom,$confname,$roles,
  683:                                      $lastactref,%domconfig);
  684:     } elsif ($action eq 'quotas') {
  685:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  686:     } elsif ($action eq 'autoenroll') {
  687:         $output = &modify_autoenroll($dom,$lastactref,%domconfig);
  688:     } elsif ($action eq 'autoupdate') {
  689:         $output = &modify_autoupdate($dom,%domconfig);
  690:     } elsif ($action eq 'autocreate') {
  691:         $output = &modify_autocreate($dom,%domconfig);
  692:     } elsif ($action eq 'directorysrch') {
  693:         $output = &modify_directorysrch($dom,$lastactref,%domconfig);
  694:     } elsif ($action eq 'usercreation') {
  695:         $output = &modify_usercreation($dom,%domconfig);
  696:     } elsif ($action eq 'selfcreation') {
  697:         $output = &modify_selfcreation($dom,%domconfig);
  698:     } elsif ($action eq 'usermodification') {
  699:         $output = &modify_usermodification($dom,%domconfig);
  700:     } elsif ($action eq 'contacts') {
  701:         $output = &modify_contacts($dom,$lastactref,%domconfig);
  702:     } elsif ($action eq 'defaults') {
  703:         $output = &modify_defaults($dom,$lastactref,%domconfig);
  704:     } elsif ($action eq 'scantron') {
  705:         $output = &modify_scantron($r,$dom,$confname,$lastactref,%domconfig);
  706:     } elsif ($action eq 'coursecategories') {
  707:         $output = &modify_coursecategories($dom,$lastactref,%domconfig);
  708:     } elsif ($action eq 'serverstatuses') {
  709:         $output = &modify_serverstatuses($dom,%domconfig);
  710:     } elsif ($action eq 'requestcourses') {
  711:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  712:     } elsif ($action eq 'requestauthor') {
  713:         $output = &modify_quotas($r,$dom,$action,$lastactref,%domconfig);
  714:     } elsif ($action eq 'helpsettings') {
  715:         $output = &modify_helpsettings($r,$dom,$confname,$lastactref,%domconfig);
  716:     } elsif ($action eq 'coursedefaults') {
  717:         $output = &modify_coursedefaults($dom,$lastactref,%domconfig);
  718:     } elsif ($action eq 'selfenrollment') {
  719:         $output = &modify_selfenrollment($dom,$lastactref,%domconfig)
  720:     } elsif ($action eq 'usersessions') {
  721:         $output = &modify_usersessions($dom,$lastactref,%domconfig);
  722:     } elsif ($action eq 'loadbalancing') {
  723:         $output = &modify_loadbalancing($dom,%domconfig);
  724:     } elsif ($action eq 'ltitools') {
  725:         $output = &modify_ltitools($r,$dom,$action,$lastactref,%domconfig);
  726:     } elsif ($action eq 'ssl') {
  727:         $output = &modify_ssl($dom,$lastactref,%domconfig);
  728:     } elsif ($action eq 'trust') {
  729:         $output = &modify_trust($dom,$lastactref,%domconfig);
  730:     }
  731:     return $output;
  732: }
  733: 
  734: sub print_config_box {
  735:     my ($r,$dom,$confname,$phase,$action,$item,$settings) = @_;
  736:     my $rowtotal = 0;
  737:     my $output;
  738:     if ($action eq 'coursecategories') {
  739:         $output = &coursecategories_javascript($settings);
  740:     } elsif ($action eq 'defaults') {
  741:         $output = &defaults_javascript($settings); 
  742:     } elsif ($action eq 'helpsettings') {
  743:         my (%privs,%levelscurrent);
  744:         my %full=();
  745:         my %levels=(
  746:                      course => {},
  747:                      domain => {},
  748:                      system => {},
  749:                    );
  750:         my $context = 'domain';
  751:         my $crstype = 'Course';
  752:         my $formname = 'display';
  753:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
  754:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
  755:         $output =
  756:             &Apache::lonuserutils::custom_roledefs_js($context,$crstype,$formname,\%full, 
  757:                                                       \@templateroles);
  758:     }
  759:     $output .=
  760:          '<table class="LC_nested_outer">
  761:           <tr>
  762:            <th align="left" valign="middle"><span class="LC_nobreak">'.
  763:            &mt($item->{text}).'&nbsp;'.
  764:            &Apache::loncommon::help_open_topic($item->{'help'}).'</span></th>'."\n".
  765:           '</tr>';
  766:     $rowtotal ++;
  767:     my $numheaders = 1;
  768:     if (ref($item->{'header'}) eq 'ARRAY') {
  769:         $numheaders = scalar(@{$item->{'header'}});
  770:     }
  771:     if ($numheaders > 1) {
  772:         my $colspan = '';
  773:         my $rightcolspan = '';
  774:         if (($action eq 'rolecolors') || ($action eq 'defaults') ||
  775:             ($action eq 'directorysrch') ||
  776:             (($action eq 'login') && ($numheaders < 4))) {
  777:             $colspan = ' colspan="2"';
  778:         }
  779:         if ($action eq 'usersessions') {
  780:             $rightcolspan = ' colspan="3"'; 
  781:         }
  782:         $output .= '
  783:           <tr>
  784:            <td>
  785:             <table class="LC_nested">
  786:              <tr class="LC_info_row">
  787:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[0]->{'col1'}).'</td>
  788:               <td class="LC_right_item"'.$rightcolspan.'>'.&mt($item->{'header'}->[0]->{'col2'}).'</td>
  789:              </tr>';
  790:         $rowtotal ++;
  791:         if (($action eq 'autoupdate') || ($action eq 'usercreation') || ($action eq 'selfcreation') ||
  792:             ($action eq 'usermodification') || ($action eq 'defaults') || ($action eq 'coursedefaults') ||
  793:             ($action eq 'selfenrollment') || ($action eq 'usersessions') || ($action eq 'ssl') ||
  794:             ($action eq 'directorysrch') || ($action eq 'trust') || ($action eq 'helpsettings') ||
  795:             ($action eq 'contacts')) {
  796:             $output .= $item->{'print'}->('top',$dom,$settings,\$rowtotal);
  797:         } elsif ($action eq 'coursecategories') {
  798:             $output .= $item->{'print'}->('top',$dom,$item,$settings,\$rowtotal);
  799:         } elsif ($action eq 'login') {
  800:             if ($numheaders == 4) {
  801:                 $colspan = ' colspan="2"';
  802:                 $output .= &print_login('service',$dom,$confname,$phase,$settings,\$rowtotal);
  803:             } else {
  804:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal);
  805:             }
  806:         } elsif (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
  807:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
  808:         } elsif ($action eq 'rolecolors') {
  809:             $output .= &print_rolecolors($phase,'student',$dom,$confname,$settings,\$rowtotal);
  810:         }
  811:         $output .= '
  812:            </table>
  813:           </td>
  814:          </tr>
  815:          <tr>
  816:            <td>
  817:             <table class="LC_nested">
  818:              <tr class="LC_info_row">
  819:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col1'}).'</td>
  820:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[1]->{'col2'}).'</td>
  821:              </tr>';
  822:             $rowtotal ++;
  823:         if (($action eq 'autoupdate') || ($action eq 'usercreation') ||
  824:             ($action eq 'selfcreation') || ($action eq 'selfenrollment') ||
  825:             ($action eq 'usersessions') || ($action eq 'coursecategories') || 
  826:             ($action eq 'trust') || ($action eq 'contacts') || ($action eq 'defaults')) {
  827:             if ($action eq 'coursecategories') {
  828:                 $output .= &print_coursecategories('middle',$dom,$item,$settings,\$rowtotal);
  829:                 $colspan = ' colspan="2"';
  830:             } elsif ($action eq 'trust') {
  831:                 $output .= $item->{'print'}->('shared',$dom,$settings,\$rowtotal);
  832:             } else {
  833:                 $output .= $item->{'print'}->('middle',$dom,$settings,\$rowtotal);
  834:             }
  835:             if ($action eq 'trust') {
  836:                 $output .= '
  837:             </table>
  838:           </td>
  839:          </tr>';
  840:                 my @trusthdrs = qw(2 3 4 5 6 7);
  841:                 my @prefixes = qw(enroll othcoau coaurem domroles catalog reqcrs);
  842:                 for (my $i=0; $i<@trusthdrs; $i++) {
  843:                     $output .= '
  844:          <tr>
  845:            <td>
  846:             <table class="LC_nested">
  847:              <tr class="LC_info_row">
  848:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col1'}).'</td>
  849:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[$trusthdrs[$i]]->{'col2'}).'</td></tr>'.
  850:                            $item->{'print'}->($prefixes[$i],$dom,$settings,\$rowtotal).'
  851:             </table>
  852:           </td>
  853:          </tr>';
  854:                 }
  855:                 $output .= '
  856:          <tr>
  857:            <td>
  858:             <table class="LC_nested">
  859:              <tr class="LC_info_row">
  860:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col1'}).'</td>
  861:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[8]->{'col2'}).'</td></tr>'.
  862:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  863:             } else {
  864:                 $output .= '
  865:            </table>
  866:           </td>
  867:          </tr>
  868:          <tr>
  869:            <td>
  870:             <table class="LC_nested">
  871:              <tr class="LC_info_row">
  872:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  873:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  874:              </tr>'."\n";
  875:                 if ($action eq 'coursecategories') {
  876:                     $output .= &print_coursecategories('bottom',$dom,$item,$settings,\$rowtotal);
  877:                 } else {
  878:                     $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  879:                 }
  880:             }
  881:             $rowtotal ++;
  882:         } elsif (($action eq 'usermodification') || ($action eq 'coursedefaults') ||
  883:                  ($action eq 'defaults') || ($action eq 'directorysrch') ||
  884:                  ($action eq 'helpsettings')) {
  885:             $output .= $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  886:         } elsif ($action eq 'ssl') {
  887:             $output .= $item->{'print'}->('connto',$dom,$settings,\$rowtotal).'
  888:             </table>
  889:           </td>
  890:          </tr>
  891:          <tr>
  892:            <td>
  893:             <table class="LC_nested">
  894:              <tr class="LC_info_row">
  895:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  896:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  897:                            $item->{'print'}->('connfrom',$dom,$settings,\$rowtotal).'
  898:             </table>
  899:           </td>
  900:          </tr>
  901:          <tr>
  902:            <td>
  903:             <table class="LC_nested">
  904:              <tr class="LC_info_row">
  905:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  906:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td></tr>'.
  907:                            $item->{'print'}->('bottom',$dom,$settings,\$rowtotal);
  908:         } elsif ($action eq 'login') {
  909:             if ($numheaders == 4) {
  910:                 $output .= &print_login('page',$dom,$confname,$phase,$settings,\$rowtotal).'
  911:            </table>
  912:           </td>
  913:          </tr>
  914:          <tr>
  915:            <td>
  916:             <table class="LC_nested">
  917:              <tr class="LC_info_row">
  918:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  919:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td></tr>'.
  920:                        &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  921:                 $rowtotal ++;
  922:             } else {
  923:                 $output .= &print_login('help',$dom,$confname,$phase,$settings,\$rowtotal);
  924:             }
  925:             $output .= '
  926:            </table>
  927:           </td>
  928:          </tr>
  929:          <tr>
  930:            <td>
  931:             <table class="LC_nested">
  932:              <tr class="LC_info_row">';
  933:             if ($numheaders == 4) {
  934:                 $output .= '
  935:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  936:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
  937:              </tr>';
  938:             } else {
  939:                 $output .= '
  940:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  941:               <td class="LC_right_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col2'}).'</td>
  942:              </tr>';
  943:             }
  944:             $rowtotal ++;
  945:             $output .= &print_login('headtag',$dom,$confname,$phase,$settings,\$rowtotal);
  946:         } elsif ($action eq 'requestcourses') {
  947:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  948:             $rowtotal ++;
  949:             $output .= &print_studentcode($settings,\$rowtotal).'
  950:            </table>
  951:           </td>
  952:          </tr>
  953:          <tr>
  954:            <td>
  955:             <table class="LC_nested">
  956:              <tr class="LC_info_row">
  957:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[2]->{'col1'}).'</td>
  958:               <td class="LC_right_item">'.&mt($item->{'header'}->[2]->{'col2'}).'</td> </tr>'.
  959:                        &textbookcourses_javascript($settings).
  960:                        &print_textbookcourses($dom,'textbooks',$settings,\$rowtotal).'
  961:             </table>
  962:            </td>
  963:           </tr>
  964:          <tr>
  965:            <td>
  966:             <table class="LC_nested">
  967:              <tr class="LC_info_row">
  968:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
  969:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td> </tr>'.
  970:                        &print_textbookcourses($dom,'templates',$settings,\$rowtotal).'
  971:             </table>
  972:            </td>
  973:           </tr>
  974:           <tr>
  975:            <td>
  976:             <table class="LC_nested">
  977:              <tr class="LC_info_row">
  978:               <td class="LC_left_item"'.$colspan.' valign="top">'.&mt($item->{'header'}->[4]->{'col1'}).'</td>
  979:               <td class="LC_right_item" valign="top">'.&mt($item->{'header'}->[4]->{'col2'}).'</td>
  980:              </tr>'.
  981:             &print_validation_rows('requestcourses',$dom,$settings,\$rowtotal);
  982:         } elsif ($action eq 'requestauthor') {
  983:             $output .= &print_requestmail($dom,$action,$settings,\$rowtotal);
  984:             $rowtotal ++;
  985:         } elsif ($action eq 'rolecolors') {
  986:             $output .= &print_rolecolors($phase,'coordinator',$dom,$confname,$settings,\$rowtotal).'
  987:            </table>
  988:           </td>
  989:          </tr>
  990:          <tr>
  991:            <td>
  992:             <table class="LC_nested">
  993:              <tr class="LC_info_row">
  994:               <td class="LC_left_item"'.$colspan.' valign="top">'.
  995:                &mt($item->{'header'}->[2]->{'col1'}).'</td>
  996:               <td class="LC_right_item" valign="top">'.
  997:                &mt($item->{'header'}->[2]->{'col2'}).'</td>
  998:              </tr>'.
  999:             &print_rolecolors($phase,'author',$dom,$confname,$settings,\$rowtotal).'
 1000:            </table>
 1001:           </td>
 1002:          </tr>
 1003:          <tr>
 1004:            <td>
 1005:             <table class="LC_nested">
 1006:              <tr class="LC_info_row">
 1007:               <td class="LC_left_item"'.$colspan.'>'.&mt($item->{'header'}->[3]->{'col1'}).'</td>
 1008:               <td class="LC_right_item">'.&mt($item->{'header'}->[3]->{'col2'}).'</td>
 1009:              </tr>'.
 1010:             &print_rolecolors($phase,'admin',$dom,$confname,$settings,\$rowtotal);
 1011:             $rowtotal += 2;
 1012:         }
 1013:     } else {
 1014:         $output .= '
 1015:           <tr>
 1016:            <td>
 1017:             <table class="LC_nested">
 1018:              <tr class="LC_info_row">';
 1019:         if ($action eq 'login') {
 1020:             $output .= '  
 1021:               <td class="LC_left_item" colspan="2">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1022:         } elsif ($action eq 'serverstatuses') {
 1023:             $output .= '
 1024:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).
 1025:               '<br />('.&mt('Automatic access for Dom. Coords.').')</td>';
 1026: 
 1027:         } else {
 1028:             $output .= '
 1029:               <td class="LC_left_item" valign="top">'.&mt($item->{'header'}->[0]->{'col1'}).'</td>';
 1030:         }
 1031:         if (defined($item->{'header'}->[0]->{'col3'})) {
 1032:             $output .= '<td class="LC_left_item" valign="top">'.
 1033:                        &mt($item->{'header'}->[0]->{'col2'});
 1034:             if ($action eq 'serverstatuses') {
 1035:                 $output .= '<br />(<tt>'.&mt('user1:domain1,user2:domain2 etc.').'</tt>)';
 1036:             } 
 1037:         } else {
 1038:             $output .= '<td class="LC_right_item" valign="top">'.
 1039:                        &mt($item->{'header'}->[0]->{'col2'});
 1040:         }
 1041:         $output .= '</td>';
 1042:         if ($item->{'header'}->[0]->{'col3'}) {
 1043:             if (defined($item->{'header'}->[0]->{'col4'})) {
 1044:                 $output .= '<td class="LC_left_item" valign="top">'.
 1045:                             &mt($item->{'header'}->[0]->{'col3'});
 1046:             } else {
 1047:                 $output .= '<td class="LC_right_item" valign="top">'.
 1048:                            &mt($item->{'header'}->[0]->{'col3'});
 1049:             }
 1050:             if ($action eq 'serverstatuses') {
 1051:                 $output .= '<br />(<tt>'.&mt('IP1,IP2 etc.').'</tt>)';
 1052:             }
 1053:             $output .= '</td>';
 1054:         }
 1055:         if ($item->{'header'}->[0]->{'col4'}) {
 1056:             $output .= '<td class="LC_right_item" valign="top">'.
 1057:                        &mt($item->{'header'}->[0]->{'col4'});
 1058:         }
 1059:         $output .= '</tr>';
 1060:         $rowtotal ++;
 1061:         if ($action eq 'quotas') {
 1062:             $output .= &print_quotas($dom,$settings,\$rowtotal,$action);
 1063:         } elsif (($action eq 'autoenroll') || ($action eq 'autocreate') || 
 1064:                  ($action eq 'serverstatuses') || ($action eq 'loadbalancing') || 
 1065:                  ($action eq 'ltitools')) {
 1066:             $output .= $item->{'print'}->($dom,$settings,\$rowtotal);
 1067:         } elsif ($action eq 'scantron') {
 1068:             $output .= &print_scantronformat($r,$dom,$confname,$settings,\$rowtotal);
 1069:         }
 1070:     }
 1071:     $output .= '
 1072:    </table>
 1073:   </td>
 1074:  </tr>
 1075: </table><br />';
 1076:     return ($output,$rowtotal);
 1077: }
 1078: 
 1079: sub print_login {
 1080:     my ($caller,$dom,$confname,$phase,$settings,$rowtotal) = @_;
 1081:     my ($css_class,$datatable);
 1082:     my %choices = &login_choices();
 1083: 
 1084:     if ($caller eq 'service') {
 1085:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 1086:         my $choice = $choices{'disallowlogin'};
 1087:         $css_class = ' class="LC_odd_row"';
 1088:         $datatable .= '<tr'.$css_class.'><td>'.$choice.'</td>'.
 1089:                       '<td align="right"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1090:                       '<th>'.$choices{'server'}.'</th>'.
 1091:                       '<th>'.$choices{'serverpath'}.'</th>'.
 1092:                       '<th>'.$choices{'custompath'}.'</th>'.
 1093:                       '<th><span class="LC_nobreak">'.$choices{'exempt'}.'</span></th></tr>'."\n";
 1094:         my %disallowed;
 1095:         if (ref($settings) eq 'HASH') {
 1096:             if (ref($settings->{'loginvia'}) eq 'HASH') {
 1097:                %disallowed = %{$settings->{'loginvia'}};
 1098:             }
 1099:         }
 1100:         foreach my $lonhost (sort(keys(%servers))) {
 1101:             my $direct = 'selected="selected"';
 1102:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1103:                 if ($disallowed{$lonhost}{'server'} ne '') {
 1104:                     $direct = '';
 1105:                 }
 1106:             }
 1107:             $datatable .= '<tr><td>'.$servers{$lonhost}.'</td>'.
 1108:                           '<td><select name="'.$lonhost.'_server">'.
 1109:                           '<option value=""'.$direct.'>'.$choices{'directlogin'}.
 1110:                           '</option>';
 1111:             foreach my $hostid (sort(keys(%servers))) {
 1112:                 next if ($servers{$hostid} eq $servers{$lonhost});
 1113:                 my $selected = '';
 1114:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1115:                     if ($hostid eq $disallowed{$lonhost}{'server'}) {
 1116:                         $selected = 'selected="selected"';
 1117:                     }
 1118:                 }
 1119:                 $datatable .= '<option value="'.$hostid.'"'.$selected.'>'.
 1120:                               $servers{$hostid}.'</option>';
 1121:             }
 1122:             $datatable .= '</select></td>'.
 1123:                           '<td><select name="'.$lonhost.'_serverpath">';
 1124:             foreach my $path ('','/','/adm/login','/adm/roles','custom') {
 1125:                 my $pathname = $path;
 1126:                 if ($path eq 'custom') {
 1127:                     $pathname = &mt('Custom Path').' ->';
 1128:                 }
 1129:                 my $selected = '';
 1130:                 if (ref($disallowed{$lonhost}) eq 'HASH') {
 1131:                     if ($path eq $disallowed{$lonhost}{'serverpath'}) {
 1132:                         $selected = 'selected="selected"';
 1133:                     }
 1134:                 } elsif ($path eq '') {
 1135:                     $selected = 'selected="selected"';
 1136:                 }
 1137:                 $datatable .= '<option value="'.$path.'"'.$selected.'>'.$pathname.'</option>';
 1138:             }
 1139:             $datatable .= '</select></td>';
 1140:             my ($custom,$exempt);
 1141:             if (ref($disallowed{$lonhost}) eq 'HASH') {
 1142:                 $custom = $disallowed{$lonhost}{'custompath'};
 1143:                 $exempt = $disallowed{$lonhost}{'exempt'};
 1144:             }
 1145:             $datatable .= '<td><input type="text" name="'.$lonhost.'_custompath" size="6" value="'.$custom.'" /></td>'.
 1146:                           '<td><input type="text" name="'.$lonhost.'_exempt" size="8" value="'.$exempt.'" /></td>'.
 1147:                           '</tr>';
 1148:         }
 1149:         $datatable .= '</table></td></tr>';
 1150:         return $datatable;
 1151:     } elsif ($caller eq 'page') {
 1152:         my %defaultchecked = ( 
 1153:                                'coursecatalog' => 'on',
 1154:                                'helpdesk'      => 'on',
 1155:                                'adminmail'     => 'off',
 1156:                                'newuser'       => 'off',
 1157:                              );
 1158:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 1159:         my (%checkedon,%checkedoff);
 1160:         foreach my $item (@toggles) {
 1161:             if ($defaultchecked{$item} eq 'on') { 
 1162:                 $checkedon{$item} = ' checked="checked" ';
 1163:                 $checkedoff{$item} = ' ';
 1164:             } elsif ($defaultchecked{$item} eq 'off') {
 1165:                 $checkedoff{$item} = ' checked="checked" ';
 1166:                 $checkedon{$item} = ' ';
 1167:             }
 1168:         }
 1169:         my @images = ('img','logo','domlogo','login');
 1170:         my @logintext = ('textcol','bgcol');
 1171:         my @bgs = ('pgbg','mainbg','sidebg');
 1172:         my @links = ('link','alink','vlink');
 1173:         my %designhash = &Apache::loncommon::get_domainconf($dom);
 1174:         my %defaultdesign = %Apache::loncommon::defaultdesign;
 1175:         my (%is_custom,%designs);
 1176:         my %defaults = (
 1177:                        font => $defaultdesign{'login.font'},
 1178:                        );
 1179:         foreach my $item (@images) {
 1180:             $defaults{$item} = $defaultdesign{'login.'.$item};
 1181:             $defaults{'showlogo'}{$item} = 1;
 1182:         }
 1183:         foreach my $item (@bgs) {
 1184:             $defaults{'bgs'}{$item} = $defaultdesign{'login.'.$item};
 1185:         }
 1186:         foreach my $item (@logintext) {
 1187:             $defaults{'logintext'}{$item} = $defaultdesign{'login.'.$item};
 1188:         }
 1189:         foreach my $item (@links) {
 1190:             $defaults{'links'}{$item} = $defaultdesign{'login.'.$item};
 1191:         }
 1192:         if (ref($settings) eq 'HASH') {
 1193:             foreach my $item (@toggles) {
 1194:                 if ($settings->{$item} eq '1') {
 1195:                     $checkedon{$item} =  ' checked="checked" ';
 1196:                     $checkedoff{$item} = ' ';
 1197:                 } elsif ($settings->{$item} eq '0') {
 1198:                     $checkedoff{$item} =  ' checked="checked" ';
 1199:                     $checkedon{$item} = ' ';
 1200:                 }
 1201:             }
 1202:             foreach my $item (@images) {
 1203:                 if (defined($settings->{$item})) {
 1204:                     $designs{$item} = $settings->{$item};
 1205:                     $is_custom{$item} = 1;
 1206:                 }
 1207:                 if (defined($settings->{'showlogo'}{$item})) {
 1208:                     $designs{'showlogo'}{$item} = $settings->{'showlogo'}{$item};
 1209:                 }
 1210:             }
 1211:             foreach my $item (@logintext) {
 1212:                 if ($settings->{$item} ne '') {
 1213:                     $designs{'logintext'}{$item} = $settings->{$item};
 1214:                     $is_custom{$item} = 1;
 1215:                 }
 1216:             }
 1217:             if ($settings->{'font'} ne '') {
 1218:                 $designs{'font'} = $settings->{'font'};
 1219:                 $is_custom{'font'} = 1;
 1220:             }
 1221:             foreach my $item (@bgs) {
 1222:                 if ($settings->{$item} ne '') {
 1223:                     $designs{'bgs'}{$item} = $settings->{$item};
 1224:                     $is_custom{$item} = 1;
 1225:                 }
 1226:             }
 1227:             foreach my $item (@links) {
 1228:                 if ($settings->{$item} ne '') {
 1229:                     $designs{'links'}{$item} = $settings->{$item};
 1230:                     $is_custom{$item} = 1;
 1231:                 }
 1232:             }
 1233:         } else {
 1234:             if ($designhash{$dom.'.login.font'} ne '') {
 1235:                 $designs{'font'} = $designhash{$dom.'.login.font'};
 1236:                 $is_custom{'font'} = 1;
 1237:             }
 1238:             foreach my $item (@images) {
 1239:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1240:                     $designs{$item} = $designhash{$dom.'.login.'.$item};
 1241:                     $is_custom{$item} = 1;
 1242:                 }
 1243:             }
 1244:             foreach my $item (@bgs) {
 1245:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1246:                     $designs{'bgs'}{$item} = $designhash{$dom.'.login.'.$item};
 1247:                     $is_custom{$item} = 1;
 1248:                 }
 1249:             }
 1250:             foreach my $item (@links) {
 1251:                 if ($designhash{$dom.'.login.'.$item} ne '') {
 1252:                     $designs{'links'}{$item} = $designhash{$dom.'.login.'.$item};
 1253:                     $is_custom{$item} = 1;
 1254:                 }
 1255:             }
 1256:         }
 1257:         my %alt_text = &Apache::lonlocal::texthash  ( img => 'Log-in banner',
 1258:                                                       logo => 'Institution Logo',
 1259:                                                       domlogo => 'Domain Logo',
 1260:                                                       login => 'Login box');
 1261:         my $itemcount = 1;
 1262:         foreach my $item (@toggles) {
 1263:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1264:             $datatable .=  
 1265:                 '<tr'.$css_class.'><td colspan="2">'.$choices{$item}.
 1266:                 '</td><td>'.
 1267:                 '<span class="LC_nobreak"><label><input type="radio" name="'.
 1268:                 $item.'"'.$checkedon{$item}.' value="1" />'.&mt('Yes').
 1269:                 '</label>&nbsp;<label><input type="radio" name="'.$item.'"'.
 1270:                 $checkedoff{$item}.' value="0" />'.&mt('No').'</label></span></td>'.
 1271:                 '</tr>';
 1272:             $itemcount ++;
 1273:         }
 1274:         $datatable .= &display_color_options($dom,$confname,$phase,'login',$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal,\@logintext);
 1275:         $datatable .= '</tr></table></td></tr>';
 1276:     } elsif ($caller eq 'help') {
 1277:         my ($defaulturl,$defaulttype,%url,%type,%lt,%langchoices);
 1278:         my $switchserver = &check_switchserver($dom,$confname);
 1279:         my $itemcount = 1;
 1280:         $defaulturl = '/adm/loginproblems.html';
 1281:         $defaulttype = 'default';
 1282:         %lt = &Apache::lonlocal::texthash (
 1283:                      del     => 'Delete?',
 1284:                      rep     => 'Replace:',
 1285:                      upl     => 'Upload:',
 1286:                      default => 'Default',
 1287:                      custom  => 'Custom',
 1288:                                              );
 1289:         %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 1290:         my @currlangs;
 1291:         if (ref($settings) eq 'HASH') {
 1292:             if (ref($settings->{'helpurl'}) eq 'HASH') {
 1293:                 foreach my $key (sort(keys(%{$settings->{'helpurl'}}))) {
 1294:                     next if ($settings->{'helpurl'}{$key} eq '');
 1295:                     $url{$key} = $settings->{'helpurl'}{$key}.'?inhibitmenu=yes';
 1296:                     $type{$key} = 'custom';
 1297:                     unless ($key eq 'nolang') {
 1298:                         push(@currlangs,$key);
 1299:                     }
 1300:                 }
 1301:             } elsif ($settings->{'helpurl'} ne '') {
 1302:                 $type{'nolang'} = 'custom';
 1303:                 $url{'nolang'} = $settings->{'helpurl'}.'?inhibitmenu=yes';
 1304:             }
 1305:         }
 1306:         foreach my $lang ('nolang',sort(@currlangs)) {
 1307:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1308:             $datatable .= '<tr'.$css_class.'>';
 1309:             if ($url{$lang} eq '') {
 1310:                 $url{$lang} = $defaulturl;
 1311:             }
 1312:             if ($type{$lang} eq '') {
 1313:                 $type{$lang} = $defaulttype;
 1314:             }
 1315:             $datatable .= '<td colspan="2"><span class="LC_nobreak">';
 1316:             if ($lang eq 'nolang') {
 1317:                 $datatable .= &mt('Log-in help page if no specific language file: [_1]',
 1318:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1319:             } else {
 1320:                 $datatable .= &mt('Log-in help page for language: [_1] is [_2]',
 1321:                                   $langchoices{$lang},
 1322:                                   &Apache::loncommon::modal_link($url{$lang},$lt{$type{$lang}},600,500));
 1323:             }
 1324:             $datatable .= '</span></td>'."\n".
 1325:                           '<td class="LC_left_item">';
 1326:             if ($type{$lang} eq 'custom') {
 1327:                 $datatable .= '<span class="LC_nobreak"><label>'.
 1328:                               '<input type="checkbox" name="loginhelpurl_del" value="'.$lang.'" />'.
 1329:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1330:             } else {
 1331:                 $datatable .= $lt{'upl'};
 1332:             }
 1333:             $datatable .='<br />';
 1334:             if ($switchserver) {
 1335:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1336:             } else {
 1337:                 $datatable .= '<input type="file" name="loginhelpurl_'.$lang.'" />';
 1338:             }
 1339:             $datatable .= '</td></tr>';
 1340:             $itemcount ++;
 1341:         }
 1342:         my @addlangs;
 1343:         foreach my $lang (sort(keys(%langchoices))) {
 1344:             next if ((grep(/^\Q$lang\E$/,@currlangs)) || ($lang eq 'x_chef'));
 1345:             push(@addlangs,$lang);
 1346:         }
 1347:         if (@addlangs > 0) {
 1348:             my %toadd;
 1349:             map { $toadd{$_} = $langchoices{$_} ; } @addlangs;
 1350:             $toadd{''} = &mt('Select');
 1351:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 1352:             $datatable .= '<tr'.$css_class.'><td class="LC_left_item" colspan="2">'.
 1353:                           &mt('Add log-in help page for a specific language:').'&nbsp;'.
 1354:                           &Apache::loncommon::select_form('','loginhelpurl_add_lang',\%toadd).
 1355:                           '</td><td class="LC_left_item">'.$lt{'upl'}.'<br />';
 1356:             if ($switchserver) {
 1357:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1358:             } else {
 1359:                 $datatable .= '<input type="file" name="loginhelpurl_add_file" />';
 1360:             }
 1361:             $datatable .= '</td></tr>';
 1362:             $itemcount ++;
 1363:         }
 1364:         $datatable .= &captcha_choice('login',$settings,$itemcount);
 1365:     } elsif ($caller eq 'headtag') {
 1366:         my %domservers = &Apache::lonnet::get_servers($dom);
 1367:         my $choice = $choices{'headtag'};
 1368:         $css_class = ' class="LC_odd_row"';
 1369:         $datatable .= '<tr'.$css_class.'><td colspan="2">'.$choice.'</td>'.
 1370:                       '<td align="left"><table><tr><th>'.$choices{'hostid'}.'</th>'.
 1371:                       '<th>'.$choices{'current'}.'</th>'.
 1372:                       '<th>'.$choices{'action'}.'</th>'.
 1373:                       '<th>'.$choices{'exempt'}.'</th></tr>'."\n";
 1374:         my (%currurls,%currexempt);
 1375:         if (ref($settings) eq 'HASH') {
 1376:             if (ref($settings->{'headtag'}) eq 'HASH') {
 1377:                 foreach my $lonhost (keys(%{$settings->{'headtag'}})) {
 1378:                     if (ref($settings->{'headtag'}{$lonhost}) eq 'HASH') {
 1379:                         $currurls{$lonhost} = $settings->{'headtag'}{$lonhost}{'url'};
 1380:                         $currexempt{$lonhost} = $settings->{'headtag'}{$lonhost}{'exempt'};
 1381:                     }
 1382:                 }
 1383:             }
 1384:         }
 1385:         my %lt = &Apache::lonlocal::texthash(
 1386:                                                del  => 'Delete?',
 1387:                                                rep  => 'Replace:',
 1388:                                                upl  => 'Upload:',
 1389:                                                curr => 'View contents',
 1390:                                                none => 'None',
 1391:         );
 1392:         my $switchserver = &check_switchserver($dom,$confname);
 1393:         foreach my $lonhost (sort(keys(%domservers))) {
 1394:             my $exempt = &check_exempt_addresses($currexempt{$lonhost});
 1395:             $datatable .= '<tr><td>'.$domservers{$lonhost}.'</td>';
 1396:             if ($currurls{$lonhost}) {
 1397:                 $datatable .= '<td class="LC_right_item"><a href="'.
 1398:                               "javascript:void(open('$currurls{$lonhost}?inhibitmenu=yes','Custom_HeadTag',
 1399:                               'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 1400:                               '">'.$lt{'curr'}.'</a></td>'.
 1401:                               '<td><span class="LC_nobreak"><label>'.
 1402:                               '<input type="checkbox" name="loginheadtag_del" value="'.$lonhost.'" />'.
 1403:                               $lt{'del'}.'</label>&nbsp;'.$lt{'rep'}.'</span>';
 1404:             } else {
 1405:                 $datatable .= '<td class="LC_right_item">'.$lt{'none'}.'</td><td>'.$lt{'upl'};
 1406:             }
 1407:             $datatable .='<br />';
 1408:             if ($switchserver) {
 1409:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1410:             } else {
 1411:                 $datatable .= '<input type="file" name="loginheadtag_'.$lonhost.'" />';
 1412:             }
 1413:             $datatable .= '</td><td><input type="textbox" name="loginheadtagexempt_'.$lonhost.'" value="'.$exempt.'" /></td></tr>';
 1414:         }
 1415:         $datatable .= '</table></td></tr>';
 1416:     }
 1417:     return $datatable;
 1418: }
 1419: 
 1420: sub login_choices {
 1421:     my %choices =
 1422:         &Apache::lonlocal::texthash (
 1423:             coursecatalog => 'Display Course/Community Catalog link?',
 1424:             adminmail     => "Display Administrator's E-mail Address?",
 1425:             helpdesk      => 'Display "Contact Helpdesk" link',
 1426:             disallowlogin => "Login page requests redirected",
 1427:             hostid        => "Server",
 1428:             server        => "Redirect to:",
 1429:             serverpath    => "Path",
 1430:             custompath    => "Custom", 
 1431:             exempt        => "Exempt IP(s)",
 1432:             directlogin   => "No redirect",
 1433:             newuser       => "Link to create a user account",
 1434:             img           => "Header",
 1435:             logo          => "Main Logo",
 1436:             domlogo       => "Domain Logo",
 1437:             login         => "Log-in Header", 
 1438:             textcol       => "Text color",
 1439:             bgcol         => "Box color",
 1440:             bgs           => "Background colors",
 1441:             links         => "Link colors",
 1442:             font          => "Font color",
 1443:             pgbg          => "Header",
 1444:             mainbg        => "Page",
 1445:             sidebg        => "Login box",
 1446:             link          => "Link",
 1447:             alink         => "Active link",
 1448:             vlink         => "Visited link",
 1449:             headtag       => "Custom markup",
 1450:             action        => "Action",
 1451:             current       => "Current",
 1452:         );
 1453:     return %choices;
 1454: }
 1455: 
 1456: sub print_rolecolors {
 1457:     my ($phase,$role,$dom,$confname,$settings,$rowtotal) = @_;
 1458:     my %choices = &color_font_choices();
 1459:     my @bgs = ('pgbg','tabbg','sidebg');
 1460:     my @links = ('link','alink','vlink');
 1461:     my @images = ('img');
 1462:     my %alt_text = &Apache::lonlocal::texthash(img => "Banner for $role role");
 1463:     my %designhash = &Apache::loncommon::get_domainconf($dom);
 1464:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1465:     my (%is_custom,%designs);
 1466:     my %defaults = &role_defaults($role,\@bgs,\@links,\@images);
 1467:     if (ref($settings) eq 'HASH') {
 1468:         if (ref($settings->{$role}) eq 'HASH') {
 1469:             if ($settings->{$role}->{'img'} ne '') {
 1470:                 $designs{'img'} = $settings->{$role}->{'img'};
 1471:                 $is_custom{'img'} = 1;
 1472:             }
 1473:             if ($settings->{$role}->{'font'} ne '') {
 1474:                 $designs{'font'} = $settings->{$role}->{'font'};
 1475:                 $is_custom{'font'} = 1;
 1476:             }
 1477:             if ($settings->{$role}->{'fontmenu'} ne '') {
 1478:                 $designs{'fontmenu'} = $settings->{$role}->{'fontmenu'};
 1479:                 $is_custom{'fontmenu'} = 1;
 1480:             }
 1481:             foreach my $item (@bgs) {
 1482:                 if ($settings->{$role}->{$item} ne '') {
 1483:                     $designs{'bgs'}{$item} = $settings->{$role}->{$item};
 1484:                     $is_custom{$item} = 1;
 1485:                 }
 1486:             }
 1487:             foreach my $item (@links) {
 1488:                 if ($settings->{$role}->{$item} ne '') {
 1489:                     $designs{'links'}{$item} = $settings->{$role}->{$item};
 1490:                     $is_custom{$item} = 1;
 1491:                 }
 1492:             }
 1493:         }
 1494:     } else {
 1495:         if ($designhash{$dom.'.'.$role.'.img'} ne '') {
 1496:             $designs{img} = $designhash{$dom.'.'.$role.'.img'};
 1497:             $is_custom{'img'} = 1;
 1498:         }
 1499:         if ($designhash{$dom.'.'.$role.'.fontmenu'} ne '') {
 1500:             $designs{fontmenu} = $designhash{$dom.'.'.$role.'.fontmenu'};
 1501:             $is_custom{'fontmenu'} = 1; 
 1502:         }
 1503:         if ($designhash{$dom.'.'.$role.'.font'} ne '') {
 1504:             $designs{font} = $designhash{$dom.'.'.$role.'.font'};
 1505:             $is_custom{'font'} = 1;
 1506:         }
 1507:         foreach my $item (@bgs) {
 1508:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1509:                 $designs{'bgs'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1510:                 $is_custom{$item} = 1;
 1511:             
 1512:             }
 1513:         }
 1514:         foreach my $item (@links) {
 1515:             if ($designhash{$dom.'.'.$role.'.'.$item} ne '') {
 1516:                 $designs{'links'}{$item} = $designhash{$dom.'.'.$role.'.'.$item};
 1517:                 $is_custom{$item} = 1;
 1518:             }
 1519:         }
 1520:     }
 1521:     my $itemcount = 1;
 1522:     my $datatable = &display_color_options($dom,$confname,$phase,$role,$itemcount,\%choices,\%is_custom,\%defaults,\%designs,\@images,\@bgs,\@links,\%alt_text,$rowtotal);
 1523:     $datatable .= '</tr></table></td></tr>';
 1524:     return $datatable;
 1525: }
 1526: 
 1527: sub role_defaults {
 1528:     my ($role,$bgs,$links,$images,$logintext) = @_;
 1529:     my %defaults;
 1530:     unless ((ref($bgs) eq 'ARRAY') && (ref($links) eq 'ARRAY') && (ref($images) eq 'ARRAY')) {
 1531:         return %defaults;
 1532:     }
 1533:     my %defaultdesign = %Apache::loncommon::defaultdesign;
 1534:     if ($role eq 'login') {
 1535:         %defaults = (
 1536:                        font => $defaultdesign{$role.'.font'},
 1537:                     );
 1538:         if (ref($logintext) eq 'ARRAY') {
 1539:             foreach my $item (@{$logintext}) {
 1540:                 $defaults{'logintext'}{$item} = $defaultdesign{$role.'.'.$item};
 1541:             }
 1542:         }
 1543:         foreach my $item (@{$images}) {
 1544:             $defaults{'showlogo'}{$item} = 1;
 1545:         }
 1546:     } else {
 1547:         %defaults = (
 1548:                        img => $defaultdesign{$role.'.img'},
 1549:                        font => $defaultdesign{$role.'.font'},
 1550:                        fontmenu => $defaultdesign{$role.'.fontmenu'},
 1551:                     );
 1552:     }
 1553:     foreach my $item (@{$bgs}) {
 1554:         $defaults{'bgs'}{$item} = $defaultdesign{$role.'.'.$item};
 1555:     }
 1556:     foreach my $item (@{$links}) {
 1557:         $defaults{'links'}{$item} = $defaultdesign{$role.'.'.$item};
 1558:     }
 1559:     foreach my $item (@{$images}) {
 1560:         $defaults{$item} = $defaultdesign{$role.'.'.$item};
 1561:     }
 1562:     return %defaults;
 1563: }
 1564: 
 1565: sub display_color_options {
 1566:     my ($dom,$confname,$phase,$role,$itemcount,$choices,$is_custom,$defaults,$designs,
 1567:         $images,$bgs,$links,$alt_text,$rowtotal,$logintext) = @_;
 1568:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
 1569:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1570:     my $datatable = '<tr'.$css_class.'>'.
 1571:         '<td>'.$choices->{'font'}.'</td>';
 1572:     if (!$is_custom->{'font'}) {
 1573:         $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'font'}.';">'.$defaults->{'font'}.'</span></td>';
 1574:     } else {
 1575:         $datatable .= '<td>&nbsp;</td>';
 1576:     }
 1577:     my $current_color = $designs->{'font'} ? $designs->{'font'} : $defaults->{'font'};
 1578: 
 1579:     $datatable .= '<td><span class="LC_nobreak">'.
 1580:                   '<input type="text" class="colorchooser" size="10" name="'.$role.'_font"'.
 1581:                   ' value="'.$current_color.'" />&nbsp;'.
 1582:                   '&nbsp;</td></tr>';
 1583:     unless ($role eq 'login') { 
 1584:         $datatable .= '<tr'.$css_class.'>'.
 1585:                       '<td>'.$choices->{'fontmenu'}.'</td>';
 1586:         if (!$is_custom->{'fontmenu'}) {
 1587:             $datatable .=  '<td>'.&mt('Default in use:').'&nbsp;<span id="css_default_'.$role.'_font" style="color: '.$defaults->{'fontmenu'}.';">'.$defaults->{'fontmenu'}.'</span></td>';
 1588:         } else {
 1589:             $datatable .= '<td>&nbsp;</td>';
 1590:         }
 1591: 	$current_color = $designs->{'fontmenu'} ?
 1592: 	    $designs->{'fontmenu'} : $defaults->{'fontmenu'};
 1593:         $datatable .= '<td><span class="LC_nobreak">'.
 1594:                       '<input class="colorchooser" type="text" size="10" name="'
 1595: 		      .$role.'_fontmenu"'.
 1596:                       ' value="'.$current_color.'" />&nbsp;'.
 1597:                       '&nbsp;</td></tr>';
 1598:     }
 1599:     my $switchserver = &check_switchserver($dom,$confname);
 1600:     foreach my $img (@{$images}) {
 1601: 	$itemcount ++;
 1602:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1603:         $datatable .= '<tr'.$css_class.'>'.
 1604:                       '<td>'.$choices->{$img};
 1605:         my ($imgfile,$img_import,$login_hdr_pick,$logincolors);
 1606:         if ($role eq 'login') {
 1607:             if ($img eq 'login') {
 1608:                 $login_hdr_pick =
 1609:                     &login_header_options($img,$role,$defaults,$is_custom,$choices);
 1610:                 $logincolors =
 1611:                     &login_text_colors($img,$role,$logintext,$phase,$choices,
 1612:                                        $designs,$defaults);
 1613:             } elsif ($img ne 'domlogo') {
 1614:                 $datatable.= &logo_display_options($img,$defaults,$designs);
 1615:             }
 1616:         }
 1617:         $datatable .= '</td>';
 1618:         if ($designs->{$img} ne '') {
 1619:             $imgfile = $designs->{$img};
 1620: 	    $img_import = ($imgfile =~ m{^/adm/});
 1621:         } else {
 1622:             $imgfile = $defaults->{$img};
 1623:         }
 1624:         if ($imgfile) {
 1625:             my ($showfile,$fullsize);
 1626:             if ($imgfile =~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 1627:                 my $urldir = $1;
 1628:                 my $filename = $2;
 1629:                 my @info = &Apache::lonnet::stat_file($designs->{$img});
 1630:                 if (@info) {
 1631:                     my $thumbfile = 'tn-'.$filename;
 1632:                     my @thumb=&Apache::lonnet::stat_file($urldir.'/'.$thumbfile);
 1633:                     if (@thumb) {
 1634:                         $showfile = $urldir.'/'.$thumbfile;
 1635:                     } else {
 1636:                         $showfile = $imgfile;
 1637:                     }
 1638:                 } else {
 1639:                     $showfile = '';
 1640:                 }
 1641:             } elsif ($imgfile =~ m-^/(adm/[^/]+)/([^/]+)$-) {
 1642:                 $showfile = $imgfile;
 1643:                 my $imgdir = $1;
 1644:                 my $filename = $2;
 1645:                 if (-e "$londocroot/$imgdir/tn-".$filename) {
 1646:                     $showfile = "/$imgdir/tn-".$filename;
 1647:                 } else {
 1648:                     my $input = $londocroot.$imgfile;
 1649:                     my $output = "$londocroot/$imgdir/tn-".$filename;
 1650:                     if (!-e $output) {
 1651:                         my ($width,$height) = &thumb_dimensions();
 1652:                         my ($fullwidth,$fullheight) = &check_dimensions($input);
 1653:                         if ($fullwidth ne '' && $fullheight ne '') {
 1654:                             if ($fullwidth > $width && $fullheight > $height) { 
 1655:                                 my $size = $width.'x'.$height;
 1656:                                 system("convert -sample $size $input $output");
 1657:                                 $showfile = "/$imgdir/tn-".$filename;
 1658:                             }
 1659:                         }
 1660:                     }
 1661:                 }
 1662:             }
 1663:             if ($showfile) {
 1664:                 if ($showfile =~ m{^/(adm|res)/}) {
 1665:                     if ($showfile =~ m{^/res/}) {
 1666:                         my $local_showfile =
 1667:                             &Apache::lonnet::filelocation('',$showfile);
 1668:                         &Apache::lonnet::repcopy($local_showfile);
 1669:                     }
 1670:                     $showfile = &Apache::loncommon::lonhttpdurl($showfile);
 1671:                 }
 1672:                 if ($imgfile) {
 1673:                     if ($imgfile  =~ m{^/(adm|res)/}) {
 1674:                         if ($imgfile =~ m{^/res/}) {
 1675:                             my $local_imgfile =
 1676:                                 &Apache::lonnet::filelocation('',$imgfile);
 1677:                             &Apache::lonnet::repcopy($local_imgfile);
 1678:                         }
 1679:                         $fullsize = &Apache::loncommon::lonhttpdurl($imgfile);
 1680:                     } else {
 1681:                         $fullsize = $imgfile;
 1682:                     }
 1683:                 }
 1684:                 $datatable .= '<td>';
 1685:                 if ($img eq 'login') {
 1686:                     $datatable .= $login_hdr_pick;
 1687:                 } 
 1688:                 $datatable .= &image_changes($is_custom->{$img},$alt_text->{$img},$img_import,
 1689:                                              $showfile,$fullsize,$role,$img,$imgfile,$logincolors);
 1690:             } else {
 1691:                 $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1692:                               &mt('Upload:').'<br />';
 1693:             }
 1694:         } else {
 1695:             $datatable .= '<td>&nbsp;</td><td class="LC_left_item">'.
 1696:                           &mt('Upload:').'<br />';
 1697:         }
 1698:         if ($switchserver) {
 1699:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 1700:         } else {
 1701:             if ($img ne 'login') { # suppress file selection for Log-in header
 1702:                 $datatable .='&nbsp;<input type="file" name="'.$role.'_'.$img.'" />';
 1703:             }
 1704:         }
 1705:         $datatable .= '</td></tr>';
 1706:     }
 1707:     $itemcount ++;
 1708:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1709:     $datatable .= '<tr'.$css_class.'>'.
 1710:                   '<td>'.$choices->{'bgs'}.'</td>';
 1711:     my $bgs_def;
 1712:     foreach my $item (@{$bgs}) {
 1713:         if (!$is_custom->{$item}) {
 1714:             $bgs_def .= '<td><span class="LC_nobreak">'.$choices->{$item}.'</span>&nbsp;<span id="css_default_'.$role.'_'.$item.'" style="background-color: '.$defaults->{'bgs'}{$item}.';">&nbsp;&nbsp;&nbsp;</span><br />'.$defaults->{'bgs'}{$item}.'</td>';
 1715:         }
 1716:     }
 1717:     if ($bgs_def) {
 1718:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$bgs_def.'</tr></table></td>';
 1719:     } else {
 1720:         $datatable .= '<td>&nbsp;</td>';
 1721:     }
 1722:     $datatable .= '<td class="LC_right_item">'.
 1723:                   '<table border="0"><tr>';
 1724: 
 1725:     foreach my $item (@{$bgs}) {
 1726:         $datatable .= '<td align="center">'.$choices->{$item};
 1727: 	my $color = $designs->{'bgs'}{$item} ? $designs->{'bgs'}{$item} : $defaults->{'bgs'}{$item};
 1728:         if ($designs->{'bgs'}{$item}) {
 1729:             $datatable .= '&nbsp;';
 1730:         }
 1731:         $datatable .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1732:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1733:     }
 1734:     $datatable .= '</tr></table></td></tr>';
 1735:     $itemcount ++;
 1736:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 1737:     $datatable .= '<tr'.$css_class.'>'.
 1738:                   '<td>'.$choices->{'links'}.'</td>';
 1739:     my $links_def;
 1740:     foreach my $item (@{$links}) {
 1741:         if (!$is_custom->{$item}) {
 1742:             $links_def .= '<td>'.$choices->{$item}.'<br /><span id="css_default_'.$role.'_'.$item.'" style="color: '.$defaults->{'links'}{$item}.';">'.$defaults->{'links'}{$item}.'</span></td>';
 1743:         }
 1744:     }
 1745:     if ($links_def) {
 1746:         $datatable .= '<td>'.&mt('Default(s) in use:').'<br /><table border="0"><tr>'.$links_def.'</tr></table></td>';
 1747:     } else {
 1748:         $datatable .= '<td>&nbsp;</td>';
 1749:     }
 1750:     $datatable .= '<td class="LC_right_item">'.
 1751:                   '<table border="0"><tr>';
 1752:     foreach my $item (@{$links}) {
 1753: 	my $color = $designs->{'links'}{$item} ? $designs->{'links'}{$item} : $defaults->{'links'}{$item};
 1754:         $datatable .= '<td align="center">'.$choices->{$item}."\n";
 1755:         if ($designs->{'links'}{$item}) {
 1756:             $datatable.='&nbsp;';
 1757:         }
 1758:         $datatable .= '<br /><input type="text" size="8" class="colorchooser" name="'.$role.'_'.$item.'" value="'.$color.
 1759:                       '" /></td>';
 1760:     }
 1761:     $$rowtotal += $itemcount;
 1762:     return $datatable;
 1763: }
 1764: 
 1765: sub logo_display_options {
 1766:     my ($img,$defaults,$designs) = @_;
 1767:     my $checkedon;
 1768:     if (ref($defaults) eq 'HASH') {
 1769:         if (ref($defaults->{'showlogo'}) eq 'HASH') {
 1770:             if ($defaults->{'showlogo'}{$img}) {
 1771:                 $checkedon = 'checked="checked" ';     
 1772:             }
 1773:         } 
 1774:     }
 1775:     if (ref($designs) eq 'HASH') {
 1776:         if (ref($designs->{'showlogo'}) eq 'HASH') {
 1777:             if (defined($designs->{'showlogo'}{$img})) {
 1778:                 if ($designs->{'showlogo'}{$img} == 0) {
 1779:                     $checkedon = '';
 1780:                 } elsif ($designs->{'showlogo'}{$img} == 1) {
 1781:                     $checkedon = 'checked="checked" ';
 1782:                 }
 1783:             }
 1784:         }
 1785:     }
 1786:     return '<br /><label>&nbsp;&nbsp;<input type="checkbox" name="'.
 1787:            'login_showlogo_'.$img.'" value="1" '.$checkedon.'/>'.
 1788:            &mt('show').'</label>'."\n";
 1789: }
 1790: 
 1791: sub login_header_options  {
 1792:     my ($img,$role,$defaults,$is_custom,$choices) = @_;
 1793:     my $output = '';
 1794:     if ((!$is_custom->{'textcol'}) || (!$is_custom->{'bgcol'})) {
 1795:         $output .= &mt('Text default(s):').'<br />';
 1796:         if (!$is_custom->{'textcol'}) {
 1797:             $output .= $choices->{'textcol'}.':&nbsp;'.$defaults->{'logintext'}{'textcol'}.
 1798:                        '&nbsp;&nbsp;&nbsp;';
 1799:         }
 1800:         if (!$is_custom->{'bgcol'}) {
 1801:             $output .= $choices->{'bgcol'}.':&nbsp;'.
 1802:                        '<span id="css_'.$role.'_font" style="background-color: '.
 1803:                        $defaults->{'logintext'}{'bgcol'}.';">&nbsp;&nbsp;&nbsp;</span>';
 1804:         }
 1805:         $output .= '<br />';
 1806:     }
 1807:     $output .='<br />';
 1808:     return $output;
 1809: }
 1810: 
 1811: sub login_text_colors {
 1812:     my ($img,$role,$logintext,$phase,$choices,$designs,$defaults) = @_;
 1813:     my $color_menu = '<table border="0"><tr>';
 1814:     foreach my $item (@{$logintext}) {
 1815:         $color_menu .= '<td align="center">'.$choices->{$item};
 1816:         my $color = $designs->{'logintext'}{$item} ? $designs->{'logintext'}{$item} : $defaults->{'logintext'}{$item};
 1817:         $color_menu .= '<br /><input type="text" class="colorchooser" size="8" name="'.$role.'_'.$item.'" value="'.$color.
 1818:                       '" onblur = "javascript:colchg_span('."'css_".$role.'_'.$item."'".',this);" /></td>';
 1819:     }
 1820:     $color_menu .= '</tr></table><br />';
 1821:     return $color_menu;
 1822: }
 1823: 
 1824: sub image_changes {
 1825:     my ($is_custom,$alt_text,$img_import,$showfile,$fullsize,$role,$img,$imgfile,$logincolors) = @_;
 1826:     my $output;
 1827:     if ($img eq 'login') {
 1828:             # suppress image for Log-in header
 1829:     } elsif (!$is_custom) {
 1830:         if ($img ne 'domlogo') {
 1831:             $output .= &mt('Default image:').'<br />';
 1832:         } else {
 1833:             $output .= &mt('Default in use:').'<br />';
 1834:         }
 1835:     }
 1836:     if ($img eq 'login') { # suppress image for Log-in header
 1837:         $output .= '<td>'.$logincolors;
 1838:     } else {
 1839:         if ($img_import) {
 1840:             $output .= '<input type="hidden" name="'.$role.'_import_'.$img.'" value="'.$imgfile.'" />';
 1841:         }
 1842:         $output .= '<a href="'.$fullsize.'" target="_blank"><img src="'.
 1843:                    $showfile.'" alt="'.$alt_text.'" border="0" /></a></td>';
 1844:         if ($is_custom) {
 1845:             $output .= '<td>'.$logincolors.'<span class="LC_nobreak"><label>'.
 1846:                        '<input type="checkbox" name="'.
 1847:                        $role.'_del_'.$img.'" value="1" />'.&mt('Delete?').
 1848:                        '</label>&nbsp;'.&mt('Replace:').'</span><br />';
 1849:         } else {
 1850:             $output .= '<td valign="middle">'.$logincolors.&mt('Upload:').'<br />';
 1851:         }
 1852:     }
 1853:     return $output;
 1854: }
 1855: 
 1856: sub print_quotas {
 1857:     my ($dom,$settings,$rowtotal,$action) = @_;
 1858:     my $context;
 1859:     if ($action eq 'quotas') {
 1860:         $context = 'tools';
 1861:     } else {
 1862:         $context = $action;
 1863:     }
 1864:     my ($datatable,$defaultquota,$authorquota,@usertools,@options,%validations);
 1865:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 1866:     my $typecount = 0;
 1867:     my ($css_class,%titles);
 1868:     if ($context eq 'requestcourses') {
 1869:         @usertools = ('official','unofficial','community','textbook','placement');
 1870:         @options =('norequest','approval','validate','autolimit');
 1871:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 1872:         %titles = &courserequest_titles();
 1873:     } elsif ($context eq 'requestauthor') {
 1874:         @usertools = ('author');
 1875:         @options = ('norequest','approval','automatic');
 1876:         %titles = &authorrequest_titles();
 1877:     } else {
 1878:         @usertools = ('aboutme','blog','webdav','portfolio');
 1879:         %titles = &tool_titles();
 1880:     }
 1881:     if (ref($types) eq 'ARRAY') {
 1882:         foreach my $type (@{$types}) {
 1883:             my ($currdefquota,$currauthorquota);
 1884:             unless (($context eq 'requestcourses') ||
 1885:                     ($context eq 'requestauthor')) {
 1886:                 if (ref($settings) eq 'HASH') {
 1887:                     if (ref($settings->{defaultquota}) eq 'HASH') {
 1888:                         $currdefquota = $settings->{defaultquota}->{$type};
 1889:                     } else {
 1890:                         $currdefquota = $settings->{$type};
 1891:                     }
 1892:                     if (ref($settings->{authorquota}) eq 'HASH') {
 1893:                         $currauthorquota = $settings->{authorquota}->{$type};
 1894:                     }
 1895:                 }
 1896:             }
 1897:             if (defined($usertypes->{$type})) {
 1898:                 $typecount ++;
 1899:                 $css_class = $typecount%2?' class="LC_odd_row"':'';
 1900:                 $datatable .= '<tr'.$css_class.'>'.
 1901:                               '<td>'.$usertypes->{$type}.'</td>'.
 1902:                               '<td class="LC_left_item">';
 1903:                 if ($context eq 'requestcourses') {
 1904:                     $datatable .= '<table><tr>';
 1905:                 }
 1906:                 my %cell;  
 1907:                 foreach my $item (@usertools) {
 1908:                     if ($context eq 'requestcourses') {
 1909:                         my ($curroption,$currlimit);
 1910:                         if (ref($settings) eq 'HASH') {
 1911:                             if (ref($settings->{$item}) eq 'HASH') {
 1912:                                 $curroption = $settings->{$item}->{$type};
 1913:                                 if ($curroption =~ /^autolimit=(\d*)$/) {
 1914:                                     $currlimit = $1; 
 1915:                                 }
 1916:                             }
 1917:                         }
 1918:                         if (!$curroption) {
 1919:                             $curroption = 'norequest';
 1920:                         }
 1921:                         $datatable .= '<th>'.$titles{$item}.'</th>';
 1922:                         foreach my $option (@options) {
 1923:                             my $val = $option;
 1924:                             if ($option eq 'norequest') {
 1925:                                 $val = 0;  
 1926:                             }
 1927:                             if ($option eq 'validate') {
 1928:                                 my $canvalidate = 0;
 1929:                                 if (ref($validations{$item}) eq 'HASH') { 
 1930:                                     if ($validations{$item}{$type}) {
 1931:                                         $canvalidate = 1;
 1932:                                     }
 1933:                                 }
 1934:                                 next if (!$canvalidate);
 1935:                             }
 1936:                             my $checked = '';
 1937:                             if ($option eq $curroption) {
 1938:                                 $checked = ' checked="checked"';
 1939:                             } elsif ($option eq 'autolimit') {
 1940:                                 if ($curroption =~ /^autolimit/) {
 1941:                                     $checked = ' checked="checked"';
 1942:                                 }                       
 1943:                             } 
 1944:                             $cell{$item} .= '<span class="LC_nobreak"><label>'.
 1945:                                   '<input type="radio" name="crsreq_'.$item.
 1946:                                   '_'.$type.'" value="'.$val.'"'.$checked.' />'.
 1947:                                   $titles{$option}.'</label>';
 1948:                             if ($option eq 'autolimit') {
 1949:                                 $cell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 1950:                                                 $item.'_limit_'.$type.'" size="1" '.
 1951:                                                 'value="'.$currlimit.'" />';
 1952:                             }
 1953:                             $cell{$item} .= '</span> ';
 1954:                             if ($option eq 'autolimit') {
 1955:                                 $cell{$item} .= $titles{'unlimited'};
 1956:                             }
 1957:                         }
 1958:                     } elsif ($context eq 'requestauthor') {
 1959:                         my $curroption;
 1960:                         if (ref($settings) eq 'HASH') {
 1961:                             $curroption = $settings->{$type};
 1962:                         }
 1963:                         if (!$curroption) {
 1964:                             $curroption = 'norequest';
 1965:                         }
 1966:                         foreach my $option (@options) {
 1967:                             my $val = $option;
 1968:                             if ($option eq 'norequest') {
 1969:                                 $val = 0;
 1970:                             }
 1971:                             my $checked = '';
 1972:                             if ($option eq $curroption) {
 1973:                                 $checked = ' checked="checked"';
 1974:                             }
 1975:                             $datatable .= '<span class="LC_nobreak"><label>'.
 1976:                                   '<input type="radio" name="authorreq_'.$type.
 1977:                                   '" value="'.$val.'"'.$checked.' />'.
 1978:                                   $titles{$option}.'</label></span>&nbsp; ';
 1979:                         }
 1980:                     } else {
 1981:                         my $checked = 'checked="checked" ';
 1982:                         if (ref($settings) eq 'HASH') {
 1983:                             if (ref($settings->{$item}) eq 'HASH') {
 1984:                                 if ($settings->{$item}->{$type} == 0) {
 1985:                                     $checked = '';
 1986:                                 } elsif ($settings->{$item}->{$type} == 1) {
 1987:                                     $checked =  'checked="checked" ';
 1988:                                 }
 1989:                             }
 1990:                         }
 1991:                         $datatable .= '<span class="LC_nobreak"><label>'.
 1992:                                       '<input type="checkbox" name="'.$context.'_'.$item.
 1993:                                       '" value="'.$type.'" '.$checked.'/>'.$titles{$item}.
 1994:                                       '</label></span>&nbsp; ';
 1995:                     }
 1996:                 }
 1997:                 if ($context eq 'requestcourses') {
 1998:                     $datatable .= '</tr><tr>';
 1999:                     foreach my $item (@usertools) {
 2000:                         $datatable .= '<td style="vertical-align: top">'.$cell{$item}.'</td>';  
 2001:                     }
 2002:                     $datatable .= '</tr></table>';
 2003:                 }
 2004:                 $datatable .= '</td>';
 2005:                 unless (($context eq 'requestcourses') ||
 2006:                         ($context eq 'requestauthor')) {
 2007:                     $datatable .= 
 2008:                               '<td class="LC_right_item">'.
 2009:                               '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2010:                               '<input type="text" name="quota_'.$type.
 2011:                               '" value="'.$currdefquota.
 2012:                               '" size="5" /></span>'.('&nbsp;' x 2).
 2013:                               '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2014:                               '<input type="text" name="authorquota_'.$type.
 2015:                               '" value="'.$currauthorquota.
 2016:                               '" size="5" /></span></td>';
 2017:                 }
 2018:                 $datatable .= '</tr>';
 2019:             }
 2020:         }
 2021:     }
 2022:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2023:         $defaultquota = '20';
 2024:         $authorquota = '500';
 2025:         if (ref($settings) eq 'HASH') {
 2026:             if (ref($settings->{'defaultquota'}) eq 'HASH') {
 2027:                 $defaultquota = $settings->{'defaultquota'}->{'default'};
 2028:             } elsif (defined($settings->{'default'})) {
 2029:                 $defaultquota = $settings->{'default'};
 2030:             }
 2031:             if (ref($settings->{'authorquota'}) eq 'HASH') {
 2032:                 $authorquota = $settings->{'authorquota'}->{'default'};
 2033:             }
 2034:         }
 2035:     }
 2036:     $typecount ++;
 2037:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2038:     $datatable .= '<tr'.$css_class.'>'.
 2039:                   '<td>'.$othertitle.'</td>'.
 2040:                   '<td class="LC_left_item">';
 2041:     if ($context eq 'requestcourses') {
 2042:         $datatable .= '<table><tr>';
 2043:     }
 2044:     my %defcell;
 2045:     foreach my $item (@usertools) {
 2046:         if ($context eq 'requestcourses') {
 2047:             my ($curroption,$currlimit);
 2048:             if (ref($settings) eq 'HASH') {
 2049:                 if (ref($settings->{$item}) eq 'HASH') {
 2050:                     $curroption = $settings->{$item}->{'default'};
 2051:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2052:                         $currlimit = $1;
 2053:                     }
 2054:                 }
 2055:             }
 2056:             if (!$curroption) {
 2057:                 $curroption = 'norequest';
 2058:             }
 2059:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2060:             foreach my $option (@options) {
 2061:                 my $val = $option;
 2062:                 if ($option eq 'norequest') {
 2063:                     $val = 0;
 2064:                 }
 2065:                 if ($option eq 'validate') {
 2066:                     my $canvalidate = 0;
 2067:                     if (ref($validations{$item}) eq 'HASH') {
 2068:                         if ($validations{$item}{'default'}) {
 2069:                             $canvalidate = 1;
 2070:                         }
 2071:                     }
 2072:                     next if (!$canvalidate);
 2073:                 }
 2074:                 my $checked = '';
 2075:                 if ($option eq $curroption) {
 2076:                     $checked = ' checked="checked"';
 2077:                 } elsif ($option eq 'autolimit') {
 2078:                     if ($curroption =~ /^autolimit/) {
 2079:                         $checked = ' checked="checked"';
 2080:                     }
 2081:                 }
 2082:                 $defcell{$item} .= '<span class="LC_nobreak"><label>'.
 2083:                                   '<input type="radio" name="crsreq_'.$item.
 2084:                                   '_default" value="'.$val.'"'.$checked.' />'.
 2085:                                   $titles{$option}.'</label>';
 2086:                 if ($option eq 'autolimit') {
 2087:                     $defcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2088:                                        $item.'_limit_default" size="1" '.
 2089:                                        'value="'.$currlimit.'" />';
 2090:                 }
 2091:                 $defcell{$item} .= '</span> ';
 2092:                 if ($option eq 'autolimit') {
 2093:                     $defcell{$item} .= $titles{'unlimited'};
 2094:                 }
 2095:             }
 2096:         } elsif ($context eq 'requestauthor') {
 2097:             my $curroption;
 2098:             if (ref($settings) eq 'HASH') {
 2099:                 $curroption = $settings->{'default'};
 2100:             }
 2101:             if (!$curroption) {
 2102:                 $curroption = 'norequest';
 2103:             }
 2104:             foreach my $option (@options) {
 2105:                 my $val = $option;
 2106:                 if ($option eq 'norequest') {
 2107:                     $val = 0;
 2108:                 }
 2109:                 my $checked = '';
 2110:                 if ($option eq $curroption) {
 2111:                     $checked = ' checked="checked"';
 2112:                 }
 2113:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2114:                               '<input type="radio" name="authorreq_default"'.
 2115:                               ' value="'.$val.'"'.$checked.' />'.
 2116:                               $titles{$option}.'</label></span>&nbsp; ';
 2117:             }
 2118:         } else {
 2119:             my $checked = 'checked="checked" ';
 2120:             if (ref($settings) eq 'HASH') {
 2121:                 if (ref($settings->{$item}) eq 'HASH') {
 2122:                     if ($settings->{$item}->{'default'} == 0) {
 2123:                         $checked = '';
 2124:                     } elsif ($settings->{$item}->{'default'} == 1) {
 2125:                         $checked = 'checked="checked" ';
 2126:                     }
 2127:                 }
 2128:             }
 2129:             $datatable .= '<span class="LC_nobreak"><label>'.
 2130:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2131:                           '" value="default" '.$checked.'/>'.$titles{$item}.
 2132:                           '</label></span>&nbsp; ';
 2133:         }
 2134:     }
 2135:     if ($context eq 'requestcourses') {
 2136:         $datatable .= '</tr><tr>';
 2137:         foreach my $item (@usertools) {
 2138:             $datatable .= '<td style="vertical-align: top">'.$defcell{$item}.'</td>';
 2139:         }
 2140:         $datatable .= '</tr></table>';
 2141:     }
 2142:     $datatable .= '</td>';
 2143:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 2144:         $datatable .= '<td class="LC_right_item">'.
 2145:                       '<span class="LC_nobreak">'.&mt('Portfolio').':&nbsp;'.
 2146:                       '<input type="text" name="defaultquota" value="'.
 2147:                       $defaultquota.'" size="5" /></span>'.('&nbsp;' x2).
 2148:                       '<span class="LC_nobreak">'.&mt('Authoring').':&nbsp;'.
 2149:                       '<input type="text" name="authorquota" value="'.
 2150:                       $authorquota.'" size="5" /></span></td>';
 2151:     }
 2152:     $datatable .= '</tr>';
 2153:     $typecount ++;
 2154:     $css_class = $typecount%2?' class="LC_odd_row"':'';
 2155:     $datatable .= '<tr'.$css_class.'>'.
 2156:                   '<td>'.&mt('LON-CAPA Advanced Users').'<br />';
 2157:     if ($context eq 'requestcourses') {
 2158:         $datatable .= &mt('(overrides affiliation, if set)').
 2159:                       '</td>'.
 2160:                       '<td class="LC_left_item">'.
 2161:                       '<table><tr>';
 2162:     } else {
 2163:         $datatable .= &mt('(overrides affiliation, if checked)').
 2164:                       '</td>'.
 2165:                       '<td class="LC_left_item" colspan="2">'.
 2166:                       '<br />';
 2167:     }
 2168:     my %advcell;
 2169:     foreach my $item (@usertools) {
 2170:         if ($context eq 'requestcourses') {
 2171:             my ($curroption,$currlimit);
 2172:             if (ref($settings) eq 'HASH') {
 2173:                 if (ref($settings->{$item}) eq 'HASH') {
 2174:                     $curroption = $settings->{$item}->{'_LC_adv'};
 2175:                     if ($curroption =~ /^autolimit=(\d*)$/) {
 2176:                         $currlimit = $1;
 2177:                     }
 2178:                 }
 2179:             }
 2180:             $datatable .= '<th>'.$titles{$item}.'</th>';
 2181:             my $checked = '';
 2182:             if ($curroption eq '') {
 2183:                 $checked = ' checked="checked"';
 2184:             }
 2185:             $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2186:                                '<input type="radio" name="crsreq_'.$item.
 2187:                                '__LC_adv" value=""'.$checked.' />'.
 2188:                                &mt('No override set').'</label></span>&nbsp; ';
 2189:             foreach my $option (@options) {
 2190:                 my $val = $option;
 2191:                 if ($option eq 'norequest') {
 2192:                     $val = 0;
 2193:                 }
 2194:                 if ($option eq 'validate') {
 2195:                     my $canvalidate = 0;
 2196:                     if (ref($validations{$item}) eq 'HASH') {
 2197:                         if ($validations{$item}{'_LC_adv'}) {
 2198:                             $canvalidate = 1;
 2199:                         }
 2200:                     }
 2201:                     next if (!$canvalidate);
 2202:                 }
 2203:                 my $checked = '';
 2204:                 if ($val eq $curroption) {
 2205:                     $checked = ' checked="checked"';
 2206:                 } elsif ($option eq 'autolimit') {
 2207:                     if ($curroption =~ /^autolimit/) {
 2208:                         $checked = ' checked="checked"';
 2209:                     }
 2210:                 }
 2211:                 $advcell{$item} .= '<span class="LC_nobreak"><label>'.
 2212:                                   '<input type="radio" name="crsreq_'.$item.
 2213:                                   '__LC_adv" value="'.$val.'"'.$checked.' />'.
 2214:                                   $titles{$option}.'</label>';
 2215:                 if ($option eq 'autolimit') {
 2216:                     $advcell{$item} .= '&nbsp;<input type="text" name="crsreq_'.
 2217:                                        $item.'_limit__LC_adv" size="1" '.
 2218:                                        'value="'.$currlimit.'" />';
 2219:                 }
 2220:                 $advcell{$item} .= '</span> ';
 2221:                 if ($option eq 'autolimit') {
 2222:                     $advcell{$item} .= $titles{'unlimited'};
 2223:                 }
 2224:             }
 2225:         } elsif ($context eq 'requestauthor') {
 2226:             my $curroption;
 2227:             if (ref($settings) eq 'HASH') {
 2228:                 $curroption = $settings->{'_LC_adv'};
 2229:             }
 2230:             my $checked = '';
 2231:             if ($curroption eq '') {
 2232:                 $checked = ' checked="checked"';
 2233:             }
 2234:             $datatable .= '<span class="LC_nobreak"><label>'.
 2235:                           '<input type="radio" name="authorreq__LC_adv"'.
 2236:                           ' value=""'.$checked.' />'.
 2237:                           &mt('No override set').'</label></span>&nbsp; ';
 2238:             foreach my $option (@options) {
 2239:                 my $val = $option;
 2240:                 if ($option eq 'norequest') {
 2241:                     $val = 0;
 2242:                 }
 2243:                 my $checked = '';
 2244:                 if ($val eq $curroption) {
 2245:                     $checked = ' checked="checked"';
 2246:                 }
 2247:                 $datatable .= '<span class="LC_nobreak"><label>'.
 2248:                               '<input type="radio" name="authorreq__LC_adv"'.
 2249:                               ' value="'.$val.'"'.$checked.' />'.
 2250:                               $titles{$option}.'</label></span>&nbsp; ';
 2251:             }
 2252:         } else {
 2253:             my $checked = 'checked="checked" ';
 2254:             if (ref($settings) eq 'HASH') {
 2255:                 if (ref($settings->{$item}) eq 'HASH') {
 2256:                     if ($settings->{$item}->{'_LC_adv'} == 0) {
 2257:                         $checked = '';
 2258:                     } elsif ($settings->{$item}->{'_LC_adv'} == 1) {
 2259:                         $checked = 'checked="checked" ';
 2260:                     }
 2261:                 }
 2262:             }
 2263:             $datatable .= '<span class="LC_nobreak"><label>'.
 2264:                           '<input type="checkbox" name="'.$context.'_'.$item.
 2265:                           '" value="_LC_adv" '.$checked.'/>'.$titles{$item}.
 2266:                           '</label></span>&nbsp; ';
 2267:         }
 2268:     }
 2269:     if ($context eq 'requestcourses') {
 2270:         $datatable .= '</tr><tr>';
 2271:         foreach my $item (@usertools) {
 2272:             $datatable .= '<td style="vertical-align: top">'.$advcell{$item}.'</td>';
 2273:         }
 2274:         $datatable .= '</tr></table>';
 2275:     }
 2276:     $datatable .= '</td></tr>';
 2277:     $$rowtotal += $typecount;
 2278:     return $datatable;
 2279: }
 2280: 
 2281: sub print_requestmail {
 2282:     my ($dom,$action,$settings,$rowtotal) = @_;
 2283:     my ($now,$datatable,%currapp);
 2284:     $now = time;
 2285:     if (ref($settings) eq 'HASH') {
 2286:         if (ref($settings->{'notify'}) eq 'HASH') {
 2287:             if ($settings->{'notify'}{'approval'} ne '') {
 2288:                 map {$currapp{$_}=1;} split(/,/,$settings->{'notify'}{'approval'});
 2289:             }
 2290:         }
 2291:     }
 2292:     my $numinrow = 2;
 2293:     my $css_class;
 2294:     $css_class = ($$rowtotal%2? ' class="LC_odd_row"':'');
 2295:     my $text;
 2296:     if ($action eq 'requestcourses') {
 2297:         $text = &mt('Receive notification of course requests requiring approval');
 2298:     } elsif ($action eq 'requestauthor') {
 2299:         $text = &mt('Receive notification of Authoring Space requests requiring approval');
 2300:     } else {
 2301:        $text = &mt('Receive notification of queued requests for self-created user accounts requiring approval');
 2302:     }
 2303:     $datatable = '<tr'.$css_class.'>'.
 2304:                  ' <td>'.$text.'</td>'.
 2305:                  ' <td class="LC_left_item">';
 2306:     my ($numdc,$table,$rows) = &active_dc_picker($dom,$numinrow,'checkbox',
 2307:                                                  $action.'notifyapproval',%currapp);
 2308:     if ($numdc > 0) {
 2309:         $datatable .= $table;
 2310:     } else {
 2311:         $datatable .= &mt('There are no active Domain Coordinators');
 2312:     }
 2313:     $datatable .='</td></tr>';
 2314:     return $datatable;
 2315: }
 2316: 
 2317: sub print_studentcode {
 2318:     my ($settings,$rowtotal) = @_;
 2319:     my $rownum = 0; 
 2320:     my ($output,%current);
 2321:     my @crstypes = ('official','unofficial','community','textbook','placement');
 2322:     if (ref($settings) eq 'HASH') {
 2323:         if (ref($settings->{'uniquecode'}) eq 'HASH') {
 2324:             foreach my $type (@crstypes) {
 2325:                 $current{$type} = $settings->{'uniquecode'}{$type};
 2326:             }
 2327:         }
 2328:     }
 2329:     $output .= '<tr>'.
 2330:                '<td class="LC_left_item">'.&mt('Generate unique six character code as course identifier?').'</td>'.
 2331:                '<td class="LC_left_item">';
 2332:     foreach my $type (@crstypes) {
 2333:         my $check = ' ';
 2334:         if ($current{$type}) {
 2335:             $check = ' checked="checked" ';
 2336:         }
 2337:         $output .= '<span class="LC_nobreak"><label>'.
 2338:                    '<input type="checkbox" name="uniquecode" value="'.$type.'"'.$check.'/>'.
 2339:                    &mt($type).'</label></span>'.('&nbsp;'x2).' ';
 2340:     }
 2341:     $output .= '</td></tr>';
 2342:     $$rowtotal ++;
 2343:     return $output;
 2344: }
 2345: 
 2346: sub print_textbookcourses {
 2347:     my ($dom,$type,$settings,$rowtotal) = @_;
 2348:     my $rownum = 0;
 2349:     my $css_class;
 2350:     my $itemcount = 1;
 2351:     my $maxnum = 0;
 2352:     my $bookshash;
 2353:     if (ref($settings) eq 'HASH') {
 2354:         $bookshash = $settings->{$type};
 2355:     }
 2356:     my %ordered;
 2357:     if (ref($bookshash) eq 'HASH') {
 2358:         foreach my $item (keys(%{$bookshash})) {
 2359:             if (ref($bookshash->{$item}) eq 'HASH') {
 2360:                 my $num = $bookshash->{$item}{'order'};
 2361:                 $ordered{$num} = $item;
 2362:             }
 2363:         }
 2364:     }
 2365:     my $confname = $dom.'-domainconfig';
 2366:     my $switchserver = &check_switchserver($dom,$confname);
 2367:     my $maxnum = scalar(keys(%ordered));
 2368:     my $datatable;
 2369:     if (keys(%ordered)) {
 2370:         my @items = sort { $a <=> $b } keys(%ordered);
 2371:         for (my $i=0; $i<@items; $i++) {
 2372:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2373:             my $key = $ordered{$items[$i]};
 2374:             my %coursehash=&Apache::lonnet::coursedescription($key);
 2375:             my $coursetitle = $coursehash{'description'};
 2376:             my ($subject,$title,$author,$publisher,$image,$imgsrc,$cdom,$cnum);
 2377:             if (ref($bookshash->{$key}) eq 'HASH') {
 2378:                 $subject = $bookshash->{$key}->{'subject'};
 2379:                 $title = $bookshash->{$key}->{'title'};
 2380:                 if ($type eq 'textbooks') {
 2381:                     $publisher = $bookshash->{$key}->{'publisher'};
 2382:                     $author = $bookshash->{$key}->{'author'};
 2383:                     $image = $bookshash->{$key}->{'image'};
 2384:                     if ($image ne '') {
 2385:                         my ($path,$imagefile) = ($image =~ m{^(.+)/([^/]+)$});
 2386:                         my $imagethumb = "$path/tn-".$imagefile;
 2387:                         $imgsrc = '<img src="'.$imagethumb.'" alt="'.&mt('Textbook image').'" />';
 2388:                     }
 2389:                 }
 2390:             }
 2391:             my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type".'_'."$key','$type'".');"';
 2392:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 2393:                          .'<select name="'.$type.'_'.$key.'"'.$chgstr.'>';
 2394:             for (my $k=0; $k<=$maxnum; $k++) {
 2395:                 my $vpos = $k+1;
 2396:                 my $selstr;
 2397:                 if ($k == $i) {
 2398:                     $selstr = ' selected="selected" ';
 2399:                 }
 2400:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2401:             }
 2402:             $datatable .= '</select>'.('&nbsp;'x2).
 2403:                 '<label><input type="checkbox" name="'.$type.'_del" value="'.$key.'" />'.
 2404:                 &mt('Delete?').'</label></span></td>'.
 2405:                 '<td colspan="2">'.
 2406:                 '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_subject_'.$i.'" value="'.$subject.'" /></span> '.
 2407:                 ('&nbsp;'x2).
 2408:                 '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_title_'.$i.'" value="'.$title.'" /></span> ';
 2409:             if ($type eq 'textbooks') {
 2410:                 $datatable .= ('&nbsp;'x2).
 2411:                               '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_publisher_'.$i.'" value="'.$publisher.'" /></span> '.
 2412:                               ('&nbsp;'x2).
 2413:                               '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_author_'.$i.'" value="'.$author.'" /></span> '.
 2414:                               ('&nbsp;'x2).
 2415:                               '<span class="LC_nobreak">'.&mt('Thumbnail:');
 2416:                 if ($image) {
 2417:                     $datatable .= $imgsrc.
 2418:                                   '<label><input type="checkbox" name="'.$type.'_image_del"'.
 2419:                                   ' value="'.$key.'" />'.&mt('Delete?').'</label></span> '.
 2420:                                   '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 2421:                 }
 2422:                 if ($switchserver) {
 2423:                     $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2424:                 } else {
 2425:                     $datatable .= '<input type="file" name="'.$type.'_image_'.$i.'" value="" />';
 2426:                 }
 2427:             }
 2428:             $datatable .= '<input type="hidden" name="'.$type.'_id_'.$i.'" value="'.$type.'_'.$key.'" /></span> '.
 2429:                           '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2430:                           $coursetitle.'</span></td></tr>'."\n";
 2431:             $itemcount ++;
 2432:         }
 2433:     }
 2434:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 2435:     my $chgstr = ' onchange="javascript:reorderBooks(this.form,'."'$type"."_addbook_pos','$type'".');"';
 2436:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 2437:                   '<input type="hidden" name="'.$type.'_maxnum" value="'.$maxnum.'" />'."\n".
 2438:                   '<select name="'.$type.'_addbook_pos"'.$chgstr.'>';
 2439:     for (my $k=0; $k<$maxnum+1; $k++) {
 2440:         my $vpos = $k+1;
 2441:         my $selstr;
 2442:         if ($k == $maxnum) {
 2443:             $selstr = ' selected="selected" ';
 2444:         }
 2445:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 2446:     }
 2447:     $datatable .= '</select>&nbsp;'."\n".
 2448:                   '<input type="checkbox" name="'.$type.'_addbook" value="1" />'.&mt('Add').'</td>'."\n".
 2449:                   '<td colspan="2">'.
 2450:                   '<span class="LC_nobreak">'.&mt('Subject:').'<input type="text" size="15" name="'.$type.'_addbook_subject" value="" /></span> '."\n".
 2451:                   ('&nbsp;'x2).
 2452:                   '<span class="LC_nobreak">'.&mt('Title:').'<input type="text" size="30" name="'.$type.'_addbook_title" value="" /></span> '."\n".
 2453:                   ('&nbsp;'x2);
 2454:     if ($type eq 'textbooks') {
 2455:         $datatable .= '<span class="LC_nobreak">'.&mt('Publisher:').'<input type="text" size="10" name="'.$type.'_addbook_publisher" value="" /></span> '."\n".
 2456:                       ('&nbsp;'x2).
 2457:                       '<span class="LC_nobreak">'.&mt('Author(s):').'<input type="text" size="25" name="'.$type.'_addbook_author" value="" /></span> '."\n".
 2458:                       ('&nbsp;'x2).
 2459:                       '<span class="LC_nobreak">'.&mt('Image:').'&nbsp;';
 2460:         if ($switchserver) {
 2461:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 2462:         } else {
 2463:             $datatable .= '<input type="file" name="'.$type.'_addbook_image" value="" />';
 2464:         }
 2465:     }
 2466:     $datatable .= '</span>'."\n".
 2467:                   '<span class="LC_nobreak">'.&mt('LON-CAPA course:').'&nbsp;'.
 2468:                   &Apache::loncommon::select_dom_form($env{'request.role.domain'},$type.'_addbook_cdom').
 2469:                   '<input type="text" size="25" name="'.$type.'_addbook_cnum" value="" />'.
 2470:                   &Apache::loncommon::selectcourse_link
 2471:                       ('display',$type.'_addbook_cnum',$type.'_addbook_cdom',undef,undef,undef,'Course');
 2472:                   '</span></td>'."\n".
 2473:                   '</tr>'."\n";
 2474:     $itemcount ++;
 2475:     return $datatable;
 2476: }
 2477: 
 2478: sub textbookcourses_javascript {
 2479:     my ($settings) = @_;
 2480:     return unless(ref($settings) eq 'HASH');
 2481:     my (%ordered,%total,%jstext);
 2482:     foreach my $type ('textbooks','templates') {
 2483:         $total{$type} = 0;
 2484:         if (ref($settings->{$type}) eq 'HASH') {
 2485:             foreach my $item (keys(%{$settings->{$type}})) {
 2486:                 if (ref($settings->{$type}->{$item}) eq 'HASH') {
 2487:                     my $num = $settings->{$type}->{$item}{'order'};
 2488:                     $ordered{$type}{$num} = $item;
 2489:                 }
 2490:             }
 2491:             $total{$type} = scalar(keys(%{$settings->{$type}}));
 2492:         }
 2493:         my @jsarray = ();
 2494:         foreach my $item (sort {$a <=> $b } (keys(%{$ordered{$type}}))) {
 2495:             push(@jsarray,$ordered{$type}{$item});
 2496:         }
 2497:         $jstext{$type} = '    var '.$type.' = Array('."'".join("','",@jsarray)."'".');'."\n";
 2498:     }
 2499:     return <<"ENDSCRIPT";
 2500: <script type="text/javascript">
 2501: // <![CDATA[
 2502: function reorderBooks(form,item,caller) {
 2503:     var changedVal;
 2504: $jstext{'textbooks'};
 2505: $jstext{'templates'};
 2506:     var newpos;
 2507:     var maxh;
 2508:     if (caller == 'textbooks') {  
 2509:         newpos = 'textbooks_addbook_pos';
 2510:         maxh = 1 + $total{'textbooks'};
 2511:     } else {
 2512:         newpos = 'templates_addbook_pos';
 2513:         maxh = 1 + $total{'templates'};
 2514:     }
 2515:     var current = new Array;
 2516:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2517:     if (item == newpos) {
 2518:         changedVal = newitemVal;
 2519:     } else {
 2520:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2521:         current[newitemVal] = newpos;
 2522:     }
 2523:     if (caller == 'textbooks') {
 2524:         for (var i=0; i<textbooks.length; i++) {
 2525:             var elementName = 'textbooks_'+textbooks[i];
 2526:             if (elementName != item) {
 2527:                 if (form.elements[elementName]) {
 2528:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2529:                     current[currVal] = elementName;
 2530:                 }
 2531:             }
 2532:         }
 2533:     }
 2534:     if (caller == 'templates') {
 2535:         for (var i=0; i<templates.length; i++) {
 2536:             var elementName = 'templates_'+templates[i];
 2537:             if (elementName != item) {
 2538:                 if (form.elements[elementName]) {
 2539:                     var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2540:                     current[currVal] = elementName;
 2541:                 }
 2542:             }
 2543:         }
 2544:     }
 2545:     var oldVal;
 2546:     for (var j=0; j<maxh; j++) {
 2547:         if (current[j] == undefined) {
 2548:             oldVal = j;
 2549:         }
 2550:     }
 2551:     if (oldVal < changedVal) {
 2552:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2553:            var elementName = current[k];
 2554:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2555:         }
 2556:     } else {
 2557:         for (var k=changedVal; k<oldVal; k++) {
 2558:             var elementName = current[k];
 2559:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2560:         }
 2561:     }
 2562:     return;
 2563: }
 2564: 
 2565: // ]]>
 2566: </script>
 2567: 
 2568: ENDSCRIPT
 2569: }
 2570: 
 2571: sub ltitools_javascript {
 2572:     my ($settings) = @_;
 2573:     return unless(ref($settings) eq 'HASH');
 2574:     my (%ordered,$total,%jstext);
 2575:     $total = 0;
 2576:     foreach my $item (keys(%{$settings})) {
 2577:         if (ref($settings->{$item}) eq 'HASH') {
 2578:             my $num = $settings->{$item}{'order'};
 2579:             $ordered{$num} = $item;
 2580:         }
 2581:     }
 2582:     $total = scalar(keys(%{$settings}));
 2583:     my @jsarray = ();
 2584:     foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 2585:         push(@jsarray,$ordered{$item});
 2586:     }
 2587:     my $jstext = '    var ltitools = Array('."'".join("','",@jsarray)."'".');'."\n";
 2588:     return <<"ENDSCRIPT";
 2589: <script type="text/javascript">
 2590: // <![CDATA[
 2591: function reorderLTI(form,item) {
 2592:     var changedVal;
 2593: $jstext
 2594:     var newpos = 'ltitools_add_pos';
 2595:     var maxh = 1 + $total;
 2596:     var current = new Array;
 2597:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 2598:     if (item == newpos) {
 2599:         changedVal = newitemVal;
 2600:     } else {
 2601:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 2602:         current[newitemVal] = newpos;
 2603:     }
 2604:     for (var i=0; i<ltitools.length; i++) {
 2605:         var elementName = 'ltitools_'+ltitools[i];
 2606:         if (elementName != item) {
 2607:             if (form.elements[elementName]) {
 2608:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 2609:                 current[currVal] = elementName;
 2610:             }
 2611:         }
 2612:     }
 2613:     var oldVal;
 2614:     for (var j=0; j<maxh; j++) {
 2615:         if (current[j] == undefined) {
 2616:             oldVal = j;
 2617:         }
 2618:     }
 2619:     if (oldVal < changedVal) {
 2620:         for (var k=oldVal+1; k<=changedVal ; k++) {
 2621:            var elementName = current[k];
 2622:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 2623:         }
 2624:     } else {
 2625:         for (var k=changedVal; k<oldVal; k++) {
 2626:             var elementName = current[k];
 2627:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 2628:         }
 2629:     }
 2630:     return;
 2631: }
 2632: 
 2633: // ]]>
 2634: </script>
 2635: 
 2636: ENDSCRIPT
 2637: }
 2638: 
 2639: sub print_autoenroll {
 2640:     my ($dom,$settings,$rowtotal) = @_;
 2641:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
 2642:     my ($defdom,$runon,$runoff,$coownerson,$coownersoff,$failsafe);
 2643:     if (ref($settings) eq 'HASH') {
 2644:         if (exists($settings->{'run'})) {
 2645:             if ($settings->{'run'} eq '0') {
 2646:                 $runoff = ' checked="checked" ';
 2647:                 $runon = ' ';
 2648:             } else {
 2649:                 $runon = ' checked="checked" ';
 2650:                 $runoff = ' ';
 2651:             }
 2652:         } else {
 2653:             if ($autorun) {
 2654:                 $runon = ' checked="checked" ';
 2655:                 $runoff = ' ';
 2656:             } else {
 2657:                 $runoff = ' checked="checked" ';
 2658:                 $runon = ' ';
 2659:             }
 2660:         }
 2661:         if (exists($settings->{'co-owners'})) {
 2662:             if ($settings->{'co-owners'} eq '0') {
 2663:                 $coownersoff = ' checked="checked" ';
 2664:                 $coownerson = ' ';
 2665:             } else {
 2666:                 $coownerson = ' checked="checked" ';
 2667:                 $coownersoff = ' ';
 2668:             }
 2669:         } else {
 2670:             $coownersoff = ' checked="checked" ';
 2671:             $coownerson = ' ';
 2672:         }
 2673:         if (exists($settings->{'sender_domain'})) {
 2674:             $defdom = $settings->{'sender_domain'};
 2675:         }
 2676:         if (exists($settings->{'autofailsafe'})) {
 2677:             $failsafe = $settings->{'autofailsafe'};
 2678:         }
 2679:     } else {
 2680:         if ($autorun) {
 2681:             $runon = ' checked="checked" ';
 2682:             $runoff = ' ';
 2683:         } else {
 2684:             $runoff = ' checked="checked" ';
 2685:             $runon = ' ';
 2686:         }
 2687:     }
 2688:     my $domform = &Apache::loncommon::select_dom_form($defdom,'sender_domain',1);
 2689:     my $notif_sender;
 2690:     if (ref($settings) eq 'HASH') {
 2691:         $notif_sender = $settings->{'sender_uname'};
 2692:     }
 2693:     my $datatable='<tr class="LC_odd_row">'.
 2694:                   '<td>'.&mt('Auto-enrollment active?').'</td>'.
 2695:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2696:                   '<input type="radio" name="autoenroll_run"'.
 2697:                   $runon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2698:                   '<label><input type="radio" name="autoenroll_run"'.
 2699:                   $runoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2700:                   '</tr><tr>'.
 2701:                   '<td>'.&mt('Notification messages - sender').
 2702:                   '</td><td class="LC_right_item"><span class="LC_nobreak">'.
 2703:                   &mt('username').':&nbsp;'.
 2704:                   '<input type="text" name="sender_uname" value="'.
 2705:                   $notif_sender.'" size="10" />&nbsp;&nbsp;'.&mt('domain').
 2706:                   ':&nbsp;'.$domform.'</span></td></tr>'.
 2707:                   '<tr class="LC_odd_row">'.
 2708:                   '<td>'.&mt('Automatically assign co-ownership').'</td>'.
 2709:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2710:                   '<input type="radio" name="autoassign_coowners"'.
 2711:                   $coownerson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2712:                   '<label><input type="radio" name="autoassign_coowners"'.
 2713:                   $coownersoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2714:                   '</tr><tr>'.
 2715:                   '<td>'.&mt('Failsafe for no drops when institutional data missing').'</td>'.
 2716:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2717:                   '<input type="text" name="autoenroll_failsafe"'.
 2718:                   ' value="'.$failsafe.'" size="4" /></td></tr>';
 2719:     $$rowtotal += 4;
 2720:     return $datatable;
 2721: }
 2722: 
 2723: sub print_autoupdate {
 2724:     my ($position,$dom,$settings,$rowtotal) = @_;
 2725:     my $datatable;
 2726:     if ($position eq 'top') {
 2727:         my $updateon = ' ';
 2728:         my $updateoff = ' checked="checked" ';
 2729:         my $classlistson = ' ';
 2730:         my $classlistsoff = ' checked="checked" ';
 2731:         if (ref($settings) eq 'HASH') {
 2732:             if ($settings->{'run'} eq '1') {
 2733:                 $updateon = $updateoff;
 2734:                 $updateoff = ' ';
 2735:             }
 2736:             if ($settings->{'classlists'} eq '1') {
 2737:                 $classlistson = $classlistsoff;
 2738:                 $classlistsoff = ' ';
 2739:             }
 2740:         }
 2741:         my %title = (
 2742:                    run => 'Auto-update active?',
 2743:                    classlists => 'Update information in classlists?',
 2744:                     );
 2745:         $datatable = '<tr class="LC_odd_row">'. 
 2746:                   '<td>'.&mt($title{'run'}).'</td>'.
 2747:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2748:                   '<input type="radio" name="autoupdate_run"'.
 2749:                   $updateon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2750:                   '<label><input type="radio" name="autoupdate_run"'.
 2751:                   $updateoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2752:                   '</tr><tr>'.
 2753:                   '<td>'.&mt($title{'classlists'}).'</td>'.
 2754:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 2755:                   '<label><input type="radio" name="classlists"'.
 2756:                   $classlistson.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2757:                   '<label><input type="radio" name="classlists"'.
 2758:                   $classlistsoff.'value="0" />'.&mt('No').'</label></span></td>'.
 2759:                   '</tr>';
 2760:         $$rowtotal += 2;
 2761:     } elsif ($position eq 'middle') {
 2762:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2763:         my $numinrow = 3;
 2764:         my $locknamesettings;
 2765:         $datatable .= &insttypes_row($settings,$types,$usertypes,
 2766:                                      $dom,$numinrow,$othertitle,
 2767:                                     'lockablenames');
 2768:         $$rowtotal ++;
 2769:     } else {
 2770:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2771:         my @fields = ('lastname','firstname','middlename','generation',
 2772:                       'permanentemail','id');
 2773:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 2774:         my $numrows = 0;
 2775:         if (ref($types) eq 'ARRAY') {
 2776:             if (@{$types} > 0) {
 2777:                 $datatable = 
 2778:                     &usertype_update_row($settings,$usertypes,\%fieldtitles,
 2779:                                          \@fields,$types,\$numrows);
 2780:                     $$rowtotal += @{$types}; 
 2781:             }
 2782:         }
 2783:         $datatable .= 
 2784:             &usertype_update_row($settings,{'default' => $othertitle},
 2785:                                  \%fieldtitles,\@fields,['default'],
 2786:                                  \$numrows);
 2787:         $$rowtotal ++;     
 2788:     }
 2789:     return $datatable;
 2790: }
 2791: 
 2792: sub print_autocreate {
 2793:     my ($dom,$settings,$rowtotal) = @_;
 2794:     my (%createon,%createoff,%currhash);
 2795:     my @types = ('xml','req');
 2796:     if (ref($settings) eq 'HASH') {
 2797:         foreach my $item (@types) {
 2798:             $createoff{$item} = ' checked="checked" ';
 2799:             $createon{$item} = ' ';
 2800:             if (exists($settings->{$item})) {
 2801:                 if ($settings->{$item}) {
 2802:                     $createon{$item} = ' checked="checked" ';
 2803:                     $createoff{$item} = ' ';
 2804:                 }
 2805:             }
 2806:         }
 2807:         if ($settings->{'xmldc'} ne '') {
 2808:             $currhash{$settings->{'xmldc'}} = 1;
 2809:         }
 2810:     } else {
 2811:         foreach my $item (@types) {
 2812:             $createoff{$item} = ' checked="checked" ';
 2813:             $createon{$item} = ' ';
 2814:         }
 2815:     }
 2816:     $$rowtotal += 2;
 2817:     my $numinrow = 2;
 2818:     my $datatable='<tr class="LC_odd_row">'.
 2819:                   '<td>'.&mt('Create pending official courses from XML files').'</td>'.
 2820:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2821:                   '<input type="radio" name="autocreate_xml"'.
 2822:                   $createon{'xml'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2823:                   '<label><input type="radio" name="autocreate_xml"'.
 2824:                   $createoff{'xml'}.' value="0" />'.&mt('No').'</label></span>'.
 2825:                   '</td></tr><tr>'.
 2826:                   '<td>'.&mt('Create pending requests for official courses (if validated)').'</td>'.
 2827:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2828:                   '<input type="radio" name="autocreate_req"'.
 2829:                   $createon{'req'}.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2830:                   '<label><input type="radio" name="autocreate_req"'.
 2831:                   $createoff{'req'}.' value="0" />'.&mt('No').'</label></span>';
 2832:     my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 2833:                                                    'autocreate_xmldc',%currhash);
 2834:     $datatable .= '</td></tr><tr class="LC_odd_row"><td>';
 2835:     if ($numdc > 1) {
 2836:         $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)').
 2837:                       '</td><td class="LC_left_item">';
 2838:     } else {
 2839:         $datatable .= &mt('Course creation processed as:').
 2840:                       '</td><td class="LC_right_item">';
 2841:     }
 2842:     $datatable .= $dctable.'</td></tr>';
 2843:     $$rowtotal += $rows;
 2844:     return $datatable;
 2845: }
 2846: 
 2847: sub print_directorysrch {
 2848:     my ($position,$dom,$settings,$rowtotal) = @_;
 2849:     my $datatable;
 2850:     if ($position eq 'top') {
 2851:         my $instsrchon = ' ';
 2852:         my $instsrchoff = ' checked="checked" ';
 2853:         my ($exacton,$containson,$beginson);
 2854:         my $instlocalon = ' ';
 2855:         my $instlocaloff = ' checked="checked" ';
 2856:         if (ref($settings) eq 'HASH') {
 2857:             if ($settings->{'available'} eq '1') {
 2858:                 $instsrchon = $instsrchoff;
 2859:                 $instsrchoff = ' ';
 2860:             }
 2861:             if ($settings->{'localonly'} eq '1') {
 2862:                 $instlocalon = $instlocaloff;
 2863:                 $instlocaloff = ' ';
 2864:             }
 2865:             if (ref($settings->{'searchtypes'}) eq 'ARRAY') {
 2866:                 foreach my $type (@{$settings->{'searchtypes'}}) {
 2867:                     if ($type eq 'exact') {
 2868:                         $exacton = ' checked="checked" ';
 2869:                     } elsif ($type eq 'contains') {
 2870:                         $containson = ' checked="checked" ';
 2871:                     } elsif ($type eq 'begins') {
 2872:                         $beginson = ' checked="checked" ';
 2873:                     }
 2874:                 }
 2875:             } else {
 2876:                 if ($settings->{'searchtypes'} eq 'exact') {
 2877:                     $exacton = ' checked="checked" ';
 2878:                 } elsif ($settings->{'searchtypes'} eq 'contains') {
 2879:                     $containson = ' checked="checked" ';
 2880:                 } elsif ($settings->{'searchtypes'} eq 'specify') {
 2881:                     $exacton = ' checked="checked" ';
 2882:                     $containson = ' checked="checked" ';
 2883:                 }
 2884:             }
 2885:         }
 2886:         my ($searchtitles,$titleorder) = &sorted_searchtitles();
 2887:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 2888: 
 2889:         my $numinrow = 4;
 2890:         my $cansrchrow = 0;
 2891:         $datatable='<tr class="LC_odd_row">'.
 2892:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Institutional directory search available?').'</span></td>'.
 2893:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2894:                    '<input type="radio" name="dirsrch_available"'.
 2895:                    $instsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2896:                    '<label><input type="radio" name="dirsrch_available"'.
 2897:                    $instsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2898:                    '</tr><tr>'.
 2899:                    '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search institution?').'</span></td>'.
 2900:                    '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2901:                    '<input type="radio" name="dirsrch_instlocalonly"'.
 2902:                    $instlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2903:                    '<label><input type="radio" name="dirsrch_instlocalonly"'.
 2904:                    $instlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2905:                    '</tr>';
 2906:         $$rowtotal += 2;
 2907:         if (ref($usertypes) eq 'HASH') {
 2908:             if (keys(%{$usertypes}) > 0) {
 2909:                 $datatable .= &insttypes_row($settings,$types,$usertypes,$dom,
 2910:                                              $numinrow,$othertitle,'cansearch');
 2911:                 $cansrchrow = 1;
 2912:             }
 2913:         }
 2914:         if ($cansrchrow) {
 2915:             $$rowtotal ++;
 2916:             $datatable .= '<tr>';
 2917:         } else {
 2918:             $datatable .= '<tr class="LC_odd_row">';
 2919:         }
 2920:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Supported search methods').
 2921:                       '</span></td><td class="LC_left_item" colspan="2"><table><tr>';
 2922:         foreach my $title (@{$titleorder}) {
 2923:             if (defined($searchtitles->{$title})) {
 2924:                 my $check = ' ';
 2925:                 if (ref($settings) eq 'HASH') {
 2926:                     if (ref($settings->{'searchby'}) eq 'ARRAY') {
 2927:                         if (grep(/^\Q$title\E$/,@{$settings->{'searchby'}})) {
 2928:                             $check = ' checked="checked" ';
 2929:                         }
 2930:                     }
 2931:                 }
 2932:                 $datatable .= '<td class="LC_left_item">'.
 2933:                               '<span class="LC_nobreak"><label>'.
 2934:                               '<input type="checkbox" name="searchby" '.
 2935:                               'value="'.$title.'"'.$check.'/>'.
 2936:                               $searchtitles->{$title}.'</label></span></td>';
 2937:             }
 2938:         }
 2939:         $datatable .= '</tr></table></td></tr>';
 2940:         $$rowtotal ++;
 2941:         if ($cansrchrow) {
 2942:             $datatable .= '<tr class="LC_odd_row">';
 2943:         } else {
 2944:             $datatable .= '<tr>';
 2945:         }
 2946:         $datatable .= '<td><span class ="LC_nobreak">'.&mt('Search latitude').'</span></td>'.   
 2947:                       '<td class="LC_left_item" colspan="2">'.
 2948:                       '<span class="LC_nobreak"><label>'.
 2949:                       '<input type="checkbox" name="searchtypes" '.
 2950:                       $exacton.' value="exact" />'.&mt('Exact match').
 2951:                       '</label>&nbsp;'.
 2952:                       '<label><input type="checkbox" name="searchtypes" '.
 2953:                       $beginson.' value="begins" />'.&mt('Begins with').
 2954:                       '</label>&nbsp;'.
 2955:                       '<label><input type="checkbox" name="searchtypes" '.
 2956:                       $containson.' value="contains" />'.&mt('Contains').
 2957:                       '</label></span></td></tr>';
 2958:         $$rowtotal ++;
 2959:     } else {
 2960:         my $domsrchon = ' checked="checked" ';
 2961:         my $domsrchoff = ' ';
 2962:         my $domlocalon = ' ';
 2963:         my $domlocaloff = ' checked="checked" ';
 2964:         if (ref($settings) eq 'HASH') {
 2965:             if ($settings->{'lclocalonly'} eq '1') {
 2966:                 $domlocalon = $domlocaloff;
 2967:                 $domlocaloff = ' ';
 2968:             }
 2969:             if ($settings->{'lcavailable'} eq '0') {
 2970:                 $domsrchoff = $domsrchon;
 2971:                 $domsrchon = ' ';
 2972:             }
 2973:         }
 2974:         $datatable='<tr class="LC_odd_row">'.
 2975:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('LON-CAPA directory search available?').'</span></td>'.
 2976:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2977:                       '<input type="radio" name="dirsrch_domavailable"'.
 2978:                       $domsrchon.' value="1" />'.&mt('Yes').'</label>&nbsp;'.
 2979:                       '<label><input type="radio" name="dirsrch_domavailable"'.
 2980:                       $domsrchoff.' value="0" />'.&mt('No').'</label></span></td>'.
 2981:                       '</tr><tr>'.
 2982:                       '<td colspan="2"><span class ="LC_nobreak">'.&mt('Other domains can search LON-CAPA domain?').'</span></td>'.
 2983:                       '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 2984:                       '<input type="radio" name="dirsrch_domlocalonly"'.
 2985:                       $domlocaloff.' value="0" />'.&mt('Yes').'</label>&nbsp;'.
 2986:                       '<label><input type="radio" name="dirsrch_domlocalonly"'.
 2987:                       $domlocalon.' value="1" />'.&mt('No').'</label></span></td>'.
 2988:                       '</tr>';
 2989:         $$rowtotal += 2;
 2990:     }
 2991:     return $datatable;
 2992: }
 2993: 
 2994: sub print_contacts {
 2995:     my ($position,$dom,$settings,$rowtotal) = @_;
 2996:     my $datatable;
 2997:     my @contacts = ('adminemail','supportemail');
 2998:     my (%checked,%to,%otheremails,%bccemails,%includestr,%includeloc,%currfield,
 2999:         $maxsize,$fields,$fieldtitles,$fieldoptions,$possoptions,@mailings);
 3000:     if ($position eq 'top') {
 3001:         if (ref($settings) eq 'HASH') {
 3002:             foreach my $item (@contacts) {
 3003:                 if (exists($settings->{$item})) {
 3004:                     $to{$item} = $settings->{$item};
 3005:                 }
 3006:             }
 3007:         }
 3008:     } elsif ($position eq 'middle') {
 3009:         @mailings = ('errormail','packagesmail','lonstatusmail','requestsmail',
 3010:                      'updatesmail','idconflictsmail');
 3011:         foreach my $type (@mailings) {
 3012:             $otheremails{$type} = '';
 3013:         }
 3014:     } else {
 3015:         @mailings = ('helpdeskmail','otherdomsmail');
 3016:         foreach my $type (@mailings) {
 3017:             $otheremails{$type} = '';
 3018:         }
 3019:         $bccemails{'helpdeskmail'} = '';
 3020:         $bccemails{'otherdomsmail'} = '';
 3021:         $includestr{'helpdeskmail'} = '';
 3022:         $includestr{'otherdomsmail'} = '';
 3023:         ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
 3024:     }
 3025:     if (ref($settings) eq 'HASH') {
 3026:         unless ($position eq 'top') {
 3027:             foreach my $type (@mailings) {
 3028:                 if (exists($settings->{$type})) {
 3029:                     if (ref($settings->{$type}) eq 'HASH') {
 3030:                         foreach my $item (@contacts) {
 3031:                             if ($settings->{$type}{$item}) {
 3032:                                 $checked{$type}{$item} = ' checked="checked" ';
 3033:                             }
 3034:                         }
 3035:                         $otheremails{$type} = $settings->{$type}{'others'};
 3036:                         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3037:                             $bccemails{$type} = $settings->{$type}{'bcc'};
 3038:                             if ($settings->{$type}{'include'} ne '') {
 3039:                                 ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3040:                                 $includestr{$type} = &unescape($includestr{$type});
 3041:                             }
 3042:                         }
 3043:                     }
 3044:                 } elsif ($type eq 'lonstatusmail') {
 3045:                     $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3046:                 }
 3047:             }
 3048:         }
 3049:         if ($position eq 'bottom') {
 3050:             foreach my $type (@mailings) {
 3051:                 $bccemails{$type} = $settings->{$type}{'bcc'};
 3052:                 if ($settings->{$type}{'include'} ne '') {
 3053:                     ($includeloc{$type},$includestr{$type}) = split(/:/,$settings->{$type}{'include'},2);
 3054:                     $includestr{$type} = &unescape($includestr{$type});
 3055:                 }
 3056:             }
 3057:             if (ref($settings->{'helpform'}) eq 'HASH') {
 3058:                 if (ref($fields) eq 'ARRAY') {
 3059:                     foreach my $field (@{$fields}) {
 3060:                         $currfield{$field} = $settings->{'helpform'}{$field};
 3061:                     }
 3062:                 }
 3063:                 if (exists($settings->{'helpform'}{'maxsize'})) {
 3064:                     $maxsize = $settings->{'helpform'}{'maxsize'};
 3065:                 } else {
 3066:                     $maxsize = '1.0';
 3067:                 }
 3068:             } else {
 3069:                 if (ref($fields) eq 'ARRAY') {
 3070:                     foreach my $field (@{$fields}) {
 3071:                         $currfield{$field} = 'yes';
 3072:                     }
 3073:                 }
 3074:                 $maxsize = '1.0';
 3075:             }
 3076:         }
 3077:     } else {
 3078:         if ($position eq 'top') {
 3079:             $to{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
 3080:             $to{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
 3081:             $checked{'errormail'}{'adminemail'} = ' checked="checked" ';
 3082:             $checked{'packagesmail'}{'adminemail'} = ' checked="checked" ';
 3083:             $checked{'lonstatusmail'}{'adminemail'} = ' checked="checked" ';
 3084:             $checked{'requestsmail'}{'adminemail'} = ' checked="checked" ';
 3085:             $checked{'updatesmail'}{'adminemail'} = ' checked="checked" ';
 3086:             $checked{'idconflictsmail'}{'adminemail'} = ' checked="checked" ';
 3087:         } elsif ($position eq 'bottom') {
 3088:             $checked{'helpdeskmail'}{'supportemail'} = ' checked="checked" ';
 3089:             $checked{'otherdomsmail'}{'supportemail'} = ' checked="checked" ';
 3090:             if (ref($fields) eq 'ARRAY') {
 3091:                 foreach my $field (@{$fields}) {
 3092:                     $currfield{$field} = 'yes';
 3093:                 }
 3094:             }
 3095:             $maxsize = '1.0';
 3096:         }
 3097:     }
 3098:     my ($titles,$short_titles) = &contact_titles();
 3099:     my $rownum = 0;
 3100:     my $css_class;
 3101:     if ($position eq 'top') {
 3102:         foreach my $item (@contacts) {
 3103:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3104:             $datatable .= '<tr'.$css_class.'>'. 
 3105:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 3106:                           '</span></td><td class="LC_right_item">'.
 3107:                           '<input type="text" name="'.$item.'" value="'.
 3108:                           $to{$item}.'" /></td></tr>';
 3109:             $rownum ++;
 3110:         }
 3111:     } else {
 3112:         foreach my $type (@mailings) {
 3113:             $css_class = $rownum%2?' class="LC_odd_row"':'';
 3114:             $datatable .= '<tr'.$css_class.'>'.
 3115:                           '<td><span class="LC_nobreak">'.
 3116:                           $titles->{$type}.': </span></td>'.
 3117:                           '<td class="LC_left_item">';
 3118:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3119:                 $datatable .= '<fieldset><legend>'.&mt('E-mail recipient(s)').'</legend>';
 3120:             }
 3121:             $datatable .= '<span class="LC_nobreak">';
 3122:             foreach my $item (@contacts) {
 3123:                 $datatable .= '<label>'.
 3124:                               '<input type="checkbox" name="'.$type.'"'.
 3125:                               $checked{$type}{$item}.
 3126:                               ' value="'.$item.'" />'.$short_titles->{$item}.
 3127:                               '</label>&nbsp;';
 3128:             }
 3129:             $datatable .= '</span><br />'.&mt('Others').':&nbsp;&nbsp;'.
 3130:                           '<input type="text" name="'.$type.'_others" '.
 3131:                           'value="'.$otheremails{$type}.'"  />';
 3132:             my %locchecked;
 3133:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
 3134:                 foreach my $loc ('s','b') {
 3135:                     if ($includeloc{$type} eq $loc) {
 3136:                         $locchecked{$loc} = ' checked="checked"';
 3137:                         last;
 3138:                     }
 3139:                 }
 3140:                 $datatable .= '<br />'.&mt('Bcc:').('&nbsp;'x6).
 3141:                               '<input type="text" name="'.$type.'_bcc" '.
 3142:                               'value="'.$bccemails{$type}.'"  /></fieldset>'.
 3143:                               '<fieldset><legend>'.&mt('Optional added text').'</legend>'.
 3144:                               &mt('Text automatically added to e-mail:').' '.
 3145:                               '<input type="text" name="'.$type.'_includestr" value="'.$includestr{$type}.'" /><br >'.
 3146:                               '<span class="LC_nobreak">'.&mt('Location:').'&nbsp;'.
 3147:                               '<label><input type="radio" name="'.$type.'_includeloc" value="s"'.$locchecked{'s'}.' />'.&mt('in subject').'</label>'.
 3148:                               ('&nbsp;'x2).
 3149:                               '<label><input type="radio" name="'.$type.'_includeloc" value="b"'.$locchecked{'b'}.' />'.&mt('in body').'</label>'.
 3150:                               '</span></fieldset>';
 3151:             }
 3152:             $datatable .= '</td></tr>'."\n";
 3153:             $rownum ++;
 3154:         }
 3155:     }
 3156:     if ($position eq 'middle') {
 3157:         my %choices;
 3158:         $choices{'reporterrors'} = &mt('E-mail error reports to [_1]',
 3159:                                        &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3160:                                        &mt('LON-CAPA core group - MSU'),600,500));
 3161:         $choices{'reportupdates'} = &mt('E-mail record of completed LON-CAPA updates to [_1]',
 3162:                                         &Apache::loncommon::modal_link('http://loncapa.org/core.html',
 3163:                                         &mt('LON-CAPA core group - MSU'),600,500));
 3164:         my @toggles = ('reporterrors','reportupdates');
 3165:         my %defaultchecked = ('reporterrors'  => 'on',
 3166:                               'reportupdates' => 'on');
 3167:         (my $reports,$rownum) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3168:                                                    \%choices,$rownum);
 3169:         $datatable .= $reports;
 3170:     } elsif ($position eq 'bottom') {
 3171:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 3172:         $datatable .= '<tr'.$css_class.'>'.
 3173:                       '<td>'.&mt('Extra helpdesk form fields:').'<br />'.
 3174:                       &mt('(e-mail, subject, and description always shown)').
 3175:                       '</td><td class="LC_left_item">';
 3176:         if ((ref($fields) eq 'ARRAY') && (ref($fieldtitles) eq 'HASH') &&
 3177:             (ref($fieldoptions) eq 'HASH') && (ref($possoptions) eq 'HASH')) {
 3178:             $datatable .= '<table><tr><th>'.&mt('Field').'</th><th>'.&mt('Status').'</th></tr>';
 3179:             foreach my $field (@{$fields}) {
 3180:                 $datatable .= '<tr><td>'.$fieldtitles->{$field};
 3181:                 if (($field eq 'screenshot') || ($field eq 'cc')) {
 3182:                     $datatable .= ' '.&mt('(logged-in users)');
 3183:                 }
 3184:                 $datatable .='</td><td>';
 3185:                 my $clickaction;
 3186:                 if ($field eq 'screenshot') {
 3187:                     $clickaction = ' onclick="screenshotSize(this);"';
 3188:                 }
 3189:                 if (ref($possoptions->{$field}) eq 'ARRAY') {
 3190:                     foreach my $option (@{$possoptions->{$field}}) {
 3191:                         my $checked;
 3192:                         if ($currfield{$field} eq $option) {
 3193:                             $checked = ' checked="checked"';
 3194:                         }
 3195:                         $datatable .= '<span class="LC_nobreak"><label>'.
 3196:                                       '<input type="radio" name="helpform_'.$field.'" '.
 3197:                                       'value="'.$option.'"'.$checked.$clickaction.' />'.$fieldoptions->{$option}.
 3198:                                       '</label></span>'.('&nbsp;'x2);
 3199:                     }
 3200:                 }
 3201:                 if ($field eq 'screenshot') {
 3202:                     my $display;
 3203:                     if ($currfield{$field} eq 'no') {
 3204:                         $display = ' style="display:none"';
 3205:                     }
 3206:                     $datatable .= '</td></tr><tr id="help_screenshotsize"'.$display.' />'.
 3207:                                   '<td>'.&mt('Maximum size for upload (MB)').'</td><td>'.
 3208:                                   '<input type="text" size="5" name="helpform_maxsize" value="'.$maxsize.'" />';
 3209:                 }
 3210:                 $datatable .= '</td></tr>';
 3211:             }
 3212:             $datatable .= '</table>';
 3213:         }
 3214:         $datatable .= '</td></tr>'."\n";
 3215:         $rownum ++;
 3216:     }
 3217:     $$rowtotal += $rownum;
 3218:     return $datatable;
 3219: }
 3220: 
 3221: sub contacts_javascript {
 3222:     return <<"ENDSCRIPT";
 3223: 
 3224: <script type="text/javascript">
 3225: // <![CDATA[
 3226: 
 3227: function screenshotSize(field) {
 3228:     if (document.getElementById('help_screenshotsize')) {
 3229:         if (field.value == 'no') {
 3230:             document.getElementById('help_screenshotsize').style.display="none";
 3231:         } else {
 3232:             document.getElementById('help_screenshotsize').style.display="";
 3233:         }
 3234:     }
 3235:     return;
 3236: }
 3237: 
 3238: // ]]>
 3239: </script>
 3240: 
 3241: ENDSCRIPT
 3242: }
 3243: 
 3244: sub print_helpsettings {
 3245:     my ($position,$dom,$settings,$rowtotal) = @_;
 3246:     my $confname = $dom.'-domainconfig';
 3247:     my $formname = 'display';
 3248:     my ($datatable,$itemcount);
 3249:     if ($position eq 'top') {
 3250:         $itemcount = 1;
 3251:         my (%choices,%defaultchecked,@toggles);
 3252:         $choices{'submitbugs'} = &mt('Display link to: [_1]?',
 3253:                                      &Apache::loncommon::modal_link('http://bugs.loncapa.org',
 3254:                                      &mt('LON-CAPA bug tracker'),600,500));
 3255:         %defaultchecked = ('submitbugs' => 'on');
 3256:         @toggles = ('submitbugs');
 3257:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 3258:                                                      \%choices,$itemcount);
 3259:         $$rowtotal ++;
 3260:     } else {
 3261:         my $css_class;
 3262:         my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
 3263:         my (%customroles,%ordered,%current);
 3264:         if (ref($settings->{'adhoc'}) eq 'HASH') {
 3265:             %current = %{$settings->{'adhoc'}};
 3266:         }
 3267:         my $count = 0;
 3268:         foreach my $key (sort(keys(%existing))) {
 3269:             if ($key=~/^rolesdef\_(\w+)$/) {
 3270:                 my $rolename = $1;
 3271:                 my (%privs,$order);
 3272:                 ($privs{'system'},$privs{'domain'},$privs{'course'}) = split(/\_/,$existing{$key});
 3273:                 $customroles{$rolename} = \%privs;
 3274:                 if (ref($current{$rolename}) eq 'HASH') {
 3275:                     $order = $current{$rolename}{'order'};
 3276:                 }
 3277:                 if ($order eq '') {
 3278:                     $order = $count;
 3279:                 }
 3280:                 $ordered{$order} = $rolename;
 3281:                 $count++;
 3282:             }
 3283:         }
 3284:         my $maxnum = scalar(keys(%ordered));
 3285:         my @roles_by_num = ();
 3286:         foreach my $item (sort {$a <=> $b } (keys(%ordered))) {
 3287:             push(@roles_by_num,$item);
 3288:         }
 3289:         my $context = 'domprefs';
 3290:         my $crstype = 'Course';
 3291:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 3292:         my @accesstypes = ('all','dh','da','none');
 3293:         my ($numstatustypes,@jsarray);
 3294:         if (ref($types) eq 'ARRAY') {
 3295:             if (@{$types} > 0) {
 3296:                 $numstatustypes = scalar(@{$types});
 3297:                 push(@accesstypes,'status');
 3298:                 @jsarray = ('bystatus');
 3299:             }
 3300:         }
 3301:         my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
 3302:         if (keys(%domhelpdesk)) {
 3303:             push(@accesstypes,('inc','exc'));
 3304:             push(@jsarray,('notinc','notexc'));
 3305:         }
 3306:         my $hiddenstr = join("','",@jsarray);
 3307:         $datatable .= &helpsettings_javascript(\@roles_by_num,$maxnum,$hiddenstr,$formname);
 3308:         my $context = 'domprefs';
 3309:         my $crstype = 'Course';
 3310:         my $prefix = 'helproles_';
 3311:         my $add_class = 'LC_hidden';
 3312:         foreach my $num (@roles_by_num) {
 3313:             my $role = $ordered{$num};
 3314:             my ($desc,$access,@statuses);
 3315:             if (ref($current{$role}) eq 'HASH') {
 3316:                 $desc = $current{$role}{'desc'};
 3317:                 $access = $current{$role}{'access'};
 3318:                 if (ref($current{$role}{'insttypes'}) eq 'ARRAY') {
 3319:                     @statuses = @{$current{$role}{'insttypes'}};
 3320:                 }
 3321:             }
 3322:             if ($desc eq '') {
 3323:                 $desc = $role;
 3324:             }
 3325:             my $identifier = 'custhelp'.$num;
 3326:             my %full=();
 3327:             my %levels= (
 3328:                          course => {},
 3329:                          domain => {},
 3330:                          system => {},
 3331:                         );
 3332:             my %levelscurrent=(
 3333:                                course => {},
 3334:                                domain => {},
 3335:                                system => {},
 3336:                               );
 3337:             &Apache::lonuserutils::custom_role_privs($customroles{$role},\%full,\%levels,\%levelscurrent);
 3338:             my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3339:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3340:             my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$num."_pos'".');"';
 3341:             $datatable .= '<tr '.$css_class.'><td valign="top"><b>'.$role.'</b><br />'.
 3342:                           '<select name="helproles_'.$num.'_pos"'.$chgstr.'>';
 3343:             for (my $k=0; $k<=$maxnum; $k++) {
 3344:                 my $vpos = $k+1;
 3345:                 my $selstr;
 3346:                 if ($k == $num) {
 3347:                     $selstr = ' selected="selected" ';
 3348:                 }
 3349:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3350:             }
 3351:             $datatable .= '</select>'.('&nbsp;'x2).
 3352:                           '<input type="hidden" name="helproles_'.$num.'" value="'.$role.'" />'.
 3353:                           '</td>'.
 3354:                           '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3355:                           &mt('Name shown to users:').
 3356:                           '<input type="text" name="helproles_'.$num.'_desc" value="'.$desc.'" />'.
 3357:                           '</fieldset>'.
 3358:                           &helpdeskroles_access($dom,$prefix,$num,$add_class,$current{$role},\@accesstypes,
 3359:                                                 $othertitle,$usertypes,$types,\%domhelpdesk).
 3360:                           '<fieldset>'.
 3361:                           '<legend>'.&mt('Role privileges').&adhocbutton($prefix,$num,'privs','show').'</legend>'.
 3362:                           &Apache::lonuserutils::custom_role_table($crstype,\%full,\%levels,
 3363:                                                                    \%levelscurrent,$identifier,
 3364:                                                                    'LC_hidden',$prefix.$num.'_privs').
 3365:                           '</fieldset></td>';
 3366:             $itemcount ++;
 3367:         }
 3368:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3369:         my $newcust = 'custhelp'.$count;
 3370:         my (%privs,%levelscurrent);
 3371:         my %full=();
 3372:         my %levels= (
 3373:                      course => {},
 3374:                      domain => {},
 3375:                      system => {},
 3376:                     );
 3377:         &Apache::lonuserutils::custom_role_privs(\%privs,\%full,\%levels,\%levelscurrent);
 3378:         my @templateroles = &Apache::lonuserutils::custom_template_roles($context,$crstype);
 3379:         my $chgstr = ' onchange="javascript:reorderHelpRoles(this.form,'."'helproles_".$count."_pos'".');"';
 3380:         $datatable .= '<tr '.$css_class.'><td valign="top"><span class="LC_nobreak"><label>'.
 3381:                       '<input type="hidden" name="helproles_maxnum" value="'.$maxnum.'" />'."\n".
 3382:                       '<select name="helproles_'.$count.'_pos"'.$chgstr.'>';
 3383:         for (my $k=0; $k<$maxnum+1; $k++) {
 3384:             my $vpos = $k+1;
 3385:             my $selstr;
 3386:             if ($k == $maxnum) {
 3387:                 $selstr = ' selected="selected" ';
 3388:             }
 3389:             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3390:         }
 3391:         $datatable .= '</select>&nbsp;'."\n".
 3392:                       '<input type="checkbox" name="newcusthelp" value="'.$count.'" />'. &mt('Add').
 3393:                       '</label></span></td>'.
 3394:                       '<td><fieldset><legend>'.&mt('Role name').'</legend>'.
 3395:                       '<span class="LC_nobreak">'.
 3396:                       &mt('Internal name:').
 3397:                       '<input type="text" size="10" name="custhelpname'.$count.'" value="" />'.
 3398:                       '</span>'.('&nbsp;'x4).
 3399:                       '<span class="LC_nobreak">'.
 3400:                       &mt('Name shown to users:').
 3401:                       '<input type="text" size="20" name="helproles_'.$count.'_desc" value="" />'.
 3402:                       '</span></fieldset>'.
 3403:                        &helpdeskroles_access($dom,$prefix,$count,'',undef,\@accesstypes,$othertitle,
 3404:                                              $usertypes,$types,\%domhelpdesk).
 3405:                       '<fieldset><legend>'.&mt('Role privileges').'</legend>'.
 3406:                       &Apache::lonuserutils::custom_role_header($context,$crstype,
 3407:                                                                 \@templateroles,$newcust).
 3408:                       &Apache::lonuserutils::custom_role_table('Course',\%full,\%levels,
 3409:                                                                \%levelscurrent,$newcust).
 3410:                       '</fieldset></td></tr>';
 3411:         $count ++;
 3412:         $$rowtotal += $count;
 3413:     }
 3414:     return $datatable;
 3415: }
 3416: 
 3417: sub adhocbutton {
 3418:     my ($prefix,$num,$field,$visibility) = @_;
 3419:     my %lt = &Apache::lonlocal::texthash(
 3420:                                           show => 'Show details',
 3421:                                           hide => 'Hide details',
 3422:                                         );
 3423:     return '<span style="text-decoration:line-through; font-weight: normal;">'.('&nbsp;'x10).
 3424:            '</span>'.('&nbsp;'x2).'<input type="button" id="'.$prefix.$num.'_'.$field.'_vis"'.
 3425:            ' value="'.$lt{$visibility}.'" style="height:20px;" '.
 3426:            'onclick="toggleHelpdeskItem('."'$num','$field'".');" />'.('&nbsp;'x2);
 3427: }
 3428: 
 3429: sub helpsettings_javascript {
 3430:     my ($roles_by_num,$total,$hiddenstr,$formname) = @_;
 3431:     return unless(ref($roles_by_num) eq 'ARRAY');
 3432:     my %html_js_lt = &Apache::lonlocal::texthash(
 3433:                                           show => 'Show details',
 3434:                                           hide => 'Hide details',
 3435:                                         );
 3436:     &html_escape(\%html_js_lt);
 3437:     my $jstext = '    var helproles = Array('."'".join("','",@{$roles_by_num})."'".');'."\n";
 3438:     return <<"ENDSCRIPT";
 3439: <script type="text/javascript">
 3440: // <![CDATA[
 3441: 
 3442: function reorderHelpRoles(form,item) {
 3443:     var changedVal;
 3444: $jstext
 3445:     var newpos = 'helproles_${total}_pos';
 3446:     var maxh = 1 + $total;
 3447:     var current = new Array();
 3448:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 3449:     if (item == newpos) {
 3450:         changedVal = newitemVal;
 3451:     } else {
 3452:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 3453:         current[newitemVal] = newpos;
 3454:     }
 3455:     for (var i=0; i<helproles.length; i++) {
 3456:         var elementName = 'helproles_'+helproles[i]+'_pos';
 3457:         if (elementName != item) {
 3458:             if (form.elements[elementName]) {
 3459:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 3460:                 current[currVal] = elementName;
 3461:             }
 3462:         }
 3463:     }
 3464:     var oldVal;
 3465:     for (var j=0; j<maxh; j++) {
 3466:         if (current[j] == undefined) {
 3467:             oldVal = j;
 3468:         }
 3469:     }
 3470:     if (oldVal < changedVal) {
 3471:         for (var k=oldVal+1; k<=changedVal ; k++) {
 3472:            var elementName = current[k];
 3473:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 3474:         }
 3475:     } else {
 3476:         for (var k=changedVal; k<oldVal; k++) {
 3477:             var elementName = current[k];
 3478:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 3479:         }
 3480:     }
 3481:     return;
 3482: }
 3483: 
 3484: function helpdeskAccess(num) {
 3485:     var curraccess = null;
 3486:     if (document.$formname.elements['helproles_'+num+'_access'].length) {
 3487:         for (var i=0; i<document.$formname.elements['helproles_'+num+'_access'].length; i++) {
 3488:             if (document.$formname.elements['helproles_'+num+'_access'][i].checked) {
 3489:                 curraccess = document.$formname.elements['helproles_'+num+'_access'][i].value;
 3490:             }
 3491:         }
 3492:     }
 3493:     var shown = Array();
 3494:     var hidden = Array();
 3495:     if (curraccess == 'none') {
 3496:         hidden = Array('$hiddenstr');
 3497:     } else {
 3498:         if (curraccess == 'status') {
 3499:             shown = Array('bystatus');
 3500:             hidden = Array('notinc','notexc');
 3501:         } else {
 3502:             if (curraccess == 'exc') {
 3503:                 shown = Array('notexc');
 3504:                 hidden = Array('notinc','bystatus');
 3505:             }
 3506:             if (curraccess == 'inc') {
 3507:                 shown = Array('notinc');
 3508:                 hidden = Array('notexc','bystatus');
 3509:             }
 3510:             if ((curraccess == 'all') || (curraccess == 'dh') || (curraccess == 'da')) {
 3511:                 hidden = Array('notinc','notexc','bystatus');
 3512:             }
 3513:         }
 3514:     }
 3515:     if (hidden.length > 0) {
 3516:         for (var i=0; i<hidden.length; i++) {
 3517:             if (document.getElementById('helproles_'+num+'_'+hidden[i])) {
 3518:                 document.getElementById('helproles_'+num+'_'+hidden[i]).style.display = 'none';
 3519:             }
 3520:         }
 3521:     }
 3522:     if (shown.length > 0) {
 3523:         for (var i=0; i<shown.length; i++) {
 3524:             if (document.getElementById('helproles_'+num+'_'+shown[i])) {
 3525:                 if (shown[i] == 'privs') {
 3526:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'block';
 3527:                 } else {
 3528:                     document.getElementById('helproles_'+num+'_'+shown[i]).style.display = 'inline-block';
 3529:                 }
 3530:             }
 3531:         }
 3532:     }
 3533:     return;
 3534: }
 3535: 
 3536: function toggleHelpdeskItem(num,field) {
 3537:     if (document.getElementById('helproles_'+num+'_'+field)) {
 3538:         if (document.getElementById('helproles_'+num+'_'+field).className.match(/(?:^|\\s)LC_hidden(?!\\S)/)) {
 3539:             document.getElementById('helproles_'+num+'_'+field).className =
 3540:                 document.getElementById('helproles_'+num+'_'+field).className.replace(/(?:^|\\s)LC_hidden(?!\\S)/g ,'');
 3541:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3542:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{hide}';
 3543:             }
 3544:         } else {
 3545:             document.getElementById('helproles_'+num+'_'+field).className += ' LC_hidden';
 3546:             if (document.getElementById('helproles_'+num+'_'+field+'_vis')) {
 3547:                 document.getElementById('helproles_'+num+'_'+field+'_vis').value = '$html_js_lt{show}';
 3548:             }
 3549:         }
 3550:     }
 3551:     return;
 3552: }
 3553: 
 3554: // ]]>
 3555: </script>
 3556: 
 3557: ENDSCRIPT
 3558: }
 3559: 
 3560: sub helpdeskroles_access {
 3561:     my ($dom,$prefix,$num,$add_class,$current,$accesstypes,$othertitle,
 3562:         $usertypes,$types,$domhelpdesk) = @_;
 3563:     return unless ((ref($accesstypes) eq 'ARRAY') && (ref($domhelpdesk) eq 'HASH'));
 3564:     my %lt = &Apache::lonlocal::texthash(
 3565:                     'rou'    => 'Role usage',
 3566:                     'whi'    => 'Which helpdesk personnel may use this role?',
 3567:                     'all'    => 'All with domain helpdesk or helpdesk assistant role',
 3568:                     'dh'     => 'All with domain helpdesk role',
 3569:                     'da'     => 'All with domain helpdesk assistant role',
 3570:                     'none'   => 'None',
 3571:                     'status' => 'Determined based on institutional status',
 3572:                     'inc'    => 'Include all, but exclude specific personnel',
 3573:                     'exc'    => 'Exclude all, but include specific personnel',
 3574:                   );
 3575:     my %usecheck = (
 3576:                      all => ' checked="checked"',
 3577:                    );
 3578:     my %displaydiv = (
 3579:                       status => 'none',
 3580:                       inc    => 'none',
 3581:                       exc    => 'none',
 3582:                       priv   => 'block',
 3583:                      );
 3584:     my $output;
 3585:     if (ref($current) eq 'HASH') {
 3586:         if (($current->{'access'} ne '') && ($current->{'access'} ne 'all')) {
 3587:             if (grep(/^\Q$current->{access}\E$/,@{$accesstypes})) {
 3588:                 $usecheck{$current->{access}} = $usecheck{'all'};
 3589:                 delete($usecheck{'all'});
 3590:                 if ($current->{access} =~ /^(status|inc|exc)$/) {
 3591:                     my $access = $1;
 3592:                     $displaydiv{$access} = 'inline';
 3593:                 } elsif ($current->{access} eq 'none') {
 3594:                     $displaydiv{'priv'} = 'none';
 3595:                 }
 3596:             }
 3597:         }
 3598:     }
 3599:     $output = '<fieldset id="'.$prefix.$num.'_usage"><legend>'.$lt{'rou'}.'</legend>'.
 3600:               '<p>'.$lt{'whi'}.'</p>';
 3601:     foreach my $access (@{$accesstypes}) {
 3602:         $output .= '<p><label><input type="radio" name="'.$prefix.$num.'_access" value="'.$access.'" '.$usecheck{$access}.
 3603:                    ' onclick="helpdeskAccess('."'$num'".');" />'.
 3604:                    $lt{$access}.'</label>';
 3605:         if ($access eq 'status') {
 3606:             $output .= '<div id="'.$prefix.$num.'_bystatus" style="display:'.$displaydiv{$access}.'">'.
 3607:                        &Apache::lonuserutils::adhoc_status_types($dom,$prefix,$num,$current->{$access},
 3608:                                                                  $othertitle,$usertypes,$types).
 3609:                        '</div>';
 3610:         } elsif (($access eq 'inc') && (keys(%{$domhelpdesk}) > 0)) {
 3611:             $output .= '<div id="'.$prefix.$num.'_notinc" style="display:'.$displaydiv{$access}.'">'.
 3612:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3613:                        '</div>';
 3614:         } elsif (($access eq 'exc') && (keys(%{$domhelpdesk}) > 0)) {
 3615:             $output .= '<div id="'.$prefix.$num.'_notexc" style="display:'.$displaydiv{$access}.'">'.
 3616:                        &Apache::lonuserutils::adhoc_staff($access,$prefix,$num,$current->{$access},$domhelpdesk).
 3617:                        '</div>';
 3618:         }
 3619:         $output .= '</p>';
 3620:     }
 3621:     $output .= '</fieldset>';
 3622:     return $output;
 3623: }
 3624: 
 3625: sub radiobutton_prefs {
 3626:     my ($settings,$toggles,$defaultchecked,$choices,$itemcount,$onclick,
 3627:         $additional,$align) = @_;
 3628:     return unless ((ref($toggles) eq 'ARRAY') && (ref($defaultchecked) eq 'HASH') &&
 3629:                    (ref($choices) eq 'HASH'));
 3630: 
 3631:     my (%checkedon,%checkedoff,$datatable,$css_class);
 3632: 
 3633:     foreach my $item (@{$toggles}) {
 3634:         if ($defaultchecked->{$item} eq 'on') {
 3635:             $checkedon{$item} = ' checked="checked" ';
 3636:             $checkedoff{$item} = ' ';
 3637:         } elsif ($defaultchecked->{$item} eq 'off') {
 3638:             $checkedoff{$item} = ' checked="checked" ';
 3639:             $checkedon{$item} = ' ';
 3640:         }
 3641:     }
 3642:     if (ref($settings) eq 'HASH') {
 3643:         foreach my $item (@{$toggles}) {
 3644:             if ($settings->{$item} eq '1') {
 3645:                 $checkedon{$item} =  ' checked="checked" ';
 3646:                 $checkedoff{$item} = ' ';
 3647:             } elsif ($settings->{$item} eq '0') {
 3648:                 $checkedoff{$item} =  ' checked="checked" ';
 3649:                 $checkedon{$item} = ' ';
 3650:             }
 3651:         }
 3652:     }
 3653:     if ($onclick) {
 3654:         $onclick = ' onclick="'.$onclick.'"';
 3655:     }
 3656:     foreach my $item (@{$toggles}) {
 3657:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3658:         $datatable .=
 3659:             '<tr'.$css_class.'><td valign="top">'.
 3660:             '<span class="LC_nobreak">'.$choices->{$item}.
 3661:             '</span></td>';
 3662:         if ($align eq 'left') {
 3663:             $datatable .= '<td class="LC_left_item">';
 3664:         } else {
 3665:             $datatable .= '<td class="LC_right_item">';
 3666:         }
 3667:         $datatable .=
 3668:             '<span class="LC_nobreak">'.
 3669:             '<label><input type="radio" name="'.
 3670:             $item.'" '.$checkedon{$item}.' value="1"'.$onclick.' />'.&mt('Yes').
 3671:             '</label>&nbsp;<label><input type="radio" name="'.$item.'" '.
 3672:             $checkedoff{$item}.' value="0"'.$onclick.' />'.&mt('No').'</label>'.
 3673:             '</span>'.$additional.
 3674:             '</td>'.
 3675:             '</tr>';
 3676:         $itemcount ++;
 3677:     }
 3678:     return ($datatable,$itemcount);
 3679: }
 3680: 
 3681: sub print_ltitools {
 3682:     my ($dom,$settings,$rowtotal) = @_;
 3683:     my $rownum = 0;
 3684:     my $css_class;
 3685:     my $itemcount = 1;
 3686:     my $maxnum = 0;
 3687:     my %ordered;
 3688:     if (ref($settings) eq 'HASH') {
 3689:         foreach my $item (keys(%{$settings})) {
 3690:             if (ref($settings->{$item}) eq 'HASH') {
 3691:                 my $num = $settings->{$item}{'order'};
 3692:                 $ordered{$num} = $item;
 3693:             }
 3694:         }
 3695:     }
 3696:     my $confname = $dom.'-domainconfig';
 3697:     my $switchserver = &check_switchserver($dom,$confname);
 3698:     my $maxnum = scalar(keys(%ordered));
 3699:     my $datatable = &ltitools_javascript($settings);
 3700:     my %lt = &ltitools_names();
 3701:     my @courseroles = ('cc','in','ta','ep','st');
 3702:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 3703:     my @fields = ('fullname','firstname','lastname','email','user','roles');
 3704:     if (keys(%ordered)) {
 3705:         my @items = sort { $a <=> $b } keys(%ordered);
 3706:         for (my $i=0; $i<@items; $i++) {
 3707:             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3708:             my $item = $ordered{$items[$i]};
 3709:             my ($title,$key,$secret,$url,$imgsrc,$version);
 3710:             if (ref($settings->{$item}) eq 'HASH') {
 3711:                 $title = $settings->{$item}->{'title'};
 3712:                 $url = $settings->{$item}->{'url'};
 3713:                 $key = $settings->{$item}->{'key'};
 3714:                 $secret = $settings->{$item}->{'secret'};
 3715:                 my $image = $settings->{$item}->{'image'};
 3716:                 if ($image ne '') {
 3717:                     $imgsrc = '<img src="'.$image.'" alt="'.&mt('Tool Provider icon').'" />';
 3718:                 }
 3719:             }
 3720:             my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_".$item."'".');"';
 3721:             $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 3722:                          .'<select name="ltitools_'.$item.'"'.$chgstr.'>';
 3723:             for (my $k=0; $k<=$maxnum; $k++) {
 3724:                 my $vpos = $k+1;
 3725:                 my $selstr;
 3726:                 if ($k == $i) {
 3727:                     $selstr = ' selected="selected" ';
 3728:                 }
 3729:                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3730:             }
 3731:             $datatable .= '</select>'.('&nbsp;'x2).
 3732:                 '<label><input type="checkbox" name="ltitools_del" value="'.$item.'" />'.
 3733:                 &mt('Delete?').'</label></span></td>'.
 3734:                 '<td colspan="2">'.
 3735:                 '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 3736:                 '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_title_'.$i.'" value="'.$title.'" /></span> '.
 3737:                 ('&nbsp;'x2).
 3738:                 '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_version_'.$i.'">'.
 3739:                 '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '.
 3740:                 ('&nbsp;'x2).
 3741:                 '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_msgtype_'.$i.'">'.
 3742:                 '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 3743:                 '<br /><br />'.
 3744:                 '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_url_'.$i.'"'.
 3745:                 ' value="'.$url.'" /></span>'.
 3746:                 ('&nbsp;'x2).
 3747:                 '<span class="LC_nobreak">'.$lt{'key'}.
 3748:                 '<input type="text" size="25" name="ltitools_key_'.$i.'" value="'.$key.'" /></span> '.
 3749:                 ('&nbsp;'x2).
 3750:                 '<span class="LC_nobreak">'.$lt{'secret'}.':'.
 3751:                 '<input type="password" size="20" name="ltitools_secret_'.$i.'" value="'.$secret.'" />'.
 3752:                 '<label><input type="checkbox" name="visible" onclick="if (this.checked) { this.form.ltitools_secret_'.$i.'.type='."'text'".' } else { this.form.ltitools_secret_'.$i.'.type='."'password'".' }" />'.&mt('Visible input').'</label>'.
 3753:                 '<input type="hidden" name="ltitools_id_'.$i.'" value="'.$item.'" /></span>'.
 3754:                 '</fieldset>'.
 3755:                 '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 3756:                 '<span class="LC_nobreak">'.&mt('Display target:');
 3757:             my %currdisp;
 3758:             if (ref($settings->{$item}->{'display'}) eq 'HASH') {
 3759:                 if ($settings->{$item}->{'display'}->{'target'} eq 'window') {
 3760:                     $currdisp{'window'} = ' checked="checked"';
 3761:                 } elsif ($settings->{$item}->{'display'}->{'target'} eq 'tab') {
 3762:                     $currdisp{'tab'} = ' checked="checked"';
 3763:                 } else {
 3764:                     $currdisp{'iframe'} = ' checked="checked"';
 3765:                 }
 3766:                 if ($settings->{$item}->{'display'}->{'width'} =~ /^(\d+)$/) {
 3767:                     $currdisp{'width'} = $1;
 3768:                 }
 3769:                 if ($settings->{$item}->{'display'}->{'height'} =~ /^(\d+)$/) {
 3770:                      $currdisp{'height'} = $1;
 3771:                 }
 3772:                 $currdisp{'linktext'} = $settings->{$item}->{'display'}->{'linktext'};
 3773:                 $currdisp{'explanation'} = $settings->{$item}->{'display'}->{'explanation'};
 3774:             } else {
 3775:                 $currdisp{'iframe'} = ' checked="checked"';
 3776:             }
 3777:             foreach my $disp ('iframe','tab','window') {
 3778:                 $datatable .= '<label><input type="radio" name="ltitools_target_'.$i.'" value="'.$disp.'"'.$currdisp{$disp}.' />'.
 3779:                               $lt{$disp}.'</label>'.('&nbsp;'x2);
 3780:             }
 3781:             $datatable .= ('&nbsp;'x4);
 3782:             foreach my $dimen ('width','height') {
 3783:                 $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 3784:                               '<input type="text" name="ltitools_'.$dimen.'_'.$i.'" size="5" value="'.$currdisp{$dimen}.'" /></label>'.
 3785:                               ('&nbsp;'x2);
 3786:             }
 3787:             $datatable .= '<br />'.
 3788:                           '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 3789:                           '<input type="text" name="ltitools_linktext_'.$i.'" size="25" value="'.$currdisp{'linktext'}.'" /></label></div>'.
 3790:                           '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 3791:                           '<textarea name="ltitools_explanation_'.$i.'" rows="5" cols="40">'.$currdisp{'explanation'}.
 3792:                           '</textarea></div><div style=""></div><br />';
 3793:             $datatable .= '<br />';
 3794:             foreach my $extra ('passback','roster') {
 3795:                 my $checkedon = '';
 3796:                 my $checkedoff = ' checked="checked"';
 3797:                 if ($settings->{$item}->{$extra}) {
 3798:                     $checkedon = $checkedoff;
 3799:                     $checkedoff = '';
 3800:                 }
 3801:                 $datatable .= $lt{$extra}.'&nbsp;'.
 3802:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="1"'.$checkedon.' />'.
 3803:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 3804:                               '<label><input type="radio" name="ltitools_'.$extra.'_'.$i.'" value="0"'.$checkedoff.' />'.
 3805:                               &mt('No').'</label>'.('&nbsp;'x4);
 3806:             }
 3807:             $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;';
 3808:             if ($imgsrc) {
 3809:                 $datatable .= $imgsrc.
 3810:                               '<label><input type="checkbox" name="ltitools_image_del"'.
 3811:                               ' value="'.$item.'" />'.&mt('Delete?').'</label></span> '.
 3812:                               '<span class="LC_nobreak">&nbsp;'.&mt('Replace:').'&nbsp;';
 3813:             } else {
 3814:                 $datatable .= '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 3815:             }
 3816:             if ($switchserver) {
 3817:                 $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3818:             } else {
 3819:                 $datatable .= '<input type="file" name="ltitools_image_'.$i.'" value="" />';
 3820:             }
 3821:             $datatable .= '</span></fieldset>';
 3822:             my (%checkedfields,%rolemaps);
 3823:             if (ref($settings->{$item}) eq 'HASH') {
 3824:                 if (ref($settings->{$item}->{'fields'}) eq 'HASH') {
 3825:                     %checkedfields = %{$settings->{$item}->{'fields'}};
 3826:                 }
 3827:                 if (ref($settings->{$item}->{'roles'}) eq 'HASH') {
 3828:                     %rolemaps = %{$settings->{$item}->{'roles'}};
 3829:                     $checkedfields{'roles'} = 1;
 3830:                 }
 3831:             }
 3832:             $datatable .= '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 3833:                           '<span class="LC_nobreak">';
 3834:             foreach my $field (@fields) {
 3835:                 my $checked;
 3836:                 if ($checkedfields{$field}) {
 3837:                     $checked = ' checked="checked"';
 3838:                 }
 3839:                 $datatable .= '<label>'.
 3840:                               '<input type="checkbox" name="ltitools_fields_'.$i.'" value="'.$field.'"'.$checked.' />'.
 3841:                               $lt{$field}.'</label>'.('&nbsp;' x2);
 3842:             }
 3843:             $datatable .= '</span></fieldset>'.
 3844:                           '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 3845:             foreach my $role (@courseroles) {
 3846:                 my ($selected,$selectnone);
 3847:                 if (!$rolemaps{$role}) {
 3848:                     $selectnone = ' selected="selected"';
 3849:                 }
 3850:                 $datatable .= '<td align="center">'. 
 3851:                               &Apache::lonnet::plaintext($role,'Course').'<br />'.
 3852:                               '<select name="ltitools_roles_'.$role.'_'.$i.'">'.
 3853:                               '<option value=""'.$selectnone.'>'.&mt('Select').'</option>';
 3854:                 foreach my $ltirole (@ltiroles) {
 3855:                     unless ($selectnone) {
 3856:                         if ($rolemaps{$role} eq $ltirole) {
 3857:                             $selected = ' selected="selected"';
 3858:                         } else {
 3859:                             $selected = '';
 3860:                         }
 3861:                     }
 3862:                     $datatable .= '<option value="'.$ltirole.'"'.$selected.'>'.$ltirole.'</option>';
 3863:                 }
 3864:                 $datatable .= '</select></td>';
 3865:             }
 3866:             $datatable .= '</tr></table></fieldset>';
 3867:             my %courseconfig;
 3868:             if (ref($settings->{$item}) eq 'HASH') {
 3869:                 if (ref($settings->{$item}->{'crsconf'}) eq 'HASH') {
 3870:                     %courseconfig = %{$settings->{$item}->{'crsconf'}};
 3871:                 }
 3872:             }
 3873:             $datatable .= '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 3874:             foreach my $item ('label','title','target','linktext','explanation') {
 3875:                 my $checked;
 3876:                 if ($courseconfig{$item}) {
 3877:                     $checked = ' checked="checked"';
 3878:                 }
 3879:                 $datatable .= '<label>'.
 3880:                        '<input type="checkbox" name="ltitools_courseconfig_'.$i.'" value="'.$item.'"'.$checked.' />'.
 3881:                        $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 3882:             }
 3883:             $datatable .= '</span></fieldset>'.
 3884:                           '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 3885:                           '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>';
 3886:             if (ref($settings->{$item}->{'custom'}) eq 'HASH') {
 3887:                 my %custom = %{$settings->{$item}->{'custom'}};
 3888:                 if (keys(%custom) > 0) {
 3889:                     foreach my $key (sort(keys(%custom))) {
 3890:                         $datatable .= '<tr><td><span class="LC_nobreak">'.
 3891:                                       '<label><input type="checkbox" name="ltitools_customdel_'.$i.'" value="'.
 3892:                                       $key.'" />'.&mt('Delete').'</label></span></td><td>'.$key.'</td>'.
 3893:                                       '<td><input type="text" name="ltitools_customval_'.$key.'_'.$i.'"'.
 3894:                                       ' value="'.$custom{$key}.'" /></td></tr>';
 3895:                     }
 3896:                 }
 3897:             }
 3898:             $datatable .= '<tr><td><span class="LC_nobreak">'.
 3899:                           '<label><input type="checkbox" name="ltitools_customadd" value="'.$i.'" />'.
 3900:                           &mt('Add').'</label></span></td><td><input type="text" name="ltitools_custom_name_'.$i.'" />'.
 3901:                           '</td><td><input type="text" name="ltitools_custom_value_'.$i.'" /></td></tr>';
 3902:             $datatable .= '</table></fieldset></td></tr>'."\n";
 3903:             $itemcount ++;
 3904:         }
 3905:     }
 3906:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 3907:     my $chgstr = ' onchange="javascript:reorderLTI(this.form,'."'ltitools_add_pos'".');"';
 3908:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'."\n".
 3909:                   '<input type="hidden" name="ltitools_maxnum" value="'.$maxnum.'" />'."\n".
 3910:                   '<select name="ltitools_add_pos"'.$chgstr.'>';
 3911:     for (my $k=0; $k<$maxnum+1; $k++) {
 3912:         my $vpos = $k+1;
 3913:         my $selstr;
 3914:         if ($k == $maxnum) {
 3915:             $selstr = ' selected="selected" ';
 3916:         }
 3917:         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 3918:     }
 3919:     $datatable .= '</select>&nbsp;'."\n".
 3920:                   '<input type="checkbox" name="ltitools_add" value="1" />'.&mt('Add').'</td>'."\n".
 3921:                   '<td colspan="2">'.
 3922:                   '<fieldset><legend>'.&mt('Required settings').'</legend>'.
 3923:                   '<span class="LC_nobreak">'.$lt{'title'}.':<input type="text" size="30" name="ltitools_add_title" value="" /></span> '."\n".
 3924:                   ('&nbsp;'x2).
 3925:                   '<span class="LC_nobreak">'.$lt{'version'}.':<select name="ltitools_add_version">'.
 3926:                   '<option value="LTI-1p0" selected="selected">1.1</option></select></span> '."\n".
 3927:                   ('&nbsp;'x2).
 3928:                   '<span class="LC_nobreak">'.$lt{'msgtype'}.':<select name="ltitools_add_msgtype">'.
 3929:                   '<option value="basic-lti-launch-request" selected="selected">Launch</option></select></span> '.
 3930:                   '<br />'.
 3931:                   '<span class="LC_nobreak">'.$lt{'url'}.':<input type="text" size="30" name="ltitools_add_url" value="" /></span> '."\n".
 3932:                   ('&nbsp;'x2).
 3933:                   '<span class="LC_nobreak">'.$lt{'key'}.':<input type="text" size="25" name="ltitools_add_key" value="" /></span> '."\n".
 3934:                   ('&nbsp;'x2).
 3935:                   '<span class="LC_nobreak">'.$lt{'secret'}.':<input type="password" size="20" name="ltitools_add_secret" value="" />'.
 3936:                   '<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".
 3937:                   '</fieldset>'.
 3938:                   '<fieldset><legend>'.&mt('Optional settings').'</legend>'.
 3939:                   '<span class="LC_nobreak">'.&mt('Display target:');
 3940:     my %defaultdisp;
 3941:     $defaultdisp{'iframe'} = ' checked="checked"';
 3942:     foreach my $disp ('iframe','tab','window') {
 3943:         $datatable .= '<label><input type="radio" name="ltitools_add_target" value="'.$disp.'"'.$defaultdisp{$disp}.' />'.
 3944:                       $lt{$disp}.'</label>'.('&nbsp;'x2);
 3945:     }
 3946:     $datatable .= ('&nbsp;'x4);
 3947:     foreach my $dimen ('width','height') {
 3948:         $datatable .= '<label>'.$lt{$dimen}.'&nbsp;'.
 3949:                       '<input type="text" name="ltitools_add_'.$dimen.'" size="5" /></label>'.
 3950:                       ('&nbsp;'x2);
 3951:     }
 3952:     $datatable .= '<br />'.
 3953:                   '<div class="LC_left_float">'.$lt{'linktext'}.'<br />'.
 3954:                   '<input type="text" name="ltitools_add_linktext" size="5" /></label></div>'.
 3955:                   '<div class="LC_left_float">'.$lt{'explanation'}.'<br />'.
 3956:                   '<textarea name=ltitools_add_explanation" rows="5" cols="40"></textarea>'.
 3957:                   '</div><div style=""></div><br />';
 3958:     foreach my $extra ('passback','roster') {
 3959:         $datatable .= $lt{$extra}.'&nbsp;'.
 3960:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="1" />'.
 3961:                       &mt('Yes').'</label>'.('&nbsp;'x2).
 3962:                       '<label><input type="radio" name="ltitools_add_'.$extra.'" value="0" checked="checked" />'.
 3963:                       &mt('No').'</label>'.('&nbsp;'x4);
 3964:     }
 3965:     $datatable .= '<br /><br /><span class="LC_nobreak">'.$lt{'icon'}.':&nbsp;'.
 3966:                   '('.&mt('if larger than 21x21 pixels, image will be scaled').')&nbsp;';
 3967:     if ($switchserver) {
 3968:         $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 3969:     } else {
 3970:         $datatable .= '<input type="file" name="ltitools_add_image" value="" />';
 3971:     }
 3972:     $datatable .= '</span></fieldset>'.
 3973:                   '<fieldset><legend>'.&mt('User data sent on launch').'</legend>'.
 3974:                   '<span class="LC_nobreak">';
 3975:     foreach my $field (@fields) {
 3976:         $datatable .= '<label>'.
 3977:                       '<input type="checkbox" name="ltitools_add_fields" value="'.$field.'" />'.
 3978:                       $lt{$field}.'</label>'.('&nbsp;' x2);
 3979:     }
 3980:     $datatable .= '</span></fieldset>'.
 3981:                   '<fieldset><legend>'.&mt('Role mapping').'</legend><table><tr>';
 3982:     foreach my $role (@courseroles) {
 3983:         my ($checked,$checkednone);
 3984:         $datatable .= '<td align="center">'.
 3985:                       &Apache::lonnet::plaintext($role,'Course').'<br />'.
 3986:                       '<select name="ltitools_add_roles_'.$role.'">'.
 3987:                       '<option value="" selected="selected">'.&mt('Select').'</option>';
 3988:         foreach my $ltirole (@ltiroles) {
 3989:             $datatable .= '<option value="'.$ltirole.'">'.$ltirole.'</option>';
 3990:         }
 3991:         $datatable .= '</select></td>';
 3992:     }
 3993:     $datatable .= '</tr></table></fieldset>'.
 3994:                   '<fieldset><legend>'.&mt('Configurable in course').'</legend><span class="LC_nobreak">';
 3995:     foreach my $item ('label','title','target','linktext','explanation') {
 3996:         $datatable .= '<label>'.
 3997:                       '<input type="checkbox" name="ltitools_courseconfig" value="'.$item.'" checked="checked" />'.
 3998:                       $lt{'crs'.$item}.'</label>'.('&nbsp;' x2)."\n";
 3999:     }
 4000:     $datatable .= '</span></fieldset>'.
 4001:                   '<fieldset><legend>'.&mt('Custom items sent on launch').'</legend>'.
 4002:                   '<table><tr><th>'.&mt('Action').'</th><th>'.&mt('Name').'</th><th>'.&mt('Value').'</th></tr>'.
 4003:                   '<tr><td><span class="LC_nobreak">'.
 4004:                   '<label><input type="checkbox" name="ltitools_add_custom" value="1" />'.
 4005:                   &mt('Add').'</label></span></td><td><input type="text" name="ltitools_add_custom_name" />'.
 4006:                   '</td><td><input type="text" name="ltitools_add_custom_value" /></td></tr>'.
 4007:                   '</table></fieldset></td></tr>'."\n".
 4008:                   '</td>'."\n".
 4009:                   '</tr>'."\n";
 4010:     $itemcount ++;
 4011:     return $datatable;
 4012: }
 4013: 
 4014: sub ltitools_names {
 4015:     my %lt = &Apache::lonlocal::texthash(
 4016:                                           'title'          => 'Title',
 4017:                                           'version'        => 'Version',
 4018:                                           'msgtype'        => 'Message Type',
 4019:                                           'url'            => 'URL',
 4020:                                           'key'            => 'Key',
 4021:                                           'secret'         => 'Secret',
 4022:                                           'icon'           => 'Icon',   
 4023:                                           'user'           => 'Username:domain',
 4024:                                           'fullname'       => 'Full Name',
 4025:                                           'firstname'      => 'First Name',
 4026:                                           'lastname'       => 'Last Name',
 4027:                                           'email'          => 'E-mail',
 4028:                                           'roles'          => 'Role',
 4029:                                           'window'         => 'Window',
 4030:                                           'tab'            => 'Tab',
 4031:                                           'iframe'         => 'iFrame',
 4032:                                           'height'         => 'Height',
 4033:                                           'width'          => 'Width',
 4034:                                           'linktext'       => 'Default Link Text',
 4035:                                           'explanation'    => 'Default Explanation',
 4036:                                           'passback'       => 'Tool can return grades:',
 4037:                                           'roster'         => 'Tool can retrieve roster:',
 4038:                                           'crstarget'      => 'Display target',
 4039:                                           'crslabel'       => 'Course label',
 4040:                                           'crstitle'       => 'Course title', 
 4041:                                           'crslinktext'    => 'Link Text',
 4042:                                           'crsexplanation' => 'Explanation',
 4043:                                         );
 4044:     return %lt;
 4045: }
 4046: 
 4047: sub print_coursedefaults {
 4048:     my ($position,$dom,$settings,$rowtotal) = @_;
 4049:     my ($css_class,$datatable,%checkedon,%checkedoff,%defaultchecked,@toggles);
 4050:     my $itemcount = 1;
 4051:     my %choices =  &Apache::lonlocal::texthash (
 4052:         canuse_pdfforms      => 'Course/Community users can create/upload PDF forms',
 4053:         uploadquota          => 'Default quota for files uploaded directly to course/community using Course Editor (MB)',
 4054:         anonsurvey_threshold => 'Responder count needed before showing submissions for anonymous surveys',
 4055:         coursecredits        => 'Credits can be specified for courses',
 4056:         uselcmath            => 'Math preview uses LON-CAPA previewer (javascript) in place of DragMath (Java)',
 4057:         usejsme              => 'Molecule editor uses JSME (HTML5) in place of JME (Java)',
 4058:         postsubmit           => 'Disable submit button/keypress following student submission',
 4059:         canclone             => "People who may clone a course (besides course's owner and coordinators)",
 4060:         mysqltables          => 'Lifetime (s) of "Temporary" MySQL tables (student performance data) on homeserver',
 4061:     );
 4062:     my %staticdefaults = (
 4063:                            anonsurvey_threshold => 10,
 4064:                            uploadquota          => 500,
 4065:                            postsubmit           => 60,
 4066:                            mysqltables          => 172800,
 4067:                          );
 4068:     if ($position eq 'top') {
 4069:         %defaultchecked = (
 4070:                             'canuse_pdfforms' => 'off',
 4071:                             'uselcmath'       => 'on',
 4072:                             'usejsme'         => 'on',
 4073:                             'canclone'        => 'none',
 4074:                           );
 4075:         @toggles = ('canuse_pdfforms','uselcmath','usejsme');
 4076:         ($datatable,$itemcount) = &radiobutton_prefs($settings,\@toggles,\%defaultchecked,
 4077:                                                      \%choices,$itemcount);
 4078:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4079:         $datatable .=
 4080:             '<tr'.$css_class.'><td valign="top">'.
 4081:             '<span class="LC_nobreak">'.$choices{'canclone'}.
 4082:             '</span></td><td class="LC_left_item">';
 4083:         my $currcanclone = 'none';
 4084:         my $onclick;
 4085:         my @cloneoptions = ('none','domain');
 4086:         my %clonetitles = (
 4087:                              none     => 'No additional course requesters',
 4088:                              domain   => "Any course requester in course's domain",
 4089:                              instcode => 'Course requests for official courses ...',
 4090:                           );
 4091:         my (%codedefaults,@code_order,@posscodes);
 4092:         if (&Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
 4093:                                                     \@code_order) eq 'ok') {
 4094:             if (@code_order > 0) {
 4095:                 push(@cloneoptions,'instcode');
 4096:                 $onclick = ' onclick="toggleDisplay(this.form,'."'cloneinstcode'".');"';
 4097:             }
 4098:         }
 4099:         if (ref($settings) eq 'HASH') {
 4100:             if ($settings->{'canclone'}) {
 4101:                 if (ref($settings->{'canclone'}) eq 'HASH') {
 4102:                     if (ref($settings->{'canclone'}{'instcode'}) eq 'ARRAY') {
 4103:                         if (@code_order > 0) {
 4104:                             $currcanclone = 'instcode';
 4105:                             @posscodes = @{$settings->{'canclone'}{'instcode'}};
 4106:                         }
 4107:                     }
 4108:                 } elsif ($settings->{'canclone'} eq 'domain') {
 4109:                     $currcanclone = $settings->{'canclone'};
 4110:                 }
 4111:             }
 4112:         }
 4113:         foreach my $option (@cloneoptions) {
 4114:             my ($checked,$additional);
 4115:             if ($currcanclone eq $option) {
 4116:                 $checked = ' checked="checked"';
 4117:             }
 4118:             if ($option eq 'instcode') {
 4119:                 if (@code_order) {
 4120:                     my $show = 'none';
 4121:                     if ($checked) {
 4122:                         $show = 'block';
 4123:                     }
 4124:                     $additional = '<div id="cloneinstcode" style="display:'.$show.'" />'.
 4125:                                   &mt('Institutional codes for new and cloned course have identical:').
 4126:                                   '<br />';
 4127:                     foreach my $item (@code_order) {
 4128:                         my $codechk;
 4129:                         if ($checked) {
 4130:                             if (grep(/^\Q$item\E$/,@posscodes)) {
 4131:                                 $codechk = ' checked="checked"';
 4132:                             }
 4133:                         }
 4134:                         $additional .= '<label>'.
 4135:                                        '<input type="checkbox" name="clonecode" value="'.$item.'"'.$codechk.' />'.
 4136:                                        $item.'</label>';
 4137:                     }
 4138:                     $additional .= ('&nbsp;'x2).'('.&mt('check as many as needed').')</div>';
 4139:                 }
 4140:             }
 4141:             $datatable .=
 4142:                 '<span class="LC_nobreak"><label><input type="radio" name="canclone"'.$checked.
 4143:                 ' value="'.$option.'"'.$onclick.' />'.$clonetitles{$option}.
 4144:                 '</label>&nbsp;'.$additional.'</span><br />';
 4145:         }
 4146:         $datatable .= '</td>'.
 4147:                       '</tr>';
 4148:         $itemcount ++;
 4149:     } else {
 4150:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4151:         my ($currdefresponder,%defcredits,%curruploadquota,%deftimeout,%currmysql);
 4152:         my $currusecredits = 0;
 4153:         my $postsubmitclient = 1;
 4154:         my @types = ('official','unofficial','community','textbook','placement');
 4155:         if (ref($settings) eq 'HASH') {
 4156:             $currdefresponder = $settings->{'anonsurvey_threshold'};
 4157:             if (ref($settings->{'uploadquota'}) eq 'HASH') {
 4158:                 foreach my $type (keys(%{$settings->{'uploadquota'}})) {
 4159:                     $curruploadquota{$type} = $settings->{'uploadquota'}{$type};
 4160:                 }
 4161:             }
 4162:             if (ref($settings->{'coursecredits'}) eq 'HASH') {
 4163:                 foreach my $type (@types) {
 4164:                     next if ($type eq 'community');
 4165:                     $defcredits{$type} = $settings->{'coursecredits'}->{$type};
 4166:                     if ($defcredits{$type} ne '') {
 4167:                         $currusecredits = 1;
 4168:                     }
 4169:                 }
 4170:             }
 4171:             if (ref($settings->{'postsubmit'}) eq 'HASH') {
 4172:                 if ($settings->{'postsubmit'}->{'client'} eq 'off') {
 4173:                     $postsubmitclient = 0;
 4174:                     foreach my $type (@types) {
 4175:                         $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4176:                     }
 4177:                 } else {
 4178:                     foreach my $type (@types) {
 4179:                         if (ref($settings->{'postsubmit'}->{'timeout'}) eq 'HASH') {
 4180:                             if ($settings->{'postsubmit'}->{'timeout'}->{$type} =~ /^\d+$/) {
 4181:                                 $deftimeout{$type} = $settings->{'postsubmit'}->{'timeout'}->{$type};
 4182:                             } else {
 4183:                                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4184:                             }
 4185:                         } else {
 4186:                             $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4187:                         }
 4188:                     }
 4189:                 }
 4190:             } else {
 4191:                 foreach my $type (@types) {
 4192:                     $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4193:                 }
 4194:             }
 4195:             if (ref($settings->{'mysqltables'}) eq 'HASH') {
 4196:                 foreach my $type (keys(%{$settings->{'mysqltables'}})) {
 4197:                     $currmysql{$type} = $settings->{'mysqltables'}{$type};
 4198:                 }
 4199:             } else {
 4200:                 foreach my $type (@types) {
 4201:                     $currmysql{$type} = $staticdefaults{'mysqltables'};
 4202:                 }
 4203:             }
 4204:         } else {
 4205:             foreach my $type (@types) {
 4206:                 $deftimeout{$type} = $staticdefaults{'postsubmit'};
 4207:             }
 4208:         }
 4209:         if (!$currdefresponder) {
 4210:             $currdefresponder = $staticdefaults{'anonsurvey_threshold'};
 4211:         } elsif ($currdefresponder < 1) {
 4212:             $currdefresponder = 1;
 4213:         }
 4214:         foreach my $type (@types) {
 4215:             if ($curruploadquota{$type} eq '') {
 4216:                 $curruploadquota{$type} = $staticdefaults{'uploadquota'};
 4217:             }
 4218:         }
 4219:         $datatable .=
 4220:                 '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4221:                 $choices{'anonsurvey_threshold'}.
 4222:                 '</span></td>'.
 4223:                 '<td class="LC_right_item"><span class="LC_nobreak">'.
 4224:                 '<input type="text" name="anonsurvey_threshold"'.
 4225:                 ' value="'.$currdefresponder.'" size="5" /></span>'.
 4226:                 '</td></tr>'."\n";
 4227:         $itemcount ++;
 4228:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4229:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4230:                       $choices{'uploadquota'}.
 4231:                       '</span></td>'.
 4232:                       '<td align="right" class="LC_right_item">'.
 4233:                       '<table><tr>';
 4234:         foreach my $type (@types) {
 4235:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 4236:                            '<input type="text" name="uploadquota_'.$type.'"'.
 4237:                            ' value="'.$curruploadquota{$type}.'" size="5" /></td>';
 4238:         }
 4239:         $datatable .= '</tr></table></td></tr>'."\n";
 4240:         $itemcount ++;
 4241:         my $onclick = "toggleDisplay(this.form,'credits');";
 4242:         my $display = 'none';
 4243:         if ($currusecredits) {
 4244:             $display = 'block';
 4245:         }
 4246:         my $additional = '<div id="credits" style="display: '.$display.'">'.
 4247:                          '<i>'.&mt('Default credits').'</i><br /><table><tr>';
 4248:         foreach my $type (@types) {
 4249:             next if ($type eq 'community');
 4250:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 4251:                            '<input type="text" name="'.$type.'_credits"'.
 4252:                            ' value="'.$defcredits{$type}.'" size="3" /></td>';
 4253:         }
 4254:         $additional .= '</tr></table></div>'."\n";
 4255:         %defaultchecked = ('coursecredits' => 'off');
 4256:         @toggles = ('coursecredits');
 4257:         my $current = {
 4258:                         'coursecredits' => $currusecredits,
 4259:                       };
 4260:         (my $table,$itemcount) =
 4261:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4262:                                \%choices,$itemcount,$onclick,$additional,'left');
 4263:         $datatable .= $table;
 4264:         $onclick = "toggleDisplay(this.form,'studentsubmission');";
 4265:         my $display = 'none';
 4266:         if ($postsubmitclient) {
 4267:             $display = 'block';
 4268:         }
 4269:         $additional = '<div id="studentsubmission" style="display: '.$display.'">'.
 4270:                       &mt('Number of seconds submit is disabled').'<br />'.
 4271:                       '<i>'.&mt('Enter 0 to remain disabled until page reload.').'</i><br />'.
 4272:                       '<table><tr>';
 4273:         foreach my $type (@types) {
 4274:             $additional .= '<td align="center">'.&mt($type).'<br />'.
 4275:                            '<input type="text" name="'.$type.'_timeout" value="'.
 4276:                            $deftimeout{$type}.'" size="5" /></td>';
 4277:         }
 4278:         $additional .= '</tr></table></div>'."\n";
 4279:         %defaultchecked = ('postsubmit' => 'on');
 4280:         @toggles = ('postsubmit');
 4281:         $current = {
 4282:                        'postsubmit' => $postsubmitclient,
 4283:                    };
 4284:         ($table,$itemcount) =
 4285:             &radiobutton_prefs($current,\@toggles,\%defaultchecked,
 4286:                                \%choices,$itemcount,$onclick,$additional,'left');
 4287:         $datatable .= $table;
 4288:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4289:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4290:                       $choices{'mysqltables'}.
 4291:                       '</span></td>'.
 4292:                       '<td align="right" class="LC_right_item">'.
 4293:                       '<table><tr>';
 4294:         foreach my $type (@types) {
 4295:             $datatable .= '<td align="center">'.&mt($type).'<br />'.
 4296:                            '<input type="text" name="mysqltables_'.$type.'"'.
 4297:                            ' value="'.$currmysql{$type}.'" size="8" /></td>';
 4298:         }
 4299:         $datatable .= '</tr></table></td></tr>'."\n";
 4300:         $itemcount ++;
 4301: 
 4302:     }
 4303:     $$rowtotal += $itemcount;
 4304:     return $datatable;
 4305: }
 4306: 
 4307: sub print_selfenrollment {
 4308:     my ($position,$dom,$settings,$rowtotal) = @_;
 4309:     my ($css_class,$datatable);
 4310:     my $itemcount = 1;
 4311:     my @types = ('official','unofficial','community','textbook','placement');
 4312:     if (($position eq 'top') || ($position eq 'middle')) {
 4313:         my ($rowsref,$titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
 4314:         my %descs = &Apache::lonuserutils::selfenroll_default_descs();
 4315:         my @rows;
 4316:         my $key;
 4317:         if ($position eq 'top') {
 4318:             $key = 'admin'; 
 4319:             if (ref($rowsref) eq 'ARRAY') {
 4320:                 @rows = @{$rowsref};
 4321:             }
 4322:         } elsif ($position eq 'middle') {
 4323:             $key = 'default';
 4324:             @rows = ('types','registered','approval','limit');
 4325:         }
 4326:         foreach my $row (@rows) {
 4327:             if (defined($titlesref->{$row})) {
 4328:                 $itemcount ++;
 4329:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4330:                 $datatable .= '<tr'.$css_class.'>'.
 4331:                               '<td>'.$titlesref->{$row}.'</td>'.
 4332:                               '<td class="LC_left_item">'.
 4333:                               '<table><tr>';
 4334:                 my (%current,%currentcap);
 4335:                 if (ref($settings) eq 'HASH') {
 4336:                     if (ref($settings->{$key}) eq 'HASH') {
 4337:                         foreach my $type (@types) {
 4338:                             if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4339:                                 $current{$type} = $settings->{$key}->{$type}->{$row};
 4340:                             }
 4341:                             if (($row eq 'limit') && ($key eq 'default')) {
 4342:                                 if (ref($settings->{$key}->{$type}) eq 'HASH') {
 4343:                                     $currentcap{$type} = $settings->{$key}->{$type}->{'cap'};
 4344:                                 }
 4345:                             }
 4346:                         }
 4347:                     }
 4348:                 }
 4349:                 my %roles = (
 4350:                              '0' => &Apache::lonnet::plaintext('dc'),
 4351:                             ); 
 4352:             
 4353:                 foreach my $type (@types) {
 4354:                     unless (($row eq 'registered') && ($key eq 'default')) {
 4355:                         $datatable .= '<th>'.&mt($type).'</th>';
 4356:                     }
 4357:                 }
 4358:                 unless (($row eq 'registered') && ($key eq 'default')) {
 4359:                     $datatable .= '</tr><tr>';
 4360:                 }
 4361:                 foreach my $type (@types) {
 4362:                     if ($type eq 'community') {
 4363:                         $roles{'1'} = &mt('Community personnel');
 4364:                     } else {
 4365:                         $roles{'1'} = &mt('Course personnel');
 4366:                     }
 4367:                     $datatable .= '<td style="vertical-align: top">';
 4368:                     if ($position eq 'top') {
 4369:                         my %checked;
 4370:                         if ($current{$type} eq '0') {
 4371:                             $checked{'0'} = ' checked="checked"';
 4372:                         } else {
 4373:                             $checked{'1'} = ' checked="checked"';
 4374:                         }
 4375:                         foreach my $role ('1','0') {
 4376:                             $datatable .= '<span class="LC_nobreak"><label>'.
 4377:                                           '<input type="radio" name="selfenrolladmin_'.$row.'_'.$type.'" '.
 4378:                                           'value="'.$role.'"'.$checked{$role}.' />'.
 4379:                                           $roles{$role}.'</label></span> ';
 4380:                         }
 4381:                     } else {
 4382:                         if ($row eq 'types') {
 4383:                             my %checked;
 4384:                             if ($current{$type} =~ /^(all|dom)$/) {
 4385:                                 $checked{$1} = ' checked="checked"';
 4386:                             } else {
 4387:                                 $checked{''} = ' checked="checked"';
 4388:                             }
 4389:                             foreach my $val ('','dom','all') {
 4390:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4391:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4392:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4393:                             }
 4394:                         } elsif ($row eq 'registered') {
 4395:                             my %checked;
 4396:                             if ($current{$type} eq '1') {
 4397:                                 $checked{'1'} = ' checked="checked"';
 4398:                             } else {
 4399:                                 $checked{'0'} = ' checked="checked"';
 4400:                             }
 4401:                             foreach my $val ('0','1') {
 4402:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4403:                                               '<input type ="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4404:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4405:                             }
 4406:                         } elsif ($row eq 'approval') {
 4407:                             my %checked;
 4408:                             if ($current{$type} =~ /^([12])$/) {
 4409:                                 $checked{$1} = ' checked="checked"';
 4410:                             } else {
 4411:                                 $checked{'0'} = ' checked="checked"';
 4412:                             }
 4413:                             for my $val (0..2) {
 4414:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4415:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4416:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4417:                             }
 4418:                         } elsif ($row eq 'limit') {
 4419:                             my %checked;
 4420:                             if ($current{$type} =~ /^(allstudents|selfenrolled)$/) {
 4421:                                 $checked{$1} = ' checked="checked"';
 4422:                             } else {
 4423:                                 $checked{'none'} = ' checked="checked"';
 4424:                             }
 4425:                             my $cap;
 4426:                             if ($currentcap{$type} =~ /^\d+$/) {
 4427:                                 $cap = $currentcap{$type};
 4428:                             }
 4429:                             foreach my $val ('none','allstudents','selfenrolled') {
 4430:                                 $datatable .= '<span class="LC_nobreak"><label>'.
 4431:                                               '<input type="radio" name="selfenrolldefault_'.$row.'_'.$type.'" '.
 4432:                                               'value="'.$val.'"'.$checked{$val}.' />'.$descs{$row}{$val}.'</label></span> ';
 4433:                             }
 4434:                             $datatable .= '<br />'.
 4435:                                           '<span class="LC_nobreak">'.&mt('Maximum allowed: ').
 4436:                                           '<input type="text" name="selfenrolldefault_cap_'.$type.'" size = "5" value="'.$cap.'" />'.
 4437:                                           '</span>'; 
 4438:                         }
 4439:                     }
 4440:                     $datatable .= '</td>';
 4441:                 }
 4442:                 $datatable .= '</tr>';
 4443:             }
 4444:             $datatable .= '</table></td></tr>';
 4445:         }
 4446:     } elsif ($position eq 'bottom') {
 4447:         $datatable .= &print_validation_rows('selfenroll',$dom,$settings,\$itemcount);
 4448:     }
 4449:     $$rowtotal += $itemcount;
 4450:     return $datatable;
 4451: }
 4452: 
 4453: sub print_validation_rows {
 4454:     my ($caller,$dom,$settings,$rowtotal) = @_;
 4455:     my ($itemsref,$namesref,$fieldsref);
 4456:     if ($caller eq 'selfenroll') { 
 4457:         ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
 4458:     } elsif ($caller eq 'requestcourses') {
 4459:         ($itemsref,$namesref,$fieldsref) = &Apache::loncoursequeueadmin::requestcourses_validation_types();
 4460:     }
 4461:     my %currvalidation;
 4462:     if (ref($settings) eq 'HASH') {
 4463:         if (ref($settings->{'validation'}) eq 'HASH') {
 4464:             %currvalidation = %{$settings->{'validation'}};
 4465:         }
 4466:     }
 4467:     my $datatable;
 4468:     my $itemcount = 0;
 4469:     foreach my $item (@{$itemsref}) {
 4470:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4471:         $datatable .= '<tr'.$css_class.'><td><span class="LC_nobreak">'.
 4472:                       $namesref->{$item}.
 4473:                       '</span></td>'.
 4474:                       '<td class="LC_left_item">';
 4475:         if (($item eq 'url') || ($item eq 'button')) {
 4476:             $datatable .= '<span class="LC_nobreak">'.
 4477:                           '<input type="text" name="'.$caller.'_validation_'.$item.'"'.
 4478:                           ' value="'.$currvalidation{$item}.'" size="50" /></span>';
 4479:         } elsif ($item eq 'fields') {
 4480:             my @currfields;
 4481:             if (ref($currvalidation{$item}) eq 'ARRAY') {
 4482:                 @currfields = @{$currvalidation{$item}};
 4483:             }
 4484:             foreach my $field (@{$fieldsref}) {
 4485:                 my $check = '';
 4486:                 if (grep(/^\Q$field\E$/,@currfields)) {
 4487:                     $check = ' checked="checked"';
 4488:                 }
 4489:                 $datatable .= '<span class="LC_nobreak"><label>'.
 4490:                               '<input type="checkbox" name="'.$caller.'_validation_fields"'.
 4491:                               ' value="'.$field.'"'.$check.' />'.$field.
 4492:                               '</label></span> ';
 4493:             }
 4494:         } elsif ($item eq 'markup') {
 4495:             $datatable .= '<textarea name="'.$caller.'_validation_markup" cols="50" rows="5" wrap="soft">'.
 4496:                            $currvalidation{$item}.
 4497:                               '</textarea>';
 4498:         }
 4499:         $datatable .= '</td></tr>'."\n";
 4500:         if (ref($rowtotal)) {
 4501:             $itemcount ++;
 4502:         }
 4503:     }
 4504:     if ($caller eq 'requestcourses') {
 4505:         my %currhash;
 4506:         if (ref($settings) eq 'HASH') {
 4507:             if (ref($settings->{'validation'}) eq 'HASH') {
 4508:                 if ($settings->{'validation'}{'dc'} ne '') {
 4509:                     $currhash{$settings->{'validation'}{'dc'}} = 1;
 4510:                 }
 4511:             }
 4512:         }
 4513:         my $numinrow = 2;
 4514:         my ($numdc,$dctable,$rows) = &active_dc_picker($dom,$numinrow,'radio',
 4515:                                                        'validationdc',%currhash);
 4516:         my $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4517:         $datatable .= '</td></tr><tr'.$css_class.'><td>';
 4518:         if ($numdc > 1) {
 4519:             $datatable .= &mt('Course creation processed as: (choose Dom. Coord.)');
 4520:         } else {
 4521:             $datatable .=  &mt('Course creation processed as: ');
 4522:         }
 4523:         $datatable .= '</td><td class="LC_left_item">'.$dctable.'</td></tr>';
 4524:         $itemcount ++;
 4525:     }
 4526:     if (ref($rowtotal)) {
 4527:         $$rowtotal += $itemcount;
 4528:     }
 4529:     return $datatable;
 4530: }
 4531: 
 4532: sub print_usersessions {
 4533:     my ($position,$dom,$settings,$rowtotal) = @_;
 4534:     my ($css_class,$datatable,$itemcount,%checked,%choices);
 4535:     my (%by_ip,%by_location,@intdoms,@instdoms);
 4536:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 4537: 
 4538:     my @alldoms = &Apache::lonnet::all_domains();
 4539:     my %serverhomes = %Apache::lonnet::serverhomeIDs;
 4540:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4541:     my %altids = &id_for_thisdom(%servers);
 4542:     if ($position eq 'top') {
 4543:         if (keys(%serverhomes) > 1) {
 4544:             my %spareid = &current_offloads_to($dom,$settings,\%servers);
 4545:             my $curroffloadnow;
 4546:             if (ref($settings) eq 'HASH') {
 4547:                 if (ref($settings->{'offloadnow'}) eq 'HASH') {
 4548:                     $curroffloadnow = $settings->{'offloadnow'};
 4549:                 }
 4550:             }
 4551:             $datatable .= &spares_row($dom,\%servers,\%spareid,\%serverhomes,\%altids,$curroffloadnow,$rowtotal);
 4552:         } else {
 4553:             $datatable .= '<tr'.$css_class.'><td colspan="2">'.
 4554:                           &mt('Nothing to set here, as the cluster to which this domain belongs only contains one server.').
 4555:                           '</td></tr>';
 4556:         }
 4557:     } else {
 4558:         my %titles = &usersession_titles();
 4559:         my ($prefix,@types);
 4560:         if ($position eq 'bottom') {
 4561:             $prefix = 'remote';
 4562:             @types = ('version','excludedomain','includedomain');
 4563:         } else {
 4564:             $prefix = 'hosted';
 4565:             @types = ('excludedomain','includedomain');
 4566:         }
 4567:         ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 4568:     }
 4569:     $$rowtotal += $itemcount;
 4570:     return $datatable;
 4571: }
 4572: 
 4573: sub rules_by_location {
 4574:     my ($settings,$prefix,$by_location,$by_ip,$types,$titles) = @_; 
 4575:     my ($datatable,$itemcount,$css_class);
 4576:     if (keys(%{$by_location}) == 0) {
 4577:         $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4578:         $datatable = '<tr'.$css_class.'><td colspan="2">'.
 4579:                      &mt('Nothing to set here, as the cluster to which this domain belongs only contains one institution.').
 4580:                      '</td></tr>';
 4581:         $itemcount = 1;
 4582:     } else {
 4583:         $itemcount = 0;
 4584:         my $numinrow = 5;
 4585:         my (%current,%checkedon,%checkedoff);
 4586:         my @locations = sort(keys(%{$by_location}));
 4587:         foreach my $type (@{$types}) {
 4588:             $checkedon{$type} = '';
 4589:             $checkedoff{$type} = ' checked="checked"';
 4590:         }
 4591:         if (ref($settings) eq 'HASH') {
 4592:             if (ref($settings->{$prefix}) eq 'HASH') {
 4593:                 foreach my $key (keys(%{$settings->{$prefix}})) {
 4594:                     $current{$key} = $settings->{$prefix}{$key};
 4595:                     if ($key eq 'version') {
 4596:                         if ($current{$key} ne '') {
 4597:                             $checkedon{$key} = ' checked="checked"';
 4598:                             $checkedoff{$key} = '';
 4599:                         }
 4600:                     } elsif (ref($current{$key}) eq 'ARRAY') {
 4601:                         $checkedon{$key} = ' checked="checked"';
 4602:                         $checkedoff{$key} = '';
 4603:                     }
 4604:                 }
 4605:             }
 4606:         }
 4607:         foreach my $type (@{$types}) {
 4608:             next if ($type ne 'version' && !@locations);
 4609:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 4610:             $datatable .= '<tr'.$css_class.'>
 4611:                            <td><span class="LC_nobreak">'.$titles->{$type}.'</span><br />
 4612:                            <span class="LC_nobreak">&nbsp;
 4613:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedoff{$type}.' value="0" />'.&mt('Not in use').'</label>&nbsp;
 4614:                            <label><input type="radio" name="'.$prefix.'_'.$type.'_inuse" '.$checkedon{$type}.' value="1" />'.&mt('In use').'</label></span></td><td>';
 4615:             if ($type eq 'version') {
 4616:                 my @lcversions = &Apache::lonnet::all_loncaparevs();
 4617:                 my $selector = '<select name="'.$prefix.'_version">';
 4618:                 foreach my $version (@lcversions) {
 4619:                     my $selected = '';
 4620:                     if ($current{'version'} eq $version) {
 4621:                         $selected = ' selected="selected"';
 4622:                     }
 4623:                     $selector .= ' <option value="'.$version.'"'.
 4624:                                  $selected.'>'.$version.'</option>';
 4625:                 }
 4626:                 $selector .= '</select> ';
 4627:                 $datatable .= &mt('remote server must be version: [_1] or later',$selector);
 4628:             } else {
 4629:                 $datatable.= '<div><input type="button" value="'.&mt('check all').'" '.
 4630:                              'onclick="javascript:checkAll(document.display.'.$prefix.'_'.$type.')"'.
 4631:                              ' />'.('&nbsp;'x2).
 4632:                              '<input type="button" value="'.&mt('uncheck all').'" '.
 4633:                              'onclick="javascript:uncheckAll(document.display.'.$prefix.'_'.$type.')" />'.
 4634:                              "\n".
 4635:                              '</div><div><table>';
 4636:                 my $rem;
 4637:                 for (my $i=0; $i<@locations; $i++) {
 4638:                     my ($showloc,$value,$checkedtype);
 4639:                     if (ref($by_location->{$locations[$i]}) eq 'ARRAY') {
 4640:                         my $ip = $by_location->{$locations[$i]}->[0];
 4641:                         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4642:                             $value = join(':',@{$by_ip->{$ip}});
 4643:                             $showloc = join(', ',@{$by_ip->{$ip}});
 4644:                             if (ref($current{$type}) eq 'ARRAY') {
 4645:                                 foreach my $loc (@{$by_ip->{$ip}}) {
 4646:                                     if (grep(/^\Q$loc\E$/,@{$current{$type}})) {
 4647:                                         $checkedtype = ' checked="checked"';
 4648:                                         last;
 4649:                                     }
 4650:                                 }
 4651:                             }
 4652:                         }
 4653:                     }
 4654:                     $rem = $i%($numinrow);
 4655:                     if ($rem == 0) {
 4656:                         if ($i > 0) {
 4657:                             $datatable .= '</tr>';
 4658:                         }
 4659:                         $datatable .= '<tr>';
 4660:                     }
 4661:                     $datatable .= '<td class="LC_left_item">'.
 4662:                                   '<span class="LC_nobreak"><label>'.
 4663:                                   '<input type="checkbox" name="'.$prefix.'_'.$type.
 4664:                                   '" value="'.$value.'"'.$checkedtype.' />'.$showloc.
 4665:                                   '</label></span></td>';
 4666:                 }
 4667:                 $rem = @locations%($numinrow);
 4668:                 my $colsleft = $numinrow - $rem;
 4669:                 if ($colsleft > 1 ) {
 4670:                     $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 4671:                                   '&nbsp;</td>';
 4672:                 } elsif ($colsleft == 1) {
 4673:                     $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 4674:                 }
 4675:                 $datatable .= '</tr></table>';
 4676:             }
 4677:             $datatable .= '</td></tr>';
 4678:             $itemcount ++;
 4679:         }
 4680:     }
 4681:     return ($datatable,$itemcount);
 4682: }
 4683: 
 4684: sub print_ssl {
 4685:     my ($position,$dom,$settings,$rowtotal) = @_;
 4686:     my ($css_class,$datatable);
 4687:     my $itemcount = 1;
 4688:     if ($position eq 'top') {
 4689:         my $primary_id = &Apache::lonnet::domain($dom,'primary');
 4690:         my $intdom = &Apache::lonnet::internet_dom($primary_id);
 4691:         my $same_institution;
 4692:         if ($intdom ne '') {
 4693:             my $internet_names = &Apache::lonnet::get_internet_names($Apache::lonnet::perlvar{'lonHostID'});
 4694:             if (ref($internet_names) eq 'ARRAY') {
 4695:                 if (grep(/^\Q$intdom\E$/,@{$internet_names})) {
 4696:                     $same_institution = 1;
 4697:                 }
 4698:             }
 4699:         }
 4700:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4701:         $datatable = '<tr'.$css_class.'><td colspan="2">';
 4702:         if ($same_institution) {
 4703:             my %domservers = &Apache::lonnet::get_servers($dom);
 4704:             $datatable .= &LONCAPA::SSL::print_certstatus(\%domservers,'web','domprefs');
 4705:         } else {
 4706:             $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.");
 4707:         }
 4708:         $datatable .= '</td></tr>';
 4709:         $itemcount ++;
 4710:     } else {
 4711:         my %titles = &ssl_titles();
 4712:         my (%by_ip,%by_location,@intdoms,@instdoms);
 4713:         &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 4714:         my @alldoms = &Apache::lonnet::all_domains();
 4715:         my %serverhomes = %Apache::lonnet::serverhomeIDs;
 4716:         my @domservers = &Apache::lonnet::get_servers($dom);
 4717:         my %servers = &Apache::lonnet::internet_dom_servers($dom);
 4718:         my %altids = &id_for_thisdom(%servers);
 4719:         if (($position eq 'connto') || ($position eq 'connfrom')) {
 4720:             my $legacy;
 4721:             unless (ref($settings) eq 'HASH') {
 4722:                 my $name;
 4723:                 if ($position eq 'connto') {
 4724:                     $name = 'loncAllowInsecure';
 4725:                 } else {
 4726:                     $name = 'londAllowInsecure';
 4727:                 }
 4728:                 my $primarylibserv = &Apache::lonnet::domain($dom,'primary');
 4729:                 my @ids=&Apache::lonnet::current_machine_ids();
 4730:                 if (($primarylibserv ne '') && (!grep(/^\Q$primarylibserv\E$/,@ids))) {
 4731:                     my %what = (
 4732:                                    $name => 1,
 4733:                                );
 4734:                     my ($result,$returnhash) =
 4735:                         &Apache::lonnet::get_remote_globals($primarylibserv,\%what);
 4736:                     if ($result eq 'ok') {
 4737:                         if (ref($returnhash) eq 'HASH') {
 4738:                             $legacy = $returnhash->{$name};
 4739:                         }
 4740:                     }
 4741:                 } else {
 4742:                     $legacy = $Apache::lonnet::perlvar{$name};
 4743:                 }
 4744:             }
 4745:             foreach my $type ('dom','intdom','other') {
 4746:                 my %checked;
 4747:                 $css_class = $itemcount%2?' class="LC_odd_row"':'';
 4748:                 $datatable .= '<tr'.$css_class.'><td>'.$titles{$type}.'</td>'.
 4749:                               '<td class="LC_right_item">';
 4750:                 my $skip; 
 4751:                 if ($type eq 'dom') {
 4752:                     unless (keys(%servers) > 1) {
 4753:                         $datatable .= &mt('Nothing to set here, as there are no other servers/VMs');    
 4754:                         $skip = 1;
 4755:                     }
 4756:                 }
 4757:                 if ($type eq 'intdom') {
 4758:                     unless (@instdoms > 1) {
 4759:                         $datatable .= &mt('Nothing to set here, as there are no other domains for this institution');
 4760:                         $skip = 1;
 4761:                     } 
 4762:                 } elsif ($type eq 'other') {
 4763:                     if (keys(%by_location) == 0) {
 4764:                         $datatable .= &mt('Nothing to set here, as there are no other institutions');
 4765:                         $skip = 1;
 4766:                     }
 4767:                 }
 4768:                 unless ($skip) {
 4769:                     $checked{'yes'} = ' checked="checked"'; 
 4770:                     if (ref($settings) eq 'HASH') {
 4771:                         if (ref($settings->{$position}) eq 'HASH') {
 4772:                             if ($settings->{$position}->{$type} =~ /^(no|req)$/) {
 4773:                                 $checked{$1} = $checked{'yes'};
 4774:                                 delete($checked{'yes'}); 
 4775:                             }
 4776:                         }
 4777:                     } else {
 4778:                         if ($legacy == 0) {
 4779:                             $checked{'req'} = $checked{'yes'};
 4780:                             delete($checked{'yes'});    
 4781:                         }
 4782:                     }
 4783:                     foreach my $option ('no','yes','req') {
 4784:                         $datatable .= '<span class="LC_nobreak"><label>'.
 4785:                                       '<input type="radio" name="'.$position.'_'.$type.'" '.
 4786:                                       'value="'.$option.'"'.$checked{$option}.' />'.$titles{$option}.
 4787:                                       '</label></span>'.('&nbsp;'x2);
 4788:                     }
 4789:                 }
 4790:                 $datatable .= '</td></tr>';
 4791:                 $itemcount ++; 
 4792:             }
 4793:         } else {
 4794:             my $prefix = 'replication';
 4795:             my @types = ('certreq','nocertreq');
 4796:             if (keys(%by_location) == 0) {
 4797:                 $datatable .= '<tr'.$css_class.'><td>'.
 4798:                               &mt('Nothing to set here, as there are no other institutions').
 4799:                               '</td></tr>';
 4800:                 $itemcount ++;
 4801:             } else {
 4802:                 ($datatable,$itemcount) = 
 4803:                     &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 4804:             }
 4805:         }
 4806:     }
 4807:     $$rowtotal += $itemcount;
 4808:     return $datatable;
 4809: }
 4810: 
 4811: sub ssl_titles {
 4812:     return &Apache::lonlocal::texthash (
 4813:                dom           => 'LON-CAPA servers/VMs from same domain',
 4814:                intdom        => 'LON-CAPA servers/VMs from same "internet" domain',
 4815:                other         => 'External LON-CAPA servers/VMs',
 4816:                connto        => 'Connections to other servers',
 4817:                connfrom      => 'Connections from other servers',
 4818:                replication   => 'Replicating content to other institutions',
 4819:                certreq       => 'Client certificate required, but specific domains exempt',
 4820:                nocertreq     => 'No client certificate required, except for specific domains',
 4821:                no            => 'SSL not used',
 4822:                yes           => 'SSL Optional (used if available)',
 4823:                req           => 'SSL Required',
 4824:     );
 4825: }
 4826: 
 4827: sub print_trust {
 4828:     my ($prefix,$dom,$settings,$rowtotal) = @_;
 4829:     my ($css_class,$datatable,%checked,%choices);
 4830:     my (%by_ip,%by_location,@intdoms,@instdoms);
 4831:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
 4832:     my $itemcount = 1;
 4833:     my %titles = &trust_titles();
 4834:     my @types = ('exc','inc');
 4835:     if ($prefix eq 'top') {
 4836:         $prefix = 'content';
 4837:     } elsif ($prefix eq 'bottom') {
 4838:         $prefix = 'msg';
 4839:     }
 4840:     ($datatable,$itemcount) = &rules_by_location($settings,$prefix,\%by_location,\%by_ip,\@types,\%titles);
 4841:     $$rowtotal += $itemcount;
 4842:     return $datatable;
 4843: }
 4844: 
 4845: sub trust_titles {
 4846:     return &Apache::lonlocal::texthash(
 4847:                content  => "Access to this domain's content by others",
 4848:                shared   => "Access to other domain's content by this domain",
 4849:                enroll   => "Enrollment in this domain's courses by others", 
 4850:                othcoau  => "Co-author roles in this domain for others",
 4851:                coaurem  => "Co-author roles for this domain's users elsewhere", 
 4852:                domroles => "Domain roles in this domain assignable to others",
 4853:                catalog  => "Course Catalog for this domain displayed elsewhere",
 4854:                reqcrs   => "Requests for creation of courses in this domain by others",
 4855:                msg      => "Users in other domains can send messages to this domain",
 4856:                exc      => "Allow all, but exclude specific domains",
 4857:                inc      => "Deny all, but include specific domains",
 4858:            );
 4859: } 
 4860: 
 4861: sub build_location_hashes {
 4862:     my ($intdoms,$by_ip,$by_location,$instdoms) = @_;
 4863:     return unless((ref($intdoms) eq 'ARRAY') && (ref($by_ip) eq 'HASH') &&
 4864:                   (ref($by_location) eq 'HASH') && (ref($instdoms) eq 'ARRAY'));
 4865:     my %iphost = &Apache::lonnet::get_iphost();
 4866:     my $primary_id = &Apache::lonnet::domain($env{'request.role.domain'},'primary');
 4867:     my $primary_ip = &Apache::lonnet::get_host_ip($primary_id);
 4868:     if (ref($iphost{$primary_ip}) eq 'ARRAY') {
 4869:         foreach my $id (@{$iphost{$primary_ip}}) {
 4870:             my $intdom = &Apache::lonnet::internet_dom($id);
 4871:             unless(grep(/^\Q$intdom\E$/,@{$intdoms})) {
 4872:                 push(@{$intdoms},$intdom);
 4873:             }
 4874:         }
 4875:     }
 4876:     foreach my $ip (keys(%iphost)) {
 4877:         if (ref($iphost{$ip}) eq 'ARRAY') {
 4878:             foreach my $id (@{$iphost{$ip}}) {
 4879:                 my $location = &Apache::lonnet::internet_dom($id);
 4880:                 if ($location) {
 4881:                     if (grep(/^\Q$location\E$/,@{$intdoms})) {
 4882:                         my $dom = &Apache::lonnet::host_domain($id);
 4883:                         unless (grep(/^\Q$dom\E/,@{$instdoms})) {
 4884:                             push(@{$instdoms},$dom);
 4885:                         }
 4886:                         next;
 4887:                     }
 4888:                     if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4889:                         unless(grep(/^\Q$location\E$/,@{$by_ip->{$ip}})) {
 4890:                             push(@{$by_ip->{$ip}},$location);
 4891:                         }
 4892:                     } else {
 4893:                         $by_ip->{$ip} = [$location];
 4894:                     }
 4895:                 }
 4896:             }
 4897:         }
 4898:     }
 4899:     foreach my $ip (sort(keys(%{$by_ip}))) {
 4900:         if (ref($by_ip->{$ip}) eq 'ARRAY') {
 4901:             @{$by_ip->{$ip}} = sort(@{$by_ip->{$ip}});
 4902:             my $first = $by_ip->{$ip}->[0];
 4903:             if (ref($by_location->{$first}) eq 'ARRAY') {
 4904:                 unless (grep(/^\Q$ip\E$/,@{$by_location->{$first}})) {
 4905:                     push(@{$by_location->{$first}},$ip);
 4906:                 }
 4907:             } else {
 4908:                 $by_location->{$first} = [$ip];
 4909:             }
 4910:         }
 4911:     }
 4912:     return;
 4913: }
 4914: 
 4915: sub current_offloads_to {
 4916:     my ($dom,$settings,$servers) = @_;
 4917:     my (%spareid,%otherdomconfigs);
 4918:     if (ref($servers) eq 'HASH') {
 4919:         foreach my $lonhost (sort(keys(%{$servers}))) {
 4920:             my $gotspares;
 4921:             if (ref($settings) eq 'HASH') {
 4922:                 if (ref($settings->{'spares'}) eq 'HASH') {
 4923:                     if (ref($settings->{'spares'}{$lonhost}) eq 'HASH') {
 4924:                         $spareid{$lonhost}{'primary'} = $settings->{'spares'}{$lonhost}{'primary'};
 4925:                         $spareid{$lonhost}{'default'} = $settings->{'spares'}{$lonhost}{'default'};
 4926:                         $gotspares = 1;
 4927:                     }
 4928:                 }
 4929:             }
 4930:             unless ($gotspares) {
 4931:                 my $gotspares;
 4932:                 my $serverhomeID =
 4933:                     &Apache::lonnet::get_server_homeID($servers->{$lonhost});
 4934:                 my $serverhomedom =
 4935:                     &Apache::lonnet::host_domain($serverhomeID);
 4936:                 if ($serverhomedom ne $dom) {
 4937:                     if (ref($otherdomconfigs{$serverhomedom} eq 'HASH')) {
 4938:                         if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4939:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4940:                                 $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4941:                                 $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4942:                                 $gotspares = 1;
 4943:                             }
 4944:                         }
 4945:                     } else {
 4946:                         $otherdomconfigs{$serverhomedom} =
 4947:                             &Apache::lonnet::get_dom('configuration',['usersessions'],$serverhomedom);
 4948:                         if (ref($otherdomconfigs{$serverhomedom}) eq 'HASH') {
 4949:                             if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}) eq 'HASH') {
 4950:                                 if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}) eq 'HASH') {
 4951:                                     if (ref($otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{$lonhost}) eq 'HASH') {
 4952:                                         $spareid{$lonhost}{'primary'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'primary'};
 4953:                                         $spareid{$lonhost}{'default'} = $otherdomconfigs{$serverhomedom}{'usersessions'}{'spares'}{'default'};
 4954:                                         $gotspares = 1;
 4955:                                     }
 4956:                                 }
 4957:                             }
 4958:                         }
 4959:                     }
 4960:                 }
 4961:             }
 4962:             unless ($gotspares) {
 4963:                 if ($lonhost eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4964:                     $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4965:                     $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4966:                } else {
 4967:                     my $server_hostname = &Apache::lonnet::hostname($lonhost);
 4968:                     my $server_homeID = &Apache::lonnet::get_server_homeID($server_hostname);
 4969:                     if ($server_homeID eq $Apache::lonnet::perlvar{'lonHostID'}) {
 4970:                         $spareid{$lonhost}{'primary'} = $Apache::lonnet::spareid{'primary'};
 4971:                         $spareid{$lonhost}{'default'} = $Apache::lonnet::spareid{'default'};
 4972:                     } else {
 4973:                         my %what = (
 4974:                              spareid => 1,
 4975:                         );
 4976:                         my ($result,$returnhash) = 
 4977:                             &Apache::lonnet::get_remote_globals($lonhost,\%what);
 4978:                         if ($result eq 'ok') { 
 4979:                             if (ref($returnhash) eq 'HASH') {
 4980:                                 if (ref($returnhash->{'spareid'}) eq 'HASH') {
 4981:                                     $spareid{$lonhost}{'primary'} = $returnhash->{'spareid'}->{'primary'};
 4982:                                     $spareid{$lonhost}{'default'} = $returnhash->{'spareid'}->{'default'};
 4983:                                 }
 4984:                             }
 4985:                         }
 4986:                     }
 4987:                 }
 4988:             }
 4989:         }
 4990:     }
 4991:     return %spareid;
 4992: }
 4993: 
 4994: sub spares_row {
 4995:     my ($dom,$servers,$spareid,$serverhomes,$altids,$curroffloadnow,$rowtotal) = @_;
 4996:     my $css_class;
 4997:     my $numinrow = 4;
 4998:     my $itemcount = 1;
 4999:     my $datatable;
 5000:     my %typetitles = &sparestype_titles();
 5001:     if ((ref($servers) eq 'HASH') && (ref($spareid) eq 'HASH') && (ref($altids) eq 'HASH')) {
 5002:         foreach my $server (sort(keys(%{$servers}))) {
 5003:             my $serverhome = &Apache::lonnet::get_server_homeID($servers->{$server});
 5004:             my ($othercontrol,$serverdom);
 5005:             if ($serverhome ne $server) {
 5006:                 $serverdom = &Apache::lonnet::host_domain($serverhome);
 5007:                 $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 5008:             } else {
 5009:                 $serverdom = &Apache::lonnet::host_domain($server);
 5010:                 if ($serverdom ne $dom) {
 5011:                     $othercontrol = &mt('Session offloading controlled by domain: [_1]','<b>'.$serverdom.'</b>');
 5012:                 }
 5013:             }
 5014:             next unless (ref($spareid->{$server}) eq 'HASH');
 5015:             my $checkednow;
 5016:             if (ref($curroffloadnow) eq 'HASH') {
 5017:                 if ($curroffloadnow->{$server}) {
 5018:                     $checkednow = ' checked="checked"';
 5019:                 }
 5020:             }
 5021:             $css_class = $itemcount%2 ? ' class="LC_odd_row"' : '';
 5022:             $datatable .= '<tr'.$css_class.'>
 5023:                            <td rowspan="2">
 5024:                             <span class="LC_nobreak">'.
 5025:                           &mt('[_1] when busy, offloads to:'
 5026:                               ,'<b>'.$server.'</b>').'</span><br />'.
 5027:                           '<span class="LC_nobreak">'."\n".
 5028:                           '<label><input type="checkbox" name="offloadnow" value="'.$server.'"'.$checkednow.' />'.
 5029:                           '&nbsp;'.&mt('Switch active users on next access').'</label></span>'.
 5030:                           "\n";
 5031:             my (%current,%canselect);
 5032:             my @choices = 
 5033:                 &possible_newspares($server,$spareid->{$server},$serverhomes,$altids);
 5034:             foreach my $type ('primary','default') {
 5035:                 if (ref($spareid->{$server}) eq 'HASH') {
 5036:                     if (ref($spareid->{$server}{$type}) eq 'ARRAY') {
 5037:                         my @spares = @{$spareid->{$server}{$type}};
 5038:                         if (@spares > 0) {
 5039:                             if ($othercontrol) {
 5040:                                 $current{$type} = join(', ',@spares);
 5041:                             } else {
 5042:                                 $current{$type} .= '<table>';
 5043:                                 my $numspares = scalar(@spares);
 5044:                                 for (my $i=0;  $i<@spares; $i++) {
 5045:                                     my $rem = $i%($numinrow);
 5046:                                     if ($rem == 0) {
 5047:                                         if ($i > 0) {
 5048:                                             $current{$type} .= '</tr>';
 5049:                                         }
 5050:                                         $current{$type} .= '<tr>';
 5051:                                     }
 5052:                                     $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;'.
 5053:                                                        $spareid->{$server}{$type}[$i].
 5054:                                                        '</label></td>'."\n";
 5055:                                 }
 5056:                                 my $rem = @spares%($numinrow);
 5057:                                 my $colsleft = $numinrow - $rem;
 5058:                                 if ($colsleft > 1 ) {
 5059:                                     $current{$type} .= '<td colspan="'.$colsleft.
 5060:                                                        '" class="LC_left_item">'.
 5061:                                                        '&nbsp;</td>';
 5062:                                 } elsif ($colsleft == 1) {
 5063:                                     $current{$type} .= '<td class="LC_left_item">&nbsp;</td>'."\n";
 5064:                                 }
 5065:                                 $current{$type} .= '</tr></table>';
 5066:                             }
 5067:                         }
 5068:                     }
 5069:                     if ($current{$type} eq '') {
 5070:                         $current{$type} = &mt('None specified');
 5071:                     }
 5072:                     if ($othercontrol) {
 5073:                         if ($type eq 'primary') {
 5074:                             $canselect{$type} = $othercontrol;
 5075:                         }
 5076:                     } else {
 5077:                         $canselect{$type} = 
 5078:                             &mt('Add new [_1]'.$type.'[_2]:','<i>','</i>').'&nbsp;'.
 5079:                             '<select name="newspare_'.$type.'_'.$server.'" '.
 5080:                             'id="newspare_'.$type.'_'.$server.'" onchange="checkNewSpares('."'$server','$type'".');">'."\n".
 5081:                             '<option value="" selected ="selected">'.&mt('Select').'</option>'."\n";
 5082:                         if (@choices > 0) {
 5083:                             foreach my $lonhost (@choices) {
 5084:                                 $canselect{$type} .= '<option value="'.$lonhost.'">'.$lonhost.'</option>'."\n";
 5085:                             }
 5086:                         }
 5087:                         $canselect{$type} .= '</select>'."\n";
 5088:                     }
 5089:                 } else {
 5090:                     $current{$type} = &mt('Could not be determined');
 5091:                     if ($type eq 'primary') {
 5092:                         $canselect{$type} =  $othercontrol;
 5093:                     }
 5094:                 }
 5095:                 if ($type eq 'default') {
 5096:                     $datatable .= '<tr'.$css_class.'>';
 5097:                 }
 5098:                 $datatable .= '<td><i>'.$typetitles{$type}.'</i></td>'."\n".
 5099:                               '<td>'.$current{$type}.'</td>'."\n".
 5100:                               '<td>'.$canselect{$type}.'</td></tr>'."\n";
 5101:             }
 5102:             $itemcount ++;
 5103:         }
 5104:     }
 5105:     $$rowtotal += $itemcount;
 5106:     return $datatable;
 5107: }
 5108: 
 5109: sub possible_newspares {
 5110:     my ($server,$currspares,$serverhomes,$altids) = @_;
 5111:     my $serverhostname = &Apache::lonnet::hostname($server);
 5112:     my %excluded;
 5113:     if ($serverhostname ne '') {
 5114:         %excluded = (
 5115:                        $serverhostname => 1,
 5116:                     );
 5117:     }
 5118:     if (ref($currspares) eq 'HASH') {
 5119:         foreach my $type (keys(%{$currspares})) {
 5120:             if (ref($currspares->{$type}) eq 'ARRAY') {
 5121:                 if (@{$currspares->{$type}} > 0) {
 5122:                     foreach my $curr (@{$currspares->{$type}}) {
 5123:                         my $hostname = &Apache::lonnet::hostname($curr);
 5124:                         $excluded{$hostname} = 1;
 5125:                     }
 5126:                 }
 5127:             }
 5128:         }
 5129:     }
 5130:     my @choices;
 5131:     if ((ref($serverhomes) eq 'HASH') && (ref($altids) eq 'HASH')) {
 5132:         if (keys(%{$serverhomes}) > 1) {
 5133:             foreach my $name (sort(keys(%{$serverhomes}))) {
 5134:                 unless ($excluded{$name}) {
 5135:                     if (exists($altids->{$serverhomes->{$name}})) {
 5136:                         push(@choices,$altids->{$serverhomes->{$name}});
 5137:                     } else {
 5138:                         push(@choices,$serverhomes->{$name});
 5139:                     }
 5140:                 }
 5141:             }
 5142:         }
 5143:     }
 5144:     return sort(@choices);
 5145: }
 5146: 
 5147: sub print_loadbalancing {
 5148:     my ($dom,$settings,$rowtotal) = @_;
 5149:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
 5150:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
 5151:     my $numinrow = 1;
 5152:     my $datatable;
 5153:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 5154:     my (%currbalancer,%currtargets,%currrules,%existing);
 5155:     if (ref($settings) eq 'HASH') {
 5156:         %existing = %{$settings};
 5157:     }
 5158:     if ((keys(%servers) > 1) || (keys(%existing) > 0)) {
 5159:         &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
 5160:                                   \%currtargets,\%currrules);
 5161:     } else {
 5162:         return;
 5163:     }
 5164:     my ($othertitle,$usertypes,$types) =
 5165:         &Apache::loncommon::sorted_inst_types($dom);
 5166:     my $rownum = 8;
 5167:     if (ref($types) eq 'ARRAY') {
 5168:         $rownum += scalar(@{$types});
 5169:     }
 5170:     my @css_class = ('LC_odd_row','LC_even_row');
 5171:     my $balnum = 0;
 5172:     my $islast;
 5173:     my (@toshow,$disabledtext);
 5174:     if (keys(%currbalancer) > 0) {
 5175:         @toshow = sort(keys(%currbalancer));
 5176:         if (scalar(@toshow) < scalar(keys(%servers)) + 1) {
 5177:             push(@toshow,'');
 5178:         }
 5179:     } else {
 5180:         @toshow = ('');
 5181:         $disabledtext = &mt('No existing load balancer');
 5182:     }
 5183:     foreach my $lonhost (@toshow) {
 5184:         if ($balnum == scalar(@toshow)-1) {
 5185:             $islast = 1;
 5186:         } else {
 5187:             $islast = 0;
 5188:         }
 5189:         my $cssidx = $balnum%2;
 5190:         my $targets_div_style = 'display: none';
 5191:         my $disabled_div_style = 'display: block';
 5192:         my $homedom_div_style = 'display: none';
 5193:         $datatable .= '<tr class="'.$css_class[$cssidx].'">'.
 5194:                       '<td rowspan="'.$rownum.'" valign="top">'.
 5195:                       '<p>';
 5196:         if ($lonhost eq '') {
 5197:             $datatable .= '<span class="LC_nobreak">';
 5198:             if (keys(%currbalancer) > 0) {
 5199:                 $datatable .= &mt('Add balancer:');
 5200:             } else {
 5201:                 $datatable .= &mt('Enable balancer:');
 5202:             }
 5203:             $datatable .= '&nbsp;'.
 5204:                           '<select name="loadbalancing_lonhost_'.$balnum.'"'.
 5205:                           ' id="loadbalancing_lonhost_'.$balnum.'"'.
 5206:                           ' onchange="toggleTargets('."'$balnum'".');">'."\n".
 5207:                           '<option value="" selected="selected">'.&mt('None').
 5208:                           '</option>'."\n";
 5209:             foreach my $server (sort(keys(%servers))) {
 5210:                 next if ($currbalancer{$server});
 5211:                 $datatable .= '<option value="'.$server.'">'.$server.'</option>'."\n";
 5212:             }
 5213:             $datatable .=
 5214:                 '</select>'."\n".
 5215:                 '<input type="hidden" name="loadbalancing_prevlonhost_'.$balnum.'" id="loadbalancing_prevlonhost_'.$balnum.'" value="" />&nbsp;</span>'."\n";
 5216:         } else {
 5217:             $datatable .= '<i>'.$lonhost.'</i><br /><span class="LC_nobreak">'.
 5218:                           '<label><input type="checkbox" name="loadbalancing_delete" value="'.$balnum.'" id="loadbalancing_delete_'.$balnum.'" onclick="javascript:balancerDeleteChange('."'$balnum'".');" />&nbsp;'.
 5219:                            &mt('Stop balancing').'</label>'.
 5220:                            '<input type="hidden" name="loadbalancing_lonhost_'.$balnum.'" value="'.$lonhost.'" id="loadbalancing_lonhost_'.$balnum.'" /></span>';
 5221:             $targets_div_style = 'display: block';
 5222:             $disabled_div_style = 'display: none';
 5223:             if ($dom eq &Apache::lonnet::host_domain($lonhost)) {
 5224:                 $homedom_div_style = 'display: block';
 5225:             }
 5226:         }
 5227:         $datatable .= '</p></td><td rowspan="'.$rownum.'" valign="top">'.
 5228:                   '<div id="loadbalancing_disabled_'.$balnum.'" style="'.
 5229:                   $disabled_div_style.'">'.$disabledtext.'</div>'."\n".
 5230:                   '<div id="loadbalancing_targets_'.$balnum.'" style="'.$targets_div_style.'">'.&mt('Offloads to:').'<br />';
 5231:         my ($numspares,@spares) = &count_servers($lonhost,%servers);
 5232:         my @sparestypes = ('primary','default');
 5233:         my %typetitles = &sparestype_titles();
 5234:         my %hostherechecked = (
 5235:                                   no => ' checked="checked"',
 5236:                               );
 5237:         foreach my $sparetype (@sparestypes) {
 5238:             my $targettable;
 5239:             for (my $i=0; $i<$numspares; $i++) {
 5240:                 my $checked;
 5241:                 if (ref($currtargets{$lonhost}) eq 'HASH') {
 5242:                     if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5243:                         if (grep(/^\Q$spares[$i]\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5244:                             $checked = ' checked="checked"';
 5245:                         }
 5246:                     }
 5247:                 }
 5248:                 my ($chkboxval,$disabled);
 5249:                 if (($lonhost ne '') && (exists($servers{$lonhost}))) {
 5250:                     $chkboxval = $spares[$i];
 5251:                 }
 5252:                 if (exists($currbalancer{$spares[$i]})) {
 5253:                     $disabled = ' disabled="disabled"';
 5254:                 }
 5255:                 $targettable .=
 5256:                     '<td><span class="LC_nobreak"><label>'.
 5257:                     '<input type="checkbox" name="loadbalancing_target_'.$balnum.'_'.$sparetype.'"'.
 5258:                     $checked.$disabled.' value="'.$chkboxval.'" id="loadbalancing_target_'.$balnum.'_'.$sparetype.'_'.$i.'" onclick="checkOffloads('."this,'$balnum','$sparetype'".');" /><span id="loadbalancing_targettxt_'.$balnum.'_'.$sparetype.'_'.$i.'">&nbsp;'.$chkboxval.
 5259:                     '</span></label></span></td>';
 5260:                 my $rem = $i%($numinrow);
 5261:                 if ($rem == 0) {
 5262:                     if (($i > 0) && ($i < $numspares-1)) {
 5263:                         $targettable .= '</tr>';
 5264:                     }
 5265:                     if ($i < $numspares-1) {
 5266:                         $targettable .= '<tr>';
 5267:                     }
 5268:                 }
 5269:             }
 5270:             if ($targettable ne '') {
 5271:                 my $rem = $numspares%($numinrow);
 5272:                 my $colsleft = $numinrow - $rem;
 5273:                 if ($colsleft > 1 ) {
 5274:                     $targettable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5275:                                     '&nbsp;</td>';
 5276:                 } elsif ($colsleft == 1) {
 5277:                     $targettable .= '<td class="LC_left_item">&nbsp;</td>';
 5278:                 }
 5279:                 $datatable .=  '<i>'.$typetitles{$sparetype}.'</i><br />'.
 5280:                                '<table><tr>'.$targettable.'</tr></table><br />';
 5281:             }
 5282:             $hostherechecked{$sparetype} = '';
 5283:             if (ref($currtargets{$lonhost}) eq 'HASH') {
 5284:                 if (ref($currtargets{$lonhost}{$sparetype}) eq 'ARRAY') {
 5285:                     if (grep(/^\Q$lonhost\E$/,@{$currtargets{$lonhost}{$sparetype}})) {
 5286:                         $hostherechecked{$sparetype} = ' checked="checked"';
 5287:                         $hostherechecked{'no'} = '';
 5288:                     }
 5289:                 }
 5290:             }
 5291:         }
 5292:         $datatable .= &mt('Hosting on balancer itself').'<br />'.
 5293:                       '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" value="no"'.
 5294:                       $hostherechecked{'no'}.' />'.&mt('No').'</label><br />';
 5295:         foreach my $sparetype (@sparestypes) {
 5296:             $datatable .= '<label><input type="radio" name="loadbalancing_target_'.$balnum.'_hosthere" '.
 5297:                           'value="'.$sparetype.'"'.$hostherechecked{$sparetype}.' /><i>'.$typetitles{$sparetype}.
 5298:                           '</i></label><br />';
 5299:         }
 5300:         $datatable .= '</div></td></tr>'.
 5301:                       &loadbalancing_rules($dom,$intdom,$currrules{$lonhost},
 5302:                                            $othertitle,$usertypes,$types,\%servers,
 5303:                                            \%currbalancer,$lonhost,
 5304:                                            $targets_div_style,$homedom_div_style,
 5305:                                            $css_class[$cssidx],$balnum,$islast);
 5306:         $$rowtotal += $rownum;
 5307:         $balnum ++;
 5308:     }
 5309:     $datatable .= '<input type="hidden" name="loadbalancing_total" id="loadbalancing_total" value="'.$balnum.'" />';
 5310:     return $datatable;
 5311: }
 5312: 
 5313: sub get_loadbalancers_config {
 5314:     my ($servers,$existing,$currbalancer,$currtargets,$currrules) = @_;
 5315:     return unless ((ref($servers) eq 'HASH') &&
 5316:                    (ref($existing) eq 'HASH') && (ref($currbalancer) eq 'HASH') &&
 5317:                    (ref($currtargets) eq 'HASH') && (ref($currrules) eq 'HASH'));
 5318:     if (keys(%{$existing}) > 0) {
 5319:         my $oldlonhost;
 5320:         foreach my $key (sort(keys(%{$existing}))) {
 5321:             if ($key eq 'lonhost') {
 5322:                 $oldlonhost = $existing->{'lonhost'};
 5323:                 $currbalancer->{$oldlonhost} = 1;
 5324:             } elsif ($key eq 'targets') {
 5325:                 if ($oldlonhost) {
 5326:                     $currtargets->{$oldlonhost} = $existing->{'targets'};
 5327:                 }
 5328:             } elsif ($key eq 'rules') {
 5329:                 if ($oldlonhost) {
 5330:                     $currrules->{$oldlonhost} = $existing->{'rules'};
 5331:                 }
 5332:             } elsif (ref($existing->{$key}) eq 'HASH') {
 5333:                 $currbalancer->{$key} = 1;
 5334:                 $currtargets->{$key} = $existing->{$key}{'targets'};
 5335:                 $currrules->{$key} = $existing->{$key}{'rules'};
 5336:             }
 5337:         }
 5338:     } else {
 5339:         my ($balancerref,$targetsref) =
 5340:                 &Apache::lonnet::get_lonbalancer_config($servers);
 5341:         if ((ref($balancerref) eq 'HASH') && (ref($targetsref) eq 'HASH')) {
 5342:             foreach my $server (sort(keys(%{$balancerref}))) {
 5343:                 $currbalancer->{$server} = 1;
 5344:                 $currtargets->{$server} = $targetsref->{$server};
 5345:             }
 5346:         }
 5347:     }
 5348:     return;
 5349: }
 5350: 
 5351: sub loadbalancing_rules {
 5352:     my ($dom,$intdom,$currrules,$othertitle,$usertypes,$types,$servers,
 5353:         $currbalancer,$lonhost,$targets_div_style,$homedom_div_style,
 5354:         $css_class,$balnum,$islast) = @_;
 5355:     my $output;
 5356:     my $num = 0;
 5357:     my ($alltypes,$othertypes,$titles) =
 5358:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
 5359:     if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH'))  {
 5360:         foreach my $type (@{$alltypes}) {
 5361:             $num ++;
 5362:             my $current;
 5363:             if (ref($currrules) eq 'HASH') {
 5364:                 $current = $currrules->{$type};
 5365:             }
 5366:             if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5367:                 if ($dom ne &Apache::lonnet::host_domain($lonhost)) {
 5368:                     $current = '';
 5369:                 }
 5370:             }
 5371:             $output .= &loadbalance_rule_row($type,$titles->{$type},$current,
 5372:                                              $servers,$currbalancer,$lonhost,$dom,
 5373:                                              $targets_div_style,$homedom_div_style,
 5374:                                              $css_class,$balnum,$num,$islast);
 5375:         }
 5376:     }
 5377:     return $output;
 5378: }
 5379: 
 5380: sub loadbalancing_titles {
 5381:     my ($dom,$intdom,$usertypes,$types) = @_;
 5382:     my %othertypes = (
 5383:            '_LC_adv'         => &mt('Advanced users from [_1]',$dom),
 5384:            '_LC_author'      => &mt('Users from [_1] with author role',$dom),
 5385:            '_LC_internetdom' => &mt('Users not from [_1], but from [_2]',$dom,$intdom),
 5386:            '_LC_external'    => &mt('Users not from [_1]',$intdom),
 5387:            '_LC_ipchangesso' => &mt('SSO users from [_1], with IP mismatch',$dom),
 5388:            '_LC_ipchange'    => &mt('Non-SSO users with IP mismatch'),
 5389:                      );
 5390:     my @alltypes = ('_LC_adv','_LC_author','_LC_internetdom','_LC_external','_LC_ipchangesso','_LC_ipchange');
 5391:     if (ref($types) eq 'ARRAY') {
 5392:         unshift(@alltypes,@{$types},'default');
 5393:     }
 5394:     my %titles;
 5395:     foreach my $type (@alltypes) {
 5396:         if ($type =~ /^_LC_/) {
 5397:             $titles{$type} = $othertypes{$type};
 5398:         } elsif ($type eq 'default') {
 5399:             $titles{$type} = &mt('All users from [_1]',$dom);
 5400:             if (ref($types) eq 'ARRAY') {
 5401:                 if (@{$types} > 0) {
 5402:                     $titles{$type} = &mt('Other users from [_1]',$dom);
 5403:                 }
 5404:             }
 5405:         } elsif (ref($usertypes) eq 'HASH') {
 5406:             $titles{$type} = $usertypes->{$type};
 5407:         }
 5408:     }
 5409:     return (\@alltypes,\%othertypes,\%titles);
 5410: }
 5411: 
 5412: sub loadbalance_rule_row {
 5413:     my ($type,$title,$current,$servers,$currbalancer,$lonhost,$dom,
 5414:         $targets_div_style,$homedom_div_style,$css_class,$balnum,$num,$islast) = @_;
 5415:     my @rulenames;
 5416:     my %ruletitles = &offloadtype_text();
 5417:     if (($type eq '_LC_ipchangesso') || ($type eq '_LC_ipchange')) {
 5418:         @rulenames = ('balancer','offloadedto','specific');
 5419:     } else {
 5420:         @rulenames = ('default','homeserver');
 5421:         if ($type eq '_LC_external') {
 5422:             push(@rulenames,'externalbalancer');
 5423:         } else {
 5424:             push(@rulenames,'specific');
 5425:         }
 5426:         push(@rulenames,'none');
 5427:     }
 5428:     my $style = $targets_div_style;
 5429:     if (($type eq '_LC_external') || ($type eq '_LC_internetdom')) {
 5430:         $style = $homedom_div_style;
 5431:     }
 5432:     my $space;
 5433:     if ($islast && $num == 1) {
 5434:         $space = '<div display="inline-block">&nbsp;</div>';
 5435:     }
 5436:     my $output =
 5437:         '<tr class="'.$css_class.'" id="balanceruletr_'.$balnum.'_'.$num.'"><td valign="top">'.$space.
 5438:         '<div id="balanceruletitle_'.$balnum.'_'.$type.'" style="'.$style.'">'.$title.'</div></td>'."\n".
 5439:         '<td valaign="top">'.$space.
 5440:         '<div id="balancerule_'.$balnum.'_'.$type.'" style="'.$style.'">'."\n";
 5441:     for (my $i=0; $i<@rulenames; $i++) {
 5442:         my $rule = $rulenames[$i];
 5443:         my ($checked,$extra);
 5444:         if ($rulenames[$i] eq 'default') {
 5445:             $rule = '';
 5446:         }
 5447:         if ($rulenames[$i] eq 'specific') {
 5448:             if (ref($servers) eq 'HASH') {
 5449:                 my $default;
 5450:                 if (($current ne '') && (exists($servers->{$current}))) {
 5451:                     $checked = ' checked="checked"';
 5452:                 }
 5453:                 unless ($checked) {
 5454:                     $default = ' selected="selected"';
 5455:                 }
 5456:                 $extra =
 5457:                     ':&nbsp;<select name="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5458:                     '" id="loadbalancing_singleserver_'.$balnum.'_'.$type.
 5459:                     '" onchange="singleServerToggle('."'$balnum','$type'".')">'."\n".
 5460:                     '<option value=""'.$default.'></option>'."\n";
 5461:                 foreach my $server (sort(keys(%{$servers}))) {
 5462:                     if (ref($currbalancer) eq 'HASH') {
 5463:                         next if (exists($currbalancer->{$server}));
 5464:                     }
 5465:                     my $selected;
 5466:                     if ($server eq $current) {
 5467:                         $selected = ' selected="selected"';
 5468:                     }
 5469:                     $extra .= '<option value="'.$server.'"'.$selected.'>'.$server.'</option>';
 5470:                 }
 5471:                 $extra .= '</select>';
 5472:             }
 5473:         } elsif ($rule eq $current) {
 5474:             $checked = ' checked="checked"';
 5475:         }
 5476:         $output .= '<span class="LC_nobreak"><label>'.
 5477:                    '<input type="radio" name="loadbalancing_rules_'.$balnum.'_'.$type.
 5478:                    '" id="loadbalancing_rules_'.$balnum.'_'.$type.'_'.$i.'" value="'.
 5479:                    $rule.'" onclick="balanceruleChange('."this.form,'$balnum','$type'".
 5480:                    ')"'.$checked.' />&nbsp;';
 5481:         if (($rulenames[$i] eq 'specific') && ($type =~ /^_LC_ipchange/)) {
 5482:             $output .= $ruletitles{'particular'};
 5483:         } else {
 5484:             $output .= $ruletitles{$rulenames[$i]};
 5485:         }
 5486:         $output .= '</label>'.$extra.'</span><br />'."\n";
 5487:     }
 5488:     $output .= '</div></td></tr>'."\n";
 5489:     return $output;
 5490: }
 5491: 
 5492: sub offloadtype_text {
 5493:     my %ruletitles = &Apache::lonlocal::texthash (
 5494:            'default'          => 'Offloads to default destinations',
 5495:            'homeserver'       => "Offloads to user's home server",
 5496:            'externalbalancer' => "Offloads to Load Balancer in user's domain",
 5497:            'specific'         => 'Offloads to specific server',
 5498:            'none'             => 'No offload',
 5499:            'balancer'         => 'Session hosted on Load Balancer, after re-authentication',
 5500:            'offloadedto'      => 'Session hosted on offload server, after re-authentication',
 5501:            'particular'       => 'Session hosted (after re-auth) on server:',
 5502:     );
 5503:     return %ruletitles;
 5504: }
 5505: 
 5506: sub sparestype_titles {
 5507:     my %typestitles = &Apache::lonlocal::texthash (
 5508:                           'primary' => 'primary',
 5509:                           'default' => 'default',
 5510:                       );
 5511:     return %typestitles;
 5512: }
 5513: 
 5514: sub contact_titles {
 5515:     my %titles = &Apache::lonlocal::texthash (
 5516:                    'supportemail'    => 'Support E-mail address',
 5517:                    'adminemail'      => 'Default Server Admin E-mail address',
 5518:                    'errormail'       => 'Error reports to be e-mailed to',
 5519:                    'packagesmail'    => 'Package update alerts to be e-mailed to',
 5520:                    'helpdeskmail'    => "Helpdesk requests for this domain's users",
 5521:                    'otherdomsmail'   => 'Helpdesk requests for other (unconfigured) domains',
 5522:                    'lonstatusmail'   => 'E-mail from nightly status check (warnings/errors)',
 5523:                    'requestsmail'    => 'E-mail from course requests requiring approval',
 5524:                    'updatesmail'     => 'E-mail from nightly check of LON-CAPA module integrity/updates',
 5525:                    'idconflictsmail' => 'E-mail from bi-nightly check for multiple users sharing same student/employee ID',
 5526:                  );
 5527:     my %short_titles = &Apache::lonlocal::texthash (
 5528:                            adminemail   => 'Admin E-mail address',
 5529:                            supportemail => 'Support E-mail',
 5530:                        );   
 5531:     return (\%titles,\%short_titles);
 5532: }
 5533: 
 5534: sub helpform_fields {
 5535:     my %titles =  &Apache::lonlocal::texthash (
 5536:                        'username'   => 'Name',
 5537:                        'user'       => 'Username/domain',
 5538:                        'phone'      => 'Phone',
 5539:                        'cc'         => 'Cc e-mail',
 5540:                        'course'     => 'Course Details',
 5541:                        'section'    => 'Sections',
 5542:                        'screenshot' => 'File upload',
 5543:     );
 5544:     my @fields = ('username','phone','user','course','section','cc','screenshot');
 5545:     my %possoptions = (
 5546:                         username     => ['yes','no','req'],
 5547:                         phone        => ['yes','no','req'],
 5548:                         user         => ['yes','no'],
 5549:                         cc           => ['yes','no'],
 5550:                         course       => ['yes','no'],
 5551:                         section      => ['yes','no'],
 5552:                         screenshot   => ['yes','no'],
 5553:                       );
 5554:     my %fieldoptions = &Apache::lonlocal::texthash (
 5555:                          'yes'  => 'Optional',
 5556:                          'req'  => 'Required',
 5557:                          'no'   => "Not shown",
 5558:     );
 5559:     return (\@fields,\%titles,\%fieldoptions,\%possoptions);
 5560: }
 5561: 
 5562: sub tool_titles {
 5563:     my %titles = &Apache::lonlocal::texthash (
 5564:                      aboutme    => 'Personal web page',
 5565:                      blog       => 'Blog',
 5566:                      webdav     => 'WebDAV',
 5567:                      portfolio  => 'Portfolio',
 5568:                      official   => 'Official courses (with institutional codes)',
 5569:                      unofficial => 'Unofficial courses',
 5570:                      community  => 'Communities',
 5571:                      textbook   => 'Textbook courses',
 5572:                      placement  => 'Placement tests',
 5573:                  );
 5574:     return %titles;
 5575: }
 5576: 
 5577: sub courserequest_titles {
 5578:     my %titles = &Apache::lonlocal::texthash (
 5579:                                    official   => 'Official',
 5580:                                    unofficial => 'Unofficial',
 5581:                                    community  => 'Communities',
 5582:                                    textbook   => 'Textbook',
 5583:                                    placement  => 'Placement tests',
 5584:                                    norequest  => 'Not allowed',
 5585:                                    approval   => 'Approval by Dom. Coord.',
 5586:                                    validate   => 'With validation',
 5587:                                    autolimit  => 'Numerical limit',
 5588:                                    unlimited  => '(blank for unlimited)',
 5589:                  );
 5590:     return %titles;
 5591: }
 5592: 
 5593: sub authorrequest_titles {
 5594:     my %titles = &Apache::lonlocal::texthash (
 5595:                                    norequest  => 'Not allowed',
 5596:                                    approval   => 'Approval by Dom. Coord.',
 5597:                                    automatic  => 'Automatic approval',
 5598:                  );
 5599:     return %titles;
 5600: }
 5601: 
 5602: sub courserequest_conditions {
 5603:     my %conditions = &Apache::lonlocal::texthash (
 5604:        approval    => '(Processing of request subject to approval by Domain Coordinator).',
 5605:        validate   => '(Processing of request subject to institutional validation).',
 5606:                  );
 5607:     return %conditions;
 5608: }
 5609: 
 5610: 
 5611: sub print_usercreation {
 5612:     my ($position,$dom,$settings,$rowtotal) = @_;
 5613:     my $numinrow = 4;
 5614:     my $datatable;
 5615:     if ($position eq 'top') {
 5616:         $$rowtotal ++;
 5617:         my $rowcount = 0;
 5618:         my ($rules,$ruleorder) = &Apache::lonnet::inst_userrules($dom,'username');
 5619:         if (ref($rules) eq 'HASH') {
 5620:             if (keys(%{$rules}) > 0) {
 5621:                 $datatable .= &user_formats_row('username',$settings,$rules,
 5622:                                                 $ruleorder,$numinrow,$rowcount);
 5623:                 $$rowtotal ++;
 5624:                 $rowcount ++;
 5625:             }
 5626:         }
 5627:         my ($idrules,$idruleorder) = &Apache::lonnet::inst_userrules($dom,'id');
 5628:         if (ref($idrules) eq 'HASH') {
 5629:             if (keys(%{$idrules}) > 0) {
 5630:                 $datatable .= &user_formats_row('id',$settings,$idrules,
 5631:                                                 $idruleorder,$numinrow,$rowcount);
 5632:                 $$rowtotal ++;
 5633:                 $rowcount ++;
 5634:             }
 5635:         }
 5636:         if ($rowcount == 0) {
 5637:             $datatable .= '<tr><td colspan="2">'.&mt('No format rules have been defined for usernames or IDs in this domain.').'</td></tr>';  
 5638:             $$rowtotal ++;
 5639:             $rowcount ++;
 5640:         }
 5641:     } elsif ($position eq 'middle') {
 5642:         my @creators = ('author','course','requestcrs');
 5643:         my ($rules,$ruleorder) =
 5644:             &Apache::lonnet::inst_userrules($dom,'username');
 5645:         my %lt = &usercreation_types();
 5646:         my %checked;
 5647:         if (ref($settings) eq 'HASH') {
 5648:             if (ref($settings->{'cancreate'}) eq 'HASH') {
 5649:                 foreach my $item (@creators) {
 5650:                     $checked{$item} = $settings->{'cancreate'}{$item};
 5651:                 }
 5652:             } elsif (ref($settings->{'cancreate'}) eq 'ARRAY') {
 5653:                 foreach my $item (@creators) {
 5654:                     if (grep(/^\Q$item\E$/,@{$settings->{'cancreate'}})) {
 5655:                         $checked{$item} = 'none';
 5656:                     }
 5657:                 }
 5658:             }
 5659:         }
 5660:         my $rownum = 0;
 5661:         foreach my $item (@creators) {
 5662:             $rownum ++;
 5663:             if ($checked{$item} eq '') {
 5664:                 $checked{$item} = 'any';
 5665:             }
 5666:             my $css_class;
 5667:             if ($rownum%2) {
 5668:                 $css_class = '';
 5669:             } else {
 5670:                 $css_class = ' class="LC_odd_row" ';
 5671:             }
 5672:             $datatable .= '<tr'.$css_class.'>'.
 5673:                          '<td><span class="LC_nobreak">'.$lt{$item}.
 5674:                          '</span></td><td align="right">';
 5675:             my @options = ('any');
 5676:             if (ref($rules) eq 'HASH') {
 5677:                 if (keys(%{$rules}) > 0) {
 5678:                     push(@options,('official','unofficial'));
 5679:                 }
 5680:             }
 5681:             push(@options,'none');
 5682:             foreach my $option (@options) {
 5683:                 my $type = 'radio';
 5684:                 my $check = ' ';
 5685:                 if ($checked{$item} eq $option) {
 5686:                     $check = ' checked="checked" ';
 5687:                 } 
 5688:                 $datatable .= '<span class="LC_nobreak"><label>'.
 5689:                               '<input type="'.$type.'" name="can_createuser_'.
 5690:                               $item.'" value="'.$option.'"'.$check.'/>&nbsp;'.
 5691:                               $lt{$option}.'</label>&nbsp;&nbsp;</span>';
 5692:             }
 5693:             $datatable .= '</td></tr>';
 5694:         }
 5695:     } else {
 5696:         my @contexts = ('author','course','domain');
 5697:         my @authtypes = ('int','krb4','krb5','loc');
 5698:         my %checked;
 5699:         if (ref($settings) eq 'HASH') {
 5700:             if (ref($settings->{'authtypes'}) eq 'HASH') {
 5701:                 foreach my $item (@contexts) {
 5702:                     if (ref($settings->{'authtypes'}{$item}) eq 'HASH') {
 5703:                         foreach my $auth (@authtypes) {
 5704:                             if ($settings->{'authtypes'}{$item}{$auth}) {
 5705:                                 $checked{$item}{$auth} = ' checked="checked" ';
 5706:                             }
 5707:                         }
 5708:                     }
 5709:                 }
 5710:             }
 5711:         } else {
 5712:             foreach my $item (@contexts) {
 5713:                 foreach my $auth (@authtypes) {
 5714:                     $checked{$item}{$auth} = ' checked="checked" ';
 5715:                 }
 5716:             }
 5717:         }
 5718:         my %title = &context_names();
 5719:         my %authname = &authtype_names();
 5720:         my $rownum = 0;
 5721:         my $css_class; 
 5722:         foreach my $item (@contexts) {
 5723:             if ($rownum%2) {
 5724:                 $css_class = '';
 5725:             } else {
 5726:                 $css_class = ' class="LC_odd_row" ';
 5727:             }
 5728:             $datatable .=   '<tr'.$css_class.'>'.
 5729:                             '<td>'.$title{$item}.
 5730:                             '</td><td class="LC_left_item">'.
 5731:                             '<span class="LC_nobreak">';
 5732:             foreach my $auth (@authtypes) {
 5733:                 $datatable .= '<label>'. 
 5734:                               '<input type="checkbox" name="'.$item.'_auth" '.
 5735:                               $checked{$item}{$auth}.' value="'.$auth.'" />'.
 5736:                               $authname{$auth}.'</label>&nbsp;';
 5737:             }
 5738:             $datatable .= '</span></td></tr>';
 5739:             $rownum ++;
 5740:         }
 5741:         $$rowtotal += $rownum;
 5742:     }
 5743:     return $datatable;
 5744: }
 5745: 
 5746: sub print_selfcreation {
 5747:     my ($position,$dom,$settings,$rowtotal) = @_;
 5748:     my (@selfcreate,$createsettings,$processing,$datatable);
 5749:     if (ref($settings) eq 'HASH') {
 5750:         if (ref($settings->{'cancreate'}) eq 'HASH') {
 5751:             $createsettings = $settings->{'cancreate'};
 5752:             if (ref($createsettings) eq 'HASH') {
 5753:                 if (ref($createsettings->{'selfcreate'}) eq 'ARRAY') {
 5754:                     @selfcreate = @{$createsettings->{'selfcreate'}};
 5755:                 } elsif ($createsettings->{'selfcreate'} ne '') {
 5756:                     if ($settings->{'cancreate'}{'selfcreate'} eq 'any') {
 5757:                         @selfcreate = ('email','login','sso');
 5758:                     } elsif ($createsettings->{'selfcreate'} ne 'none') {
 5759:                         @selfcreate = ($createsettings->{'selfcreate'});
 5760:                     }
 5761:                 }
 5762:                 if (ref($createsettings->{'selfcreateprocessing'}) eq 'HASH') {
 5763:                     $processing = $createsettings->{'selfcreateprocessing'};
 5764:                 }
 5765:             }
 5766:         }
 5767:     }
 5768:     my %radiohash;
 5769:     my $numinrow = 4;
 5770:     map { $radiohash{'cancreate_'.$_} = 1; } @selfcreate;
 5771:     if ($position eq 'top') {
 5772:         my %choices = &Apache::lonlocal::texthash (
 5773:                                                       cancreate_login      => 'Institutional Login',
 5774:                                                       cancreate_sso        => 'Institutional Single Sign On',
 5775:                                                   );
 5776:         my @toggles = sort(keys(%choices));
 5777:         my %defaultchecked = (
 5778:                                'cancreate_login' => 'off',
 5779:                                'cancreate_sso'   => 'off',
 5780:                              );
 5781:         my ($onclick,$itemcount);
 5782:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5783:                                                      \%choices,$itemcount,$onclick);
 5784:         $$rowtotal += $itemcount;
 5785:         
 5786:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5787: 
 5788:         if (ref($usertypes) eq 'HASH') {
 5789:             if (keys(%{$usertypes}) > 0) {
 5790:                 $datatable .= &insttypes_row($createsettings,$types,$usertypes,
 5791:                                              $dom,$numinrow,$othertitle,
 5792:                                              'statustocreate',$$rowtotal);
 5793:                 $$rowtotal ++;
 5794:             }
 5795:         }
 5796:         my @fields = ('lastname','firstname','middlename','permanentemail','id','inststatus');
 5797:         my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 5798:         $fieldtitles{'inststatus'} = &mt('Institutional status');
 5799:         my $rem;
 5800:         my $numperrow = 2;
 5801:         my $css_class = $$rowtotal%2?' class="LC_odd_row"':'';
 5802:         $datatable .= '<tr'.$css_class.'>'.
 5803:                      '<td class="LC_left_item">'.&mt('Mapping of Shibboleth environment variable names to user data fields (SSO auth)').'</td>'.
 5804:                      '<td class="LC_left_item">'."\n".
 5805:                      '<table><tr><td>'."\n";
 5806:         for (my $i=0; $i<@fields; $i++) {
 5807:             $rem = $i%($numperrow);
 5808:             if ($rem == 0) {
 5809:                 if ($i > 0) {
 5810:                     $datatable .= '</tr>';
 5811:                 }
 5812:                 $datatable .= '<tr>';
 5813:             }
 5814:             my $currval;
 5815:             if (ref($createsettings) eq 'HASH') {
 5816:                 if (ref($createsettings->{'shibenv'}) eq 'HASH') {
 5817:                     $currval = $createsettings->{'shibenv'}{$fields[$i]};
 5818:                 }
 5819:             }
 5820:             $datatable .= '<td class="LC_left_item">'.
 5821:                           '<span class="LC_nobreak">'.
 5822:                           '<input type="text" name="shibenv_'.$fields[$i].'" '.
 5823:                           'value="'.$currval.'" size="10" />&nbsp;'.
 5824:                           $fieldtitles{$fields[$i]}.'</span></td>';
 5825:         }
 5826:         my $colsleft = $numperrow - $rem;
 5827:         if ($colsleft > 1 ) {
 5828:             $datatable .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 5829:                          '&nbsp;</td>';
 5830:         } elsif ($colsleft == 1) {
 5831:             $datatable .= '<td class="LC_left_item">&nbsp;</td>';
 5832:         }
 5833:         $datatable .= '</tr></table></td></tr>';
 5834:         $$rowtotal ++;
 5835:     } elsif ($position eq 'middle') {
 5836:         my %domconf = &Apache::lonnet::get_dom('configuration',['usermodification'],$dom);
 5837:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 5838:         $usertypes->{'default'} = $othertitle;
 5839:         if (ref($types) eq 'ARRAY') {
 5840:             push(@{$types},'default');
 5841:             $usertypes->{'default'} = $othertitle;
 5842:             foreach my $status (@{$types}) {
 5843:                 $datatable .= &modifiable_userdata_row('selfcreate',$status,$domconf{'usermodification'},
 5844:                                                        $numinrow,$$rowtotal,$usertypes);
 5845:                 $$rowtotal ++;
 5846:             }
 5847:         }
 5848:     } else {
 5849:         my %choices = &Apache::lonlocal::texthash (
 5850:                                                       cancreate_email => 'E-mail address as username',
 5851:                                                   );
 5852:         my @toggles = sort(keys(%choices));
 5853:         my %defaultchecked = (
 5854:                                'cancreate_email' => 'off',
 5855:                              );
 5856:         my $itemcount = 0;
 5857:         my $display = 'none';
 5858:         if (grep(/^\Qemail\E$/,@selfcreate)) {
 5859:             $display = 'block';
 5860:         }
 5861:         my $onclick = "toggleDisplay(this.form,'emailoptions');";
 5862:         my $additional = '<div id="emailoptions" style="display: '.$display.'">';
 5863:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
 5864:         my $usertypes = {};
 5865:         my $order = [];
 5866:         if ((ref($domdefaults{'inststatustypes'}) eq 'HASH') && (ref($domdefaults{'inststatusguest'}) eq 'ARRAY')) {
 5867:             $usertypes = $domdefaults{'inststatustypes'};
 5868:             $order = $domdefaults{'inststatusguest'};
 5869:         }
 5870:         if (ref($order) eq 'ARRAY') {
 5871:             push(@{$order},'default');
 5872:             if (@{$order} > 1) {
 5873:                 $usertypes->{'default'} = &mt('Other users');
 5874:                 $additional .= '<table><tr>';
 5875:                 foreach my $status (@{$order}) {
 5876:                     $additional .= '<th>'.$usertypes->{$status}.'</th>';
 5877:                 }
 5878:                 $additional .= '</tr><tr>';
 5879:                 foreach my $status (@{$order}) {
 5880:                     $additional .= '<td>'.&email_as_username($rowtotal,$processing,$status).'</td>';
 5881:                 }
 5882:                 $additional .= '</tr></table>';
 5883:             } else {
 5884:                 $usertypes->{'default'} = &mt('All users');
 5885:                 $additional .= &email_as_username($rowtotal,$processing);
 5886:             }
 5887:         }
 5888:         $additional .= '</div>'."\n";
 5889: 
 5890:         ($datatable,$itemcount) = &radiobutton_prefs(\%radiohash,\@toggles,\%defaultchecked,
 5891:                                                      \%choices,$$rowtotal,$onclick,$additional);
 5892:         $$rowtotal ++;
 5893:         $datatable .= &print_requestmail($dom,'selfcreation',$createsettings,$rowtotal);
 5894:         $$rowtotal ++;
 5895:         my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
 5896:         $numinrow = 1;
 5897:         if (ref($order) eq 'ARRAY') {
 5898:             foreach my $status (@{$order}) {
 5899:                 $datatable .= &modifiable_userdata_row('cancreate','emailusername_'.$status,$settings,
 5900:                                                        $numinrow,$$rowtotal,$usertypes,$infofields,$infotitles);
 5901:                 $$rowtotal ++;
 5902:             }
 5903:         }
 5904:         my ($emailrules,$emailruleorder) =
 5905:             &Apache::lonnet::inst_userrules($dom,'email');
 5906:         if (ref($emailrules) eq 'HASH') {
 5907:             if (keys(%{$emailrules}) > 0) {
 5908:                 $datatable .= &user_formats_row('email',$settings,$emailrules,
 5909:                                                 $emailruleorder,$numinrow,$$rowtotal);
 5910:                 $$rowtotal ++;
 5911:             }
 5912:         }
 5913:         $datatable .= &captcha_choice('cancreate',$createsettings,$$rowtotal);
 5914:     }
 5915:     return $datatable;
 5916: }
 5917: 
 5918: sub email_as_username {
 5919:     my ($rowtotal,$processing,$type) = @_;
 5920:     my %choices =
 5921:         &Apache::lonlocal::texthash (
 5922:                                       automatic => 'Automatic approval',
 5923:                                       approval  => 'Queued for approval',
 5924:                                     );
 5925:     my $output;
 5926:     foreach my $option ('automatic','approval') {
 5927:         my $checked;
 5928:         if (ref($processing) eq 'HASH') {
 5929:             if ($type eq '') {   
 5930:                 if (!exists($processing->{'default'})) {
 5931:                     if ($option eq 'automatic') {
 5932:                         $checked = ' checked="checked"';
 5933:                     }
 5934:                 } else {
 5935:                     if ($processing->{'default'} eq $option) {
 5936:                         $checked = ' checked="checked"';
 5937:                     }
 5938:                 }
 5939:             } else {
 5940:                 if (!exists($processing->{$type})) {
 5941:                     if ($option eq 'automatic') {
 5942:                         $checked = ' checked="checked"';
 5943:                     }
 5944:                 } else {
 5945:                     if ($processing->{$type} eq $option) {
 5946:                         $checked = ' checked="checked"';
 5947:                     }
 5948:                 }
 5949:             }
 5950:         } elsif ($option eq 'automatic') {
 5951:             $checked = ' checked="checked"'; 
 5952:         }
 5953:         my $name = 'cancreate_emailprocess';
 5954:         if (($type ne '') && ($type ne 'default')) {
 5955:             $name .= '_'.$type;
 5956:         }
 5957:         $output .= '<span class="LC_nobreak"><label>'.
 5958:                    '<input type="radio" name="'.$name.'"'.
 5959:                    $checked.' value="'.$option.'" />'.
 5960:                    $choices{$option}.'</label></span>';
 5961:         if ($type eq '') {
 5962:             $output .= '&nbsp;';
 5963:         } else {
 5964:             $output .= '<br />';
 5965:         }
 5966:     }
 5967:     $$rowtotal ++;
 5968:     return $output;
 5969: }
 5970: 
 5971: sub captcha_choice {
 5972:     my ($context,$settings,$itemcount) = @_;
 5973:     my ($keyentry,$currpub,$currpriv,%checked,$rowname,$pubtext,$privtext,
 5974:         $vertext,$currver);
 5975:     my %lt = &captcha_phrases();
 5976:     $keyentry = 'hidden';
 5977:     if ($context eq 'cancreate') {
 5978:         $rowname = &mt('CAPTCHA validation');
 5979:     } elsif ($context eq 'login') {
 5980:         $rowname =  &mt('"Contact helpdesk" CAPTCHA validation');
 5981:     }
 5982:     if (ref($settings) eq 'HASH') {
 5983:         if ($settings->{'captcha'}) {
 5984:             $checked{$settings->{'captcha'}} = ' checked="checked"';
 5985:         } else {
 5986:             $checked{'original'} = ' checked="checked"';
 5987:         }
 5988:         if ($settings->{'captcha'} eq 'recaptcha') {
 5989:             $pubtext = $lt{'pub'};
 5990:             $privtext = $lt{'priv'};
 5991:             $keyentry = 'text';
 5992:             $vertext = $lt{'ver'};
 5993:             $currver = $settings->{'recaptchaversion'};
 5994:             if ($currver ne '2') {
 5995:                 $currver = 1;
 5996:             }
 5997:         }
 5998:         if (ref($settings->{'recaptchakeys'}) eq 'HASH') {
 5999:             $currpub = $settings->{'recaptchakeys'}{'public'};
 6000:             $currpriv = $settings->{'recaptchakeys'}{'private'};
 6001:         }
 6002:     } else {
 6003:         $checked{'original'} = ' checked="checked"';
 6004:     }
 6005:     my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6006:     my $output = '<tr'.$css_class.'>'.
 6007:                  '<td class="LC_left_item">'.$rowname.'</td><td class="LC_left_item" colspan="2">'."\n".
 6008:                  '<table><tr><td>'."\n";
 6009:     foreach my $option ('original','recaptcha','notused') {
 6010:         $output .= '<span class="LC_nobreak"><label><input type="radio" name="'.$context.'_captcha" value="'.
 6011:                    $option.'" '.$checked{$option}.' onchange="javascript:updateCaptcha('."this,'$context'".');" />'.
 6012:                    $lt{$option}.'</label></span>';
 6013:         unless ($option eq 'notused') {
 6014:             $output .= ('&nbsp;'x2)."\n";
 6015:         }
 6016:     }
 6017: #
 6018: # Note: If reCAPTCHA is to be used for LON-CAPA servers in a domain, a domain coordinator should visit:
 6019: # https://www.google.com/recaptcha and generate a Public and Private key. For domains with multiple
 6020: # servers a single key pair will be used for all servers, so the internet domain (e.g., yourcollege.edu)
 6021: # specified for use with the key should be broad enough to accommodate all servers in the LON-CAPA domain.
 6022: #
 6023:     $output .= '</td></tr>'."\n".
 6024:                '<tr><td>'."\n".
 6025:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchapubtxt">'.$pubtext.'</span>&nbsp;'."\n".
 6026:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapub" name="'.$context.'_recaptchapub" value="'.
 6027:                $currpub.'" size="40" /></span><br />'."\n".
 6028:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchaprivtxt">'.$privtext.'</span>&nbsp;'."\n".
 6029:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchapriv" name="'.$context.'_recaptchapriv" value="'.
 6030:                $currpriv.'" size="40" /></span><br />'.
 6031:                '<span class="LC_nobreak"><span id="'.$context.'_recaptchavertxt">'.$vertext.'</span>&nbsp;'."\n".
 6032:                '<input type="'.$keyentry.'" id="'.$context.'_recaptchaversion" name="'.$context.'_recaptchaversion" value="'.
 6033:                $currver.'" size="3" /></span><br />'.
 6034:                '</td></tr></table>'."\n".
 6035:                '</td></tr>';
 6036:     return $output;
 6037: }
 6038: 
 6039: sub user_formats_row {
 6040:     my ($type,$settings,$rules,$ruleorder,$numinrow,$rowcount) = @_;
 6041:     my $output;
 6042:     my %text = (
 6043:                    'username' => 'new usernames',
 6044:                    'id'       => 'IDs',
 6045:                    'email'    => 'self-created accounts (e-mail)',
 6046:                );
 6047:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 6048:     $output = '<tr '.$css_class.'>'.
 6049:               '<td><span class="LC_nobreak">';
 6050:     if ($type eq 'email') {
 6051:         $output .= &mt("Formats disallowed for $text{$type}: ");
 6052:     } else {
 6053:         $output .= &mt("Format rules to check for $text{$type}: ");
 6054:     }
 6055:     $output .= '</span></td>'.
 6056:                '<td class="LC_left_item" colspan="2"><table>';
 6057:     my $rem;
 6058:     if (ref($ruleorder) eq 'ARRAY') {
 6059:         for (my $i=0; $i<@{$ruleorder}; $i++) {
 6060:             if (ref($rules->{$ruleorder->[$i]}) eq 'HASH') {
 6061:                 my $rem = $i%($numinrow);
 6062:                 if ($rem == 0) {
 6063:                     if ($i > 0) {
 6064:                         $output .= '</tr>';
 6065:                     }
 6066:                     $output .= '<tr>';
 6067:                 }
 6068:                 my $check = ' ';
 6069:                 if (ref($settings) eq 'HASH') {
 6070:                     if (ref($settings->{$type.'_rule'}) eq 'ARRAY') {
 6071:                         if (grep(/^\Q$ruleorder->[$i]\E$/,@{$settings->{$type.'_rule'}})) {
 6072:                             $check = ' checked="checked" ';
 6073:                         }
 6074:                     }
 6075:                 }
 6076:                 $output .= '<td class="LC_left_item">'.
 6077:                            '<span class="LC_nobreak"><label>'.
 6078:                            '<input type="checkbox" name="'.$type.'_rule" '.
 6079:                            'value="'.$ruleorder->[$i].'"'.$check.'/>'.
 6080:                            $rules->{$ruleorder->[$i]}{'name'}.'</label></span></td>';
 6081:             }
 6082:         }
 6083:         $rem = @{$ruleorder}%($numinrow);
 6084:     }
 6085:     my $colsleft = $numinrow - $rem;
 6086:     if ($colsleft > 1 ) {
 6087:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 6088:                    '&nbsp;</td>';
 6089:     } elsif ($colsleft == 1) {
 6090:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 6091:     }
 6092:     $output .= '</tr></table></td></tr>';
 6093:     return $output;
 6094: }
 6095: 
 6096: sub usercreation_types {
 6097:     my %lt = &Apache::lonlocal::texthash (
 6098:                     author     => 'When adding a co-author',
 6099:                     course     => 'When adding a user to a course',
 6100:                     requestcrs => 'When requesting a course',
 6101:                     any        => 'Any',
 6102:                     official   => 'Institutional only ',
 6103:                     unofficial => 'Non-institutional only',
 6104:                     none       => 'None',
 6105:     );
 6106:     return %lt;
 6107: }
 6108: 
 6109: sub selfcreation_types {
 6110:     my %lt = &Apache::lonlocal::texthash (
 6111:                     selfcreate => 'User creates own account',
 6112:                     any        => 'Any',
 6113:                     official   => 'Institutional only ',
 6114:                     unofficial => 'Non-institutional only',
 6115:                     email      => 'E-mail address',
 6116:                     login      => 'Institutional Login',
 6117:                     sso        => 'SSO',
 6118:              );
 6119: }
 6120: 
 6121: sub authtype_names {
 6122:     my %lt = &Apache::lonlocal::texthash(
 6123:                       int    => 'Internal',
 6124:                       krb4   => 'Kerberos 4',
 6125:                       krb5   => 'Kerberos 5',
 6126:                       loc    => 'Local',
 6127:                   );
 6128:     return %lt;
 6129: }
 6130: 
 6131: sub context_names {
 6132:     my %context_title = &Apache::lonlocal::texthash(
 6133:        author => 'Creating users when an Author',
 6134:        course => 'Creating users when in a course',
 6135:        domain => 'Creating users when a Domain Coordinator',
 6136:     );
 6137:     return %context_title;
 6138: }
 6139: 
 6140: sub print_usermodification {
 6141:     my ($position,$dom,$settings,$rowtotal) = @_;
 6142:     my $numinrow = 4;
 6143:     my ($context,$datatable,$rowcount);
 6144:     if ($position eq 'top') {
 6145:         $rowcount = 0;
 6146:         $context = 'author'; 
 6147:         foreach my $role ('ca','aa') {
 6148:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6149:                                                    $numinrow,$rowcount);
 6150:             $$rowtotal ++;
 6151:             $rowcount ++;
 6152:         }
 6153:     } elsif ($position eq 'bottom') {
 6154:         $context = 'course';
 6155:         $rowcount = 0;
 6156:         foreach my $role ('st','ep','ta','in','cr') {
 6157:             $datatable .= &modifiable_userdata_row($context,$role,$settings,
 6158:                                                    $numinrow,$rowcount);
 6159:             $$rowtotal ++;
 6160:             $rowcount ++;
 6161:         }
 6162:     }
 6163:     return $datatable;
 6164: }
 6165: 
 6166: sub print_defaults {
 6167:     my ($position,$dom,$settings,$rowtotal) = @_;
 6168:     my $rownum = 0;
 6169:     my ($datatable,$css_class,$titles);
 6170:     unless ($position eq 'bottom') {
 6171:         $titles = &defaults_titles($dom);
 6172:     }
 6173:     if ($position eq 'top') {
 6174:         my @items = ('auth_def','auth_arg_def','lang_def','timezone_def',
 6175:                      'datelocale_def','portal_def');
 6176:         my %defaults;
 6177:         if (ref($settings) eq 'HASH') {
 6178:             %defaults = %{$settings};
 6179:         } else {
 6180:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 6181:             foreach my $item (@items) {
 6182:                 $defaults{$item} = $domdefaults{$item};
 6183:             }
 6184:         }
 6185:         foreach my $item (@items) {
 6186:             if ($rownum%2) {
 6187:                 $css_class = '';
 6188:             } else {
 6189:                 $css_class = ' class="LC_odd_row" ';
 6190:             }
 6191:             $datatable .= '<tr'.$css_class.'>'.
 6192:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 6193:                           '</span></td><td class="LC_right_item" colspan="3">';
 6194:             if ($item eq 'auth_def') {
 6195:                 my @authtypes = ('internal','krb4','krb5','localauth');
 6196:                 my %shortauth = (
 6197:                                  internal => 'int',
 6198:                                  krb4 => 'krb4',
 6199:                                  krb5 => 'krb5',
 6200:                                  localauth  => 'loc'
 6201:                                 );
 6202:                 my %authnames = &authtype_names();
 6203:                 foreach my $auth (@authtypes) {
 6204:                     my $checked = ' ';
 6205:                     if ($defaults{$item} eq $auth) {
 6206:                         $checked = ' checked="checked" ';
 6207:                     }
 6208:                     $datatable .= '<label><input type="radio" name="'.$item.
 6209:                                   '" value="'.$auth.'"'.$checked.'/>'.
 6210:                                   $authnames{$shortauth{$auth}}.'</label>&nbsp;&nbsp;';
 6211:                 }
 6212:             } elsif ($item eq 'timezone_def') {
 6213:                 my $includeempty = 1;
 6214:                 $datatable .= &Apache::loncommon::select_timezone($item,$defaults{$item},undef,$includeempty);
 6215:             } elsif ($item eq 'datelocale_def') {
 6216:                 my $includeempty = 1;
 6217:                 $datatable .= &Apache::loncommon::select_datelocale($item,$defaults{$item},undef,$includeempty);
 6218:             } elsif ($item eq 'lang_def') {
 6219:                 my $includeempty = 1;
 6220:                 $datatable .= &Apache::loncommon::select_language($item,$defaults{$item},$includeempty);
 6221:             } else {
 6222:                 my $size;
 6223:                 if ($item eq 'portal_def') {
 6224:                     $size = ' size="25"';
 6225:                 }
 6226:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6227:                               $defaults{$item}.'"'.$size.' />';
 6228:             }
 6229:             $datatable .= '</td></tr>';
 6230:             $rownum ++;
 6231:         }
 6232:     } elsif ($position eq 'middle') {
 6233:         my @items = ('intauth_cost','intauth_check','intauth_switch');
 6234:         my %defaults;
 6235:         if (ref($settings) eq 'HASH') {
 6236:             %defaults = %{$settings};
 6237:             if ($defaults{'intauth_cost'} !~ /^\d+$/) {
 6238:                 $defaults{'intauth_cost'} = 10;
 6239:             }
 6240:             if ($defaults{'intauth_check'} !~ /^(0|1|2)$/) {
 6241:                 $defaults{'intauth_check'} = 0;
 6242:             }
 6243:             if ($defaults{'intauth_switch'} !~ /^(0|1|2)$/) {
 6244:                 $defaults{'intauth_switch'} = 0;
 6245:             }
 6246:         } else {
 6247:             %defaults = (
 6248:                           'intauth_cost'   => 10,
 6249:                           'intauth_check'  => 0,
 6250:                           'intauth_switch' => 0,
 6251:                         );
 6252:         }
 6253:         foreach my $item (@items) {
 6254:             if ($rownum%2) {
 6255:                 $css_class = '';
 6256:             } else {
 6257:                 $css_class = ' class="LC_odd_row" ';
 6258:             }
 6259:             $datatable .= '<tr'.$css_class.'>'.
 6260:                           '<td><span class="LC_nobreak">'.$titles->{$item}.
 6261:                           '</span></td><td class="LC_left_item" colspan="3">';
 6262:             if ($item eq 'intauth_switch') {
 6263:                 my @options = (0,1,2);
 6264:                 my %optiondesc = &Apache::lonlocal::texthash (
 6265:                                    0 => 'No',
 6266:                                    1 => 'Yes',
 6267:                                    2 => 'Yes, and copy existing passwd file to passwd.bak file',
 6268:                                  );
 6269:                 $datatable .= '<table width="100%">';
 6270:                 foreach my $option (@options) {
 6271:                     my $checked = ' ';
 6272:                     if ($defaults{$item} eq $option) {
 6273:                         $checked = ' checked="checked"';
 6274:                     }
 6275:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6276:                                   '<label><input type="radio" name="'.$item.
 6277:                                   '" value="'.$option.'"'.$checked.' />'.
 6278:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6279:                 }
 6280:                 $datatable .= '</table>';
 6281:             } elsif ($item eq 'intauth_check') {
 6282:                 my @options = (0,1,2);
 6283:                 my %optiondesc = &Apache::lonlocal::texthash (
 6284:                                    0 => 'No',
 6285:                                    1 => 'Yes, allow login then update passwd file using default cost (if higher)',
 6286:                                    2 => 'Yes, disallow login if stored cost is less than domain default',
 6287:                                  );
 6288:                 $datatable .= '<table wisth="100%">';
 6289:                 foreach my $option (@options) {
 6290:                     my $checked = ' ';
 6291:                     my $onclick;
 6292:                     if ($defaults{$item} eq $option) {
 6293:                         $checked = ' checked="checked"';
 6294:                     }
 6295:                     if ($option == 2) {
 6296:                         $onclick = ' onclick="javascript:warnIntAuth(this);"';
 6297:                     }
 6298:                     $datatable .= '<tr><td class="LC_left_item"><span class="LC_nobreak">'.
 6299:                                   '<label><input type="radio" name="'.$item.
 6300:                                   '" value="'.$option.'"'.$checked.$onclick.' />'.
 6301:                                   $optiondesc{$option}.'</label></span></td></tr>';
 6302:                 }
 6303:                 $datatable .= '</table>';
 6304:             } else {
 6305:                 $datatable .= '<input type="text" name="'.$item.'" value="'.
 6306:                               $defaults{$item}.'" size="3" onblur="javascript:warnIntAuth(this);" />'; 
 6307:             }
 6308:             $datatable .= '</td></tr>';
 6309:             $rownum ++;
 6310:         }
 6311:     } else {
 6312:         my %defaults;
 6313:         if (ref($settings) eq 'HASH') {
 6314:             if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH') &&
 6315:                 (ref($settings->{'inststatusguest'}) eq 'ARRAY')) {
 6316:                 my $maxnum = @{$settings->{'inststatusorder'}};
 6317:                 for (my $i=0; $i<$maxnum; $i++) {
 6318:                     $css_class = $rownum%2?' class="LC_odd_row"':'';
 6319:                     my $item = $settings->{'inststatusorder'}->[$i];
 6320:                     my $title = $settings->{'inststatustypes'}->{$item};
 6321:                     my $guestok;
 6322:                     if (grep(/^\Q$item\E$/,@{$settings->{'inststatusguest'}})) {
 6323:                         $guestok = 1;
 6324:                     }
 6325:                     my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'$item'".');"';
 6326:                     $datatable .= '<tr'.$css_class.'>'.
 6327:                                   '<td><span class="LC_nobreak">'.
 6328:                                   '<select name="inststatus_pos_'.$item.'"'.$chgstr.'>';
 6329:                     for (my $k=0; $k<=$maxnum; $k++) {
 6330:                         my $vpos = $k+1;
 6331:                         my $selstr;
 6332:                         if ($k == $i) {
 6333:                             $selstr = ' selected="selected" ';
 6334:                         }
 6335:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6336:                     }
 6337:                     my ($checkedon,$checkedoff);
 6338:                     $checkedoff = ' checked="checked"';
 6339:                     if ($guestok) {
 6340:                         $checkedon = $checkedoff;
 6341:                         $checkedoff = ''; 
 6342:                     }
 6343:                     $datatable .= '</select>&nbsp;'.&mt('Internal ID:').'&nbsp;<b>'.$item.'</b>&nbsp;'.
 6344:                                   '<input type="checkbox" name="inststatus_delete" value="'.$item.'" />'.
 6345:                                   &mt('delete').'</span></td>'.
 6346:                                   '<td class="LC_left_item"><span class="LC_nobreak">'.&mt('Name displayed:').
 6347:                                   '<input type="text" size="20" name="inststatus_title_'.$item.'" value="'.$title.'" />'.
 6348:                                   '</span></td>'.
 6349:                                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6350:                                   '<label><input type="radio" value="1" name="inststatus_guest_'.$item.'"'.$checkedon.' />'.
 6351:                                   &mt('Yes').'</label>'.('&nbsp;'x2).
 6352:                                   '<label><input type="radio" value="0" name="inststatus_guest_'.$item.'"'.$checkedoff.' />'.
 6353:                                   &mt('No').'</label></span></td></tr>';
 6354:                 }
 6355:                 $css_class = $rownum%2?' class="LC_odd_row"':'';
 6356:                 my $chgstr = ' onchange="javascript:reorderTypes(this.form,'."'addinststatus_pos'".');"';
 6357:                 $datatable .= '<tr '.$css_class.'>'.
 6358:                               '<td><span class="LC_nobreak"><select name="addinststatus_pos"'.$chgstr.'>';
 6359:                 for (my $k=0; $k<=$maxnum; $k++) {
 6360:                     my $vpos = $k+1;
 6361:                     my $selstr;
 6362:                     if ($k == $maxnum) {
 6363:                         $selstr = ' selected="selected" ';
 6364:                     }
 6365:                     $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6366:                 }
 6367:                 $datatable .= '</select>&nbsp;'.&mt('Internal ID:').
 6368:                               '<input type="text" size="10" name="addinststatus" value="" />'.
 6369:                               '&nbsp;'.&mt('(new)').
 6370:                               '</span></td><td class="LC_left_item"><span class="LC_nobreak">'.
 6371:                               &mt('Name displayed:').
 6372:                               '<input type="text" size="20" name="addinststatus_title" value="" /></span></td>'.
 6373:                               '<td class="LC_right_item"><span class="LC_nobreak">'.
 6374:                               '<label><input type="radio" value="1" name="addinststatus_guest" />'.
 6375:                               &mt('Yes').'</label>'.('&nbsp;'x2).
 6376:                               '<label><input type="radio" value="0" name="addinststatus_guest" />'.
 6377:                               &mt('No').'</label></span></td></tr>';
 6378:                               '</tr>'."\n";
 6379:                 $rownum ++;
 6380:             }
 6381:         }
 6382:     }
 6383:     $$rowtotal += $rownum;
 6384:     return $datatable;
 6385: }
 6386: 
 6387: sub get_languages_hash {
 6388:     my %langchoices;
 6389:     foreach my $id (&Apache::loncommon::languageids()) {
 6390:         my $code = &Apache::loncommon::supportedlanguagecode($id);
 6391:         if ($code ne '') {
 6392:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
 6393:         }
 6394:     }
 6395:     return %langchoices;
 6396: }
 6397: 
 6398: sub defaults_titles {
 6399:     my ($dom) = @_;
 6400:     my %titles = &Apache::lonlocal::texthash (
 6401:                    'auth_def'      => 'Default authentication type',
 6402:                    'auth_arg_def'  => 'Default authentication argument',
 6403:                    'lang_def'      => 'Default language',
 6404:                    'timezone_def'  => 'Default timezone',
 6405:                    'datelocale_def' => 'Default locale for dates',
 6406:                    'portal_def'     => 'Portal/Default URL',
 6407:                    'intauth_cost'   => 'Encryption cost for bcrypt (positive integer)',
 6408:                    'intauth_check'  => 'Check bcrypt cost if authenticated',
 6409:                    'intauth_switch' => 'Existing crypt-based switched to bcrypt on authentication',
 6410:                  );
 6411:     if ($dom) {
 6412:         my $uprimary_id = &Apache::lonnet::domain($dom,'primary');
 6413:         my $uint_dom = &Apache::lonnet::internet_dom($uprimary_id);
 6414:         my $protocol = $Apache::lonnet::protocol{$uprimary_id};
 6415:         $protocol = 'http' if ($protocol ne 'https');
 6416:         if ($uint_dom) {
 6417:             $titles{'portal_def'} .= ' '.&mt('(for example: [_1])',$protocol.'://loncapa.'.
 6418:                                          $uint_dom);
 6419:         }
 6420:     }
 6421:     return (\%titles);
 6422: }
 6423: 
 6424: sub print_scantronformat {
 6425:     my ($r,$dom,$confname,$settings,$rowtotal) = @_;
 6426:     my $itemcount = 1;
 6427:     my ($datatable,$css_class,$scantronurl,$is_custom,%error,%scantronurls,
 6428:         %confhash);
 6429:     my $switchserver = &check_switchserver($dom,$confname);
 6430:     my %lt = &Apache::lonlocal::texthash (
 6431:                 default => 'Default bubblesheet format file error',
 6432:                 custom  => 'Custom bubblesheet format file error',
 6433:              );
 6434:     my %scantronfiles = (
 6435:         default => 'default.tab',
 6436:         custom => 'custom.tab',
 6437:     );
 6438:     foreach my $key (keys(%scantronfiles)) {
 6439:         $scantronurls{$key} = '/res/'.$dom.'/'.$confname.'/scantron/'
 6440:                               .$scantronfiles{$key};
 6441:     }
 6442:     my @defaultinfo = &Apache::lonnet::stat_file($scantronurls{'default'});
 6443:     if ((!@defaultinfo) || ($defaultinfo[0] eq 'no_such_dir')) {
 6444:         if (!$switchserver) {
 6445:             my $servadm = $r->dir_config('lonAdmEMail');
 6446:             my ($configuserok,$author_ok) = &config_check($dom,$confname,$servadm);
 6447:             if ($configuserok eq 'ok') {
 6448:                 if ($author_ok eq 'ok') {
 6449:                     my %legacyfile = (
 6450:  default => $Apache::lonnet::perlvar{'lonTabDir'}.'/default_scantronformat.tab', 
 6451:  custom  => $Apache::lonnet::perlvar{'lonTabDir'}.'/scantronformat.tab', 
 6452:                     );
 6453:                     my %md5chk;
 6454:                     foreach my $type (keys(%legacyfile)) {
 6455:                         ($md5chk{$type}) = split(/ /,`md5sum $legacyfile{$type}`);
 6456:                         chomp($md5chk{$type});
 6457:                     }
 6458:                     if ($md5chk{'default'} ne $md5chk{'custom'}) {
 6459:                         foreach my $type (keys(%legacyfile)) {
 6460:                             ($scantronurls{$type},my $error) = 
 6461:                                 &legacy_scantronformat($r,$dom,$confname,
 6462:                                                  $type,$legacyfile{$type},
 6463:                                                  $scantronurls{$type},
 6464:                                                  $scantronfiles{$type});
 6465:                             if ($error ne '') {
 6466:                                 $error{$type} = $error;
 6467:                             }
 6468:                         }
 6469:                         if (keys(%error) == 0) {
 6470:                             $is_custom = 1;
 6471:                             $confhash{'scantron'}{'scantronformat'} = 
 6472:                                 $scantronurls{'custom'};
 6473:                             my $putresult = 
 6474:                                 &Apache::lonnet::put_dom('configuration',
 6475:                                                          \%confhash,$dom);
 6476:                             if ($putresult ne 'ok') {
 6477:                                 $error{'custom'} = 
 6478:                                     '<span class="LC_error">'.
 6479:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6480:                             }
 6481:                         }
 6482:                     } else {
 6483:                         ($scantronurls{'default'},my $error) =
 6484:                             &legacy_scantronformat($r,$dom,$confname,
 6485:                                           'default',$legacyfile{'default'},
 6486:                                           $scantronurls{'default'},
 6487:                                           $scantronfiles{'default'});
 6488:                         if ($error eq '') {
 6489:                             $confhash{'scantron'}{'scantronformat'} = ''; 
 6490:                             my $putresult =
 6491:                                 &Apache::lonnet::put_dom('configuration',
 6492:                                                          \%confhash,$dom);
 6493:                             if ($putresult ne 'ok') {
 6494:                                 $error{'default'} =
 6495:                                     '<span class="LC_error">'.
 6496:                                     &mt('An error occurred updating the domain configuration: [_1]',$putresult).'</span>';
 6497:                             }
 6498:                         } else {
 6499:                             $error{'default'} = $error;
 6500:                         }
 6501:                     }
 6502:                 }
 6503:             }
 6504:         } else {
 6505:             $error{'default'} = &mt("Unable to copy default bubblesheet formatfile to domain's RES space: [_1]",$switchserver);
 6506:         }
 6507:     }
 6508:     if (ref($settings) eq 'HASH') {
 6509:         if ($settings->{'scantronformat'} eq "/res/$dom/$confname/scantron/custom.tab") {
 6510:             my @info = &Apache::lonnet::stat_file($settings->{'scantronformat'});
 6511:             if ((!@info) || ($info[0] eq 'no_such_dir')) {
 6512:                 $scantronurl = '';
 6513:             } else {
 6514:                 $scantronurl = $settings->{'scantronformat'};
 6515:             }
 6516:             $is_custom = 1;
 6517:         } else {
 6518:             $scantronurl = $scantronurls{'default'};
 6519:         }
 6520:     } else {
 6521:         if ($is_custom) {
 6522:             $scantronurl = $scantronurls{'custom'};
 6523:         } else {
 6524:             $scantronurl = $scantronurls{'default'};
 6525:         }
 6526:     }
 6527:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6528:     $datatable .= '<tr'.$css_class.'>';
 6529:     if (!$is_custom) {
 6530:         $datatable .= '<td>'.&mt('Default in use:').'<br />'.
 6531:                       '<span class="LC_nobreak">';
 6532:         if ($scantronurl) {
 6533:             $datatable .= &Apache::loncommon::modal_link($scantronurl,&mt('Default bubblesheet format file'),600,500,
 6534:                                                          undef,undef,undef,undef,'background-color:#ffffff');
 6535:         } else {
 6536:             $datatable = &mt('File unavailable for display');
 6537:         }
 6538:         $datatable .= '</span></td>';
 6539:         if (keys(%error) == 0) { 
 6540:             $datatable .= '<td valign="bottom">';
 6541:             if (!$switchserver) {
 6542:                 $datatable .= &mt('Upload:').'<br />';
 6543:             }
 6544:         } else {
 6545:             my $errorstr;
 6546:             foreach my $key (sort(keys(%error))) {
 6547:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6548:             }
 6549:             $datatable .= '<td>'.$errorstr;
 6550:         }
 6551:     } else {
 6552:         if (keys(%error) > 0) {
 6553:             my $errorstr;
 6554:             foreach my $key (sort(keys(%error))) {
 6555:                 $errorstr .= $lt{$key}.': '.$error{$key}.'<br />';
 6556:             } 
 6557:             $datatable .= '<td>'.$errorstr.'</td><td>&nbsp;';
 6558:         } elsif ($scantronurl) {
 6559:             my $link =  &Apache::loncommon::modal_link($scantronurl,&mt('Custom bubblesheet format file'),600,500,
 6560:                                                        undef,undef,undef,undef,'background-color:#ffffff');
 6561:             $datatable .= '<td><span class="LC_nobreak">'.
 6562:                           $link.
 6563:                           '<label><input type="checkbox" name="scantronformat_del"'.
 6564:                           ' value="1" />'.&mt('Delete?').'</label></span></td>'.
 6565:                           '<td><span class="LC_nobreak">&nbsp;'.
 6566:                           &mt('Replace:').'</span><br />';
 6567:         }
 6568:     }
 6569:     if (keys(%error) == 0) {
 6570:         if ($switchserver) {
 6571:             $datatable .= &mt('Upload to library server: [_1]',$switchserver);
 6572:         } else {
 6573:             $datatable .='<span class="LC_nobreak">&nbsp;'.
 6574:                          '<input type="file" name="scantronformat" /></span>';
 6575:         }
 6576:     }
 6577:     $datatable .= '</td></tr>';
 6578:     $$rowtotal ++;
 6579:     return $datatable;
 6580: }
 6581: 
 6582: sub legacy_scantronformat {
 6583:     my ($r,$dom,$confname,$file,$legacyfile,$newurl,$newfile) = @_;
 6584:     my ($url,$error);
 6585:     my @statinfo = &Apache::lonnet::stat_file($newurl);
 6586:     if ((!@statinfo) || ($statinfo[0] eq 'no_such_dir')) {
 6587:         (my $result,$url) =
 6588:             &publishlogo($r,'copy',$legacyfile,$dom,$confname,'scantron',
 6589:                          '','',$newfile);
 6590:         if ($result ne 'ok') {
 6591:             $error = &mt("An error occurred publishing the [_1] bubblesheet format file in RES space. Error was: [_2].",$newfile,$result);
 6592:         }
 6593:     }
 6594:     return ($url,$error);
 6595: }
 6596: 
 6597: sub print_coursecategories {
 6598:     my ($position,$dom,$hdritem,$settings,$rowtotal) = @_;
 6599:     my $datatable;
 6600:     if ($position eq 'top') {
 6601:         my (%checked);
 6602:         my @catitems = ('unauth','auth');
 6603:         my @cattypes = ('std','domonly','codesrch','none');
 6604:         $checked{'unauth'} = 'std';
 6605:         $checked{'auth'} = 'std';
 6606:         if (ref($settings) eq 'HASH') {
 6607:             foreach my $type (@cattypes) {
 6608:                 if ($type eq $settings->{'unauth'}) {
 6609:                     $checked{'unauth'} = $type;
 6610:                 }
 6611:                 if ($type eq $settings->{'auth'}) {
 6612:                     $checked{'auth'} = $type;
 6613:                 }
 6614:             }
 6615:         }
 6616:         my %lt = &Apache::lonlocal::texthash (
 6617:                                                unauth   => 'Catalog type for unauthenticated users',
 6618:                                                auth     => 'Catalog type for authenticated users',
 6619:                                                none     => 'No catalog',
 6620:                                                std      => 'Standard catalog',
 6621:                                                domonly  => 'Domain-only catalog',
 6622:                                                codesrch => "Code search form",
 6623:                                              );
 6624:        my $itemcount = 0;
 6625:        foreach my $item (@catitems) {
 6626:            my $css_class = $itemcount%2? ' class="LC_odd_row"':'';
 6627:            $datatable .= '<tr '.$css_class.'>'.
 6628:                          '<td>'.$lt{$item}.'</td>'.
 6629:                          '<td class="LC_right_item"><span class="LC_nobreak">';
 6630:            foreach my $type (@cattypes) {
 6631:                my $ischecked;
 6632:                if ($checked{$item} eq $type) {
 6633:                    $ischecked=' checked="checked"';
 6634:                }
 6635:                $datatable .= '<label>'.
 6636:                              '<input type="radio" name="coursecat_'.$item.'" value="'.$type.'"'.$ischecked.
 6637:                              ' />'.$lt{$type}.'</label>&nbsp;';
 6638:            }
 6639:            $datatable .= '</td></tr>';
 6640:            $itemcount ++;
 6641:         }
 6642:         $$rowtotal += $itemcount;
 6643:     } elsif ($position eq 'middle') {
 6644:         my $toggle_cats_crs = ' ';
 6645:         my $toggle_cats_dom = ' checked="checked" ';
 6646:         my $can_cat_crs = ' ';
 6647:         my $can_cat_dom = ' checked="checked" ';
 6648:         my $toggle_catscomm_comm = ' ';
 6649:         my $toggle_catscomm_dom = ' checked="checked" ';
 6650:         my $can_catcomm_comm = ' ';
 6651:         my $can_catcomm_dom = ' checked="checked" ';
 6652:         my $toggle_catsplace_place = ' ';
 6653:         my $toggle_catsplace_dom = ' checked="checked" ';
 6654:         my $can_catplace_place = ' ';
 6655:         my $can_catplace_dom = ' checked="checked" ';
 6656: 
 6657:         if (ref($settings) eq 'HASH') {
 6658:             if ($settings->{'togglecats'} eq 'crs') {
 6659:                 $toggle_cats_crs = $toggle_cats_dom;
 6660:                 $toggle_cats_dom = ' ';
 6661:             }
 6662:             if ($settings->{'categorize'} eq 'crs') {
 6663:                 $can_cat_crs = $can_cat_dom;
 6664:                 $can_cat_dom = ' ';
 6665:             }
 6666:             if ($settings->{'togglecatscomm'} eq 'comm') {
 6667:                 $toggle_catscomm_comm = $toggle_catscomm_dom;
 6668:                 $toggle_catscomm_dom = ' ';
 6669:             }
 6670:             if ($settings->{'categorizecomm'} eq 'comm') {
 6671:                 $can_catcomm_comm = $can_catcomm_dom;
 6672:                 $can_catcomm_dom = ' ';
 6673:             }
 6674:             if ($settings->{'togglecatsplace'} eq 'place') {
 6675:                 $toggle_catsplace_place = $toggle_catsplace_dom;
 6676:                 $toggle_catsplace_dom = ' ';
 6677:             }
 6678:             if ($settings->{'categorizeplace'} eq 'place') {
 6679:                 $can_catplace_place = $can_catplace_dom;
 6680:                 $can_catplace_dom = ' ';
 6681:             }
 6682:         }
 6683:         my %title = &Apache::lonlocal::texthash (
 6684:                      togglecats      => 'Show/Hide a course in catalog',
 6685:                      togglecatscomm  => 'Show/Hide a community in catalog',
 6686:                      togglecatsplace => 'Show/Hide a placement test in catalog',
 6687:                      categorize      => 'Assign a category to a course',
 6688:                      categorizecomm  => 'Assign a category to a community',
 6689:                      categorizeplace => 'Assign a category to a placement test',
 6690:                     );
 6691:         my %level = &Apache::lonlocal::texthash (
 6692:                      dom   => 'Set in Domain',
 6693:                      crs   => 'Set in Course',
 6694:                      comm  => 'Set in Community',
 6695:                      place => 'Set in Placement Test',
 6696:                     );
 6697:         $datatable = '<tr class="LC_odd_row">'.
 6698:                   '<td>'.$title{'togglecats'}.'</td>'.
 6699:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6700:                   '<input type="radio" name="togglecats"'.
 6701:                   $toggle_cats_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6702:                   '<label><input type="radio" name="togglecats"'.
 6703:                   $toggle_cats_crs.' value="crs" />'.$level{'crs'}.'</label></span></td>'.
 6704:                   '</tr><tr>'.
 6705:                   '<td>'.$title{'categorize'}.'</td>'.
 6706:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6707:                   '<label><input type="radio" name="categorize"'.
 6708:                   $can_cat_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6709:                   '<label><input type="radio" name="categorize"'.
 6710:                   $can_cat_crs.'value="crs" />'.$level{'crs'}.'</label></span></td>'.
 6711:                   '</tr><tr class="LC_odd_row">'.
 6712:                   '<td>'.$title{'togglecatscomm'}.'</td>'.
 6713:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6714:                   '<input type="radio" name="togglecatscomm"'.
 6715:                   $toggle_catscomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6716:                   '<label><input type="radio" name="togglecatscomm"'.
 6717:                   $toggle_catscomm_comm.' value="comm" />'.$level{'comm'}.'</label></span></td>'.
 6718:                   '</tr><tr>'.
 6719:                   '<td>'.$title{'categorizecomm'}.'</td>'.
 6720:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6721:                   '<label><input type="radio" name="categorizecomm"'.
 6722:                   $can_catcomm_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6723:                   '<label><input type="radio" name="categorizecomm"'.
 6724:                   $can_catcomm_comm.'value="comm" />'.$level{'comm'}.'</label></span></td>'.
 6725:                   '</tr><tr>'.
 6726:                   '<td>'.$title{'togglecatsplace'}.'</td>'.
 6727:                   '<td class="LC_right_item"><span class="LC_nobreak"><label>'.
 6728:                   '<input type="radio" name="togglecatsplace"'.
 6729:                   $toggle_catsplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6730:                   '<label><input type="radio" name="togglecatscomm"'.
 6731:                   $toggle_catsplace_place.' value="comm" />'.$level{'place'}.'</label></span></td>'.
 6732:                   '</tr><tr>'.
 6733:                   '<td>'.$title{'categorizeplace'}.'</td>'.
 6734:                   '<td class="LC_right_item"><span class="LC_nobreak">'.
 6735:                   '<label><input type="radio" name="categorizeplace"'.
 6736:                   $can_catplace_dom.' value="dom" />'.$level{'dom'}.'</label>&nbsp;'.
 6737:                   '<label><input type="radio" name="categorizeplace"'.
 6738:                   $can_catplace_place.'value="place" />'.$level{'place'}.'</label></span></td>'.
 6739:                   '</tr>';
 6740:         $$rowtotal += 6;
 6741:     } else {
 6742:         my $css_class;
 6743:         my $itemcount = 1;
 6744:         my $cathash; 
 6745:         if (ref($settings) eq 'HASH') {
 6746:             $cathash = $settings->{'cats'};
 6747:         }
 6748:         if (ref($cathash) eq 'HASH') {
 6749:             my (@cats,@trails,%allitems,%idx,@jsarray);
 6750:             &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,
 6751:                                                    \%allitems,\%idx,\@jsarray);
 6752:             my $maxdepth = scalar(@cats);
 6753:             my $colattrib = '';
 6754:             if ($maxdepth > 2) {
 6755:                 $colattrib = ' colspan="2" ';
 6756:             }
 6757:             my @path;
 6758:             if (@cats > 0) {
 6759:                 if (ref($cats[0]) eq 'ARRAY') {
 6760:                     my $numtop = @{$cats[0]};
 6761:                     my $maxnum = $numtop;
 6762:                     my %default_names = (
 6763:                           instcode    => &mt('Official courses'),
 6764:                           communities => &mt('Communities'),
 6765:                           placement   => &mt('Placement Tests'),
 6766:                     );
 6767: 
 6768:                     if ((!grep(/^instcode$/,@{$cats[0]})) || 
 6769:                         ($cathash->{'instcode::0'} eq '') ||
 6770:                         (!grep(/^communities$/,@{$cats[0]})) || 
 6771:                         ($cathash->{'communities::0'} eq '') ||
 6772:                         (!grep(/^placement$/,@{$cats[0]})) ||
 6773:                         ($cathash->{'placement::0'} eq '')) {
 6774:                         $maxnum ++;
 6775:                     }
 6776:                     my $lastidx;
 6777:                     for (my $i=0; $i<$numtop; $i++) {
 6778:                         my $parent = $cats[0][$i];
 6779:                         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6780:                         my $item = &escape($parent).'::0';
 6781:                         my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$item','$idx{$item}'".');"';
 6782:                         $lastidx = $idx{$item};
 6783:                         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 6784:                                       .'<select name="'.$item.'"'.$chgstr.'>';
 6785:                         for (my $k=0; $k<=$maxnum; $k++) {
 6786:                             my $vpos = $k+1;
 6787:                             my $selstr;
 6788:                             if ($k == $i) {
 6789:                                 $selstr = ' selected="selected" ';
 6790:                             }
 6791:                             $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6792:                         }
 6793:                         $datatable .= '</select></span></td><td>';
 6794:                         if ($parent eq 'instcode' || $parent eq 'communities' || $parent eq 'placement') {
 6795:                             $datatable .=  '<span class="LC_nobreak">'
 6796:                                            .$default_names{$parent}.'</span>';
 6797:                             if ($parent eq 'instcode') {
 6798:                                 $datatable .= '<br /><span class="LC_nobreak">('
 6799:                                               .&mt('with institutional codes')
 6800:                                               .')</span></td><td'.$colattrib.'>';
 6801:                             } else {
 6802:                                 $datatable .= '<table><tr><td>';
 6803:                             }
 6804:                             $datatable .= '<span class="LC_nobreak">'
 6805:                                           .'<label><input type="radio" name="'
 6806:                                           .$parent.'" value="1" checked="checked" />'
 6807:                                           .&mt('Display').'</label>';
 6808:                             if ($parent eq 'instcode') {
 6809:                                 $datatable .= '&nbsp;';
 6810:                             } else {
 6811:                                 $datatable .= '</span></td></tr><tr><td>'
 6812:                                               .'<span class="LC_nobreak">';
 6813:                             }
 6814:                             $datatable .= '<label><input type="radio" name="'
 6815:                                           .$parent.'" value="0" />'
 6816:                                           .&mt('Do not display').'</label></span>';
 6817:                             if (($parent eq 'communities') || ($parent eq 'placement')) {
 6818:                                 $datatable .= '</td></tr></table>';
 6819:                             }
 6820:                             $datatable .= '</td>';
 6821:                         } else {
 6822:                             $datatable .= $parent
 6823:                                           .'&nbsp;<span class="LC_nobreak"><label>'
 6824:                                           .'<input type="checkbox" name="deletecategory" '
 6825:                                           .'value="'.$item.'" />'.&mt('Delete').'</label></span></td>';
 6826:                         }
 6827:                         my $depth = 1;
 6828:                         push(@path,$parent);
 6829:                         $datatable .= &build_category_rows($itemcount,\@cats,$depth,$parent,\@path,\%idx);
 6830:                         pop(@path);
 6831:                         $datatable .= '</tr><tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr>';
 6832:                         $itemcount ++;
 6833:                     }
 6834:                     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6835:                     my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','$lastidx'".');"';
 6836:                     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="addcategory_pos"'.$chgstr.'>';
 6837:                     for (my $k=0; $k<=$maxnum; $k++) {
 6838:                         my $vpos = $k+1;
 6839:                         my $selstr;
 6840:                         if ($k == $numtop) {
 6841:                             $selstr = ' selected="selected" ';
 6842:                         }
 6843:                         $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6844:                     }
 6845:                     $datatable .= '</select></span></td><td colspan="2">'.&mt('Add category:').'&nbsp;'
 6846:                                   .'<input type="text" size="20" name="addcategory_name" value="" /></td>'
 6847:                                   .'</tr>'."\n";
 6848:                     $itemcount ++;
 6849:                     foreach my $default ('instcode','communities','placement') {
 6850:                         if ((!grep(/^\Q$default\E$/,@{$cats[0]})) || ($cathash->{$default.'::0'} eq '')) {
 6851:                             $css_class = $itemcount%2?' class="LC_odd_row"':'';
 6852:                             my $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','$default"."_pos','$lastidx'".');"';
 6853:                             $datatable .= '<tr><td colspan="'.$maxdepth.'" class="LC_row_separator"></td></tr><tr '.$css_class.'><td>'.
 6854:                                           '<span class="LC_nobreak"><select name="'.$default.'_pos"'.$chgstr.'>';
 6855:                             for (my $k=0; $k<=$maxnum; $k++) {
 6856:                                 my $vpos = $k+1;
 6857:                                 my $selstr;
 6858:                                 if ($k == $maxnum) {
 6859:                                     $selstr = ' selected="selected" ';
 6860:                                 }
 6861:                                 $datatable .= '<option value="'.$k.'"'.$selstr.'>'.$vpos.'</option>';
 6862:                             }
 6863:                             $datatable .= '</select></span></td>'.
 6864:                                           '<td><span class="LC_nobreak">'.
 6865:                                           $default_names{$default}.'</span>';
 6866:                             if ($default eq 'instcode') {
 6867:                                 $datatable .= '<br /><span class="LC_nobreak">(' 
 6868:                                               .&mt('with institutional codes').')</span>';
 6869:                             }
 6870:                             $datatable .= '</td>'
 6871:                                           .'<td><span class="LC_nobreak"><label><input type="radio" name="'.$default.'" value="1" />'
 6872:                                           .&mt('Display').'</label>&nbsp;'
 6873:                                           .'<label><input type="radio" name="'.$default.'" value="0" checked="checked"/>'
 6874:                                           .&mt('Do not display').'</label></span></td></tr>';
 6875:                         }
 6876:                     }
 6877:                 }
 6878:             } else {
 6879:                 $datatable .= &initialize_categories($itemcount);
 6880:             }
 6881:         } else {
 6882:             $datatable .= '<td class="LC_right_item">'.$hdritem->{'header'}->[1]->{'col2'}.'</td>'
 6883:                           .&initialize_categories($itemcount);
 6884:         }
 6885:         $$rowtotal += $itemcount;
 6886:     }
 6887:     return $datatable;
 6888: }
 6889: 
 6890: sub print_serverstatuses {
 6891:     my ($dom,$settings,$rowtotal) = @_;
 6892:     my $datatable;
 6893:     my @pages = &serverstatus_pages();
 6894:     my (%namedaccess,%machineaccess);
 6895:     foreach my $type (@pages) {
 6896:         $namedaccess{$type} = '';
 6897:         $machineaccess{$type}= '';
 6898:     }
 6899:     if (ref($settings) eq 'HASH') {
 6900:         foreach my $type (@pages) {
 6901:             if (exists($settings->{$type})) {
 6902:                 if (ref($settings->{$type}) eq 'HASH') {
 6903:                     foreach my $key (keys(%{$settings->{$type}})) {
 6904:                         if ($key eq 'namedusers') {
 6905:                             $namedaccess{$type} = $settings->{$type}->{$key};
 6906:                         } elsif ($key eq 'machines') {
 6907:                             $machineaccess{$type} = $settings->{$type}->{$key};
 6908:                         }
 6909:                     }
 6910:                 }
 6911:             }
 6912:         }
 6913:     }
 6914:     my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
 6915:     my $rownum = 0;
 6916:     my $css_class;
 6917:     foreach my $type (@pages) {
 6918:         $rownum ++;
 6919:         $css_class = $rownum%2?' class="LC_odd_row"':'';
 6920:         $datatable .= '<tr'.$css_class.'>'.
 6921:                       '<td><span class="LC_nobreak">'.
 6922:                       $titles->{$type}.'</span></td>'.
 6923:                       '<td class="LC_left_item">'.
 6924:                       '<input type="text" name="'.$type.'_namedusers" '.
 6925:                       'value="'.$namedaccess{$type}.'" size="30" /></td>'.
 6926:                       '<td class="LC_right_item">'.
 6927:                       '<span class="LC_nobreak">'.
 6928:                       '<input type="text" name="'.$type.'_machines" '.
 6929:                       'value="'.$machineaccess{$type}.'" size="10" />'.
 6930:                       '</td></tr>'."\n";
 6931:     }
 6932:     $$rowtotal += $rownum;
 6933:     return $datatable;
 6934: }
 6935: 
 6936: sub serverstatus_pages {
 6937:     return ('userstatus','lonstatus','loncron','server-status','codeversions',
 6938:             'checksums','clusterstatus','certstatus','metadata_keywords',
 6939:             'metadata_harvest','takeoffline','takeonline','showenv','toggledebug',
 6940:             'ping','domconf','uniquecodes','diskusage','coursecatalog');
 6941: }
 6942: 
 6943: sub defaults_javascript {
 6944:     my ($settings) = @_;
 6945:     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.');
 6946:     my $intauthcost = &mt('Warning: bcrypt encryption cost for internal authentication must be an integer.');
 6947:     &js_escape(\$intauthcheck);
 6948:     &js_escape(\$intauthcost);
 6949:     my $intauthjs = <<"ENDSCRIPT";
 6950: 
 6951: function warnIntAuth(field) {
 6952:     if (field.name == 'intauth_check') {
 6953:         if (field.value == '2') {
 6954:             alert('$intauthcheck');
 6955:         }
 6956:     }
 6957:     if (field.name == 'intauth_cost') {
 6958:         field.value.replace(/\s/g,'');
 6959:         if (field.value != '') {
 6960:             var regexdigit=/^\\d+\$/;
 6961:             if (!regexdigit.test(field.value)) {
 6962:                 alert('$intauthcost');
 6963:             }
 6964:         }
 6965:     }
 6966:     return;
 6967: }
 6968: 
 6969: ENDSCRIPT
 6970: 
 6971:     if (ref($settings) ne 'HASH') {
 6972:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 6973:     }
 6974:     if ((ref($settings->{'inststatusorder'}) eq 'ARRAY') && (ref($settings->{'inststatustypes'}) eq 'HASH')) {
 6975:         my $maxnum = scalar(@{$settings->{'inststatusorder'}});
 6976:         if ($maxnum eq '') {
 6977:             $maxnum = 0;
 6978:         }
 6979:         $maxnum ++;
 6980:         my $jstext = '    var inststatuses = Array('."'".join("','",@{$settings->{'inststatusorder'}})."'".');';  
 6981:         return <<"ENDSCRIPT";
 6982: <script type="text/javascript">
 6983: // <![CDATA[
 6984: function reorderTypes(form,caller) {
 6985:     var changedVal;
 6986: $jstext 
 6987:     var newpos = 'addinststatus_pos';
 6988:     var current = new Array;
 6989:     var maxh = $maxnum;
 6990:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 6991:     var oldVal;
 6992:     if (caller == newpos) {
 6993:         changedVal = newitemVal;
 6994:     } else {
 6995:         var curritem = 'inststatus_pos_'+caller;
 6996:         changedVal = form.elements[curritem].options[form.elements[curritem].selectedIndex].value;
 6997:         current[newitemVal] = newpos;
 6998:     }
 6999:     for (var i=0; i<inststatuses.length; i++) {
 7000:         if (inststatuses[i] != caller) {
 7001:             var elementName = 'inststatus_pos_'+inststatuses[i];
 7002:             if (form.elements[elementName]) {
 7003:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 7004:                 current[currVal] = elementName;
 7005:             }
 7006:         }
 7007:     }
 7008:     for (var j=0; j<maxh; j++) {
 7009:         if (current[j] == undefined) {
 7010:             oldVal = j;
 7011:         }
 7012:     }
 7013:     if (oldVal < changedVal) {
 7014:         for (var k=oldVal+1; k<=changedVal ; k++) {
 7015:            var elementName = current[k];
 7016:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 7017:         }
 7018:     } else {
 7019:         for (var k=changedVal; k<oldVal; k++) {
 7020:             var elementName = current[k];
 7021:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 7022:         }
 7023:     }
 7024:     return;
 7025: }
 7026: 
 7027: $intauthjs
 7028: 
 7029: // ]]>
 7030: </script>
 7031: 
 7032: ENDSCRIPT
 7033:     } else {
 7034:         return &Apache::lonhtmlcommon::scripttag($intauthjs);
 7035:     }
 7036: }
 7037: 
 7038: sub coursecategories_javascript {
 7039:     my ($settings) = @_;
 7040:     my ($output,$jstext,$cathash);
 7041:     if (ref($settings) eq 'HASH') {
 7042:         $cathash = $settings->{'cats'};
 7043:     }
 7044:     if (ref($cathash) eq 'HASH') {
 7045:         my (@cats,@jsarray,%idx);
 7046:         &Apache::loncommon::gather_categories($cathash,\@cats,\%idx,\@jsarray);
 7047:         if (@jsarray > 0) {
 7048:             $jstext = '    var categories = Array('.scalar(@jsarray).');'."\n";
 7049:             for (my $i=0; $i<@jsarray; $i++) {
 7050:                 if (ref($jsarray[$i]) eq 'ARRAY') {
 7051:                     my $catstr = join('","',@{$jsarray[$i]});
 7052:                     $jstext .= '    categories['.$i.'] = Array("'.$catstr.'");'."\n";
 7053:                 }
 7054:             }
 7055:         }
 7056:     } else {
 7057:         $jstext  = '    var categories = Array(1);'."\n".
 7058:                    '    categories[0] = Array("instcode_pos");'."\n"; 
 7059:     }
 7060:     my $instcode_reserved = &mt('The name: [_1] is a reserved category.','"instcode"');
 7061:     my $communities_reserved = &mt('The name: [_1] is a reserved category.','"communities"');
 7062:     my $placement_reserved = &mt('The name: [_1] is a reserved category.','"placement"');
 7063:     my $choose_again = "\n".&mt('Please use a different name for the new top level category.'); 
 7064:     &js_escape(\$instcode_reserved);
 7065:     &js_escape(\$communities_reserved);
 7066:     &js_escape(\$placement_reserved);
 7067:     &js_escape(\$choose_again);
 7068:     $output = <<"ENDSCRIPT";
 7069: <script type="text/javascript">
 7070: // <![CDATA[
 7071: function reorderCats(form,parent,item,idx) {
 7072:     var changedVal;
 7073: $jstext
 7074:     var newpos = 'addcategory_pos';
 7075:     if (parent == '') {
 7076:         var has_instcode = 0;
 7077:         var maxtop = categories[idx].length;
 7078:         for (var j=0; j<maxtop; j++) {
 7079:             if (categories[idx][j] == 'instcode::0') {
 7080:                 has_instcode == 1;
 7081:             }
 7082:         }
 7083:         if (has_instcode == 0) {
 7084:             categories[idx][maxtop] = 'instcode_pos';
 7085:         }
 7086:     } else {
 7087:         newpos += '_'+parent;
 7088:     }
 7089:     var maxh = 1 + categories[idx].length;
 7090:     var current = new Array;
 7091:     var newitemVal = form.elements[newpos].options[form.elements[newpos].selectedIndex].value;
 7092:     if (item == newpos) {
 7093:         changedVal = newitemVal;
 7094:     } else {
 7095:         changedVal = form.elements[item].options[form.elements[item].selectedIndex].value;
 7096:         current[newitemVal] = newpos;
 7097:     }
 7098:     for (var i=0; i<categories[idx].length; i++) {
 7099:         var elementName = categories[idx][i];
 7100:         if (elementName != item) {
 7101:             if (form.elements[elementName]) {
 7102:                 var currVal = form.elements[elementName].options[form.elements[elementName].selectedIndex].value;
 7103:                 current[currVal] = elementName;
 7104:             }
 7105:         }
 7106:     }
 7107:     var oldVal;
 7108:     for (var j=0; j<maxh; j++) {
 7109:         if (current[j] == undefined) {
 7110:             oldVal = j;
 7111:         }
 7112:     }
 7113:     if (oldVal < changedVal) {
 7114:         for (var k=oldVal+1; k<=changedVal ; k++) {
 7115:            var elementName = current[k];
 7116:            form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex - 1;
 7117:         }
 7118:     } else {
 7119:         for (var k=changedVal; k<oldVal; k++) {
 7120:             var elementName = current[k];
 7121:             form.elements[elementName].selectedIndex = form.elements[elementName].selectedIndex + 1;
 7122:         }
 7123:     }
 7124:     return;
 7125: }
 7126: 
 7127: function categoryCheck(form) {
 7128:     if (form.elements['addcategory_name'].value == 'instcode') {
 7129:         alert('$instcode_reserved\\n$choose_again');
 7130:         return false;
 7131:     }
 7132:     if (form.elements['addcategory_name'].value == 'communities') {
 7133:         alert('$communities_reserved\\n$choose_again');
 7134:         return false;
 7135:     }
 7136:     if (form.elements['addcategory_name'].value == 'placement') {
 7137:         alert('$placement_reserved\\n$choose_again');
 7138:         return false;
 7139:     }
 7140:     return true;
 7141: }
 7142: 
 7143: // ]]>
 7144: </script>
 7145: 
 7146: ENDSCRIPT
 7147:     return $output;
 7148: }
 7149: 
 7150: sub initialize_categories {
 7151:     my ($itemcount) = @_;
 7152:     my ($datatable,$css_class,$chgstr);
 7153:     my %default_names = (
 7154:                       instcode    => 'Official courses (with institutional codes)',
 7155:                       communities => 'Communities',
 7156:                       placement   => 'Placement Tests',
 7157:                         );
 7158:     my $select0 = ' selected="selected"';
 7159:     my $select1 = '';
 7160:     foreach my $default ('instcode','communities','placement') {
 7161:         $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7162:         $chgstr = ' onchange="javascript:reorderCats(this.form,'."'',$default"."_pos','0'".');"';
 7163:         if (($default eq 'communities') || ($default eq 'placement')) {
 7164:             $select1 = $select0;
 7165:             $select0 = '';
 7166:         }
 7167:         $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7168:                      .'<select name="'.$default.'_pos">'
 7169:                      .'<option value="0"'.$select0.'>1</option>'
 7170:                      .'<option value="1"'.$select1.'>2</option>'
 7171:                      .'<option value="2">3</option></select>&nbsp;'
 7172:                      .$default_names{$default}
 7173:                      .'</span></td><td><span class="LC_nobreak">'
 7174:                      .'<label><input type="radio" name="'.$default.'" value="1" checked="checked" />'
 7175:                      .&mt('Display').'</label>&nbsp;<label>'
 7176:                      .'<input type="radio" name="'.$default.'" value="0" />'.&mt('Do not display')
 7177:                  .'</label></span></td></tr>';
 7178:         $itemcount ++;
 7179:     }
 7180:     $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7181:     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'','addcategory_pos','0'".');"';
 7182:     $datatable .= '<tr '.$css_class.'><td><span class="LC_nobreak">'
 7183:                   .'<select name="addcategory_pos"'.$chgstr.'>'
 7184:                   .'<option value="0">1</option>'
 7185:                   .'<option value="1">2</option>'
 7186:                   .'<option value="2" selected="selected">3</option></select>&nbsp;'
 7187:                   .&mt('Add category').'</td><td>'.&mt('Name:')
 7188:                   .'&nbsp;<input type="text" size="20" name="addcategory_name" value="" /></td></tr>';
 7189:     return $datatable;
 7190: }
 7191: 
 7192: sub build_category_rows {
 7193:     my ($itemcount,$cats,$depth,$parent,$path,$idx) = @_;
 7194:     my ($text,$name,$item,$chgstr);
 7195:     if (ref($cats) eq 'ARRAY') {
 7196:         my $maxdepth = scalar(@{$cats});
 7197:         if (ref($cats->[$depth]) eq 'HASH') {
 7198:             if (ref($cats->[$depth]{$parent}) eq 'ARRAY') {
 7199:                 my $numchildren = @{$cats->[$depth]{$parent}};
 7200:                 my $css_class = $itemcount%2?' class="LC_odd_row"':'';
 7201:                 $text .= '<td><table class="LC_data_table">';
 7202:                 my ($idxnum,$parent_name,$parent_item);
 7203:                 my $higher = $depth - 1;
 7204:                 if ($higher == 0) {
 7205:                     $parent_name = &escape($parent).'::'.$higher;
 7206:                 } else {
 7207:                     if (ref($path) eq 'ARRAY') {
 7208:                         $parent_name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7209:                     }
 7210:                 }
 7211:                 $parent_item = 'addcategory_pos_'.$parent_name;
 7212:                 for (my $j=0; $j<=$numchildren; $j++) {
 7213:                     if ($j < $numchildren) {
 7214:                         $name = $cats->[$depth]{$parent}[$j];
 7215:                         $item = &escape($name).':'.&escape($parent).':'.$depth;
 7216:                         $idxnum = $idx->{$item};
 7217:                     } else {
 7218:                         $name = $parent_name;
 7219:                         $item = $parent_item;
 7220:                     }
 7221:                     $chgstr = ' onchange="javascript:reorderCats(this.form,'."'$parent_name','$item','$idxnum'".');"';
 7222:                     $text .= '<tr '.$css_class.'><td><span class="LC_nobreak"><select name="'.$item.'"'.$chgstr.'>';
 7223:                     for (my $i=0; $i<=$numchildren; $i++) {
 7224:                         my $vpos = $i+1;
 7225:                         my $selstr;
 7226:                         if ($j == $i) {
 7227:                             $selstr = ' selected="selected" ';
 7228:                         }
 7229:                         $text .= '<option value="'.$i.'"'.$selstr.'>'.$vpos.'</option>';
 7230:                     }
 7231:                     $text .= '</select>&nbsp;';
 7232:                     if ($j < $numchildren) {
 7233:                         my $deeper = $depth+1;
 7234:                         $text .= $name.'&nbsp;'
 7235:                                  .'<label><input type="checkbox" name="deletecategory" value="'
 7236:                                  .$item.'" />'.&mt('Delete').'</label></span></td><td>';
 7237:                         if(ref($path) eq 'ARRAY') {
 7238:                             push(@{$path},$name);
 7239:                             $text .= &build_category_rows($itemcount,$cats,$deeper,$name,$path,$idx);
 7240:                             pop(@{$path});
 7241:                         }
 7242:                     } else {
 7243:                         $text .= &mt('Add subcategory:').'&nbsp;</span><input type="textbox" size="20" name="addcategory_name_';
 7244:                         if ($j == $numchildren) {
 7245:                             $text .= $name;
 7246:                         } else {
 7247:                             $text .= $item;
 7248:                         }
 7249:                         $text .= '" value="" />';
 7250:                     }
 7251:                     $text .= '</td></tr>';
 7252:                 }
 7253:                 $text .= '</table></td>';
 7254:             } else {
 7255:                 my $higher = $depth-1;
 7256:                 if ($higher == 0) {
 7257:                     $name = &escape($parent).'::'.$higher;
 7258:                 } else {
 7259:                     if (ref($path) eq 'ARRAY') {
 7260:                         $name = &escape($parent).':'.&escape($path->[-2]).':'.$higher;
 7261:                     }
 7262:                 }
 7263:                 my $colspan;
 7264:                 if ($parent ne 'instcode') {
 7265:                     $colspan = $maxdepth - $depth - 1;
 7266:                     $text .= '<td colspan="'.$colspan.'">'.&mt('Add subcategory:').'<input type="textbox" size="20" name="subcat_'.$name.'" value="" /></td>';
 7267:                 }
 7268:             }
 7269:         }
 7270:     }
 7271:     return $text;
 7272: }
 7273: 
 7274: sub modifiable_userdata_row {
 7275:     my ($context,$item,$settings,$numinrow,$rowcount,$usertypes,$fieldsref,$titlesref) = @_;
 7276:     my ($role,$rolename,$statustype);
 7277:     $role = $item;
 7278:     if ($context eq 'cancreate') {
 7279:         if ($item =~ /^emailusername_(.+)$/) {
 7280:             $statustype = $1;
 7281:             $role = 'emailusername';
 7282:             if (ref($usertypes) eq 'HASH') {
 7283:                 if ($usertypes->{$statustype}) {
 7284:                     $rolename = &mt('Data provided by [_1]',$usertypes->{$statustype});
 7285:                 } else {
 7286:                     $rolename = &mt('Data provided by user');
 7287:                 }
 7288:             }
 7289:         }
 7290:     } elsif ($context eq 'selfcreate') {
 7291:         if (ref($usertypes) eq 'HASH') {
 7292:             $rolename = $usertypes->{$role};
 7293:         } else {
 7294:             $rolename = $role;
 7295:         }
 7296:     } else {
 7297:         if ($role eq 'cr') {
 7298:             $rolename = &mt('Custom role');
 7299:         } else {
 7300:             $rolename = &Apache::lonnet::plaintext($role);
 7301:         }
 7302:     }
 7303:     my (@fields,%fieldtitles);
 7304:     if (ref($fieldsref) eq 'ARRAY') {
 7305:         @fields = @{$fieldsref};
 7306:     } else {
 7307:         @fields = ('lastname','firstname','middlename','generation',
 7308:                    'permanentemail','id');
 7309:     }
 7310:     if ((ref($titlesref) eq 'HASH')) {
 7311:         %fieldtitles = %{$titlesref};
 7312:     } else {
 7313:         %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
 7314:     }
 7315:     my $output;
 7316:     my $css_class = $rowcount%2?' class="LC_odd_row"':'';
 7317:     $output = '<tr '.$css_class.'>'.
 7318:               '<td><span class="LC_nobreak">'.$rolename.'</span></td>'.
 7319:               '<td class="LC_left_item" colspan="2"><table>';
 7320:     my $rem;
 7321:     my %checks;
 7322:     if (ref($settings) eq 'HASH') {
 7323:         if (ref($settings->{$context}) eq 'HASH') {
 7324:             if (ref($settings->{$context}->{$role}) eq 'HASH') {
 7325:                 my $hashref = $settings->{$context}->{$role};
 7326:                 if ($role eq 'emailusername') {
 7327:                     if ($statustype) {
 7328:                         if (ref($settings->{$context}->{$role}->{$statustype}) eq 'HASH') {
 7329:                             $hashref = $settings->{$context}->{$role}->{$statustype};
 7330:                             if (ref($hashref) eq 'HASH') { 
 7331:                                 foreach my $field (@fields) {
 7332:                                     if ($hashref->{$field}) {
 7333:                                         $checks{$field} = $hashref->{$field};
 7334:                                     }
 7335:                                 }
 7336:                             }
 7337:                         }
 7338:                     }
 7339:                 } else {
 7340:                     if (ref($hashref) eq 'HASH') {
 7341:                         foreach my $field (@fields) {
 7342:                             if ($hashref->{$field}) {
 7343:                                 $checks{$field} = ' checked="checked" ';
 7344:                             }
 7345:                         }
 7346:                     }
 7347:                 }
 7348:             }
 7349:         }
 7350:     }
 7351:      
 7352:     for (my $i=0; $i<@fields; $i++) {
 7353:         my $rem = $i%($numinrow);
 7354:         if ($rem == 0) {
 7355:             if ($i > 0) {
 7356:                 $output .= '</tr>';
 7357:             }
 7358:             $output .= '<tr>';
 7359:         }
 7360:         my $check = ' ';
 7361:         unless ($role eq 'emailusername') {
 7362:             if (exists($checks{$fields[$i]})) {
 7363:                 $check = $checks{$fields[$i]}
 7364:             } else {
 7365:                 if ($role eq 'st') {
 7366:                     if (ref($settings) ne 'HASH') {
 7367:                         $check = ' checked="checked" '; 
 7368:                     }
 7369:                 }
 7370:             }
 7371:         }
 7372:         $output .= '<td class="LC_left_item">'.
 7373:                    '<span class="LC_nobreak">';
 7374:         if ($role eq 'emailusername') {
 7375:             unless ($checks{$fields[$i]} =~ /^(required|optional)$/) {
 7376:                 $checks{$fields[$i]} = 'omit';
 7377:             }
 7378:             foreach my $option ('required','optional','omit') {
 7379:                 my $checked='';
 7380:                 if ($checks{$fields[$i]} eq $option) {
 7381:                     $checked='checked="checked" ';
 7382:                 }
 7383:                 $output .= '<label>'.
 7384:                            '<input type="radio" name="canmodify_'.$item.'_'.$fields[$i].'" value="'.$option.'" '.$checked.'/>'.
 7385:                            &mt($option).'</label>'.('&nbsp;' x2);
 7386:             }
 7387:             $output .= '<i>'.$fieldtitles{$fields[$i]}.'</i>';
 7388:         } else {
 7389:             $output .= '<label>'.
 7390:                        '<input type="checkbox" name="canmodify_'.$role.'" '.
 7391:                        'value="'.$fields[$i].'"'.$check.'/>'.$fieldtitles{$fields[$i]}.
 7392:                        '</label>';
 7393:         }
 7394:         $output .= '</span></td>';
 7395:         $rem = @fields%($numinrow);
 7396:     }
 7397:     my $colsleft = $numinrow - $rem;
 7398:     if ($colsleft > 1 ) {
 7399:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">'.
 7400:                    '&nbsp;</td>';
 7401:     } elsif ($colsleft == 1) {
 7402:         $output .= '<td class="LC_left_item">&nbsp;</td>';
 7403:     }
 7404:     $output .= '</tr></table></td></tr>';
 7405:     return $output;
 7406: }
 7407: 
 7408: sub insttypes_row {
 7409:     my ($settings,$types,$usertypes,$dom,$numinrow,$othertitle,$context,$rownum) = @_;
 7410:     my %lt = &Apache::lonlocal::texthash (
 7411:                       cansearch => 'Users allowed to search',
 7412:                       statustocreate => 'Institutional affiliation(s) able to create own account (login/SSO)',
 7413:                       lockablenames => 'User preference to lock name',
 7414:              );
 7415:     my $showdom;
 7416:     if ($context eq 'cansearch') {
 7417:         $showdom = ' ('.$dom.')';
 7418:     }
 7419:     my $class = 'LC_left_item';
 7420:     if ($context eq 'statustocreate') {
 7421:         $class = 'LC_right_item';
 7422:     }
 7423:     my $css_class = ' class="LC_odd_row"';
 7424:     if ($rownum ne '') { 
 7425:         $css_class = ($rownum%2? ' class="LC_odd_row"':'');
 7426:     }
 7427:     my $output = '<tr'.$css_class.'>'.
 7428:                  '<td>'.$lt{$context}.$showdom.
 7429:                  '</td><td class="'.$class.'" colspan="2"><table>';
 7430:     my $rem;
 7431:     if (ref($types) eq 'ARRAY') {
 7432:         for (my $i=0; $i<@{$types}; $i++) {
 7433:             if (defined($usertypes->{$types->[$i]})) {
 7434:                 my $rem = $i%($numinrow);
 7435:                 if ($rem == 0) {
 7436:                     if ($i > 0) {
 7437:                         $output .= '</tr>';
 7438:                     }
 7439:                     $output .= '<tr>';
 7440:                 }
 7441:                 my $check = ' ';
 7442:                 if (ref($settings) eq 'HASH') {
 7443:                     if (ref($settings->{$context}) eq 'ARRAY') {
 7444:                         if (grep(/^\Q$types->[$i]\E$/,@{$settings->{$context}})) {
 7445:                             $check = ' checked="checked" ';
 7446:                         }
 7447:                     } elsif ($context eq 'statustocreate') {
 7448:                         $check = ' checked="checked" ';
 7449:                     }
 7450:                 }
 7451:                 $output .= '<td class="LC_left_item">'.
 7452:                            '<span class="LC_nobreak"><label>'.
 7453:                            '<input type="checkbox" name="'.$context.'" '.
 7454:                            'value="'.$types->[$i].'"'.$check.'/>'.
 7455:                            $usertypes->{$types->[$i]}.'</label></span></td>';
 7456:             }
 7457:         }
 7458:         $rem = @{$types}%($numinrow);
 7459:     }
 7460:     my $colsleft = $numinrow - $rem;
 7461:     if (($rem == 0) && (@{$types} > 0)) {
 7462:         $output .= '<tr>';
 7463:     }
 7464:     if ($colsleft > 1) {
 7465:         $output .= '<td colspan="'.$colsleft.'" class="LC_left_item">';
 7466:     } else {
 7467:         $output .= '<td class="LC_left_item">';
 7468:     }
 7469:     my $defcheck = ' ';
 7470:     if (ref($settings) eq 'HASH') {  
 7471:         if (ref($settings->{$context}) eq 'ARRAY') {
 7472:             if (grep(/^default$/,@{$settings->{$context}})) {
 7473:                 $defcheck = ' checked="checked" ';
 7474:             }
 7475:         } elsif ($context eq 'statustocreate') {
 7476:             $defcheck = ' checked="checked" ';
 7477:         }
 7478:     }
 7479:     $output .= '<span class="LC_nobreak"><label>'.
 7480:                '<input type="checkbox" name="'.$context.'" '.
 7481:                'value="default"'.$defcheck.'/>'.
 7482:                $othertitle.'</label></span></td>'.
 7483:                '</tr></table></td></tr>';
 7484:     return $output;
 7485: }
 7486: 
 7487: sub sorted_searchtitles {
 7488:     my %searchtitles = &Apache::lonlocal::texthash(
 7489:                          'uname' => 'username',
 7490:                          'lastname' => 'last name',
 7491:                          'lastfirst' => 'last name, first name',
 7492:                      );
 7493:     my @titleorder = ('uname','lastname','lastfirst');
 7494:     return (\%searchtitles,\@titleorder);
 7495: }
 7496: 
 7497: sub sorted_searchtypes {
 7498:     my %srchtypes_desc = (
 7499:                            exact    => 'is exact match',
 7500:                            contains => 'contains ..',
 7501:                            begins   => 'begins with ..',
 7502:                          );
 7503:     my @srchtypeorder = ('exact','begins','contains');
 7504:     return (\%srchtypes_desc,\@srchtypeorder);
 7505: }
 7506: 
 7507: sub usertype_update_row {
 7508:     my ($settings,$usertypes,$fieldtitles,$fields,$types,$rownums) = @_;
 7509:     my $datatable;
 7510:     my $numinrow = 4;
 7511:     foreach my $type (@{$types}) {
 7512:         if (defined($usertypes->{$type})) {
 7513:             $$rownums ++;
 7514:             my $css_class = $$rownums%2?' class="LC_odd_row"':'';
 7515:             $datatable .= '<tr'.$css_class.'><td>'.$usertypes->{$type}.
 7516:                           '</td><td class="LC_left_item"><table>';
 7517:             for (my $i=0; $i<@{$fields}; $i++) {
 7518:                 my $rem = $i%($numinrow);
 7519:                 if ($rem == 0) {
 7520:                     if ($i > 0) {
 7521:                         $datatable .= '</tr>';
 7522:                     }
 7523:                     $datatable .= '<tr>';
 7524:                 }
 7525:                 my $check = ' ';
 7526:                 if (ref($settings) eq 'HASH') {
 7527:                     if (ref($settings->{'fields'}) eq 'HASH') {
 7528:                         if (ref($settings->{'fields'}{$type}) eq 'ARRAY') {
 7529:                             if (grep(/^\Q$fields->[$i]\E$/,@{$settings->{'fields'}{$type}})) {
 7530:                                 $check = ' checked="checked" ';
 7531:                             }
 7532:                         }
 7533:                     }
 7534:                 }
 7535: 
 7536:                 if ($i == @{$fields}-1) {
 7537:                     my $colsleft = $numinrow - $rem;
 7538:                     if ($colsleft > 1) {
 7539:                         $datatable .= '<td colspan="'.$colsleft.'">';
 7540:                     } else {
 7541:                         $datatable .= '<td>';
 7542:                     }
 7543:                 } else {
 7544:                     $datatable .= '<td>';
 7545:                 }
 7546:                 $datatable .= '<span class="LC_nobreak"><label>'.
 7547:                               '<input type="checkbox" name="updateable_'.$type.
 7548:                               '_'.$fields->[$i].'" value="1"'.$check.'/>'.
 7549:                               $fieldtitles->{$fields->[$i]}.'</label></span></td>';
 7550:             }
 7551:             $datatable .= '</tr></table></td></tr>';
 7552:         }
 7553:     }
 7554:     return $datatable;
 7555: }
 7556: 
 7557: sub modify_login {
 7558:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
 7559:     my ($resulttext,$errors,$colchgtext,%changes,%colchanges,%newfile,%newurl,
 7560:         %curr_loginvia,%loginhash,@currlangs,@newlangs,$addedfile,%title,@offon);
 7561:     %title = ( coursecatalog => 'Display course catalog',
 7562:                adminmail => 'Display administrator E-mail address',
 7563:                helpdesk  => 'Display "Contact Helpdesk" link',
 7564:                newuser => 'Link for visitors to create a user account',
 7565:                loginheader => 'Log-in box header');
 7566:     @offon = ('off','on');
 7567:     if (ref($domconfig{login}) eq 'HASH') {
 7568:         if (ref($domconfig{login}{loginvia}) eq 'HASH') {
 7569:             foreach my $lonhost (keys(%{$domconfig{login}{loginvia}})) {
 7570:                 $curr_loginvia{$lonhost} = $domconfig{login}{loginvia}{$lonhost};
 7571:             }
 7572:         }
 7573:     }
 7574:     ($errors,%colchanges) = &modify_colors($r,$dom,$confname,['login'],
 7575:                                            \%domconfig,\%loginhash);
 7576:     my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7577:     foreach my $item (@toggles) {
 7578:         $loginhash{login}{$item} = $env{'form.'.$item};
 7579:     }
 7580:     $loginhash{login}{loginheader} = $env{'form.loginheader'};
 7581:     if (ref($colchanges{'login'}) eq 'HASH') {  
 7582:         $colchgtext = &display_colorchgs($dom,\%colchanges,['login'],
 7583:                                          \%loginhash);
 7584:     }
 7585: 
 7586:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
 7587:     my %domservers = &Apache::lonnet::get_servers($dom);
 7588:     my @loginvia_attribs = ('serverpath','custompath','exempt');
 7589:     if (keys(%servers) > 1) {
 7590:         foreach my $lonhost (keys(%servers)) {
 7591:             next if ($env{'form.'.$lonhost.'_server'} eq $lonhost);
 7592:             if (ref($curr_loginvia{$lonhost}) eq 'HASH') {
 7593:                 if ($env{'form.'.$lonhost.'_server'} eq $curr_loginvia{$lonhost}{'server'}) {
 7594:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $curr_loginvia{$lonhost}{'server'};
 7595:                 } elsif ($curr_loginvia{$lonhost}{'server'} ne '') {
 7596:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7597:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7598:                         $changes{'loginvia'}{$lonhost} = 1;
 7599:                     } else {
 7600:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = '';
 7601:                         $changes{'loginvia'}{$lonhost} = 1;
 7602:                     }
 7603:                 } else {
 7604:                     if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7605:                         $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7606:                         $changes{'loginvia'}{$lonhost} = 1;
 7607:                     }
 7608:                 }
 7609:                 if ($loginhash{login}{loginvia}{$lonhost}{'server'} eq '') {
 7610:                     foreach my $item (@loginvia_attribs) {
 7611:                         $loginhash{login}{loginvia}{$lonhost}{$item} = '';
 7612:                     }
 7613:                 } else {
 7614:                     foreach my $item (@loginvia_attribs) {
 7615:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7616:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7617:                             $env{'form.'.$lonhost.'_custompath'} =~ s/\s+//g;
 7618:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7619:                                 $new = '/';
 7620:                             }
 7621:                         }
 7622:                         if (($item eq 'custompath') && 
 7623:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 7624:                             $new = '';
 7625:                         }
 7626:                         if ($new ne $curr_loginvia{$lonhost}{$item}) {
 7627:                             $changes{'loginvia'}{$lonhost} = 1;
 7628:                         }
 7629:                         if ($item eq 'exempt') {
 7630:                             $new = &check_exempt_addresses($new);
 7631:                         }
 7632:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 7633:                     }
 7634:                 }
 7635:             } else {
 7636:                 if (defined($servers{$env{'form.'.$lonhost.'_server'}})) {
 7637:                     $loginhash{login}{loginvia}{$lonhost}{'server'} = $env{'form.'.$lonhost.'_server'};
 7638:                     $changes{'loginvia'}{$lonhost} = 1;
 7639:                     foreach my $item (@loginvia_attribs) {
 7640:                         my $new = $env{'form.'.$lonhost.'_'.$item};
 7641:                         if (($item eq 'serverpath') && ($new eq 'custom')) {
 7642:                             if ($env{'form.'.$lonhost.'_custompath'} eq '') {
 7643:                                 $new = '/';
 7644:                             }
 7645:                         }
 7646:                         if (($item eq 'custompath') && 
 7647:                             ($env{'form.'.$lonhost.'_serverpath'} ne 'custom')) {
 7648:                             $new = '';
 7649:                         }
 7650:                         $loginhash{login}{loginvia}{$lonhost}{$item} = $new;
 7651:                     }
 7652:                 }
 7653:             }
 7654:         }
 7655:     }
 7656: 
 7657:     my $servadm = $r->dir_config('lonAdmEMail');
 7658:     my %langchoices = &Apache::lonlocal::texthash(&get_languages_hash());
 7659:     if (ref($domconfig{'login'}) eq 'HASH') {
 7660:         if (ref($domconfig{'login'}{'helpurl'}) eq 'HASH') {
 7661:             foreach my $lang (sort(keys(%{$domconfig{'login'}{'helpurl'}}))) {
 7662:                 if ($lang eq 'nolang') {
 7663:                     push(@currlangs,$lang);
 7664:                 } elsif (defined($langchoices{$lang})) {
 7665:                     push(@currlangs,$lang);
 7666:                 } else {
 7667:                     next;
 7668:                 }
 7669:             }
 7670:         }
 7671:     }
 7672:     my @delurls = &Apache::loncommon::get_env_multiple('form.loginhelpurl_del');
 7673:     if (@currlangs > 0) {
 7674:         foreach my $lang (@currlangs) {
 7675:             if (grep(/^\Q$lang\E$/,@delurls)) {
 7676:                 $changes{'helpurl'}{$lang} = 1;
 7677:             } elsif ($env{'form.loginhelpurl_'.$lang.'.filename'}) {
 7678:                 $changes{'helpurl'}{$lang} = 1;
 7679:                 $newfile{$lang} = $env{'form.loginhelpurl_'.$lang.'.filename'};
 7680:                 push(@newlangs,$lang);
 7681:             } else {
 7682:                 $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 7683:             }
 7684:         }
 7685:     }
 7686:     unless (grep(/^nolang$/,@currlangs)) {
 7687:         if ($env{'form.loginhelpurl_nolang.filename'}) {
 7688:             $changes{'helpurl'}{'nolang'} = 1;
 7689:             $newfile{'nolang'} = $env{'form.loginhelpurl_nolang.filename'};
 7690:             push(@newlangs,'nolang');
 7691:         }
 7692:     }
 7693:     if ($env{'form.loginhelpurl_add_lang'}) {
 7694:         if ((defined($langchoices{$env{'form.loginhelpurl_add_lang'}})) &&
 7695:             ($env{'form.loginhelpurl_add_file.filename'})) {
 7696:             $newfile{$env{'form.loginhelpurl_add_lang'}} = $env{'form.loginhelpurl_add_file.filename'};
 7697:             $addedfile = $env{'form.loginhelpurl_add_lang'};
 7698:         }
 7699:     }
 7700:     if ((@newlangs > 0) || ($addedfile)) {
 7701:         my $error;
 7702:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7703:         if ($configuserok eq 'ok') {
 7704:             if ($switchserver) {
 7705:                 $error = &mt("Upload of custom help file is not permitted to this server: [_1]",$switchserver);
 7706:             } elsif ($author_ok eq 'ok') {
 7707:                 my @allnew = @newlangs;
 7708:                 if ($addedfile ne '') {
 7709:                     push(@allnew,$addedfile);
 7710:                 }
 7711:                 foreach my $lang (@allnew) {
 7712:                     my $formelem = 'loginhelpurl_'.$lang;
 7713:                     if ($lang eq $env{'form.loginhelpurl_add_lang'}) {
 7714:                         $formelem = 'loginhelpurl_add_file';
 7715:                     }
 7716:                     (my $result,$newurl{$lang}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 7717:                                                                "help/$lang",'','',$newfile{$lang});
 7718:                     if ($result eq 'ok') {
 7719:                         $loginhash{'login'}{'helpurl'}{$lang} = $newurl{$lang};
 7720:                         $changes{'helpurl'}{$lang} = 1;
 7721:                     } else {
 7722:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$newfile{$lang},$result);
 7723:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 7724:                         if ((grep(/^\Q$lang\E$/,@currlangs)) &&
 7725:                             (!grep(/^\Q$lang\E$/,@delurls))) {
 7726:                             $loginhash{'login'}{'helpurl'}{$lang} = $domconfig{'login'}{'helpurl'}{$lang};
 7727:                         }
 7728:                     }
 7729:                 }
 7730:             } else {
 7731:                 $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);
 7732:             }
 7733:         } else {
 7734:             $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);
 7735:         }
 7736:         if ($error) {
 7737:             &Apache::lonnet::logthis($error);
 7738:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7739:         }
 7740:     }
 7741: 
 7742:     my (%currheadtagurls,%currexempt,@newhosts,%newheadtagurls,%possexempt);
 7743:     if (ref($domconfig{'login'}) eq 'HASH') {
 7744:         if (ref($domconfig{'login'}{'headtag'}) eq 'HASH') {
 7745:             foreach my $lonhost (keys(%{$domconfig{'login'}{'headtag'}})) {
 7746:                 if ($domservers{$lonhost}) {
 7747:                     if (ref($domconfig{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 7748:                         $currheadtagurls{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'url'};
 7749:                         $currexempt{$lonhost} = $domconfig{'login'}{'headtag'}{$lonhost}{'exempt'};
 7750:                     }
 7751:                 }
 7752:             }
 7753:         }
 7754:     }
 7755:     my @delheadtagurls = &Apache::loncommon::get_env_multiple('form.loginheadtag_del');
 7756:     foreach my $lonhost (sort(keys(%domservers))) {
 7757:         if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 7758:             $changes{'headtag'}{$lonhost} = 1;
 7759:         } else {
 7760:             if ($env{'form.loginheadtagexempt_'.$lonhost}) {
 7761:                 $possexempt{$lonhost} = &check_exempt_addresses($env{'form.loginheadtagexempt_'.$lonhost});
 7762:             }
 7763:             if ($env{'form.loginheadtag_'.$lonhost.'.filename'}) {
 7764:                 push(@newhosts,$lonhost);
 7765:             } elsif ($currheadtagurls{$lonhost}) {
 7766:                 $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $currheadtagurls{$lonhost};
 7767:                 if ($currexempt{$lonhost}) {
 7768:                     if ((!exists($possexempt{$lonhost})) || ($possexempt{$lonhost} ne $currexempt{$lonhost})) {
 7769:                         $changes{'headtag'}{$lonhost} = 1;
 7770:                     }
 7771:                 } elsif ($possexempt{$lonhost}) {
 7772:                     $changes{'headtag'}{$lonhost} = 1;
 7773:                 }
 7774:                 if ($possexempt{$lonhost}) {
 7775:                     $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 7776:                 }
 7777:             }
 7778:         }
 7779:     }
 7780:     if (@newhosts) {
 7781:         my $error;
 7782:         my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 7783:         if ($configuserok eq 'ok') {
 7784:             if ($switchserver) {
 7785:                 $error = &mt("Upload of custom markup is not permitted to this server: [_1]",$switchserver);
 7786:             } elsif ($author_ok eq 'ok') {
 7787:                 foreach my $lonhost (@newhosts) {
 7788:                     my $formelem = 'loginheadtag_'.$lonhost;
 7789:                     (my $result,$newheadtagurls{$lonhost}) = &publishlogo($r,'upload',$formelem,$dom,$confname,
 7790:                                                                           "login/headtag/$lonhost",'','',
 7791:                                                                           $env{'form.loginheadtag_'.$lonhost.'.filename'});
 7792:                     if ($result eq 'ok') {
 7793:                         $loginhash{'login'}{'headtag'}{$lonhost}{'url'} = $newheadtagurls{$lonhost};
 7794:                         $changes{'headtag'}{$lonhost} = 1;
 7795:                         if ($possexempt{$lonhost}) {
 7796:                             $loginhash{'login'}{'headtag'}{$lonhost}{'exempt'} = $possexempt{$lonhost};
 7797:                         }
 7798:                     } else {
 7799:                         my $puberror = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",
 7800:                                            $newheadtagurls{$lonhost},$result);
 7801:                         $errors .= '<li><span class="LC_error">'.$puberror.'</span></li>';
 7802:                         if ((grep(/^\Q$lonhost\E$/,keys(%currheadtagurls))) &&
 7803:                             (!grep(/^\Q$lonhost\E$/,@delheadtagurls))) {
 7804:                             $loginhash{'login'}{'headtag'}{$lonhost} = $currheadtagurls{$lonhost};
 7805:                         }
 7806:                     }
 7807:                 }
 7808:             } else {
 7809:                 $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);
 7810:             }
 7811:         } else {
 7812:             $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);
 7813:         }
 7814:         if ($error) {
 7815:             &Apache::lonnet::logthis($error);
 7816:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 7817:         }
 7818:     }
 7819:     &process_captcha('login',\%changes,$loginhash{'login'},$domconfig{'login'});
 7820: 
 7821:     my $defaulthelpfile = '/adm/loginproblems.html';
 7822:     my $defaulttext = &mt('Default in use');
 7823: 
 7824:     my $putresult = &Apache::lonnet::put_dom('configuration',\%loginhash,
 7825:                                              $dom);
 7826:     if ($putresult eq 'ok') {
 7827:         my @toggles = ('coursecatalog','adminmail','helpdesk','newuser');
 7828:         my %defaultchecked = (
 7829:                     'coursecatalog' => 'on',
 7830:                     'helpdesk'      => 'on',
 7831:                     'adminmail'     => 'off',
 7832:                     'newuser'       => 'off',
 7833:         );
 7834:         if (ref($domconfig{'login'}) eq 'HASH') {
 7835:             foreach my $item (@toggles) {
 7836:                 if ($defaultchecked{$item} eq 'on') { 
 7837:                     if (($domconfig{'login'}{$item} eq '0') &&
 7838:                         ($env{'form.'.$item} eq '1')) {
 7839:                         $changes{$item} = 1;
 7840:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 7841:                               $domconfig{'login'}{$item} eq '1') &&
 7842:                              ($env{'form.'.$item} eq '0')) {
 7843:                         $changes{$item} = 1;
 7844:                     }
 7845:                 } elsif ($defaultchecked{$item} eq 'off') {
 7846:                     if (($domconfig{'login'}{$item} eq '1') &&
 7847:                         ($env{'form.'.$item} eq '0')) {
 7848:                         $changes{$item} = 1;
 7849:                     } elsif (($domconfig{'login'}{$item} eq '' ||
 7850:                               $domconfig{'login'}{$item} eq '0') &&
 7851:                              ($env{'form.'.$item} eq '1')) {
 7852:                         $changes{$item} = 1;
 7853:                     }
 7854:                 }
 7855:             }
 7856:         }
 7857:         if (keys(%changes) > 0 || $colchgtext) {
 7858:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 7859:             if (ref($lastactref) eq 'HASH') {
 7860:                 $lastactref->{'domainconfig'} = 1;
 7861:             }
 7862:             $resulttext = &mt('Changes made:').'<ul>';
 7863:             foreach my $item (sort(keys(%changes))) {
 7864:                 if ($item eq 'loginvia') {
 7865:                     if (ref($changes{$item}) eq 'HASH') {
 7866:                         $resulttext .= '<li>'.&mt('Log-in page availability:').'<ul>';
 7867:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 7868:                             if (defined($servers{$loginhash{login}{loginvia}{$lonhost}{'server'}})) {
 7869:                                 if (ref($loginhash{login}{loginvia}{$lonhost}) eq 'HASH') {
 7870:                                     my $protocol = $Apache::lonnet::protocol{$env{'form.'.$lonhost.'_server'}};
 7871:                                     $protocol = 'http' if ($protocol ne 'https');
 7872:                                     my $target = $protocol.'://'.$servers{$env{'form.'.$lonhost.'_server'}};
 7873: 
 7874:                                     if ($loginhash{login}{loginvia}{$lonhost}{'serverpath'} eq 'custom') {
 7875:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'custompath'};
 7876:                                     } else {
 7877:                                         $target .= $loginhash{login}{loginvia}{$lonhost}{'serverpath'}; 
 7878:                                     }
 7879:                                     $resulttext .= '<li>'.&mt('Server: [_1] log-in page redirects to [_2].',$servers{$lonhost},'<a href="'.$target.'">'.$target.'</a>');
 7880:                                     if ($loginhash{login}{loginvia}{$lonhost}{'exempt'} ne '') {
 7881:                                         $resulttext .= '&nbsp;'.&mt('No redirection for clients from following IPs:').'&nbsp;'.$loginhash{login}{loginvia}{$lonhost}{'exempt'};
 7882:                                     }
 7883:                                     $resulttext .= '</li>';
 7884:                                 } else {
 7885:                                     $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$lonhost).'</li>';
 7886:                                 }
 7887:                             } else {
 7888:                                 $resulttext .= '<li>'.&mt('Server: [_1] has standard log-in page.',$servers{$lonhost}).'</li>';
 7889:                             }
 7890:                         }
 7891:                         $resulttext .= '</ul></li>';
 7892:                     }
 7893:                 } elsif ($item eq 'helpurl') {
 7894:                     if (ref($changes{$item}) eq 'HASH') {
 7895:                         foreach my $lang (sort(keys(%{$changes{$item}}))) {
 7896:                             if (grep(/^\Q$lang\E$/,@delurls)) {
 7897:                                 my ($chg,$link);
 7898:                                 $link = &Apache::loncommon::modal_link($defaulthelpfile,$defaulttext,600,500);
 7899:                                 if ($lang eq 'nolang') {
 7900:                                     $chg = &mt('custom log-in help file removed for no preferred language; [_1]',$link);
 7901:                                 } else {
 7902:                                     $chg = &mt('custom log-in help file removed for specific language: [_1]; [_2]',$langchoices{$lang},$link);
 7903:                                 }
 7904:                                 $resulttext .= '<li>'.$chg.'</li>';
 7905:                             } else {
 7906:                                 my $chg;
 7907:                                 if ($lang eq 'nolang') {
 7908:                                     $chg = &mt('custom log-in help file for no preferred language');
 7909:                                 } else {
 7910:                                     $chg = &mt('custom log-in help file for specific language: [_1]',$langchoices{$lang});
 7911:                                 }
 7912:                                 $resulttext .= '<li>'.&Apache::loncommon::modal_link(
 7913:                                                       $loginhash{'login'}{'helpurl'}{$lang}.
 7914:                                                       '?inhibitmenu=yes',$chg,600,500).
 7915:                                                '</li>';
 7916:                             }
 7917:                         }
 7918:                     }
 7919:                 } elsif ($item eq 'headtag') {
 7920:                     if (ref($changes{$item}) eq 'HASH') {
 7921:                         foreach my $lonhost (sort(keys(%{$changes{$item}}))) {
 7922:                             if (grep(/^\Q$lonhost\E$/,@delheadtagurls)) {
 7923:                                 $resulttext .= '<li>'.&mt('custom markup file removed for [_1]',$domservers{$lonhost}).'</li>';
 7924:                             } elsif (ref($loginhash{'login'}{'headtag'}{$lonhost}) eq 'HASH') {
 7925:                                 $resulttext .= '<li><a href="'.
 7926:                                                "javascript:void(open('$loginhash{'login'}{'headtag'}{$lonhost}{'url'}?inhibitmenu=yes','Custom_HeadTag',
 7927:                                                'menubar=0,toolbar=1,scrollbars=1,width=600,height=500,resizable=yes'))".
 7928:                                                '">'.&mt('custom markup').'</a> '.&mt('(for [_1])',$servers{$lonhost}).' ';
 7929:                                 if ($possexempt{$lonhost}) {
 7930:                                     $resulttext .= &mt('not included for client IP(s): [_1]',$possexempt{$lonhost});
 7931:                                 } else {
 7932:                                     $resulttext .= &mt('included for any client IP');
 7933:                                 }
 7934:                                 $resulttext .= '</li>';
 7935:                             }
 7936:                         }
 7937:                     }
 7938:                 } elsif ($item eq 'captcha') {
 7939:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7940:                         my $chgtxt;
 7941:                         if ($loginhash{'login'}{$item} eq 'notused') {
 7942:                             $chgtxt .= &mt('No CAPTCHA validation in use for helpdesk form.');
 7943:                         } else {
 7944:                             my %captchas = &captcha_phrases();
 7945:                             if ($captchas{$loginhash{'login'}{$item}}) {
 7946:                                 $chgtxt .= &mt("Validation for helpdesk form set to $captchas{$loginhash{'login'}{$item}}.");
 7947:                             } else {
 7948:                                 $chgtxt .= &mt('Validation for helpdesk form set to unknown type.');
 7949:                             }
 7950:                         }
 7951:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 7952:                     }
 7953:                 } elsif ($item eq 'recaptchakeys') {
 7954:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7955:                         my ($privkey,$pubkey);
 7956:                         if (ref($loginhash{'login'}{$item}) eq 'HASH') {
 7957:                             $pubkey = $loginhash{'login'}{$item}{'public'};
 7958:                             $privkey = $loginhash{'login'}{$item}{'private'};
 7959:                         }
 7960:                         my $chgtxt .= &mt('ReCAPTCHA keys changes').'<ul>';
 7961:                         if (!$pubkey) {
 7962:                             $chgtxt .= '<li>'.&mt('Public key deleted').'</li>';
 7963:                         } else {
 7964:                             $chgtxt .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
 7965:                         }
 7966:                         if (!$privkey) {
 7967:                             $chgtxt .= '<li>'.&mt('Private key deleted').'</li>';
 7968:                         } else {
 7969:                             $chgtxt .= '<li>'.&mt('Private key set to [_1]',$privkey).'</li>';
 7970:                         }
 7971:                         $chgtxt .= '</ul>';
 7972:                         $resulttext .= '<li>'.$chgtxt.'</li>';
 7973:                     }
 7974:                 } elsif ($item eq 'recaptchaversion') {
 7975:                     if (ref($loginhash{'login'}) eq 'HASH') {
 7976:                         if ($loginhash{'login'}{'captcha'} eq 'recaptcha') {
 7977:                             $resulttext .= '<li>'.&mt('ReCAPTCHA for helpdesk form set to version [_1]',$loginhash{'login'}{'recaptchaversion'}).
 7978:                                            '</li>';
 7979:                         }
 7980:                     }
 7981:                 } else {
 7982:                     $resulttext .= '<li>'.&mt("$title{$item} set to $offon[$env{'form.'.$item}]").'</li>';
 7983:                 }
 7984:             }
 7985:             $resulttext .= $colchgtext.'</ul>';
 7986:         } else {
 7987:             $resulttext = &mt('No changes made to log-in page settings');
 7988:         }
 7989:     } else {
 7990:         $resulttext = '<span class="LC_error">'.
 7991: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 7992:     }
 7993:     if ($errors) {
 7994:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
 7995:                        $errors.'</ul>';
 7996:     }
 7997:     return $resulttext;
 7998: }
 7999: 
 8000: sub check_exempt_addresses {
 8001:     my ($iplist) = @_;
 8002:     $iplist =~ s/^\s+//;
 8003:     $iplist =~ s/\s+$//;
 8004:     my @poss_ips = split(/\s*[,:]\s*/,$iplist);
 8005:     my (@okips,$new);
 8006:     foreach my $ip (@poss_ips) {
 8007:         if ($ip =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) {
 8008:             if (($1 <= 255) && ($2 <= 255) && ($3 <= 255) && ($4 <= 255)) {
 8009:                 push(@okips,$ip);
 8010:             }
 8011:         }
 8012:     }
 8013:     if (@okips > 0) {
 8014:         $new = join(',',@okips);
 8015:     } else {
 8016:         $new = '';
 8017:     }
 8018:     return $new;
 8019: }
 8020: 
 8021: sub color_font_choices {
 8022:     my %choices =
 8023:         &Apache::lonlocal::texthash (
 8024:             img => "Header",
 8025:             bgs => "Background colors",
 8026:             links => "Link colors",
 8027:             images => "Images",
 8028:             font => "Font color",
 8029:             fontmenu => "Font menu",
 8030:             pgbg => "Page",
 8031:             tabbg => "Header",
 8032:             sidebg => "Border",
 8033:             link => "Link",
 8034:             alink => "Active link",
 8035:             vlink => "Visited link",
 8036:         );
 8037:     return %choices;
 8038: }
 8039: 
 8040: sub modify_rolecolors {
 8041:     my ($r,$dom,$confname,$roles,$lastactref,%domconfig) = @_;
 8042:     my ($resulttext,%rolehash);
 8043:     $rolehash{'rolecolors'} = {};
 8044:     if (ref($domconfig{'rolecolors'}) ne 'HASH') {
 8045:         if ($domconfig{'rolecolors'} eq '') {
 8046:             $domconfig{'rolecolors'} = {};
 8047:         }
 8048:     }
 8049:     my ($errors,%changes) = &modify_colors($r,$dom,$confname,$roles,
 8050:                          $domconfig{'rolecolors'},$rolehash{'rolecolors'});
 8051:     my $putresult = &Apache::lonnet::put_dom('configuration',\%rolehash,
 8052:                                              $dom);
 8053:     if ($putresult eq 'ok') {
 8054:         if (keys(%changes) > 0) {
 8055:             &Apache::loncommon::devalidate_domconfig_cache($dom);
 8056:             if (ref($lastactref) eq 'HASH') {
 8057:                 $lastactref->{'domainconfig'} = 1;
 8058:             }
 8059:             $resulttext = &display_colorchgs($dom,\%changes,$roles,
 8060:                                              $rolehash{'rolecolors'});
 8061:         } else {
 8062:             $resulttext = &mt('No changes made to default color schemes');
 8063:         }
 8064:     } else {
 8065:         $resulttext = '<span class="LC_error">'.
 8066: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 8067:     }
 8068:     if ($errors) {
 8069:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 8070:                        $errors.'</ul>';
 8071:     }
 8072:     return $resulttext;
 8073: }
 8074: 
 8075: sub modify_colors {
 8076:     my ($r,$dom,$confname,$roles,$domconfig,$confhash) = @_;
 8077:     my (%changes,%choices);
 8078:     my @bgs;
 8079:     my @links = ('link','alink','vlink');
 8080:     my @logintext;
 8081:     my @images;
 8082:     my $servadm = $r->dir_config('lonAdmEMail');
 8083:     my $errors;
 8084:     my %defaults;
 8085:     foreach my $role (@{$roles}) {
 8086:         if ($role eq 'login') {
 8087:             %choices = &login_choices();
 8088:             @logintext = ('textcol','bgcol');
 8089:         } else {
 8090:             %choices = &color_font_choices();
 8091:         }
 8092:         if ($role eq 'login') {
 8093:             @images = ('img','logo','domlogo','login');
 8094:             @bgs = ('pgbg','mainbg','sidebg');
 8095:         } else {
 8096:             @images = ('img');
 8097:             @bgs = ('pgbg','tabbg','sidebg');
 8098:         }
 8099:         my %defaults = &role_defaults($role,\@bgs,\@links,\@images,\@logintext);
 8100:         unless ($env{'form.'.$role.'_font'} eq $defaults{'font'}) {
 8101:             $confhash->{$role}{'font'} = $env{'form.'.$role.'_font'};
 8102:         }
 8103:         if ($role eq 'login') {
 8104:             foreach my $item (@logintext) {
 8105:                 $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8106:                 if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8107:                     $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8108:                 }
 8109:                 unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'logintext'}{$item})) {
 8110:                     $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8111:                 }
 8112:             }
 8113:         } else {
 8114:             $env{'form.'.$role.'_fontmenu'} = lc($env{'form.'.$role.'_fontmenu'});
 8115:             if ($env{'form.'.$role.'_fontmenu'} =~ /^\w+/) {
 8116:                 $env{'form.'.$role.'_fontmenu'} = '#'.$env{'form.'.$role.'_fontmenu'};
 8117:             }
 8118:             unless($env{'form.'.$role.'_fontmenu'} eq lc($defaults{'fontmenu'})) {
 8119:                 $confhash->{$role}{'fontmenu'} = $env{'form.'.$role.'_fontmenu'};
 8120:             }
 8121:         }
 8122:         foreach my $item (@bgs) {
 8123:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8124:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8125:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8126:             }
 8127:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'bgs'}{$item})) {
 8128:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8129:             }
 8130:         }
 8131:         foreach my $item (@links) {
 8132:             $env{'form.'.$role.'_'.$item} = lc($env{'form.'.$role.'_'.$item});
 8133:             if ($env{'form.'.$role.'_'.$item} =~ /^\w+/) {
 8134:                 $env{'form.'.$role.'_'.$item} = '#'.$env{'form.'.$role.'_'.$item};
 8135:             }
 8136:             unless ($env{'form.'.$role.'_'.$item} eq lc($defaults{'links'}{$item})) {
 8137:                 $confhash->{$role}{$item} = $env{'form.'.$role.'_'.$item};
 8138:             }
 8139:         }
 8140:         my ($configuserok,$author_ok,$switchserver) = 
 8141:             &config_check($dom,$confname,$servadm);
 8142:         my ($width,$height) = &thumb_dimensions();
 8143:         if (ref($domconfig->{$role}) ne 'HASH') {
 8144:             $domconfig->{$role} = {};
 8145:         }
 8146:         foreach my $img (@images) {
 8147:             if (($role eq 'login') && (($img eq 'img') || ($img eq 'logo'))) {  
 8148:                 if (defined($env{'form.login_showlogo_'.$img})) {
 8149:                     $confhash->{$role}{'showlogo'}{$img} = 1;
 8150:                 } else { 
 8151:                     $confhash->{$role}{'showlogo'}{$img} = 0;
 8152:                 }
 8153:             } 
 8154: 	    if ( ! $env{'form.'.$role.'_'.$img.'.filename'} 
 8155: 		 && !defined($domconfig->{$role}{$img})
 8156: 		 && !$env{'form.'.$role.'_del_'.$img}
 8157: 		 && $env{'form.'.$role.'_import_'.$img}) {
 8158: 		# import the old configured image from the .tab setting
 8159: 		# if they haven't provided a new one 
 8160: 		$domconfig->{$role}{$img} = 
 8161: 		    $env{'form.'.$role.'_import_'.$img};
 8162: 	    }
 8163:             if ($env{'form.'.$role.'_'.$img.'.filename'} ne '') {
 8164:                 my $error;
 8165:                 if ($configuserok eq 'ok') {
 8166:                     if ($switchserver) {
 8167:                         $error = &mt("Upload of [_1] image for $role page(s) is not permitted to this server: [_2]",$choices{$img},$switchserver);
 8168:                     } else {
 8169:                         if ($author_ok eq 'ok') {
 8170:                             my ($result,$logourl) = 
 8171:                                 &publishlogo($r,'upload',$role.'_'.$img,
 8172:                                            $dom,$confname,$img,$width,$height);
 8173:                             if ($result eq 'ok') {
 8174:                                 $confhash->{$role}{$img} = $logourl;
 8175:                                 $changes{$role}{'images'}{$img} = 1;
 8176:                             } else {
 8177:                                 $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);
 8178:                             }
 8179:                         } else {
 8180:                             $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);
 8181:                         }
 8182:                     }
 8183:                 } else {
 8184:                     $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);
 8185:                 }
 8186:                 if ($error) {
 8187:                     &Apache::lonnet::logthis($error);
 8188:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8189:                 }
 8190:             } elsif ($domconfig->{$role}{$img} ne '') {
 8191:                 if ($domconfig->{$role}{$img} !~ m-^(/res/\Q$dom\E/\Q$confname\E/\Q$img\E)/([^/]+)$-) {
 8192:                     my $error;
 8193:                     if ($configuserok eq 'ok') {
 8194: # is confname an author?
 8195:                         if ($switchserver eq '') {
 8196:                             if ($author_ok eq 'ok') {
 8197:                                 my ($result,$logourl) = 
 8198:                                &publishlogo($r,'copy',$domconfig->{$role}{$img},
 8199:                                             $dom,$confname,$img,$width,$height);
 8200:                                 if ($result eq 'ok') {
 8201:                                     $confhash->{$role}{$img} = $logourl;
 8202: 				    $changes{$role}{'images'}{$img} = 1;
 8203:                                 }
 8204:                             }
 8205:                         }
 8206:                     }
 8207:                 }
 8208:             }
 8209:         }
 8210:         if (ref($domconfig) eq 'HASH') {
 8211:             if (ref($domconfig->{$role}) eq 'HASH') {
 8212:                 foreach my $img (@images) {
 8213:                     if ($domconfig->{$role}{$img} ne '') {
 8214:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8215:                             $confhash->{$role}{$img} = '';
 8216:                             $changes{$role}{'images'}{$img} = 1;
 8217:                         } else {
 8218:                             if ($confhash->{$role}{$img} eq '') {
 8219:                                 $confhash->{$role}{$img} = $domconfig->{$role}{$img};
 8220:                             }
 8221:                         }
 8222:                     } else {
 8223:                         if ($env{'form.'.$role.'_del_'.$img}) {
 8224:                             $confhash->{$role}{$img} = '';
 8225:                             $changes{$role}{'images'}{$img} = 1;
 8226:                         } 
 8227:                     }
 8228:                     if (($role eq 'login') && (($img eq 'logo') || ($img eq 'img'))) {
 8229:                         if (ref($domconfig->{'login'}{'showlogo'}) eq 'HASH') {
 8230:                             if ($confhash->{$role}{'showlogo'}{$img} ne 
 8231:                                 $domconfig->{$role}{'showlogo'}{$img}) {
 8232:                                 $changes{$role}{'showlogo'}{$img} = 1; 
 8233:                             }
 8234:                         } else {
 8235:                             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8236:                                 $changes{$role}{'showlogo'}{$img} = 1;
 8237:                             }
 8238:                         }
 8239:                     }
 8240:                 }
 8241:                 if ($domconfig->{$role}{'font'} ne '') {
 8242:                     if ($confhash->{$role}{'font'} ne $domconfig->{$role}{'font'}) {
 8243:                         $changes{$role}{'font'} = 1;
 8244:                     }
 8245:                 } else {
 8246:                     if ($confhash->{$role}{'font'}) {
 8247:                         $changes{$role}{'font'} = 1;
 8248:                     }
 8249:                 }
 8250:                 if ($role ne 'login') {
 8251:                     if ($domconfig->{$role}{'fontmenu'} ne '') {
 8252:                         if ($confhash->{$role}{'fontmenu'} ne $domconfig->{$role}{'fontmenu'}) {
 8253:                             $changes{$role}{'fontmenu'} = 1;
 8254:                         }
 8255:                     } else {
 8256:                         if ($confhash->{$role}{'fontmenu'}) {
 8257:                             $changes{$role}{'fontmenu'} = 1;
 8258:                         }
 8259:                     }
 8260:                 }
 8261:                 foreach my $item (@bgs) {
 8262:                     if ($domconfig->{$role}{$item} ne '') {
 8263:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8264:                             $changes{$role}{'bgs'}{$item} = 1;
 8265:                         } 
 8266:                     } else {
 8267:                         if ($confhash->{$role}{$item}) {
 8268:                             $changes{$role}{'bgs'}{$item} = 1;
 8269:                         }
 8270:                     }
 8271:                 }
 8272:                 foreach my $item (@links) {
 8273:                     if ($domconfig->{$role}{$item} ne '') {
 8274:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8275:                             $changes{$role}{'links'}{$item} = 1;
 8276:                         }
 8277:                     } else {
 8278:                         if ($confhash->{$role}{$item}) {
 8279:                             $changes{$role}{'links'}{$item} = 1;
 8280:                         }
 8281:                     }
 8282:                 }
 8283:                 foreach my $item (@logintext) {
 8284:                     if ($domconfig->{$role}{$item} ne '') {
 8285:                         if ($confhash->{$role}{$item} ne $domconfig->{$role}{$item}) {
 8286:                             $changes{$role}{'logintext'}{$item} = 1;
 8287:                         }
 8288:                     } else {
 8289:                         if ($confhash->{$role}{$item}) {
 8290:                             $changes{$role}{'logintext'}{$item} = 1;
 8291:                         }
 8292:                     }
 8293:                 }
 8294:             } else {
 8295:                 &default_change_checker($role,\@images,\@links,\@bgs,
 8296:                                         \@logintext,$confhash,\%changes); 
 8297:             }
 8298:         } else {
 8299:             &default_change_checker($role,\@images,\@links,\@bgs,
 8300:                                     \@logintext,$confhash,\%changes); 
 8301:         }
 8302:     }
 8303:     return ($errors,%changes);
 8304: }
 8305: 
 8306: sub config_check {
 8307:     my ($dom,$confname,$servadm) = @_;
 8308:     my ($configuserok,$author_ok,$switchserver,%currroles);
 8309:     my $uhome = &Apache::lonnet::homeserver($confname,$dom,1);
 8310:     ($configuserok,%currroles) = &check_configuser($uhome,$dom,
 8311:                                                    $confname,$servadm);
 8312:     if ($configuserok eq 'ok') {
 8313:         $switchserver = &check_switchserver($dom,$confname);
 8314:         if ($switchserver eq '') {
 8315:             $author_ok = &check_authorstatus($dom,$confname,%currroles);
 8316:         }
 8317:     }
 8318:     return ($configuserok,$author_ok,$switchserver);
 8319: }
 8320: 
 8321: sub default_change_checker {
 8322:     my ($role,$images,$links,$bgs,$logintext,$confhash,$changes) = @_;
 8323:     foreach my $item (@{$links}) {
 8324:         if ($confhash->{$role}{$item}) {
 8325:             $changes->{$role}{'links'}{$item} = 1;
 8326:         }
 8327:     }
 8328:     foreach my $item (@{$bgs}) {
 8329:         if ($confhash->{$role}{$item}) {
 8330:             $changes->{$role}{'bgs'}{$item} = 1;
 8331:         }
 8332:     }
 8333:     foreach my $item (@{$logintext}) {
 8334:         if ($confhash->{$role}{$item}) {
 8335:             $changes->{$role}{'logintext'}{$item} = 1;
 8336:         }
 8337:     }
 8338:     foreach my $img (@{$images}) {
 8339:         if ($env{'form.'.$role.'_del_'.$img}) {
 8340:             $confhash->{$role}{$img} = '';
 8341:             $changes->{$role}{'images'}{$img} = 1;
 8342:         }
 8343:         if ($role eq 'login') {
 8344:             if ($confhash->{$role}{'showlogo'}{$img} == 0) {
 8345:                 $changes->{$role}{'showlogo'}{$img} = 1;
 8346:             }
 8347:         }
 8348:     }
 8349:     if ($confhash->{$role}{'font'}) {
 8350:         $changes->{$role}{'font'} = 1;
 8351:     }
 8352: }
 8353: 
 8354: sub display_colorchgs {
 8355:     my ($dom,$changes,$roles,$confhash) = @_;
 8356:     my (%choices,$resulttext);
 8357:     if (!grep(/^login$/,@{$roles})) {
 8358:         $resulttext = &mt('Changes made:').'<br />';
 8359:     }
 8360:     foreach my $role (@{$roles}) {
 8361:         if ($role eq 'login') {
 8362:             %choices = &login_choices();
 8363:         } else {
 8364:             %choices = &color_font_choices();
 8365:         }
 8366:         if (ref($changes->{$role}) eq 'HASH') {
 8367:             if ($role ne 'login') {
 8368:                 $resulttext .= '<h4>'.&mt($role).'</h4>';
 8369:             }
 8370:             foreach my $key (sort(keys(%{$changes->{$role}}))) {
 8371:                 if ($role ne 'login') {
 8372:                     $resulttext .= '<ul>';
 8373:                 }
 8374:                 if (ref($changes->{$role}{$key}) eq 'HASH') {
 8375:                     if ($role ne 'login') {
 8376:                         $resulttext .= '<li>'.&mt($choices{$key}).':<ul>';
 8377:                     }
 8378:                     foreach my $item (sort(keys(%{$changes->{$role}{$key}}))) {
 8379:                         if (($role eq 'login') && ($key eq 'showlogo')) {
 8380:                             if ($confhash->{$role}{$key}{$item}) {
 8381:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to be displayed").'</li>';
 8382:                             } else {
 8383:                                 $resulttext .= '<li>'.&mt("$choices{$item} set to not be displayed").'</li>';
 8384:                             }
 8385:                         } elsif ($confhash->{$role}{$item} eq '') {
 8386:                             $resulttext .= '<li>'.&mt("$choices{$item} set to default").'</li>';
 8387:                         } else {
 8388:                             my $newitem = $confhash->{$role}{$item};
 8389:                             if ($key eq 'images') {
 8390:                                 $newitem = '<img src="'.$confhash->{$role}{$item}.'" alt="'.$choices{$item}.'" valign="bottom" />';
 8391:                             }
 8392:                             $resulttext .= '<li>'.&mt("$choices{$item} set to [_1]",$newitem).'</li>';
 8393:                         }
 8394:                     }
 8395:                     if ($role ne 'login') {
 8396:                         $resulttext .= '</ul></li>';
 8397:                     }
 8398:                 } else {
 8399:                     if ($confhash->{$role}{$key} eq '') {
 8400:                         $resulttext .= '<li>'.&mt("$choices{$key} set to default").'</li>';
 8401:                     } else {
 8402:                         $resulttext .= '<li>'.&mt("$choices{$key} set to [_1]",$confhash->{$role}{$key}).'</li>';
 8403:                     }
 8404:                 }
 8405:                 if ($role ne 'login') {
 8406:                     $resulttext .= '</ul>';
 8407:                 }
 8408:             }
 8409:         }
 8410:     }
 8411:     return $resulttext;
 8412: }
 8413: 
 8414: sub thumb_dimensions {
 8415:     return ('200','50');
 8416: }
 8417: 
 8418: sub check_dimensions {
 8419:     my ($inputfile) = @_;
 8420:     my ($fullwidth,$fullheight);
 8421:     if ($inputfile =~ m|^[/\w.\-]+$|) {
 8422:         if (open(PIPE,"identify $inputfile 2>&1 |")) {
 8423:             my $imageinfo = <PIPE>;
 8424:             if (!close(PIPE)) {
 8425:                 &Apache::lonnet::logthis("Failed to close PIPE opened to retrieve image information for $inputfile");
 8426:             }
 8427:             chomp($imageinfo);
 8428:             my ($fullsize) = 
 8429:                 ($imageinfo =~ /^\Q$inputfile\E\s+\w+\s+(\d+x\d+)/);
 8430:             if ($fullsize) {
 8431:                 ($fullwidth,$fullheight) = split(/x/,$fullsize);
 8432:             }
 8433:         }
 8434:     }
 8435:     return ($fullwidth,$fullheight);
 8436: }
 8437: 
 8438: sub check_configuser {
 8439:     my ($uhome,$dom,$confname,$servadm) = @_;
 8440:     my ($configuserok,%currroles);
 8441:     if ($uhome eq 'no_host') {
 8442:         srand( time() ^ ($$ + ($$ << 15))  ); # Seed rand.
 8443:         my $configpass = &LONCAPA::Enrollment::create_password();
 8444:         $configuserok = 
 8445:             &Apache::lonnet::modifyuser($dom,$confname,'','internal',
 8446:                              $configpass,'','','','','',undef,$servadm);
 8447:     } else {
 8448:         $configuserok = 'ok';
 8449:         %currroles = 
 8450:             &Apache::lonnet::get_my_roles($confname,$dom,'userroles');
 8451:     }
 8452:     return ($configuserok,%currroles);
 8453: }
 8454: 
 8455: sub check_authorstatus {
 8456:     my ($dom,$confname,%currroles) = @_;
 8457:     my $author_ok;
 8458:     if (!$currroles{':'.$dom.':au'}) {
 8459:         my $start = time;
 8460:         my $end = 0;
 8461:         $author_ok = 
 8462:             &Apache::lonnet::assignrole($dom,$confname,'/'.$dom.'/',
 8463:                                         'au',$end,$start,'','','domconfig');
 8464:     } else {
 8465:         $author_ok = 'ok';
 8466:     }
 8467:     return $author_ok;
 8468: }
 8469: 
 8470: sub publishlogo {
 8471:     my ($r,$action,$formname,$dom,$confname,$subdir,$thumbwidth,$thumbheight,$savefileas) = @_;
 8472:     my ($output,$fname,$logourl,$madethumb);
 8473:     if ($action eq 'upload') {
 8474:         $fname=$env{'form.'.$formname.'.filename'};
 8475:         chop($env{'form.'.$formname});
 8476:     } else {
 8477:         ($fname) = ($formname =~ /([^\/]+)$/);
 8478:     }
 8479:     if ($savefileas ne '') {
 8480:         $fname = $savefileas;
 8481:     }
 8482:     $fname=&Apache::lonnet::clean_filename($fname);
 8483: # See if there is anything left
 8484:     unless ($fname) { return ('error: no uploaded file'); }
 8485:     $fname="$subdir/$fname";
 8486:     my $docroot=$r->dir_config('lonDocRoot');
 8487:     my $filepath="$docroot/priv";
 8488:     my $relpath = "$dom/$confname";
 8489:     my ($fnamepath,$file,$fetchthumb);
 8490:     $file=$fname;
 8491:     if ($fname=~m|/|) {
 8492:         ($fnamepath,$file) = ($fname =~ m|^(.*)/([^/]+)$|);
 8493:     }
 8494:     my @parts=split(/\//,"$filepath/$relpath/$fnamepath");
 8495:     my $count;
 8496:     for ($count=5;$count<=$#parts;$count++) {
 8497:         $filepath.="/$parts[$count]";
 8498:         if ((-e $filepath)!=1) {
 8499:             mkdir($filepath,02770);
 8500:         }
 8501:     }
 8502:     # Check for bad extension and disallow upload
 8503:     if ($file=~/\.(\w+)$/ &&
 8504:         (&Apache::loncommon::fileembstyle($1) eq 'hdn')) {
 8505:         $output = 
 8506:             &mt('Invalid file extension ([_1]) - reserved for internal use.',$1); 
 8507:     } elsif ($file=~/\.(\w+)$/ &&
 8508:         !defined(&Apache::loncommon::fileembstyle($1))) {
 8509:         $output = &mt('Unrecognized file extension ([_1]) - rename the file with a proper extension and re-upload.',$1);
 8510:     } elsif ($file=~/\.(\d+)\.(\w+)$/) {
 8511:         $output = &mt('Filename not allowed - rename the file to remove the number immediately before the file extension([_1]) and re-upload.',$2);
 8512:     } elsif (-d "$filepath/$file") {
 8513:         $output = &mt('Filename is a directory name - rename the file and re-upload');
 8514:     } else {
 8515:         my $source = $filepath.'/'.$file;
 8516:         my $logfile;
 8517:         if (!open($logfile,">>$source".'.log')) {
 8518:             return (&mt('No write permission to Authoring Space'));
 8519:         }
 8520:         print $logfile
 8521: "\n================= Publish ".localtime()." ================\n".
 8522: $env{'user.name'}.':'.$env{'user.domain'}."\n";
 8523: # Save the file
 8524:         if (!open(FH,'>'.$source)) {
 8525:             &Apache::lonnet::logthis('Failed to create '.$source);
 8526:             return (&mt('Failed to create file'));
 8527:         }
 8528:         if ($action eq 'upload') {
 8529:             if (!print FH ($env{'form.'.$formname})) {
 8530:                 &Apache::lonnet::logthis('Failed to write to '.$source);
 8531:                 return (&mt('Failed to write file'));
 8532:             }
 8533:         } else {
 8534:             my $original = &Apache::lonnet::filelocation('',$formname);
 8535:             if(!copy($original,$source)) {
 8536:                 &Apache::lonnet::logthis('Failed to copy '.$original.' to '.$source);
 8537:                 return (&mt('Failed to write file'));
 8538:             }
 8539:         }
 8540:         close(FH);
 8541:         chmod(0660, $source); # Permissions to rw-rw---.
 8542: 
 8543:         my $targetdir=$docroot.'/res/'.$dom.'/'.$confname .'/'.$fnamepath;
 8544:         my $copyfile=$targetdir.'/'.$file;
 8545: 
 8546:         my @parts=split(/\//,$targetdir);
 8547:         my $path="/$parts[1]/$parts[2]/$parts[3]/$parts[4]";
 8548:         for (my $count=5;$count<=$#parts;$count++) {
 8549:             $path.="/$parts[$count]";
 8550:             if (!-e $path) {
 8551:                 print $logfile "\nCreating directory ".$path;
 8552:                 mkdir($path,02770);
 8553:             }
 8554:         }
 8555:         my $versionresult;
 8556:         if (-e $copyfile) {
 8557:             $versionresult = &logo_versioning($targetdir,$file,$logfile);
 8558:         } else {
 8559:             $versionresult = 'ok';
 8560:         }
 8561:         if ($versionresult eq 'ok') {
 8562:             if (copy($source,$copyfile)) {
 8563:                 print $logfile "\nCopied original source to ".$copyfile."\n";
 8564:                 $output = 'ok';
 8565:                 $logourl = '/res/'.$dom.'/'.$confname.'/'.$fname;
 8566:                 push(@{$modified_urls},[$copyfile,$source]);
 8567:                 my $metaoutput = 
 8568:                     &write_metadata($dom,$confname,$formname,$targetdir,$file,$logfile);
 8569:                 unless ($registered_cleanup) {
 8570:                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8571:                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8572:                     $registered_cleanup=1;
 8573:                 }
 8574:             } else {
 8575:                 print $logfile "\nUnable to write ".$copyfile.':'.$!."\n";
 8576:                 $output = &mt('Failed to copy file to RES space').", $!";
 8577:             }
 8578:             if (($thumbwidth =~ /^\d+$/) && ($thumbheight =~ /^\d+$/)) {
 8579:                 my $inputfile = $filepath.'/'.$file;
 8580:                 my $outfile = $filepath.'/'.'tn-'.$file;
 8581:                 my ($fullwidth,$fullheight) = &check_dimensions($inputfile);
 8582:                 if ($fullwidth ne '' && $fullheight ne '') { 
 8583:                     if ($fullwidth > $thumbwidth && $fullheight > $thumbheight) {
 8584:                         my $thumbsize = $thumbwidth.'x'.$thumbheight;
 8585:                         system("convert -sample $thumbsize $inputfile $outfile");
 8586:                         chmod(0660, $filepath.'/tn-'.$file);
 8587:                         if (-e $outfile) {
 8588:                             my $copyfile=$targetdir.'/tn-'.$file;
 8589:                             if (copy($outfile,$copyfile)) {
 8590:                                 print $logfile "\nCopied source to ".$copyfile."\n";
 8591:                                 my $thumb_metaoutput = 
 8592:                                     &write_metadata($dom,$confname,$formname,
 8593:                                                     $targetdir,'tn-'.$file,$logfile);
 8594:                                 push(@{$modified_urls},[$copyfile,$outfile]);
 8595:                                 unless ($registered_cleanup) {
 8596:                                     my $handlers = $r->get_handlers('PerlCleanupHandler');
 8597:                                     $r->set_handlers('PerlCleanupHandler' => [\&notifysubscribed,@{$handlers}]);
 8598:                                     $registered_cleanup=1;
 8599:                                 }
 8600:                                 $madethumb = 1;
 8601:                             } else {
 8602:                                 print $logfile "\nUnable to write ".$copyfile.
 8603:                                                ':'.$!."\n";
 8604:                             }
 8605:                         }
 8606:                     }
 8607:                 }
 8608:             }
 8609:         } else {
 8610:             $output = $versionresult;
 8611:         }
 8612:     }
 8613:     return ($output,$logourl,$madethumb);
 8614: }
 8615: 
 8616: sub logo_versioning {
 8617:     my ($targetdir,$file,$logfile) = @_;
 8618:     my $target = $targetdir.'/'.$file;
 8619:     my ($maxversion,$fn,$extn,$output);
 8620:     $maxversion = 0;
 8621:     if ($file =~ /^(.+)\.(\w+)$/) {
 8622:         $fn=$1;
 8623:         $extn=$2;
 8624:     }
 8625:     opendir(DIR,$targetdir);
 8626:     while (my $filename=readdir(DIR)) {
 8627:         if ($filename=~/\Q$fn\E\.(\d+)\.\Q$extn\E$/) {
 8628:             $maxversion=($1>$maxversion)?$1:$maxversion;
 8629:         }
 8630:     }
 8631:     $maxversion++;
 8632:     print $logfile "\nCreating old version ".$maxversion."\n";
 8633:     my $copyfile=$targetdir.'/'.$fn.'.'.$maxversion.'.'.$extn;
 8634:     if (copy($target,$copyfile)) {
 8635:         print $logfile "Copied old target to ".$copyfile."\n";
 8636:         $copyfile=$copyfile.'.meta';
 8637:         if (copy($target.'.meta',$copyfile)) {
 8638:             print $logfile "Copied old target metadata to ".$copyfile."\n";
 8639:             $output = 'ok';
 8640:         } else {
 8641:             print $logfile "Unable to write metadata ".$copyfile.':'.$!."\n";
 8642:             $output = &mt('Failed to copy old meta').", $!, ";
 8643:         }
 8644:     } else {
 8645:         print $logfile "Unable to write ".$copyfile.':'.$!."\n";
 8646:         $output = &mt('Failed to copy old target').", $!, ";
 8647:     }
 8648:     return $output;
 8649: }
 8650: 
 8651: sub write_metadata {
 8652:     my ($dom,$confname,$formname,$targetdir,$file,$logfile) = @_;
 8653:     my (%metadatafields,%metadatakeys,$output);
 8654:     $metadatafields{'title'}=$formname;
 8655:     $metadatafields{'creationdate'}=time;
 8656:     $metadatafields{'lastrevisiondate'}=time;
 8657:     $metadatafields{'copyright'}='public';
 8658:     $metadatafields{'modifyinguser'}=$env{'user.name'}.':'.
 8659:                                          $env{'user.domain'};
 8660:     $metadatafields{'authorspace'}=$confname.':'.$dom;
 8661:     $metadatafields{'domain'}=$dom;
 8662:     {
 8663:         print $logfile "\nWrite metadata file for ".$targetdir.'/'.$file;
 8664:         my $mfh;
 8665:         if (open($mfh,'>'.$targetdir.'/'.$file.'.meta')) {
 8666:             foreach (sort(keys(%metadatafields))) {
 8667:                 unless ($_=~/\./) {
 8668:                     my $unikey=$_;
 8669:                     $unikey=~/^([A-Za-z]+)/;
 8670:                     my $tag=$1;
 8671:                     $tag=~tr/A-Z/a-z/;
 8672:                     print $mfh "\n\<$tag";
 8673:                     foreach (split(/\,/,$metadatakeys{$unikey})) {
 8674:                         my $value=$metadatafields{$unikey.'.'.$_};
 8675:                         $value=~s/\"/\'\'/g;
 8676:                         print $mfh ' '.$_.'="'.$value.'"';
 8677:                     }
 8678:                     print $mfh '>'.
 8679:                         &HTML::Entities::encode($metadatafields{$unikey},'<>&"')
 8680:                             .'</'.$tag.'>';
 8681:                 }
 8682:             }
 8683:             $output = 'ok';
 8684:             print $logfile "\nWrote metadata";
 8685:             close($mfh);
 8686:         } else {
 8687:             print $logfile "\nFailed to open metadata file";
 8688:             $output = &mt('Could not write metadata');
 8689:         }
 8690:     }
 8691:     return $output;
 8692: }
 8693: 
 8694: sub notifysubscribed {
 8695:     foreach my $targetsource (@{$modified_urls}){
 8696:         next unless (ref($targetsource) eq 'ARRAY');
 8697:         my ($target,$source)=@{$targetsource};
 8698:         if ($source ne '') {
 8699:             if (open(my $logfh,'>>'.$source.'.log')) {
 8700:                 print $logfh "\nCleanup phase: Notifications\n";
 8701:                 my @subscribed=&subscribed_hosts($target);
 8702:                 foreach my $subhost (@subscribed) {
 8703:                     print $logfh "\nNotifying host ".$subhost.':';
 8704:                     my $reply=&Apache::lonnet::critical('update:'.$target,$subhost);
 8705:                     print $logfh $reply;
 8706:                 }
 8707:                 my @subscribedmeta=&subscribed_hosts("$target.meta");
 8708:                 foreach my $subhost (@subscribedmeta) {
 8709:                     print $logfh "\nNotifying host for metadata only ".$subhost.':';
 8710:                     my $reply=&Apache::lonnet::critical('update:'.$target.'.meta',
 8711:                                                         $subhost);
 8712:                     print $logfh $reply;
 8713:                 }
 8714:                 print $logfh "\n============ Done ============\n";
 8715:                 close($logfh);
 8716:             }
 8717:         }
 8718:     }
 8719:     return OK;
 8720: }
 8721: 
 8722: sub subscribed_hosts {
 8723:     my ($target) = @_;
 8724:     my @subscribed;
 8725:     if (open(my $fh,"<$target.subscription")) {
 8726:         while (my $subline=<$fh>) {
 8727:             if ($subline =~ /^($match_lonid):/) {
 8728:                 my $host = $1;
 8729:                 if ($host ne $Apache::lonnet::perlvar{'lonHostID'}) {
 8730:                     unless (grep(/^\Q$host\E$/,@subscribed)) {
 8731:                         push(@subscribed,$host);
 8732:                     }
 8733:                 }
 8734:             }
 8735:         }
 8736:     }
 8737:     return @subscribed;
 8738: }
 8739: 
 8740: sub check_switchserver {
 8741:     my ($dom,$confname) = @_;
 8742:     my ($allowed,$switchserver);
 8743:     my $home = &Apache::lonnet::homeserver($confname,$dom);
 8744:     if ($home eq 'no_host') {
 8745:         $home = &Apache::lonnet::domain($dom,'primary');
 8746:     }
 8747:     my @ids=&Apache::lonnet::current_machine_ids();
 8748:     foreach my $id (@ids) { if ($id eq $home) { $allowed=1; } }
 8749:     if (!$allowed) {
 8750: 	$switchserver='<a href="/adm/switchserver?otherserver='.$home.'&amp;role=dc./'.$dom.'/&amp;destinationurl=/adm/domainprefs">'.&mt('Switch Server').'</a>';
 8751:     }
 8752:     return $switchserver;
 8753: }
 8754: 
 8755: sub modify_quotas {
 8756:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 8757:     my ($context,@usertools,@options,%validations,%titles,%confhash,%toolshash,
 8758:         %limithash,$toolregexp,%conditions,$resulttext,%changes,$confname,$configuserok,
 8759:         $author_ok,$switchserver,$errors,$validationitemsref,$validationnamesref,
 8760:         $validationfieldsref);
 8761:     if ($action eq 'quotas') {
 8762:         $context = 'tools'; 
 8763:     } else {
 8764:         $context = $action;
 8765:     }
 8766:     if ($context eq 'requestcourses') {
 8767:         @usertools = ('official','unofficial','community','textbook','placement');
 8768:         @options =('norequest','approval','validate','autolimit');
 8769:         %validations = &Apache::lonnet::auto_courserequest_checks($dom);
 8770:         %titles = &courserequest_titles();
 8771:         $toolregexp = join('|',@usertools);
 8772:         %conditions = &courserequest_conditions();
 8773:         $confname = $dom.'-domainconfig';
 8774:         my $servadm = $r->dir_config('lonAdmEMail');
 8775:         ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 8776:         ($validationitemsref,$validationnamesref,$validationfieldsref) = 
 8777:             &Apache::loncoursequeueadmin::requestcourses_validation_types();
 8778:     } elsif ($context eq 'requestauthor') {
 8779:         @usertools = ('author');
 8780:         %titles = &authorrequest_titles();
 8781:     } else {
 8782:         @usertools = ('aboutme','blog','webdav','portfolio');
 8783:         %titles = &tool_titles();
 8784:     }
 8785:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 8786:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
 8787:     foreach my $key (keys(%env)) {
 8788:         if ($context eq 'requestcourses') {
 8789:             if ($key =~ /^form\.crsreq_($toolregexp)_(.+)$/) {
 8790:                 my $item = $1;
 8791:                 my $type = $2;
 8792:                 if ($type =~ /^limit_(.+)/) {
 8793:                     $limithash{$item}{$1} = $env{$key};
 8794:                 } else {
 8795:                     $confhash{$item}{$type} = $env{$key};
 8796:                 }
 8797:             }
 8798:         } elsif ($context eq 'requestauthor') {
 8799:             if ($key =~ /^\Qform.authorreq_\E(.+)$/) {
 8800:                 $confhash{$1} = $env{$key};
 8801:             }
 8802:         } else {
 8803:             if ($key =~ /^form\.quota_(.+)$/) {
 8804:                 $confhash{'defaultquota'}{$1} = $env{$key};
 8805:             } elsif ($key =~ /^form\.authorquota_(.+)$/) {
 8806:                 $confhash{'authorquota'}{$1} = $env{$key};
 8807:             } elsif ($key =~ /^form\.\Q$context\E_(.+)$/) {
 8808:                 @{$toolshash{$1}} = &Apache::loncommon::get_env_multiple($key);
 8809:             }
 8810:         }
 8811:     }
 8812:     if (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 8813:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.'.$context.'notifyapproval');
 8814:         @approvalnotify = sort(@approvalnotify);
 8815:         $confhash{'notify'}{'approval'} = join(',',@approvalnotify);
 8816:         my @crstypes = ('official','unofficial','community','textbook','placement');
 8817:         my @hasuniquecode = &Apache::loncommon::get_env_multiple('form.uniquecode');
 8818:         foreach my $type (@hasuniquecode) {
 8819:             if (grep(/^\Q$type\E$/,@crstypes)) {
 8820:                 $confhash{'uniquecode'}{$type} = 1;
 8821:             }
 8822:         }
 8823:         my (%newbook,%allpos);
 8824:         if ($context eq 'requestcourses') {
 8825:             foreach my $type ('textbooks','templates') {
 8826:                 @{$allpos{$type}} = (); 
 8827:                 my $invalid;
 8828:                 if ($type eq 'textbooks') {
 8829:                     $invalid = &mt('Invalid LON-CAPA course for textbook');
 8830:                 } else {
 8831:                     $invalid = &mt('Invalid LON-CAPA course for template');
 8832:                 }
 8833:                 if ($env{'form.'.$type.'_addbook'}) {
 8834:                     if (($env{'form.'.$type.'_addbook_cnum'} =~ /^$match_courseid$/) &&
 8835:                         ($env{'form.'.$type.'_addbook_cdom'} =~ /^$match_domain$/)) {
 8836:                         if (&Apache::lonnet::homeserver($env{'form.'.$type.'_addbook_cnum'},
 8837:                                                         $env{'form.'.$type.'_addbook_cdom'}) eq 'no_host') {
 8838:                             $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 8839:                         } else {
 8840:                             $newbook{$type} = $env{'form.'.$type.'_addbook_cdom'}.'_'.$env{'form.'.$type.'_addbook_cnum'};
 8841:                             my $position = $env{'form.'.$type.'_addbook_pos'};
 8842:                             $position =~ s/\D+//g;
 8843:                             if ($position ne '') {
 8844:                                 $allpos{$type}[$position] = $newbook{$type};
 8845:                             }
 8846:                         }
 8847:                     } else {
 8848:                         $errors .= '<li><span class="LC_error">'.$invalid.'</span></li>';
 8849:                     }
 8850:                 }
 8851:             } 
 8852:         }
 8853:         if (ref($domconfig{$action}) eq 'HASH') {
 8854:             if (ref($domconfig{$action}{'notify'}) eq 'HASH') {
 8855:                 if ($domconfig{$action}{'notify'}{'approval'} ne $confhash{'notify'}{'approval'}) {
 8856:                     $changes{'notify'}{'approval'} = 1;
 8857:                 }
 8858:             } else {
 8859:                 if ($confhash{'notify'}{'approval'}) {
 8860:                     $changes{'notify'}{'approval'} = 1;
 8861:                 }
 8862:             }
 8863:             if (ref($domconfig{$action}{'uniquecode'}) eq 'HASH') {
 8864:                 if (ref($confhash{'uniquecode'}) eq 'HASH') {
 8865:                     foreach my $crstype (keys(%{$domconfig{$action}{'uniquecode'}})) {
 8866:                         unless ($confhash{'uniquecode'}{$crstype}) {
 8867:                             $changes{'uniquecode'} = 1;
 8868:                         }
 8869:                     }
 8870:                     unless ($changes{'uniquecode'}) {
 8871:                         foreach my $crstype (keys(%{$confhash{'uniquecode'}})) {
 8872:                             unless ($domconfig{$action}{'uniquecode'}{$crstype}) {
 8873:                                 $changes{'uniquecode'} = 1;
 8874:                             }
 8875:                         }
 8876:                     }
 8877:                } else {
 8878:                    $changes{'uniquecode'} = 1;
 8879:                }
 8880:             } elsif (ref($confhash{'uniquecode'}) eq 'HASH') {
 8881:                 $changes{'uniquecode'} = 1;
 8882:             }
 8883:             if ($context eq 'requestcourses') {
 8884:                 foreach my $type ('textbooks','templates') {
 8885:                     if (ref($domconfig{$action}{$type}) eq 'HASH') {
 8886:                         my %deletions;
 8887:                         my @todelete = &Apache::loncommon::get_env_multiple('form.'.$type.'_del');
 8888:                         if (@todelete) {
 8889:                             map { $deletions{$_} = 1; } @todelete;
 8890:                         }
 8891:                         my %imgdeletions;
 8892:                         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.'.$type.'_image_del');
 8893:                         if (@todeleteimages) {
 8894:                             map { $imgdeletions{$_} = 1; } @todeleteimages;
 8895:                         }
 8896:                         my $maxnum = $env{'form.'.$type.'_maxnum'};
 8897:                         for (my $i=0; $i<=$maxnum; $i++) {
 8898:                             my $itemid = $env{'form.'.$type.'_id_'.$i};
 8899:                             my ($key) = ($itemid =~ /^\Q$type\E_(\w+)$/); 
 8900:                             if (ref($domconfig{$action}{$type}{$key}) eq 'HASH') {
 8901:                                 if ($deletions{$key}) {
 8902:                                     if ($domconfig{$action}{$type}{$key}{'image'}) {
 8903:                                         #FIXME need to obsolete item in RES space
 8904:                                     }
 8905:                                     next;
 8906:                                 } else {
 8907:                                     my $newpos = $env{'form.'.$itemid};
 8908:                                     $newpos =~ s/\D+//g;
 8909:                                     foreach my $item ('subject','title','publisher','author') {
 8910:                                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8911:                                                  ($type eq 'templates'));
 8912:                                         $confhash{$type}{$key}{$item} = $env{'form.'.$type.'_'.$item.'_'.$i};
 8913:                                         if ($domconfig{$action}{$type}{$key}{$item} ne $confhash{$type}{$key}{$item}) {
 8914:                                             $changes{$type}{$key} = 1;
 8915:                                         }
 8916:                                     }
 8917:                                     $allpos{$type}[$newpos] = $key;
 8918:                                 }
 8919:                                 if ($imgdeletions{$key}) {
 8920:                                     $changes{$type}{$key} = 1;
 8921:                                     #FIXME need to obsolete item in RES space
 8922:                                 } elsif ($env{'form.'.$type.'_image_'.$i.'.filename'}) {
 8923:                                     my ($cdom,$cnum) = split(/_/,$key);
 8924:                                     my ($imgurl,$error) = &process_textbook_image($r,$dom,$confname,$type.'_image_'.$i,
 8925:                                                                                   $cdom,$cnum,$type,$configuserok,
 8926:                                                                                   $switchserver,$author_ok);
 8927:                                     if ($imgurl) {
 8928:                                         $confhash{$type}{$key}{'image'} = $imgurl;
 8929:                                         $changes{$type}{$key} = 1; 
 8930:                                     }
 8931:                                     if ($error) {
 8932:                                         &Apache::lonnet::logthis($error);
 8933:                                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8934:                                     } 
 8935:                                 } elsif ($domconfig{$action}{$type}{$key}{'image'}) {
 8936:                                     $confhash{$type}{$key}{'image'} = 
 8937:                                         $domconfig{$action}{$type}{$key}{'image'};
 8938:                                 }
 8939:                             }
 8940:                         }
 8941:                     }
 8942:                 }
 8943:             }
 8944:         } else {
 8945:             if ($confhash{'notify'}{'approval'}) {
 8946:                 $changes{'notify'}{'approval'} = 1;
 8947:             }
 8948:             if (ref($confhash{'uniquecode'} eq 'HASH')) {
 8949:                 $changes{'uniquecode'} = 1;
 8950:             }
 8951:         }
 8952:         if ($context eq 'requestcourses') {
 8953:             foreach my $type ('textbooks','templates') {
 8954:                 if ($newbook{$type}) {
 8955:                     $changes{$type}{$newbook{$type}} = 1;
 8956:                     foreach my $item ('subject','title','publisher','author') {
 8957:                         next if ((($item eq 'author') || ($item eq 'publisher')) &&
 8958:                                  ($type eq 'template'));
 8959:                         $env{'form.'.$type.'_addbook_'.$item} =~ s/(`)/'/g;
 8960:                         if ($env{'form.'.$type.'_addbook_'.$item}) {
 8961:                             $confhash{$type}{$newbook{$type}}{$item} = $env{'form.'.$type.'_addbook_'.$item};
 8962:                         }
 8963:                     }
 8964:                     if ($type eq 'textbooks') {
 8965:                         if ($env{'form.'.$type.'_addbook_image.filename'} ne '') {
 8966:                             my ($cdom,$cnum) = split(/_/,$newbook{$type});
 8967:                             my ($imageurl,$error) =
 8968:                                 &process_textbook_image($r,$dom,$confname,$type.'_addbook_image',$cdom,$cnum,$type,
 8969:                                                         $configuserok,$switchserver,$author_ok);
 8970:                             if ($imageurl) {
 8971:                                 $confhash{$type}{$newbook{$type}}{'image'} = $imageurl;
 8972:                             }
 8973:                             if ($error) {
 8974:                                 &Apache::lonnet::logthis($error);
 8975:                                 $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 8976:                             }
 8977:                         }
 8978:                     }
 8979:                 }
 8980:                 if (@{$allpos{$type}} > 0) {
 8981:                     my $idx = 0;
 8982:                     foreach my $item (@{$allpos{$type}}) {
 8983:                         if ($item ne '') {
 8984:                             $confhash{$type}{$item}{'order'} = $idx;
 8985:                             if (ref($domconfig{$action}) eq 'HASH') {
 8986:                                 if (ref($domconfig{$action}{$type}) eq 'HASH') {
 8987:                                     if (ref($domconfig{$action}{$type}{$item}) eq 'HASH') {
 8988:                                         if ($domconfig{$action}{$type}{$item}{'order'} ne $idx) {
 8989:                                             $changes{$type}{$item} = 1;
 8990:                                         }
 8991:                                     }
 8992:                                 }
 8993:                             }
 8994:                             $idx ++;
 8995:                         }
 8996:                     }
 8997:                 }
 8998:             }
 8999:             if (ref($validationitemsref) eq 'ARRAY') {
 9000:                 foreach my $item (@{$validationitemsref}) {
 9001:                     if ($item eq 'fields') {
 9002:                         my @changed;
 9003:                         @{$confhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.requestcourses_validation_'.$item);
 9004:                         if (@{$confhash{'validation'}{$item}} > 0) {
 9005:                             @{$confhash{'validation'}{$item}} = sort(@{$confhash{'validation'}{$item}});
 9006:                         }
 9007:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9008:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9009:                                 if (ref($domconfig{'requestcourses'}{'validation'}{$item}) eq 'ARRAY') {
 9010:                                     @changed = &Apache::loncommon::compare_arrays($confhash{'validation'}{$item},
 9011:                                                                                   $domconfig{'requestcourses'}{'validation'}{$item});
 9012:                                 } else {
 9013:                                     @changed = @{$confhash{'validation'}{$item}};
 9014:                                 }
 9015:                             } else {
 9016:                                 @changed = @{$confhash{'validation'}{$item}};
 9017:                             }
 9018:                         } else {
 9019:                             @changed = @{$confhash{'validation'}{$item}};
 9020:                         }
 9021:                         if (@changed) {
 9022:                             if ($confhash{'validation'}{$item}) {
 9023:                                 $changes{'validation'}{$item} = join(', ',@{$confhash{'validation'}{$item}});
 9024:                             } else {
 9025:                                 $changes{'validation'}{$item} = &mt('None');
 9026:                             }
 9027:                         }
 9028:                     } else {
 9029:                         $confhash{'validation'}{$item} = $env{'form.requestcourses_validation_'.$item};
 9030:                         if ($item eq 'markup') {
 9031:                             if ($env{'form.requestcourses_validation_'.$item}) {
 9032:                                 $env{'form.requestcourses_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
 9033:                             }
 9034:                         }
 9035:                         if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9036:                             if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9037:                                 if ($domconfig{'requestcourses'}{'validation'}{$item} ne $confhash{'validation'}{$item}) {
 9038:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9039:                                 }
 9040:                             } else {
 9041:                                 if ($confhash{'validation'}{$item} ne '') {
 9042:                                     $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9043:                                 }
 9044:                             }
 9045:                         } else {
 9046:                             if ($confhash{'validation'}{$item} ne '') {
 9047:                                 $changes{'validation'}{$item} = $confhash{'validation'}{$item};
 9048:                             }
 9049:                         }
 9050:                     }
 9051:                 }
 9052:             }
 9053:             if ($env{'form.validationdc'}) {
 9054:                 my $newval = $env{'form.validationdc'};
 9055:                 my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
 9056:                 if (exists($domcoords{$newval})) {
 9057:                     $confhash{'validation'}{'dc'} = $newval;
 9058:                 }
 9059:             }
 9060:             if (ref($confhash{'validation'}) eq 'HASH') {
 9061:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9062:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9063:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9064:                             unless ($confhash{'validation'}{'dc'} eq $domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9065:                                 if ($confhash{'validation'}{'dc'} eq '') {
 9066:                                     $changes{'validation'}{'dc'} = &mt('None');
 9067:                                 } else {
 9068:                                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9069:                                 }
 9070:                             }
 9071:                         } elsif ($confhash{'validation'}{'dc'} ne '') {
 9072:                             $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9073:                         }
 9074:                     } elsif ($confhash{'validation'}{'dc'} ne '') {
 9075:                         $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9076:                     }
 9077:                 } elsif ($confhash{'validation'}{'dc'} ne '') {
 9078:                     $changes{'validation'}{'dc'} = $confhash{'validation'}{'dc'};
 9079:                 }
 9080:             } else {
 9081:                 if (ref($domconfig{'requestcourses'}) eq 'HASH') {
 9082:                     if (ref($domconfig{'requestcourses'}{'validation'}) eq 'HASH') {
 9083:                         if ($domconfig{'requestcourses'}{'validation'}{'dc'}) {
 9084:                             $changes{'validation'}{'dc'} = &mt('None');
 9085:                         }
 9086:                     }
 9087:                 }
 9088:             }
 9089:         }
 9090:     } else {
 9091:         $confhash{'defaultquota'}{'default'} = $env{'form.defaultquota'};
 9092:         $confhash{'authorquota'}{'default'} = $env{'form.authorquota'};
 9093:     }
 9094:     foreach my $item (@usertools) {
 9095:         foreach my $type (@{$types},'default','_LC_adv') {
 9096:             my $unset; 
 9097:             if ($context eq 'requestcourses') {
 9098:                 $unset = '0';
 9099:                 if ($type eq '_LC_adv') {
 9100:                     $unset = '';
 9101:                 }
 9102:                 if ($confhash{$item}{$type} eq 'autolimit') {
 9103:                     $confhash{$item}{$type} .= '=';
 9104:                     unless ($limithash{$item}{$type} =~ /\D/) {
 9105:                         $confhash{$item}{$type} .= $limithash{$item}{$type};
 9106:                     }
 9107:                 }
 9108:             } elsif ($context eq 'requestauthor') {
 9109:                 $unset = '0';
 9110:                 if ($type eq '_LC_adv') {
 9111:                     $unset = '';
 9112:                 }
 9113:             } else {
 9114:                 if (grep(/^\Q$type\E$/,@{$toolshash{$item}})) {
 9115:                     $confhash{$item}{$type} = 1;
 9116:                 } else {
 9117:                     $confhash{$item}{$type} = 0;
 9118:                 }
 9119:             }
 9120:             if (ref($domconfig{$action}) eq 'HASH') {
 9121:                 if ($action eq 'requestauthor') {
 9122:                     if ($domconfig{$action}{$type} ne $confhash{$type}) {
 9123:                         $changes{$type} = 1;
 9124:                     }
 9125:                 } elsif (ref($domconfig{$action}{$item}) eq 'HASH') {
 9126:                     if ($domconfig{$action}{$item}{$type} ne $confhash{$item}{$type}) {
 9127:                         $changes{$item}{$type} = 1;
 9128:                     }
 9129:                 } else {
 9130:                     if ($context eq 'requestcourses') {
 9131:                         if ($confhash{$item}{$type} ne $unset) {
 9132:                             $changes{$item}{$type} = 1;
 9133:                         }
 9134:                     } else {
 9135:                         if (!$confhash{$item}{$type}) {
 9136:                             $changes{$item}{$type} = 1;
 9137:                         }
 9138:                     }
 9139:                 }
 9140:             } else {
 9141:                 if ($context eq 'requestcourses') {
 9142:                     if ($confhash{$item}{$type} ne $unset) {
 9143:                         $changes{$item}{$type} = 1;
 9144:                     }
 9145:                 } elsif ($context eq 'requestauthor') {
 9146:                     if ($confhash{$type} ne $unset) {
 9147:                         $changes{$type} = 1;
 9148:                     }
 9149:                 } else {
 9150:                     if (!$confhash{$item}{$type}) {
 9151:                         $changes{$item}{$type} = 1;
 9152:                     }
 9153:                 }
 9154:             }
 9155:         }
 9156:     }
 9157:     unless (($context eq 'requestcourses') || ($context eq 'requestauthor')) {
 9158:         if (ref($domconfig{'quotas'}) eq 'HASH') {
 9159:             if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 9160:                 foreach my $key (keys(%{$domconfig{'quotas'}{'defaultquota'}})) {
 9161:                     if (exists($confhash{'defaultquota'}{$key})) {
 9162:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{'defaultquota'}{$key}) {
 9163:                             $changes{'defaultquota'}{$key} = 1;
 9164:                         }
 9165:                     } else {
 9166:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{'defaultquota'}{$key};
 9167:                     }
 9168:                 }
 9169:             } else {
 9170:                 foreach my $key (keys(%{$domconfig{'quotas'}})) {
 9171:                     if (exists($confhash{'defaultquota'}{$key})) {
 9172:                         if ($confhash{'defaultquota'}{$key} ne $domconfig{'quotas'}{$key}) {
 9173:                             $changes{'defaultquota'}{$key} = 1;
 9174:                         }
 9175:                     } else {
 9176:                         $confhash{'defaultquota'}{$key} = $domconfig{'quotas'}{$key};
 9177:                     }
 9178:                 }
 9179:             }
 9180:             if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9181:                 foreach my $key (keys(%{$domconfig{'quotas'}{'authorquota'}})) {
 9182:                     if (exists($confhash{'authorquota'}{$key})) {
 9183:                         if ($confhash{'authorquota'}{$key} ne $domconfig{'quotas'}{'authorquota'}{$key}) {
 9184:                             $changes{'authorquota'}{$key} = 1;
 9185:                         }
 9186:                     } else {
 9187:                         $confhash{'authorquota'}{$key} = $domconfig{'quotas'}{'authorquota'}{$key};
 9188:                     }
 9189:                 }
 9190:             }
 9191:         }
 9192:         if (ref($confhash{'defaultquota'}) eq 'HASH') {
 9193:             foreach my $key (keys(%{$confhash{'defaultquota'}})) {
 9194:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9195:                     if (ref($domconfig{'quotas'}{'defaultquota'}) eq 'HASH') {
 9196:                         if (!exists($domconfig{'quotas'}{'defaultquota'}{$key})) {
 9197:                             $changes{'defaultquota'}{$key} = 1;
 9198:                         }
 9199:                     } else {
 9200:                         if (!exists($domconfig{'quotas'}{$key})) {
 9201:                             $changes{'defaultquota'}{$key} = 1;
 9202:                         }
 9203:                     }
 9204:                 } else {
 9205:                     $changes{'defaultquota'}{$key} = 1;
 9206:                 }
 9207:             }
 9208:         }
 9209:         if (ref($confhash{'authorquota'}) eq 'HASH') {
 9210:             foreach my $key (keys(%{$confhash{'authorquota'}})) {
 9211:                 if (ref($domconfig{'quotas'}) eq 'HASH') {
 9212:                     if (ref($domconfig{'quotas'}{'authorquota'}) eq 'HASH') {
 9213:                         if (!exists($domconfig{'quotas'}{'authorquota'}{$key})) {
 9214:                             $changes{'authorquota'}{$key} = 1;
 9215:                         }
 9216:                     } else {
 9217:                         $changes{'authorquota'}{$key} = 1;
 9218:                     }
 9219:                 } else {
 9220:                     $changes{'authorquota'}{$key} = 1;
 9221:                 }
 9222:             }
 9223:         }
 9224:     }
 9225: 
 9226:     if ($context eq 'requestauthor') {
 9227:         $domdefaults{'requestauthor'} = \%confhash;
 9228:     } else {
 9229:         foreach my $key (keys(%confhash)) {
 9230:             unless (($context eq 'requestcourses') && (($key eq 'textbooks') || ($key eq 'templates'))) {
 9231:                 $domdefaults{$key} = $confhash{$key};
 9232:             }
 9233:         }
 9234:     }
 9235: 
 9236:     my %quotahash = (
 9237:                       $action => { %confhash }
 9238:                     );
 9239:     my $putresult = &Apache::lonnet::put_dom('configuration',\%quotahash,
 9240:                                              $dom);
 9241:     if ($putresult eq 'ok') {
 9242:         if (keys(%changes) > 0) {
 9243:             my $cachetime = 24*60*60;
 9244:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
 9245:             if (ref($lastactref) eq 'HASH') {
 9246:                 $lastactref->{'domdefaults'} = 1;
 9247:             }
 9248:             $resulttext = &mt('Changes made:').'<ul>';
 9249:             unless (($context eq 'requestcourses') ||
 9250:                     ($context eq 'requestauthor')) {
 9251:                 if (ref($changes{'defaultquota'}) eq 'HASH') {
 9252:                     $resulttext .= '<li>'.&mt('Portfolio default quotas').'<ul>';
 9253:                     foreach my $type (@{$types},'default') {
 9254:                         if (defined($changes{'defaultquota'}{$type})) {
 9255:                             my $typetitle = $usertypes->{$type};
 9256:                             if ($type eq 'default') {
 9257:                                 $typetitle = $othertitle;
 9258:                             }
 9259:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'defaultquota'}{$type}).'</li>';
 9260:                         }
 9261:                     }
 9262:                     $resulttext .= '</ul></li>';
 9263:                 }
 9264:                 if (ref($changes{'authorquota'}) eq 'HASH') {
 9265:                     $resulttext .= '<li>'.&mt('Authoring Space default quotas').'<ul>';
 9266:                     foreach my $type (@{$types},'default') {
 9267:                         if (defined($changes{'authorquota'}{$type})) {
 9268:                             my $typetitle = $usertypes->{$type};
 9269:                             if ($type eq 'default') {
 9270:                                 $typetitle = $othertitle;
 9271:                             }
 9272:                             $resulttext .= '<li>'.&mt('[_1] set to [_2] MB',$typetitle,$confhash{'authorquota'}{$type}).'</li>';
 9273:                         }
 9274:                     }
 9275:                     $resulttext .= '</ul></li>';
 9276:                 }
 9277:             }
 9278:             my %newenv;
 9279:             foreach my $item (@usertools) {
 9280:                 my (%haschgs,%inconf);
 9281:                 if ($context eq 'requestauthor') {
 9282:                     %haschgs = %changes;
 9283:                     %inconf = %confhash;
 9284:                 } else {
 9285:                     if (ref($changes{$item}) eq 'HASH') {
 9286:                         %haschgs = %{$changes{$item}};
 9287:                     }
 9288:                     if (ref($confhash{$item}) eq 'HASH') {
 9289:                         %inconf = %{$confhash{$item}};
 9290:                     }
 9291:                 }
 9292:                 if (keys(%haschgs) > 0) {
 9293:                     my $newacc = 
 9294:                         &Apache::lonnet::usertools_access($env{'user.name'},
 9295:                                                           $env{'user.domain'},
 9296:                                                           $item,'reload',$context);
 9297:                     if (($context eq 'requestcourses') ||
 9298:                         ($context eq 'requestauthor')) {
 9299:                         if ($env{'environment.canrequest.'.$item} ne $newacc) {
 9300:                             $newenv{'environment.canrequest.'.$item} = $newacc;
 9301:                         }
 9302:                     } else {
 9303:                         if ($env{'environment.availabletools.'.$item} ne $newacc) { 
 9304:                             $newenv{'environment.availabletools.'.$item} = $newacc;
 9305:                         }
 9306:                     }
 9307:                     unless ($context eq 'requestauthor') {
 9308:                         $resulttext .= '<li>'.$titles{$item}.'<ul>';
 9309:                     }
 9310:                     foreach my $type (@{$types},'default','_LC_adv') {
 9311:                         if ($haschgs{$type}) {
 9312:                             my $typetitle = $usertypes->{$type};
 9313:                             if ($type eq 'default') {
 9314:                                 $typetitle = $othertitle;
 9315:                             } elsif ($type eq '_LC_adv') {
 9316:                                 $typetitle = 'LON-CAPA Advanced Users'; 
 9317:                             }
 9318:                             if ($inconf{$type}) {
 9319:                                 if ($context eq 'requestcourses') {
 9320:                                     my $cond;
 9321:                                     if ($inconf{$type} =~ /^autolimit=(\d*)$/) {
 9322:                                         if ($1 eq '') {
 9323:                                             $cond = &mt('(Automatic processing of any request).');
 9324:                                         } else {
 9325:                                             $cond = &mt('(Automatic processing of requests up to limit of [quant,_1,request] per user).',$1);
 9326:                                         }
 9327:                                     } else { 
 9328:                                         $cond = $conditions{$inconf{$type}};
 9329:                                     }
 9330:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1].',$typetitle).' '.$cond.'</li>';
 9331:                                 } elsif ($context eq 'requestauthor') {
 9332:                                     $resulttext .= '<li>'.&mt('Set to "[_1]" for "[_2]".',
 9333:                                                              $titles{$inconf{$type}},$typetitle);
 9334: 
 9335:                                 } else {
 9336:                                     $resulttext .= '<li>'.&mt('Set to be available to [_1]',$typetitle).'</li>';
 9337:                                 }
 9338:                             } else {
 9339:                                 if ($type eq '_LC_adv') {
 9340:                                     if ($inconf{$type} eq '0') {
 9341:                                         $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9342:                                     } else { 
 9343:                                         $resulttext .= '<li>'.&mt('No override set for [_1]',$typetitle).'</li>';
 9344:                                     }
 9345:                                 } else {
 9346:                                     $resulttext .= '<li>'.&mt('Set to be unavailable to [_1]',$typetitle).'</li>';
 9347:                                 }
 9348:                             }
 9349:                         }
 9350:                     }
 9351:                     unless ($context eq 'requestauthor') {
 9352:                         $resulttext .= '</ul></li>';
 9353:                     }
 9354:                 }
 9355:             }
 9356:             if (($action eq 'requestcourses') || ($action eq 'requestauthor')) {
 9357:                 if (ref($changes{'notify'}) eq 'HASH') {
 9358:                     if ($changes{'notify'}{'approval'}) {
 9359:                         if (ref($confhash{'notify'}) eq 'HASH') {
 9360:                             if ($confhash{'notify'}{'approval'}) {
 9361:                                 $resulttext .= '<li>'.&mt('Notification of requests requiring approval will be sent to: ').$confhash{'notify'}{'approval'}.'</li>';
 9362:                             } else {
 9363:                                 $resulttext .= '<li>'.&mt('No Domain Coordinators will receive notification of requests requiring approval.').'</li>';
 9364:                             }
 9365:                         }
 9366:                     }
 9367:                 }
 9368:             }
 9369:             if ($action eq 'requestcourses') {
 9370:                 my @offon = ('off','on');
 9371:                 if ($changes{'uniquecode'}) {
 9372:                     if (ref($confhash{'uniquecode'}) eq 'HASH') {
 9373:                         my $codestr = join(' ',map{ &mt($_); } sort(keys(%{$confhash{'uniquecode'}})));
 9374:                         $resulttext .= '<li>'.
 9375:                                        &mt('Generation of six character code as course identifier for distribution to students set to on for: [_1].','<b>'.$codestr.'</b>').
 9376:                                        '</li>';
 9377:                     } else {
 9378:                         $resulttext .= '<li>'.&mt('Generation of six character code as course identifier for distribution to students set to off.').
 9379:                                        '</li>';
 9380:                     }
 9381:                 }
 9382:                 foreach my $type ('textbooks','templates') {
 9383:                     if (ref($changes{$type}) eq 'HASH') {
 9384:                         $resulttext .= '<li>'.&mt("Available $type updated").'<ul>';
 9385:                         foreach my $key (sort(keys(%{$changes{$type}}))) {
 9386:                             my %coursehash = &Apache::lonnet::coursedescription($key);
 9387:                             my $coursetitle = $coursehash{'description'};
 9388:                             my $position = $confhash{$type}{$key}{'order'} + 1;
 9389:                             $resulttext .= '<li>';
 9390:                             foreach my $item ('subject','title','publisher','author') {
 9391:                                 next if ((($item eq 'author') || ($item eq 'publisher')) &&
 9392:                                          ($type eq 'templates'));
 9393:                                 my $name = $item.':';
 9394:                                 $name =~ s/^(\w)/\U$1/;
 9395:                                 $resulttext .= &mt($name).' '.$confhash{$type}{$key}{$item}.'<br />';
 9396:                             }
 9397:                             $resulttext .= ' '.&mt('Order: [_1]',$position).'<br />';
 9398:                             if ($type eq 'textbooks') {
 9399:                                 if ($confhash{$type}{$key}{'image'}) {
 9400:                                     $resulttext .= ' '.&mt('Image: [_1]',
 9401:                                                    '<img src="'.$confhash{$type}{$key}{'image'}.'"'.
 9402:                                                    ' alt="Textbook cover" />').'<br />';
 9403:                                 }
 9404:                             }
 9405:                             $resulttext .= ' '.&mt('LON-CAPA Course: [_1]',$coursetitle).'</li>';
 9406:                         }
 9407:                         $resulttext .= '</ul></li>';
 9408:                     }
 9409:                 }
 9410:                 if (ref($changes{'validation'}) eq 'HASH') {
 9411:                     if ((ref($validationitemsref) eq 'ARRAY') && (ref($validationnamesref) eq 'HASH')) {
 9412:                         $resulttext .= '<li>'.&mt('Validation of courses/communities updated').'<ul>';
 9413:                         foreach my $item (@{$validationitemsref}) {
 9414:                             if (exists($changes{'validation'}{$item})) {
 9415:                                 if ($item eq 'markup') {
 9416:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9417:                                                               '<br /><pre>'.$changes{'validation'}{$item}.'</pre>').'</li>';
 9418:                                 } else {
 9419:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$validationnamesref->{$item},
 9420:                                                               '<b>'.$changes{'validation'}{$item}.'</b>').'</li>';
 9421:                                 }
 9422:                             }
 9423:                         }
 9424:                         if (exists($changes{'validation'}{'dc'})) {
 9425:                             $resulttext .= '<li>'.&mt('Validated course requests identified as processed by: [_1]',
 9426:                                                      '<b>'.$changes{'validation'}{'dc'}.'</b>').'</li>';
 9427:                         }
 9428:                     }
 9429:                 }
 9430:             }
 9431:             $resulttext .= '</ul>';
 9432:             if (keys(%newenv)) {
 9433:                 &Apache::lonnet::appenv(\%newenv);
 9434:             }
 9435:         } else {
 9436:             if ($context eq 'requestcourses') {
 9437:                 $resulttext = &mt('No changes made to rights to request creation of courses.');
 9438:             } elsif ($context eq 'requestauthor') {
 9439:                 $resulttext = &mt('No changes made to rights to request author space.');
 9440:             } else {
 9441:                 $resulttext = &mt('No changes made to availability of personal information pages, blogs, portfolios or default quotas');
 9442:             }
 9443:         }
 9444:     } else {
 9445:         $resulttext = '<span class="LC_error">'.
 9446: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
 9447:     }
 9448:     if ($errors) {
 9449:         $resulttext .= '<p>'.&mt('The following errors occurred when modifying Textbook settings.').
 9450:                        '<ul>'.$errors.'</ul></p>';
 9451:     }
 9452:     return $resulttext;
 9453: }
 9454: 
 9455: sub process_textbook_image {
 9456:     my ($r,$dom,$confname,$caller,$cdom,$cnum,$type,$configuserok,$switchserver,$author_ok) = @_;
 9457:     my $filename = $env{'form.'.$caller.'.filename'};
 9458:     my ($error,$url);
 9459:     my ($width,$height) = (50,50);
 9460:     if ($configuserok eq 'ok') {
 9461:         if ($switchserver) {
 9462:             $error = &mt('Upload of textbook image is not permitted to this server: [_1]',
 9463:                          $switchserver);
 9464:         } elsif ($author_ok eq 'ok') {
 9465:             my ($result,$imageurl) =
 9466:                 &publishlogo($r,'upload',$caller,$dom,$confname,
 9467:                              "$type/$dom/$cnum/cover",$width,$height);
 9468:             if ($result eq 'ok') {
 9469:                 $url = $imageurl;
 9470:             } else {
 9471:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
 9472:             }
 9473:         } else {
 9474:             $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);
 9475:         }
 9476:     } else {
 9477:         $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);
 9478:     }
 9479:     return ($url,$error);
 9480: }
 9481: 
 9482: sub modify_ltitools {
 9483:     my ($r,$dom,$action,$lastactref,%domconfig) = @_;
 9484:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
 9485:     my ($newid,@allpos,%changes,%confhash,%encconfig,$errors,$resulttext);
 9486:     my $confname = $dom.'-domainconfig';
 9487:     my $servadm = $r->dir_config('lonAdmEMail');
 9488:     my ($configuserok,$author_ok,$switchserver) = &config_check($dom,$confname,$servadm);
 9489:     my (%posslti,%possfield);
 9490:     my @courseroles = ('cc','in','ta','ep','st');
 9491:     my @ltiroles = qw(Instructor ContentDeveloper TeachingAssistant Learner);
 9492:     map { $posslti{$_} = 1; } @ltiroles;
 9493:     my @allfields = ('fullname','firstname','lastname','email','user','roles');
 9494:     map { $possfield{$_} = 1; } @allfields;
 9495:     my %lt = &ltitools_names(); 
 9496:     if ($env{'form.ltitools_add'}) {
 9497:         my $title = $env{'form.ltitools_add_title'};
 9498:         $title =~ s/(`)/'/g;
 9499:         ($newid,my $error) = &get_ltitools_id($dom,$title);
 9500:         if ($newid) {
 9501:             my $position = $env{'form.ltitools_add_pos'};
 9502:             $position =~ s/\D+//g;
 9503:             if ($position ne '') {
 9504:                 $allpos[$position] = $newid;
 9505:             }
 9506:             $changes{$newid} = 1;
 9507:             foreach my $item ('title','url','key','secret') {
 9508:                 $env{'form.ltitools_add_'.$item} =~ s/(`)/'/g;
 9509:                 if ($env{'form.ltitools_add_'.$item}) {
 9510:                     if (($item eq 'key') || ($item eq 'secret')) {
 9511:                         $encconfig{$newid}{$item} = $env{'form.ltitools_add_'.$item};
 9512:                     } else {
 9513:                         $confhash{$newid}{$item} = $env{'form.ltitools_add_'.$item};
 9514:                     }
 9515:                 }
 9516:             }
 9517:             if ($env{'form.ltitools_add_version'} eq 'LTI-1p0') {
 9518:                 $confhash{$newid}{'version'} = $env{'form.ltitools_add_version'};
 9519:             }
 9520:             if ($env{'form.ltitools_add_msgtype'} eq 'basic-lti-launch-request') {
 9521:                 $confhash{$newid}{'msgtype'} = $env{'form.ltitools_add_msgtype'};
 9522:             }
 9523:             foreach my $item ('width','height','linktext','explanation') {
 9524:                 $env{'form.ltitools_add_'.$item} =~ s/^\s+//;
 9525:                 $env{'form.ltitools_add_'.$item} =~ s/\s+$//;
 9526:                 if (($item eq 'width') || ($item eq 'height')) {
 9527:                     if ($env{'form.ltitools_add_'.$item} =~ /^\d+$/) {
 9528:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item};
 9529:                     }
 9530:                 } else {
 9531:                     if ($env{'form.ltitools_add_'.$item} ne '') {
 9532:                         $confhash{$newid}{'display'}{$item} = $env{'form.ltitools_add_'.$item}; 
 9533:                     }
 9534:                 }
 9535:             }
 9536:             if ($env{'form.ltitools_add_target'} eq 'window') {
 9537:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
 9538:             } elsif ($env{'form.ltitools_add_target'} eq 'tab') {
 9539:                 $confhash{$newid}{'display'}{'target'} = $env{'form.ltitools_add_target'};
 9540:             } else {
 9541:                 $confhash{$newid}{'display'}{'target'} = 'iframe';
 9542:             }
 9543:             foreach my $item ('passback','roster') {
 9544:                 if ($env{'form.ltitools_add_'.$item}) {
 9545:                     $confhash{$newid}{$item} = 1;
 9546:                 }
 9547:             }
 9548:             if ($env{'form.ltitools_add_image.filename'} ne '') {
 9549:                 my ($imageurl,$error) =
 9550:                     &process_ltitools_image($r,$dom,$confname,'ltitools_add_image',$dom,
 9551:                                             $configuserok,$switchserver,$author_ok);
 9552:                 if ($imageurl) {
 9553:                     $confhash{$newid}{'image'} = $imageurl;
 9554:                 }
 9555:                 if ($error) {
 9556:                     &Apache::lonnet::logthis($error);
 9557:                     $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9558:                 }
 9559:             }
 9560:             my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_add_fields');
 9561:             foreach my $field (@fields) {
 9562:                 if ($possfield{$field}) {
 9563:                     if ($field eq 'roles') {
 9564:                         foreach my $role (@courseroles) {
 9565:                             my $choice = $env{'form.ltitools_add_roles_'.$role};
 9566:                             if (($choice ne '') && ($posslti{$choice})) {
 9567:                                 $confhash{$newid}{'roles'}{$role} = $choice;
 9568:                                 if ($role eq 'cc') {
 9569:                                     $confhash{$newid}{'roles'}{'co'} = $choice; 
 9570:                                 }
 9571:                             }
 9572:                         }
 9573:                     } else {
 9574:                         $confhash{$newid}{'fields'}{$field} = 1;
 9575:                     }
 9576:                 }
 9577:             }
 9578:             my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig');
 9579:             foreach my $item (@courseconfig) {
 9580:                 $confhash{$newid}{'crsconf'}{$item} = 1;
 9581:             }
 9582:             if ($env{'form.ltitools_add_custom'}) {
 9583:                 my $name = $env{'form.ltitools_add_custom_name'};
 9584:                 my $value = $env{'form.ltitools_add_custom_value'};
 9585:                 $value =~ s/(`)/'/g;
 9586:                 $name =~ s/(`)/'/g;
 9587:                 $confhash{$newid}{'custom'}{$name} = $value;
 9588:             }
 9589:         } else {
 9590:             my $error = &mt('Failed to acquire unique ID for new external tool');   
 9591:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9592:         }
 9593:     }
 9594:     if (ref($domconfig{$action}) eq 'HASH') {
 9595:         my %deletions;
 9596:         my @todelete = &Apache::loncommon::get_env_multiple('form.ltitools_del');
 9597:         if (@todelete) {
 9598:             map { $deletions{$_} = 1; } @todelete;
 9599:         }
 9600:         my %customadds;
 9601:         my @newcustom = &Apache::loncommon::get_env_multiple('form.ltitools_customadd');
 9602:         if (@newcustom) {
 9603:             map { $customadds{$_} = 1; } @newcustom;
 9604:         } 
 9605:         my %imgdeletions;
 9606:         my @todeleteimages = &Apache::loncommon::get_env_multiple('form.ltitools_image_del');
 9607:         if (@todeleteimages) {
 9608:             map { $imgdeletions{$_} = 1; } @todeleteimages;
 9609:         }
 9610:         my $maxnum = $env{'form.ltitools_maxnum'};
 9611:         for (my $i=0; $i<=$maxnum; $i++) {
 9612:             my $itemid = $env{'form.ltitools_id_'.$i};
 9613:             if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
 9614:                 if ($deletions{$itemid}) {
 9615:                     if ($domconfig{$action}{$itemid}{'image'}) {
 9616:                         #FIXME need to obsolete item in RES space
 9617:                     }
 9618:                     $changes{$itemid} = $domconfig{$action}{$itemid}{'title'};
 9619:                     next;
 9620:                 } else {
 9621:                     my $newpos = $env{'form.ltitools_'.$itemid};
 9622:                     $newpos =~ s/\D+//g;
 9623:                     foreach my $item ('title','url') {
 9624:                         $confhash{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
 9625:                         if ($domconfig{$action}{$itemid}{$item} ne $confhash{$itemid}{$item}) {
 9626:                             $changes{$itemid} = 1;
 9627:                         }
 9628:                     }
 9629:                     foreach my $item ('key','secret') {
 9630:                         $encconfig{$itemid}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
 9631:                         if ($domconfig{$action}{$itemid}{$item} ne $encconfig{$itemid}{$item}) {
 9632:                             $changes{$itemid} = 1;
 9633:                         }
 9634:                     }
 9635:                     if ($env{'form.ltitools_version_'.$i} eq 'LTI-1p0') {
 9636:                         $confhash{$itemid}{'version'} = $env{'form.ltitools_version_'.$i};
 9637:                     }
 9638:                     if ($env{'form.ltitools_msgtype_'.$i} eq 'basic-lti-launch-request') {
 9639:                         $confhash{$itemid}{'msgtype'} = $env{'form.ltitools_msgtype_'.$i};
 9640:                     }
 9641:                     foreach my $size ('width','height') {
 9642:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/^\s+//;
 9643:                         $env{'form.ltitools_'.$size.'_'.$i} =~ s/\s+$//;
 9644:                         if ($env{'form.ltitools_'.$size.'_'.$i} =~ /^\d+$/) {
 9645:                             $confhash{$itemid}{'display'}{$size} = $env{'form.ltitools_'.$size.'_'.$i};
 9646:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9647:                                 if ($domconfig{$action}{$itemid}{'display'}{$size} ne $confhash{$itemid}{'display'}{$size}) {
 9648:                                     $changes{$itemid} = 1;
 9649:                                 }
 9650:                             } else {
 9651:                                 $changes{$itemid} = 1;
 9652:                             }
 9653:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9654:                             if ($domconfig{$action}{$itemid}{'display'}{$size} ne '') {
 9655:                                 $changes{$itemid} = 1;
 9656:                             }
 9657:                         }
 9658:                     }
 9659:                     foreach my $item ('linktext','explanation') {
 9660:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/^\s+//;
 9661:                         $env{'form.ltitools_'.$item.'_'.$i} =~ s/\s+$//;
 9662:                         if ($env{'form.ltitools_'.$item.'_'.$i} ne '') {
 9663:                             $confhash{$itemid}{'display'}{$item} = $env{'form.ltitools_'.$item.'_'.$i};
 9664:                             if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9665:                                 if ($domconfig{$action}{$itemid}{'display'}{$item} ne $confhash{$itemid}{'display'}{$item}) {
 9666:                                     $changes{$itemid} = 1;
 9667:                                 }
 9668:                             } else {
 9669:                                 $changes{$itemid} = 1;
 9670:                             }
 9671:                         } elsif (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9672:                             if ($domconfig{$action}{$itemid}{'display'}{$item} ne '') {
 9673:                                 $changes{$itemid} = 1;
 9674:                             }
 9675:                         }
 9676:                     }
 9677:                     if ($env{'form.ltitools_target_'.$i} eq 'window') {
 9678:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
 9679:                     } elsif ($env{'form.ltitools_target_'.$i} eq 'tab') {
 9680:                         $confhash{$itemid}{'display'}{'target'} = $env{'form.ltitools_target_'.$i};
 9681:                     } else {
 9682:                         $confhash{$itemid}{'display'}{'target'} = 'iframe';
 9683:                     }
 9684:                     if (ref($domconfig{$action}{$itemid}{'display'}) eq 'HASH') {
 9685:                         if ($domconfig{$action}{$itemid}{'display'}{'target'} ne $confhash{$itemid}{'display'}{'target'}) {
 9686:                             $changes{$itemid} = 1;
 9687:                         }
 9688:                     } else {
 9689:                         $changes{$itemid} = 1;
 9690:                     }
 9691:                     foreach my $extra ('passback','roster') {
 9692:                         if ($env{'form.ltitools_'.$extra.'_'.$i}) {
 9693:                             $confhash{$itemid}{$extra} = 1;
 9694:                         }
 9695:                         if ($domconfig{$action}{$itemid}{$extra} ne $confhash{$itemid}{$extra}) {
 9696:                             $changes{$itemid} = 1;
 9697:                         }
 9698:                     }
 9699:                     my @courseconfig = &Apache::loncommon::get_env_multiple('form.ltitools_courseconfig_'.$i);
 9700:                     foreach my $item ('label','title','target','linktext','explanation') {
 9701:                         if (grep(/^\Q$item\E$/,@courseconfig)) {
 9702:                             $confhash{$itemid}{'crsconf'}{$item} = 1;
 9703:                             if (ref($domconfig{$action}{$itemid}{'crsconf'}) eq 'HASH') {
 9704:                                 if ($domconfig{$action}{$itemid}{'crsconf'}{$item} ne $confhash{$itemid}{'crsconf'}{$item}) {
 9705:                                     $changes{$itemid} = 1;
 9706:                                 }
 9707:                             } else {
 9708:                                 $changes{$itemid} = 1;
 9709:                             }
 9710:                         }
 9711:                     }
 9712:                     my @fields = &Apache::loncommon::get_env_multiple('form.ltitools_fields_'.$i);
 9713:                     foreach my $field (@fields) {
 9714:                         if ($possfield{$field}) {
 9715:                             if ($field eq 'roles') {
 9716:                                 foreach my $role (@courseroles) {
 9717:                                     my $choice = $env{'form.ltitools_roles_'.$role.'_'.$i};
 9718:                                     if (($choice ne '') && ($posslti{$choice})) {
 9719:                                         $confhash{$itemid}{'roles'}{$role} = $choice;
 9720:                                         if ($role eq 'cc') {
 9721:                                             $confhash{$itemid}{'roles'}{'co'} = $choice;
 9722:                                         }
 9723:                                     }
 9724:                                     if (ref($domconfig{$action}{$itemid}{'roles'}) eq 'HASH') {
 9725:                                         if ($domconfig{$action}{$itemid}{'roles'}{$role} ne $confhash{$itemid}{'roles'}{$role}) {
 9726:                                             $changes{$itemid} = 1;
 9727:                                         }
 9728:                                     } elsif ($confhash{$itemid}{'roles'}{$role}) {
 9729:                                         $changes{$itemid} = 1;
 9730:                                     }
 9731:                                 }
 9732:                             } else {
 9733:                                 $confhash{$itemid}{'fields'}{$field} = 1;
 9734:                                 if (ref($domconfig{$action}{$itemid}{'fields'}) eq 'HASH') {
 9735:                                     if ($domconfig{$action}{$itemid}{'fields'}{$field} ne $confhash{$itemid}{'fields'}{$field}) {
 9736:                                         $changes{$itemid} = 1;
 9737:                                     }
 9738:                                 } else {
 9739:                                     $changes{$itemid} = 1;
 9740:                                 }
 9741:                             }
 9742:                         }
 9743:                     }
 9744:                     $allpos[$newpos] = $itemid;
 9745:                 }
 9746:                 if ($imgdeletions{$itemid}) {
 9747:                     $changes{$itemid} = 1;
 9748:                     #FIXME need to obsolete item in RES space
 9749:                 } elsif ($env{'form.ltitools_image_'.$i.'.filename'}) {
 9750:                     my ($imgurl,$error) = &process_ltitools_image($r,$dom,$confname,'ltitools_image_'.$i,
 9751:                                                                  $itemid,$configuserok,$switchserver,
 9752:                                                                  $author_ok);
 9753:                     if ($imgurl) {
 9754:                         $confhash{$itemid}{'image'} = $imgurl;
 9755:                         $changes{$itemid} = 1;
 9756:                     }
 9757:                     if ($error) {
 9758:                         &Apache::lonnet::logthis($error);
 9759:                         $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
 9760:                     }
 9761:                 } elsif ($domconfig{$action}{$itemid}{'image'}) {
 9762:                     $confhash{$itemid}{'image'} =
 9763:                        $domconfig{$action}{$itemid}{'image'};
 9764:                 }
 9765:                 if ($customadds{$i}) {
 9766:                     my $name = $env{'form.ltitools_custom_name_'.$i};
 9767:                     $name =~ s/(`)/'/g;
 9768:                     $name =~ s/^\s+//;
 9769:                     $name =~ s/\s+$//;
 9770:                     my $value = $env{'form.ltitools_custom_value_'.$i};
 9771:                     $value =~ s/(`)/'/g;
 9772:                     $value =~ s/^\s+//;
 9773:                     $value =~ s/\s+$//;
 9774:                     if ($name ne '') {
 9775:                         $confhash{$itemid}{'custom'}{$name} = $value;
 9776:                         $changes{$itemid} = 1;
 9777:                     }
 9778:                 }
 9779:                 my %customdels;
 9780:                 my @customdeletions = &Apache::loncommon::get_env_multiple('form.ltitools_customdel_'.$i); 
 9781:                 if (@customdeletions) {
 9782:                     $changes{$itemid} = 1;
 9783:                 }
 9784:                 map { $customdels{$_} = 1; } @customdeletions;
 9785:                 if (ref($domconfig{$action}{$itemid}{'custom'}) eq 'HASH') {
 9786:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}{'custom'}})) {
 9787:                         unless ($customdels{$key}) {
 9788:                             if ($env{'form.ltitools_customval_'.$key.'_'.$i} ne '') {
 9789:                                 $confhash{$itemid}{'custom'}{$key} = $env{'form.ltitools_customval_'.$key.'_'.$i}; 
 9790:                             }
 9791:                             if ($domconfig{$action}{$itemid}{'custom'}{$key} ne $env{'form.ltitools_customval_'.$key.'_'.$i}) {
 9792:                                 $changes{$itemid} = 1;
 9793:                             }
 9794:                         }
 9795:                     }
 9796:                 }
 9797:                 unless ($changes{$itemid}) {
 9798:                     foreach my $key (keys(%{$domconfig{$action}{$itemid}})) {
 9799:                         if (ref($domconfig{$action}{$itemid}{$key}) eq 'HASH') {
 9800:                             if (ref($confhash{$itemid}{$key}) eq 'HASH') {
 9801:                                 foreach my $innerkey (keys(%{$domconfig{$action}{$itemid}{$key}})) {
 9802:                                     unless (exists($confhash{$itemid}{$key}{$innerkey})) {
 9803:                                         $changes{$itemid} = 1;
 9804:                                         last;
 9805:                                     }
 9806:                                 }
 9807:                             } elsif (keys(%{$domconfig{$action}{$itemid}{$key}}) > 0) {
 9808:                                 $changes{$itemid} = 1;
 9809:                             }
 9810:                         }
 9811:                         last if ($changes{$itemid});
 9812:                     }
 9813:                 }
 9814:             }
 9815:         }
 9816:     }
 9817:     if (@allpos > 0) {
 9818:         my $idx = 0;
 9819:         foreach my $itemid (@allpos) {
 9820:             if ($itemid ne '') {
 9821:                 $confhash{$itemid}{'order'} = $idx;
 9822:                 if (ref($domconfig{$action}) eq 'HASH') {
 9823:                     if (ref($domconfig{$action}{$itemid}) eq 'HASH') {
 9824:                         if ($domconfig{$action}{$itemid}{'order'} ne $idx) {
 9825:                             $changes{$itemid} = 1;
 9826:                         }
 9827:                     }
 9828:                 }
 9829:                 $idx ++;
 9830:             }
 9831:         }
 9832:     }
 9833:     my %ltitoolshash = (
 9834:                           $action => { %confhash }
 9835:                        );
 9836:     my $putresult = &Apache::lonnet::put_dom('configuration',\%ltitoolshash,
 9837:                                              $dom);
 9838:     if ($putresult eq 'ok') {
 9839:         my %ltienchash = (
 9840:                              $action => { %encconfig }
 9841:                          );
 9842:         &Apache::lonnet::put_dom('encconfig',\%ltienchash,$dom);
 9843:         if (keys(%changes) > 0) {
 9844:             my $cachetime = 24*60*60;
 9845:             my %ltiall = %confhash;
 9846:             foreach my $id (keys(%ltiall)) {
 9847:                 if (ref($encconfig{$id}) eq 'HASH') {
 9848:                     foreach my $item ('key','secret') {
 9849:                         $ltiall{$id}{$item} = $encconfig{$id}{$item};
 9850:                     }
 9851:                 }
 9852:             }
 9853:             &Apache::lonnet::do_cache_new('ltitools',$dom,\%ltiall,$cachetime);
 9854:             if (ref($lastactref) eq 'HASH') {
 9855:                 $lastactref->{'ltitools'} = 1;
 9856:             }
 9857:             $resulttext = &mt('Changes made:').'<ul>';
 9858:             my %bynum;
 9859:             foreach my $itemid (sort(keys(%changes))) {
 9860:                 my $position = $confhash{$itemid}{'order'};
 9861:                 $bynum{$position} = $itemid;
 9862:             }
 9863:             foreach my $pos (sort { $a <=> $b } keys(%bynum)) {
 9864:                 my $itemid = $bynum{$pos}; 
 9865:                 if (ref($confhash{$itemid}) ne 'HASH') {
 9866:                     $resulttext .= '<li>'.&mt('Deleted: [_1]',$changes{$itemid}).'</li>';
 9867:                 } else {
 9868:                     $resulttext .= '<li><b>'.$confhash{$itemid}{'title'}.'</b>';
 9869:                     if ($confhash{$itemid}{'image'}) {
 9870:                         $resulttext .= '&nbsp;'.
 9871:                                        '<img src="'.$confhash{$itemid}{'image'}.'"'.
 9872:                                        ' alt="'.&mt('Tool Provider icon').'" />';
 9873:                     }
 9874:                     $resulttext .= '</li><ul>';
 9875:                     my $position = $pos + 1;
 9876:                     $resulttext .= '<li>'.&mt('Order: [_1]',$position).'</li>';
 9877:                     foreach my $item ('version','msgtype','url') {
 9878:                         if ($confhash{$itemid}{$item} ne '') {
 9879:                             $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{$item}.'</li>';
 9880:                         }
 9881:                     }
 9882:                     if ($encconfig{$itemid}{'key'} ne '') {
 9883:                         $resulttext .= '<li>'.$lt{'key'}.':&nbsp;'.$encconfig{$itemid}{'key'}.'</li>';
 9884:                     }
 9885:                     if ($encconfig{$itemid}{'secret'} ne '') {
 9886:                         $resulttext .= '<li>'.$lt{'secret'}.':&nbsp;';
 9887:                         my $num = length($encconfig{$itemid}{'secret'});
 9888:                         $resulttext .= ('*'x$num).'</li>';
 9889:                     }
 9890:                     $resulttext .= '<li>'.&mt('Configurable in course:');
 9891:                     my @possconfig = ('label','title','target','linktext','explanation');
 9892:                     my $numconfig = 0; 
 9893:                     if (ref($confhash{$itemid}{'crsconf'}) eq 'HASH') { 
 9894:                         foreach my $item (@possconfig) {
 9895:                             if ($confhash{$itemid}{'crsconf'}{$item}) {
 9896:                                 $numconfig ++;
 9897:                                 $resulttext .= ' "'.$lt{'crs'.$item}.'"';
 9898:                             }
 9899:                         }
 9900:                     }
 9901:                     if (!$numconfig) {
 9902:                         $resulttext .= &mt('None');
 9903:                     }
 9904:                     $resulttext .= '</li>';
 9905:                     foreach my $item ('passback','roster') {
 9906:                         $resulttext .= '<li>'.$lt{$item}.'&nbsp;';
 9907:                         if ($confhash{$itemid}{$item}) {
 9908:                             $resulttext .= &mt('Yes');
 9909:                         } else {
 9910:                             $resulttext .= &mt('No');
 9911:                         }
 9912:                         $resulttext .= '</li>';
 9913:                     }
 9914:                     if (ref($confhash{$itemid}{'display'}) eq 'HASH') {
 9915:                         my $displaylist;
 9916:                         if ($confhash{$itemid}{'display'}{'target'}) {
 9917:                             $displaylist = &mt('Display target').':&nbsp;'.
 9918:                                            $confhash{$itemid}{'display'}{'target'}.',';
 9919:                         }
 9920:                         foreach my $size ('width','height') { 
 9921:                             if ($confhash{$itemid}{'display'}{$size}) {
 9922:                                 $displaylist .= ('&nbsp;'x2).$lt{$size}.':&nbsp;'.
 9923:                                                 $confhash{$itemid}{'display'}{$size}.',';
 9924:                             }
 9925:                         }
 9926:                         if ($displaylist) {
 9927:                             $displaylist =~ s/,$//;
 9928:                             $resulttext .= '<li>'.$displaylist.'</li>';
 9929:                         }
 9930:                         foreach my $item ('linktext','explanation') {
 9931:                             if ($confhash{$itemid}{'display'}{$item}) {
 9932:                                 $resulttext .= '<li>'.$lt{$item}.':&nbsp;'.$confhash{$itemid}{'display'}{$item}.'</li>';
 9933:                             }
 9934:                         }
 9935:                     }
 9936:                     if (ref($confhash{$itemid}{'fields'}) eq 'HASH') {
 9937:                         my $fieldlist;
 9938:                         foreach my $field (@allfields) {
 9939:                             if ($confhash{$itemid}{'fields'}{$field}) {
 9940:                                 $fieldlist .= ('&nbsp;'x2).$lt{$field}.',';
 9941:                             }
 9942:                         }
 9943:                         if ($fieldlist) {
 9944:                             $fieldlist =~ s/,$//;
 9945:                             $resulttext .= '<li>'.&mt('Data sent').':'.$fieldlist.'</li>';
 9946:                         }
 9947:                     }
 9948:                     if (ref($confhash{$itemid}{'roles'}) eq 'HASH') {
 9949:                         my $rolemaps;
 9950:                         foreach my $role (@courseroles) {
 9951:                             if ($confhash{$itemid}{'roles'}{$role}) {
 9952:                                 $rolemaps .= ('&nbsp;'x2).&Apache::lonnet::plaintext($role,'Course').'='.
 9953:                                              $confhash{$itemid}{'roles'}{$role}.',';
 9954:                             }
 9955:                         }
 9956:                         if ($rolemaps) {
 9957:                             $rolemaps =~ s/,$//; 
 9958:                             $resulttext .= '<li>'.&mt('Role mapping:').$rolemaps.'</li>';
 9959:                         }
 9960:                     }
 9961:                     if (ref($confhash{$itemid}{'custom'}) eq 'HASH') {
 9962:                         my $customlist;
 9963:                         if (keys(%{$confhash{$itemid}{'custom'}})) {
 9964:                             foreach my $key (sort(keys(%{$confhash{$itemid}{'custom'}}))) {
 9965:                                 $customlist .= $key.':'.$confhash{$itemid}{'custom'}{$key}.('&nbsp;'x2);
 9966:                             } 
 9967:                         }
 9968:                         if ($customlist) {
 9969:                             $resulttext .= '<li>'.&mt('Custom items').':'.$customlist.'</li>';
 9970:                         }
 9971:                     } 
 9972:                     $resulttext .= '</ul></li>';
 9973:                 }
 9974:             }
 9975:             $resulttext .= '</ul>';
 9976:         } else {
 9977:             $resulttext = &mt('No changes made.');
 9978:         }
 9979:     } else {
 9980:         $errors .= '<li><span class="LC_error">'.&mt('Failed to save changes').'</span></li>';
 9981:     }
 9982:     if ($errors) {
 9983:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
 9984:                        $errors.'</ul>';
 9985:     }
 9986:     return $resulttext;
 9987: }
 9988: 
 9989: sub process_ltitools_image {
 9990:     my ($r,$dom,$confname,$caller,$itemid,$configuserok,$switchserver,$author_ok) = @_;
 9991:     my $filename = $env{'form.'.$caller.'.filename'};
 9992:     my ($error,$url);
 9993:     my ($width,$height) = (21,21);
 9994:     if ($configuserok eq 'ok') {
 9995:         if ($switchserver) {
 9996:             $error = &mt('Upload of Tool Provider (LTI) icon is not permitted to this server: [_1]',
 9997:                          $switchserver);
 9998:         } elsif ($author_ok eq 'ok') {
 9999:             my ($result,$imageurl,$madethumb) =
10000:                 &publishlogo($r,'upload',$caller,$dom,$confname,
10001:                              "ltitools/$itemid/icon",$width,$height);
10002:             if ($result eq 'ok') {
10003:                 if ($madethumb) {
10004:                     my ($path,$imagefile) = ($imageurl =~ m{^(.+)/([^/]+)$});
10005:                     my $imagethumb = "$path/tn-".$imagefile;
10006:                     $url = $imagethumb;
10007:                 } else {
10008:                     $url = $imageurl;
10009:                 }
10010:             } else {
10011:                 $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$filename,$result);
10012:             }
10013:         } else {
10014:             $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);
10015:         }
10016:     } else {
10017:         $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);
10018:     }
10019:     return ($url,$error);
10020: }
10021: 
10022: sub get_ltitools_id {
10023:     my ($cdom,$title) = @_;
10024:     # get lock on ltitools db
10025:     my $lockhash = {
10026:                       lock => $env{'user.name'}.
10027:                               ':'.$env{'user.domain'},
10028:                    };
10029:     my $tries = 0;
10030:     my $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
10031:     my ($id,$error);
10032:  
10033:     while (($gotlock ne 'ok') && ($tries<10)) {
10034:         $tries ++;
10035:         sleep (0.1);
10036:         $gotlock = &Apache::lonnet::newput_dom('ltitools',$lockhash,$cdom);
10037:     }
10038:     if ($gotlock eq 'ok') {
10039:         my %currids = &Apache::lonnet::dump_dom('ltitools',$cdom);
10040:         if ($currids{'lock'}) {
10041:             delete($currids{'lock'});
10042:             if (keys(%currids)) {
10043:                 my @curr = sort { $a <=> $b } keys(%currids);
10044:                 if ($curr[-1] =~ /^\d+$/) {
10045:                     $id = 1 + $curr[-1];
10046:                 }
10047:             } else {
10048:                 $id = 1;
10049:             }
10050:             if ($id) {
10051:                 unless (&Apache::lonnet::newput_dom('ltitools',{ $id => $title },$cdom) eq 'ok') {
10052:                     $error = 'nostore';
10053:                 }
10054:             } else {
10055:                 $error = 'nonumber';
10056:             }
10057:         }
10058:         my $dellockoutcome = &Apache::lonnet::del_dom('ltitools',['lock'],$cdom);
10059:     } else {
10060:         $error = 'nolock';
10061:     }
10062:     return ($id,$error);
10063: }
10064: 
10065: sub modify_autoenroll {
10066:     my ($dom,$lastactref,%domconfig) = @_;
10067:     my ($resulttext,%changes);
10068:     my %currautoenroll;
10069:     if (ref($domconfig{'autoenroll'}) eq 'HASH') {
10070:         foreach my $key (keys(%{$domconfig{'autoenroll'}})) {
10071:             $currautoenroll{$key} = $domconfig{'autoenroll'}{$key};
10072:         }
10073:     }
10074:     my $autorun = &Apache::lonnet::auto_run(undef,$dom),
10075:     my %title = ( run => 'Auto-enrollment active',
10076:                   sender => 'Sender for notification messages',
10077:                   coowners => 'Automatic assignment of co-ownership to instructors of record (institutional data)',
10078:                   failsafe => 'Failsafe for no drops if institutional data missing for a section');
10079:     my @offon = ('off','on');
10080:     my $sender_uname = $env{'form.sender_uname'};
10081:     my $sender_domain = $env{'form.sender_domain'};
10082:     if ($sender_domain eq '') {
10083:         $sender_uname = '';
10084:     } elsif ($sender_uname eq '') {
10085:         $sender_domain = '';
10086:     }
10087:     my $coowners = $env{'form.autoassign_coowners'};
10088:     my $failsafe = $env{'form.autoenroll_failsafe'};
10089:     $failsafe =~ s{^\s+|\s+$}{}g;
10090:     if ($failsafe =~ /\D/) {
10091:         undef($failsafe);
10092:     }
10093:     my %autoenrollhash =  (
10094:                        autoenroll => { 'run' => $env{'form.autoenroll_run'},
10095:                                        'sender_uname' => $sender_uname,
10096:                                        'sender_domain' => $sender_domain,
10097:                                        'co-owners' => $coowners,
10098:                                        'autofailsafe' => $failsafe,
10099:                                 }
10100:                      );
10101:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autoenrollhash,
10102:                                              $dom);
10103:     if ($putresult eq 'ok') {
10104:         if (exists($currautoenroll{'run'})) {
10105:              if ($currautoenroll{'run'} ne $env{'form.autoenroll_run'}) {
10106:                  $changes{'run'} = 1;
10107:              }
10108:         } elsif ($autorun) {
10109:             if ($env{'form.autoenroll_run'} ne '1') {
10110:                  $changes{'run'} = 1;
10111:             }
10112:         }
10113:         if ($currautoenroll{'sender_uname'} ne $sender_uname) {
10114:             $changes{'sender'} = 1;
10115:         }
10116:         if ($currautoenroll{'sender_domain'} ne $sender_domain) {
10117:             $changes{'sender'} = 1;
10118:         }
10119:         if ($currautoenroll{'co-owners'} ne '') {
10120:             if ($currautoenroll{'co-owners'} ne $coowners) {
10121:                 $changes{'coowners'} = 1;
10122:             }
10123:         } elsif ($coowners) {
10124:             $changes{'coowners'} = 1;
10125:         }
10126:         if ($currautoenroll{'autofailsafe'} ne $failsafe) {
10127:             $changes{'autofailsafe'} = 1;
10128:         }
10129:         if (keys(%changes) > 0) {
10130:             $resulttext = &mt('Changes made:').'<ul>';
10131:             if ($changes{'run'}) {
10132:                 $resulttext .= '<li>'.&mt("$title{'run'} set to $offon[$env{'form.autoenroll_run'}]").'</li>';
10133:             }
10134:             if ($changes{'sender'}) {
10135:                 if ($sender_uname eq '' || $sender_domain eq '') {
10136:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to default (course owner).").'</li>';
10137:                 } else {
10138:                     $resulttext .= '<li>'.&mt("$title{'sender'} set to [_1]",$sender_uname.':'.$sender_domain).'</li>';
10139:                 }
10140:             }
10141:             if ($changes{'coowners'}) {
10142:                 $resulttext .= '<li>'.&mt("$title{'coowners'} set to $offon[$env{'form.autoassign_coowners'}]").'</li>';
10143:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
10144:                 if (ref($lastactref) eq 'HASH') {
10145:                     $lastactref->{'domainconfig'} = 1;
10146:                 }
10147:             }
10148:             if ($changes{'autofailsafe'}) {
10149:                 if ($failsafe ne '') {
10150:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section set to: [_1]',$failsafe).'</li>';
10151:                 } else {
10152:                     $resulttext .= '<li>'.&mt('Failsafe for no drops if institutional data missing for a section: deleted');
10153:                 }
10154:                 &Apache::lonnet::get_domain_defaults($dom,1);
10155:                 if (ref($lastactref) eq 'HASH') {
10156:                     $lastactref->{'domdefaults'} = 1;
10157:                 }
10158:             }
10159:             $resulttext .= '</ul>';
10160:         } else {
10161:             $resulttext = &mt('No changes made to auto-enrollment settings');
10162:         }
10163:     } else {
10164:         $resulttext = '<span class="LC_error">'.
10165: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10166:     }
10167:     return $resulttext;
10168: }
10169: 
10170: sub modify_autoupdate {
10171:     my ($dom,%domconfig) = @_;
10172:     my ($resulttext,%currautoupdate,%fields,%changes);
10173:     if (ref($domconfig{'autoupdate'}) eq 'HASH') {
10174:         foreach my $key (keys(%{$domconfig{'autoupdate'}})) {
10175:             $currautoupdate{$key} = $domconfig{'autoupdate'}{$key};
10176:         }
10177:     }
10178:     my @offon = ('off','on');
10179:     my %title = &Apache::lonlocal::texthash (
10180:                    run => 'Auto-update:',
10181:                    classlists => 'Updates to user information in classlists?'
10182:                 );
10183:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10184:     my %fieldtitles = &Apache::lonlocal::texthash (
10185:                         id => 'Student/Employee ID',
10186:                         permanentemail => 'E-mail address',
10187:                         lastname => 'Last Name',
10188:                         firstname => 'First Name',
10189:                         middlename => 'Middle Name',
10190:                         generation => 'Generation',
10191:                       );
10192:     $othertitle = &mt('All users');
10193:     if (keys(%{$usertypes}) >  0) {
10194:         $othertitle = &mt('Other users');
10195:     }
10196:     foreach my $key (keys(%env)) {
10197:         if ($key =~ /^form\.updateable_(.+)_([^_]+)$/) {
10198:             my ($usertype,$item) = ($1,$2);
10199:             if (grep(/^\Q$item\E$/,keys(%fieldtitles))) {
10200:                 if ($usertype eq 'default') {   
10201:                     push(@{$fields{$1}},$2);
10202:                 } elsif (ref($types) eq 'ARRAY') {
10203:                     if (grep(/^\Q$usertype\E$/,@{$types})) {
10204:                         push(@{$fields{$1}},$2);
10205:                     }
10206:                 }
10207:             }
10208:         }
10209:     }
10210:     my @lockablenames = &Apache::loncommon::get_env_multiple('form.lockablenames');
10211:     @lockablenames = sort(@lockablenames);
10212:     if (ref($currautoupdate{'lockablenames'}) eq 'ARRAY') {
10213:         my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
10214:         if (@changed) {
10215:             $changes{'lockablenames'} = 1;
10216:         }
10217:     } else {
10218:         if (@lockablenames) {
10219:             $changes{'lockablenames'} = 1;
10220:         }
10221:     }
10222:     my %updatehash = (
10223:                       autoupdate => { run => $env{'form.autoupdate_run'},
10224:                                       classlists => $env{'form.classlists'},
10225:                                       fields => {%fields},
10226:                                       lockablenames => \@lockablenames,
10227:                                     }
10228:                      );
10229:     foreach my $key (keys(%currautoupdate)) {
10230:         if (($key eq 'run') || ($key eq 'classlists')) {
10231:             if (exists($updatehash{autoupdate}{$key})) {
10232:                 if ($currautoupdate{$key} ne $updatehash{autoupdate}{$key}) {
10233:                     $changes{$key} = 1;
10234:                 }
10235:             }
10236:         } elsif ($key eq 'fields') {
10237:             if (ref($currautoupdate{$key}) eq 'HASH') {
10238:                 foreach my $item (@{$types},'default') {
10239:                     if (ref($currautoupdate{$key}{$item}) eq 'ARRAY') {
10240:                         my $change = 0;
10241:                         foreach my $type (@{$currautoupdate{$key}{$item}}) {
10242:                             if (!exists($fields{$item})) {
10243:                                 $change = 1;
10244:                                 last;
10245:                             } elsif (ref($fields{$item}) eq 'ARRAY') {
10246:                                 if (!grep(/^\Q$type\E$/,@{$fields{$item}})) {
10247:                                     $change = 1;
10248:                                     last;
10249:                                 }
10250:                             }
10251:                         }
10252:                         if ($change) {
10253:                             push(@{$changes{$key}},$item);
10254:                         }
10255:                     } 
10256:                 }
10257:             }
10258:         } elsif ($key eq 'lockablenames') {
10259:             if (ref($currautoupdate{$key}) eq 'ARRAY') {
10260:                 my @changed = &Apache::loncommon::compare_arrays($currautoupdate{'lockablenames'},\@lockablenames);
10261:                 if (@changed) {
10262:                     $changes{'lockablenames'} = 1;
10263:                 }
10264:             } else {
10265:                 if (@lockablenames) {
10266:                     $changes{'lockablenames'} = 1;
10267:                 }
10268:             }
10269:         }
10270:     }
10271:     unless (grep(/^\Qlockablenames\E$/,keys(%currautoupdate))) {
10272:         if (@lockablenames) {
10273:             $changes{'lockablenames'} = 1;
10274:         }
10275:     }
10276:     foreach my $item (@{$types},'default') {
10277:         if (defined($fields{$item})) {
10278:             if (ref($currautoupdate{'fields'}) eq 'HASH') {
10279:                 if (ref($currautoupdate{'fields'}{$item}) eq 'ARRAY') {
10280:                     my $change = 0;
10281:                     if (ref($fields{$item}) eq 'ARRAY') {
10282:                         foreach my $type (@{$fields{$item}}) {
10283:                             if (!grep(/^\Q$type\E$/,@{$currautoupdate{'fields'}{$item}})) {
10284:                                 $change = 1;
10285:                                 last;
10286:                             }
10287:                         }
10288:                     }
10289:                     if ($change) {
10290:                         push(@{$changes{'fields'}},$item);
10291:                     }
10292:                 } else {
10293:                     push(@{$changes{'fields'}},$item);
10294:                 }
10295:             } else {
10296:                 push(@{$changes{'fields'}},$item);
10297:             }
10298:         }
10299:     }
10300:     my $putresult = &Apache::lonnet::put_dom('configuration',\%updatehash,
10301:                                              $dom);
10302:     if ($putresult eq 'ok') {
10303:         if (keys(%changes) > 0) {
10304:             $resulttext = &mt('Changes made:').'<ul>';
10305:             foreach my $key (sort(keys(%changes))) {
10306:                 if ($key eq 'lockablenames') {
10307:                     $resulttext .= '<li>';
10308:                     if (@lockablenames) {
10309:                         $usertypes->{'default'} = $othertitle;
10310:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update), available for the following affiliations:").' '.
10311:                                    join(', ', map { $usertypes->{$_}; } @lockablenames).'</li>';
10312:                     } else {
10313:                         $resulttext .= &mt("User preference to disable replacement of user's name with institutional data (by auto-update) is unavailable.");
10314:                     }
10315:                     $resulttext .= '</li>';
10316:                 } elsif (ref($changes{$key}) eq 'ARRAY') {
10317:                     foreach my $item (@{$changes{$key}}) {
10318:                         my @newvalues;
10319:                         foreach my $type (@{$fields{$item}}) {
10320:                             push(@newvalues,$fieldtitles{$type});
10321:                         }
10322:                         my $newvaluestr;
10323:                         if (@newvalues > 0) {
10324:                             $newvaluestr = join(', ',@newvalues);
10325:                         } else {
10326:                             $newvaluestr = &mt('none');
10327:                         }
10328:                         if ($item eq 'default') {
10329:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$othertitle,$newvaluestr).'</li>';
10330:                         } else {
10331:                             $resulttext .= '<li>'.&mt("Updates for '[_1]' set to: '[_2]'",$usertypes->{$item},$newvaluestr).'</li>';
10332:                         }
10333:                     }
10334:                 } else {
10335:                     my $newvalue;
10336:                     if ($key eq 'run') {
10337:                         $newvalue = $offon[$env{'form.autoupdate_run'}];
10338:                     } else {
10339:                         $newvalue = $offon[$env{'form.'.$key}];
10340:                     }
10341:                     $resulttext .= '<li>'.&mt("[_1] set to $newvalue",$title{$key}).'</li>';
10342:                 }
10343:             }
10344:             $resulttext .= '</ul>';
10345:         } else {
10346:             $resulttext = &mt('No changes made to autoupdates');
10347:         }
10348:     } else {
10349:         $resulttext = '<span class="LC_error">'.
10350: 	    &mt('An error occurred: [_1]',$putresult).'</span>';
10351:     }
10352:     return $resulttext;
10353: }
10354: 
10355: sub modify_autocreate {
10356:     my ($dom,%domconfig) = @_;
10357:     my ($resulttext,%changes,%currautocreate,%newvals,%autocreatehash);
10358:     if (ref($domconfig{'autocreate'}) eq 'HASH') {
10359:         foreach my $key (keys(%{$domconfig{'autocreate'}})) {
10360:             $currautocreate{$key} = $domconfig{'autocreate'}{$key};
10361:         }
10362:     }
10363:     my %title= ( xml => 'Auto-creation of courses in XML course description files',
10364:                  req => 'Auto-creation of validated requests for official courses',
10365:                  xmldc => 'Identity of course creator of courses from XML files',
10366:                );
10367:     my @types = ('xml','req');
10368:     foreach my $item (@types) {
10369:         $newvals{$item} = $env{'form.autocreate_'.$item};
10370:         $newvals{$item} =~ s/\D//g;
10371:         $newvals{$item} = 0 if ($newvals{$item} eq '');
10372:     }
10373:     $newvals{'xmldc'} = $env{'form.autocreate_xmldc'};
10374:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
10375:     unless (exists($domcoords{$newvals{'xmldc'}})) {
10376:         $newvals{'xmldc'} = '';
10377:     } 
10378:     %autocreatehash =  (
10379:                         autocreate => { xml => $newvals{'xml'},
10380:                                         req => $newvals{'req'},
10381:                                       }
10382:                        );
10383:     if ($newvals{'xmldc'} ne '') {
10384:         $autocreatehash{'autocreate'}{'xmldc'} = $newvals{'xmldc'};
10385:     }
10386:     my $putresult = &Apache::lonnet::put_dom('configuration',\%autocreatehash,
10387:                                              $dom);
10388:     if ($putresult eq 'ok') {
10389:         my @items = @types;
10390:         if ($newvals{'xml'}) {
10391:             push(@items,'xmldc');
10392:         }
10393:         foreach my $item (@items) {
10394:             if (exists($currautocreate{$item})) {
10395:                 if ($currautocreate{$item} ne $newvals{$item}) {
10396:                     $changes{$item} = 1;
10397:                 }
10398:             } elsif ($newvals{$item}) {
10399:                 $changes{$item} = 1;
10400:             }
10401:         }
10402:         if (keys(%changes) > 0) {
10403:             my @offon = ('off','on'); 
10404:             $resulttext = &mt('Changes made:').'<ul>';
10405:             foreach my $item (@types) {
10406:                 if ($changes{$item}) {
10407:                     my $newtxt = $offon[$newvals{$item}];
10408:                     $resulttext .= '<li>'.
10409:                                    &mt("$title{$item} set to [_1]$newtxt [_2]",
10410:                                        '<b>','</b>').
10411:                                    '</li>';
10412:                 }
10413:             }
10414:             if ($changes{'xmldc'}) {
10415:                 my ($dcname,$dcdom) = split(':',$newvals{'xmldc'});
10416:                 my $newtxt = &Apache::loncommon::plainname($dcname,$dcdom);
10417:                 $resulttext .= '<li>'.&mt("$title{'xmldc'} set to [_1]",'<b>'.$newtxt.'</b>').'</li>'; 
10418:             }
10419:             $resulttext .= '</ul>';
10420:         } else {
10421:             $resulttext = &mt('No changes made to auto-creation settings');
10422:         }
10423:     } else {
10424:         $resulttext = '<span class="LC_error">'.
10425:             &mt('An error occurred: [_1]',$putresult).'</span>';
10426:     }
10427:     return $resulttext;
10428: }
10429: 
10430: sub modify_directorysrch {
10431:     my ($dom,$lastactref,%domconfig) = @_;
10432:     my ($resulttext,%changes);
10433:     my %currdirsrch;
10434:     if (ref($domconfig{'directorysrch'}) eq 'HASH') {
10435:         foreach my $key (keys(%{$domconfig{'directorysrch'}})) {
10436:             $currdirsrch{$key} = $domconfig{'directorysrch'}{$key};
10437:         }
10438:     }
10439:     my %title = ( available => 'Institutional directory search available',
10440:                   localonly => 'Other domains can search institution',
10441:                   lcavailable => 'LON-CAPA directory search available',
10442:                   lclocalonly => 'Other domains can search LON-CAPA domain',
10443:                   searchby => 'Search types',
10444:                   searchtypes => 'Search latitude');
10445:     my @offon = ('off','on');
10446:     my @otherdoms = ('Yes','No');
10447: 
10448:     my @searchtypes = &Apache::loncommon::get_env_multiple('form.searchtypes');  
10449:     my @cansearch = &Apache::loncommon::get_env_multiple('form.cansearch');
10450:     my @searchby = &Apache::loncommon::get_env_multiple('form.searchby');
10451: 
10452:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
10453:     if (keys(%{$usertypes}) == 0) {
10454:         @cansearch = ('default');
10455:     } else {
10456:         if (ref($currdirsrch{'cansearch'}) eq 'ARRAY') {
10457:             foreach my $type (@{$currdirsrch{'cansearch'}}) {
10458:                 if (!grep(/^\Q$type\E$/,@cansearch)) {
10459:                     push(@{$changes{'cansearch'}},$type);
10460:                 }
10461:             }
10462:             foreach my $type (@cansearch) {
10463:                 if (!grep(/^\Q$type\E$/,@{$currdirsrch{'cansearch'}})) {
10464:                     push(@{$changes{'cansearch'}},$type);
10465:                 }
10466:             }
10467:         } else {
10468:             push(@{$changes{'cansearch'}},@cansearch);
10469:         }
10470:     }
10471: 
10472:     if (ref($currdirsrch{'searchby'}) eq 'ARRAY') {
10473:         foreach my $by (@{$currdirsrch{'searchby'}}) {
10474:             if (!grep(/^\Q$by\E$/,@searchby)) {
10475:                 push(@{$changes{'searchby'}},$by);
10476:             }
10477:         }
10478:         foreach my $by (@searchby) {
10479:             if (!grep(/^\Q$by\E$/,@{$currdirsrch{'searchby'}})) {
10480:                 push(@{$changes{'searchby'}},$by);
10481:             }
10482:         }
10483:     } else {
10484:         push(@{$changes{'searchby'}},@searchby);
10485:     }
10486: 
10487:     if (ref($currdirsrch{'searchtypes'}) eq 'ARRAY') {
10488:         foreach my $type (@{$currdirsrch{'searchtypes'}}) {
10489:             if (!grep(/^\Q$type\E$/,@searchtypes)) {
10490:                 push(@{$changes{'searchtypes'}},$type);
10491:             }
10492:         }
10493:         foreach my $type (@searchtypes) {
10494:             if (!grep(/^\Q$type\E$/,@{$currdirsrch{'searchtypes'}})) {
10495:                 push(@{$changes{'searchtypes'}},$type);
10496:             }
10497:         }
10498:     } else {
10499:         if (exists($currdirsrch{'searchtypes'})) {
10500:             foreach my $type (@searchtypes) {  
10501:                 if ($type ne $currdirsrch{'searchtypes'}) { 
10502:                     push(@{$changes{'searchtypes'}},$type);
10503:                 }
10504:             }
10505:             if (!grep(/^\Q$currdirsrch{'searchtypes'}\E/,@searchtypes)) {
10506:                 push(@{$changes{'searchtypes'}},$currdirsrch{'searchtypes'});
10507:             }   
10508:         } else {
10509:             push(@{$changes{'searchtypes'}},@searchtypes); 
10510:         }
10511:     }
10512: 
10513:     my %dirsrch_hash =  (
10514:             directorysrch => { available => $env{'form.dirsrch_available'},
10515:                                cansearch => \@cansearch,
10516:                                localonly => $env{'form.dirsrch_instlocalonly'},
10517:                                lclocalonly => $env{'form.dirsrch_domlocalonly'},
10518:                                lcavailable => $env{'form.dirsrch_domavailable'},
10519:                                searchby => \@searchby,
10520:                                searchtypes => \@searchtypes,
10521:                              }
10522:             );
10523:     my $putresult = &Apache::lonnet::put_dom('configuration',\%dirsrch_hash,
10524:                                              $dom);
10525:     if ($putresult eq 'ok') {
10526:         if (exists($currdirsrch{'available'})) {
10527:              if ($currdirsrch{'available'} ne $env{'form.dirsrch_available'}) {
10528:                  $changes{'available'} = 1;
10529:              }
10530:         } else {
10531:             if ($env{'form.dirsrch_available'} eq '1') {
10532:                 $changes{'available'} = 1;
10533:             }
10534:         }
10535:         if (exists($currdirsrch{'lcavailable'})) {
10536:             if ($currdirsrch{'lcavailable'} ne $env{'form.dirsrch_domavailable'}) {
10537:                 $changes{'lcavailable'} = 1;
10538:             }
10539:         } else {
10540:             if ($env{'form.dirsrch_lcavailable'} eq '1') {
10541:                 $changes{'lcavailable'} = 1;
10542:             }
10543:         }
10544:         if (exists($currdirsrch{'localonly'})) {
10545:             if ($currdirsrch{'localonly'} ne $env{'form.dirsrch_instlocalonly'}) {
10546:                 $changes{'localonly'} = 1;
10547:             }
10548:         } else {
10549:             if ($env{'form.dirsrch_instlocalonly'} eq '1') {
10550:                 $changes{'localonly'} = 1;
10551:             }
10552:         }
10553:         if (exists($currdirsrch{'lclocalonly'})) {
10554:             if ($currdirsrch{'lclocalonly'} ne $env{'form.dirsrch_domlocalonly'}) {
10555:                 $changes{'lclocalonly'} = 1;
10556:             }
10557:         } else {
10558:             if ($env{'form.dirsrch_domlocalonly'} eq '1') {
10559:                 $changes{'lclocalonly'} = 1;
10560:             }
10561:         }
10562:         if (keys(%changes) > 0) {
10563:             $resulttext = &mt('Changes made:').'<ul>';
10564:             if ($changes{'available'}) {
10565:                 $resulttext .= '<li>'.&mt("$title{'available'} set to: $offon[$env{'form.dirsrch_available'}]").'</li>';
10566:             }
10567:             if ($changes{'lcavailable'}) {
10568:                 $resulttext .= '<li>'.&mt("$title{'lcavailable'} set to: $offon[$env{'form.dirsrch_domavailable'}]").'</li>';
10569:             }
10570:             if ($changes{'localonly'}) {
10571:                 $resulttext .= '<li>'.&mt("$title{'localonly'} set to: $otherdoms[$env{'form.dirsrch_instlocalonly'}]").'</li>';
10572:             }
10573:             if ($changes{'lclocalonly'}) {
10574:                 $resulttext .= '<li>'.&mt("$title{'lclocalonly'} set to: $otherdoms[$env{'form.dirsrch_domlocalonly'}]").'</li>';
10575:             }
10576:             if (ref($changes{'cansearch'}) eq 'ARRAY') {
10577:                 my $chgtext;
10578:                 if (ref($usertypes) eq 'HASH') {
10579:                     if (keys(%{$usertypes}) > 0) {
10580:                         foreach my $type (@{$types}) {
10581:                             if (grep(/^\Q$type\E$/,@cansearch)) {
10582:                                 $chgtext .= $usertypes->{$type}.'; ';
10583:                             }
10584:                         }
10585:                         if (grep(/^default$/,@cansearch)) {
10586:                             $chgtext .= $othertitle;
10587:                         } else {
10588:                             $chgtext =~ s/\; $//;
10589:                         }
10590:                         $resulttext .=
10591:                             '<li>'.
10592:                             &mt("Users from domain '[_1]' permitted to search the institutional directory set to: [_2]",
10593:                                 '<span class="LC_cusr_emph">'.$dom.'</span>',$chgtext).
10594:                             '</li>';
10595:                     }
10596:                 }
10597:             }
10598:             if (ref($changes{'searchby'}) eq 'ARRAY') {
10599:                 my ($searchtitles,$titleorder) = &sorted_searchtitles();
10600:                 my $chgtext;
10601:                 foreach my $type (@{$titleorder}) {
10602:                     if (grep(/^\Q$type\E$/,@searchby)) {
10603:                         if (defined($searchtitles->{$type})) {
10604:                             $chgtext .= $searchtitles->{$type}.'; ';
10605:                         }
10606:                     }
10607:                 }
10608:                 $chgtext =~ s/\; $//;
10609:                 $resulttext .= '<li>'.&mt("$title{'searchby'} set to: [_1]",$chgtext).'</li>';
10610:             }
10611:             if (ref($changes{'searchtypes'}) eq 'ARRAY') {
10612:                 my ($srchtypes_desc,$srchtypeorder) = &sorted_searchtypes(); 
10613:                 my $chgtext;
10614:                 foreach my $type (@{$srchtypeorder}) {
10615:                     if (grep(/^\Q$type\E$/,@searchtypes)) {
10616:                         if (defined($srchtypes_desc->{$type})) {
10617:                             $chgtext .= $srchtypes_desc->{$type}.'; ';
10618:                         }
10619:                     }
10620:                 }
10621:                 $chgtext =~ s/\; $//;
10622:                 $resulttext .= '<li>'.&mt($title{'searchtypes'}.' set to: "[_1]"',$chgtext).'</li>';
10623:             }
10624:             $resulttext .= '</ul>';
10625:             &Apache::lonnet::do_cache_new('directorysrch',$dom,$dirsrch_hash{'directorysrch'},3600);
10626:             if (ref($lastactref) eq 'HASH') {
10627:                 $lastactref->{'directorysrch'} = 1;
10628:             }
10629:         } else {
10630:             $resulttext = &mt('No changes made to directory search settings');
10631:         }
10632:     } else {
10633:         $resulttext = '<span class="LC_error">'.
10634:                       &mt('An error occurred: [_1]',$putresult).'</span>';
10635:     }
10636:     return $resulttext;
10637: }
10638: 
10639: sub modify_contacts {
10640:     my ($dom,$lastactref,%domconfig) = @_;
10641:     my ($resulttext,%currsetting,%newsetting,%changes,%contacts_hash);
10642:     if (ref($domconfig{'contacts'}) eq 'HASH') {
10643:         foreach my $key (keys(%{$domconfig{'contacts'}})) {
10644:             $currsetting{$key} = $domconfig{'contacts'}{$key};
10645:         }
10646:     }
10647:     my (%others,%to,%bcc,%includestr,%includeloc);
10648:     my @contacts = ('supportemail','adminemail');
10649:     my @mailings = ('errormail','packagesmail','helpdeskmail','otherdomsmail',
10650:                     'lonstatusmail','requestsmail','updatesmail','idconflictsmail');
10651:     my @toggles = ('reporterrors','reportupdates');
10652:     my ($fields,$fieldtitles,$fieldoptions,$possoptions) = &helpform_fields();
10653:     foreach my $type (@mailings) {
10654:         @{$newsetting{$type}} = 
10655:             &Apache::loncommon::get_env_multiple('form.'.$type);
10656:         foreach my $item (@contacts) {
10657:             if (grep(/^\Q$item\E$/,@{$newsetting{$type}})) {
10658:                 $contacts_hash{contacts}{$type}{$item} = 1;
10659:             } else {
10660:                 $contacts_hash{contacts}{$type}{$item} = 0;
10661:             }
10662:         }
10663:         $others{$type} = $env{'form.'.$type.'_others'};
10664:         $contacts_hash{contacts}{$type}{'others'} = $others{$type};
10665:         if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10666:             $bcc{$type} = $env{'form.'.$type.'_bcc'};
10667:             $contacts_hash{contacts}{$type}{'bcc'} = $bcc{$type};
10668:             if (($env{'form.'.$type.'_includestr'} ne '') && ($env{'form.'.$type.'_includeloc'} =~ /^s|b$/)) {
10669:                 $includestr{$type} = $env{'form.'.$type.'_includestr'};
10670:                 $includeloc{$type} = $env{'form.'.$type.'_includeloc'};
10671:                 $contacts_hash{contacts}{$type}{'include'} = $includeloc{$type}.':'.&escape($includestr{$type});
10672:             }
10673:         }
10674:     }
10675:     foreach my $item (@contacts) {
10676:         $to{$item} = $env{'form.'.$item};
10677:         $contacts_hash{'contacts'}{$item} = $to{$item};
10678:     }
10679:     foreach my $item (@toggles) {
10680:         if ($env{'form.'.$item} =~ /^(0|1)$/) {
10681:             $contacts_hash{'contacts'}{$item} = $env{'form.'.$item};
10682:         }
10683:     }
10684:     if ((ref($fields) eq 'ARRAY') && (ref($possoptions) eq 'HASH')) {
10685:         foreach my $field (@{$fields}) {
10686:             if (ref($possoptions->{$field}) eq 'ARRAY') {
10687:                 my $value = $env{'form.helpform_'.$field};
10688:                 $value =~ s/^\s+|\s+$//g;
10689:                 if (grep(/^\Q$value\E$/,@{$possoptions->{$field}})) {
10690:                     $contacts_hash{contacts}{'helpform'}{$field} = $value;
10691:                     if ($field eq 'screenshot') {
10692:                         $env{'form.helpform_maxsize'} =~ s/^\s+|\s+$//g;
10693:                         if ($env{'form.helpform_maxsize'} =~ /^\d+\.?\d*$/) {
10694:                             $contacts_hash{contacts}{'helpform'}{'maxsize'} = $env{'form.helpform_maxsize'};
10695:                         }
10696:                     }
10697:                 }
10698:             }
10699:         }
10700:     }
10701:     if (keys(%currsetting) > 0) {
10702:         foreach my $item (@contacts) {
10703:             if ($to{$item} ne $currsetting{$item}) {
10704:                 $changes{$item} = 1;
10705:             }
10706:         }
10707:         foreach my $type (@mailings) {
10708:             foreach my $item (@contacts) {
10709:                 if (ref($currsetting{$type}) eq 'HASH') {
10710:                     if ($currsetting{$type}{$item} ne $contacts_hash{contacts}{$type}{$item}) {
10711:                         push(@{$changes{$type}},$item);
10712:                     }
10713:                 } else {
10714:                     push(@{$changes{$type}},@{$newsetting{$type}});
10715:                 }
10716:             }
10717:             if ($others{$type} ne $currsetting{$type}{'others'}) {
10718:                 push(@{$changes{$type}},'others');
10719:             }
10720:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10721:                 if ($bcc{$type} ne $currsetting{$type}{'bcc'}) {
10722:                     push(@{$changes{$type}},'bcc'); 
10723:                 }
10724:                 my ($currloc,$currstr) = split(/:/,$currsetting{$type}{'include'},2);
10725:                 if (($includeloc{$type} ne $currloc) || (&escape($includestr{$type}) ne $currstr)) {
10726:                     push(@{$changes{$type}},'include');
10727:                 }
10728:             }
10729:         }
10730:         if (ref($fields) eq 'ARRAY') {
10731:             if (ref($currsetting{'helpform'}) eq 'HASH') {
10732:                 foreach my $field (@{$fields}) {
10733:                     if ($currsetting{'helpform'}{$field} ne $contacts_hash{'contacts'}{'helpform'}{$field}) {
10734:                         push(@{$changes{'helpform'}},$field);
10735:                     }
10736:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10737:                         if ($currsetting{'helpform'}{'maxsize'} ne $contacts_hash{'contacts'}{'helpform'}{'maxsize'}) {
10738:                             push(@{$changes{'helpform'}},'maxsize');
10739:                         }
10740:                     }
10741:                 }
10742:             } else {
10743:                 foreach my $field (@{$fields}) {
10744:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
10745:                         push(@{$changes{'helpform'}},$field);
10746:                     }
10747:                     if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10748:                         if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
10749:                             push(@{$changes{'helpform'}},'maxsize');
10750:                         }
10751:                     }
10752:                 }
10753:             }
10754:         }
10755:     } else {
10756:         my %default;
10757:         $default{'supportemail'} = $Apache::lonnet::perlvar{'lonSupportEMail'};
10758:         $default{'adminemail'} = $Apache::lonnet::perlvar{'lonAdmEMail'};
10759:         $default{'errormail'} = 'adminemail';
10760:         $default{'packagesmail'} = 'adminemail';
10761:         $default{'helpdeskmail'} = 'supportemail';
10762:         $default{'otherdomsmail'} = 'supportemail';
10763:         $default{'lonstatusmail'} = 'adminemail';
10764:         $default{'requestsmail'} = 'adminemail';
10765:         $default{'updatesmail'} = 'adminemail';
10766:         foreach my $item (@contacts) {
10767:            if ($to{$item} ne $default{$item}) {
10768:                $changes{$item} = 1;
10769:            }
10770:         }
10771:         foreach my $type (@mailings) {
10772:             if ((@{$newsetting{$type}} != 1) || ($newsetting{$type}[0] ne $default{$type})) {
10773:                 push(@{$changes{$type}},@{$newsetting{$type}});
10774:             }
10775:             if ($others{$type} ne '') {
10776:                 push(@{$changes{$type}},'others');
10777:             }
10778:             if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10779:                 if ($bcc{$type} ne '') {
10780:                     push(@{$changes{$type}},'bcc');
10781:                 }
10782:                 if (($includeloc{$type} =~ /^b|s$/) && ($includestr{$type} ne '')) {
10783:                     push(@{$changes{$type}},'include');
10784:                 }
10785:             }
10786:         }
10787:         if (ref($fields) eq 'ARRAY') {
10788:             foreach my $field (@{$fields}) {
10789:                 if ($contacts_hash{'contacts'}{'helpform'}{$field} ne 'yes') {
10790:                     push(@{$changes{'helpform'}},$field);
10791:                 }
10792:                 if (($field eq 'screenshot') && ($contacts_hash{'contacts'}{'helpform'}{'screenshot'} ne 'no')) {
10793:                     if ($contacts_hash{'contacts'}{'helpform'}{'maxsize'} != 1) {
10794:                         push(@{$changes{'helpform'}},'maxsize');
10795:                     }
10796:                 }
10797:             }
10798:         }
10799:     }
10800:     foreach my $item (@toggles) {
10801:         if (($env{'form.'.$item} == 1) && ($currsetting{$item} == 0)) {
10802:             $changes{$item} = 1;
10803:         } elsif ((!$env{'form.'.$item}) &&
10804:                  (($currsetting{$item} eq '') || ($currsetting{$item} == 1))) {
10805:             $changes{$item} = 1;
10806:         }
10807:     }
10808:     my $putresult = &Apache::lonnet::put_dom('configuration',\%contacts_hash,
10809:                                              $dom);
10810:     if ($putresult eq 'ok') {
10811:         if (keys(%changes) > 0) {
10812:             &Apache::loncommon::devalidate_domconfig_cache($dom);
10813:             if (ref($lastactref) eq 'HASH') {
10814:                 $lastactref->{'domainconfig'} = 1;
10815:             }
10816:             my ($titles,$short_titles)  = &contact_titles();
10817:             $resulttext = &mt('Changes made:').'<ul>';
10818:             foreach my $item (@contacts) {
10819:                 if ($changes{$item}) {
10820:                     $resulttext .= '<li>'.$titles->{$item}.
10821:                                     &mt(' set to: ').
10822:                                     '<span class="LC_cusr_emph">'.
10823:                                     $to{$item}.'</span></li>';
10824:                 }
10825:             }
10826:             foreach my $type (@mailings) {
10827:                 if (ref($changes{$type}) eq 'ARRAY') {
10828:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10829:                         $resulttext .= '<li>'.$titles->{$type}.' -- '.&mt('sent to').': ';
10830:                     } else {
10831:                         $resulttext .= '<li>'.$titles->{$type}.': ';
10832:                     }
10833:                     my @text;
10834:                     foreach my $item (@{$newsetting{$type}}) {
10835:                         push(@text,$short_titles->{$item});
10836:                     }
10837:                     if ($others{$type} ne '') {
10838:                         push(@text,$others{$type});
10839:                     }
10840:                     if (@text) {
10841:                         $resulttext .= '<span class="LC_cusr_emph">'.
10842:                                        join(', ',@text).'</span>';
10843:                     }
10844:                     if (($type eq 'helpdeskmail') || ($type eq 'otherdomsmail')) {
10845:                         if ($bcc{$type} ne '') {
10846:                             my $bcctext;
10847:                             if (@text) {
10848:                                 $bcctext = '&nbsp;'.&mt('with Bcc to');
10849:                             } else {
10850:                                 $bcctext = '(Bcc)';
10851:                             }
10852:                             $resulttext .= $bcctext.': <span class="LC_cusr_emph">'.$bcc{$type}.'</span>';
10853:                         } elsif (!@text) {
10854:                             $resulttext .= &mt('No one');
10855:                         }   
10856:                         if ($includestr{$type} ne '') {
10857:                             if ($includeloc{$type} eq 'b') {
10858:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail body:').' '.$includestr{$type};
10859:                             } elsif ($includeloc{$type} eq 's') {
10860:                                 $resulttext .= '<br />'.&mt('Text automatically added to e-mail subject:').' '.$includestr{$type};
10861:                             }
10862:                         }
10863:                     } elsif (!@text) {
10864:                         $resulttext .= &mt('No recipients');
10865:                     }
10866:                     $resulttext .= '</li>';
10867:                 }
10868:             }
10869:             my @offon = ('off','on');
10870:             if ($changes{'reporterrors'}) {
10871:                 $resulttext .= '<li>'.
10872:                                &mt('E-mail error reports to [_1] set to "'.
10873:                                    $offon[$env{'form.reporterrors'}].'".',
10874:                                    &Apache::loncommon::modal_link('http://loncapa.org/core.html',
10875:                                        &mt('LON-CAPA core group - MSU'),600,500)).
10876:                                '</li>';
10877:             }
10878:             if ($changes{'reportupdates'}) {
10879:                 $resulttext .= '<li>'.
10880:                                 &mt('E-mail record of completed LON-CAPA updates to [_1] set to "'.
10881:                                     $offon[$env{'form.reportupdates'}].'".',
10882:                                     &Apache::loncommon::modal_link('http://loncapa.org/core.html',
10883:                                         &mt('LON-CAPA core group - MSU'),600,500)).
10884:                                 '</li>';
10885:             }
10886:             if ((ref($changes{'helpform'}) eq 'ARRAY') && (ref($fields) eq 'ARRAY')) {
10887:                 my (@optional,@required,@unused,$maxsizechg);
10888:                 foreach my $field (@{$changes{'helpform'}}) {
10889:                     if ($field eq 'maxsize') {
10890:                         $maxsizechg = 1;
10891:                         next;
10892:                     }
10893:                     if ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'yes') {
10894:                         push(@optional,$field);
10895:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'no') {
10896:                         push(@unused,$field);
10897:                     } elsif ($contacts_hash{'contacts'}{'helpform'}{$field} eq 'req') {
10898:                         push(@required,$field);
10899:                     }
10900:                 }
10901:                 if (@optional) {
10902:                     $resulttext .= '<li>'.
10903:                                    &mt('Help form fields changed to "Optional": [_1].',
10904:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @optional)).'</span>'.
10905:                                    '</li>';
10906:                 }
10907:                 if (@required) {
10908:                     $resulttext .= '<li>'.
10909:                                    &mt('Help form fields changed to "Required": [_1].',
10910:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @required)).'</span>'.
10911:                                    '</li>';
10912:                 }
10913:                 if (@unused) {
10914:                     $resulttext .= '<li>'.
10915:                                    &mt('Help form fields changed to "Not shown": [_1].',
10916:                                        '<span class="LC_cusr_emph">'.join(', ',map { $fieldtitles->{$_}; } @unused)).'</span>'.
10917:                                    '</li>';
10918:                 }
10919:                 if ($maxsizechg) {
10920:                     $resulttext .= '<li>'.
10921:                                    &mt('Max size for file uploaded to help form by logged-in user set to [_1] MB.',
10922:                                        $contacts_hash{'contacts'}{'helpform'}{'maxsize'}).
10923:                                    '</li>';
10924: 
10925:                 }
10926:             }
10927:             $resulttext .= '</ul>';
10928:         } else {
10929:             $resulttext = &mt('No changes made to contacts and form settings');
10930:         }
10931:     } else {
10932:         $resulttext = '<span class="LC_error">'.
10933:             &mt('An error occurred: [_1].',$putresult).'</span>';
10934:     }
10935:     return $resulttext;
10936: }
10937: 
10938: sub modify_usercreation {
10939:     my ($dom,%domconfig) = @_;
10940:     my ($resulttext,%curr_usercreation,%changes,%authallowed,%cancreate,%save_usercreate);
10941:     my $warningmsg;
10942:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
10943:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
10944:             if ($key eq 'cancreate') {
10945:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
10946:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
10947:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
10948:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') ||
10949:                             ($item eq 'recaptchaversion')) {
10950:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10951:                         } else {
10952:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
10953:                         }
10954:                     }
10955:                 }
10956:             } elsif ($key eq 'email_rule') {
10957:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
10958:             } else {
10959:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
10960:             }
10961:         }
10962:     }
10963:     my @username_rule = &Apache::loncommon::get_env_multiple('form.username_rule');
10964:     my @id_rule = &Apache::loncommon::get_env_multiple('form.id_rule');
10965:     my @contexts = ('author','course','requestcrs');
10966:     foreach my $item(@contexts) {
10967:         $cancreate{$item} = $env{'form.can_createuser_'.$item};
10968:     }
10969:     if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
10970:         foreach my $item (@contexts) {
10971:             if ($curr_usercreation{'cancreate'}{$item} ne $cancreate{$item}) {
10972:                 push(@{$changes{'cancreate'}},$item);
10973:             }
10974:         }
10975:     } elsif (ref($curr_usercreation{'cancreate'}) eq 'ARRAY') {
10976:         foreach my $item (@contexts) {
10977:             if (!grep(/^\Q$item\E$/,@{$curr_usercreation{'cancreate'}})) {
10978:                 if ($cancreate{$item} ne 'any') {
10979:                     push(@{$changes{'cancreate'}},$item);
10980:                 }
10981:             } else {
10982:                 if ($cancreate{$item} ne 'none') {
10983:                     push(@{$changes{'cancreate'}},$item);
10984:                 }
10985:             }
10986:         }
10987:     } else {
10988:         foreach my $item (@contexts)  {
10989:             push(@{$changes{'cancreate'}},$item);
10990:         }
10991:     }
10992: 
10993:     if (ref($curr_usercreation{'username_rule'}) eq 'ARRAY') {
10994:         foreach my $type (@{$curr_usercreation{'username_rule'}}) {
10995:             if (!grep(/^\Q$type\E$/,@username_rule)) {
10996:                 push(@{$changes{'username_rule'}},$type);
10997:             }
10998:         }
10999:         foreach my $type (@username_rule) {
11000:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'username_rule'}})) {
11001:                 push(@{$changes{'username_rule'}},$type);
11002:             }
11003:         }
11004:     } else {
11005:         push(@{$changes{'username_rule'}},@username_rule);
11006:     }
11007: 
11008:     if (ref($curr_usercreation{'id_rule'}) eq 'ARRAY') {
11009:         foreach my $type (@{$curr_usercreation{'id_rule'}}) {
11010:             if (!grep(/^\Q$type\E$/,@id_rule)) {
11011:                 push(@{$changes{'id_rule'}},$type);
11012:             }
11013:         }
11014:         foreach my $type (@id_rule) {
11015:             if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'id_rule'}})) {
11016:                 push(@{$changes{'id_rule'}},$type);
11017:             }
11018:         }
11019:     } else {
11020:         push(@{$changes{'id_rule'}},@id_rule);
11021:     }
11022: 
11023:     my @authen_contexts = ('author','course','domain');
11024:     my @authtypes = ('int','krb4','krb5','loc');
11025:     my %authhash;
11026:     foreach my $item (@authen_contexts) {
11027:         my @authallowed =  &Apache::loncommon::get_env_multiple('form.'.$item.'_auth');
11028:         foreach my $auth (@authtypes) {
11029:             if (grep(/^\Q$auth\E$/,@authallowed)) {
11030:                 $authhash{$item}{$auth} = 1;
11031:             } else {
11032:                 $authhash{$item}{$auth} = 0;
11033:             }
11034:         }
11035:     }
11036:     if (ref($curr_usercreation{'authtypes'}) eq 'HASH') {
11037:         foreach my $item (@authen_contexts) {
11038:             if (ref($curr_usercreation{'authtypes'}{$item}) eq 'HASH') {
11039:                 foreach my $auth (@authtypes) {
11040:                     if ($authhash{$item}{$auth} ne $curr_usercreation{'authtypes'}{$item}{$auth}) {
11041:                         push(@{$changes{'authtypes'}},$item);
11042:                         last;
11043:                     }
11044:                 }
11045:             }
11046:         }
11047:     } else {
11048:         foreach my $item (@authen_contexts) {
11049:             push(@{$changes{'authtypes'}},$item);
11050:         }
11051:     }
11052: 
11053:     $save_usercreate{'cancreate'}{'course'} = $cancreate{'course'}; 
11054:     $save_usercreate{'cancreate'}{'author'} = $cancreate{'author'};
11055:     $save_usercreate{'cancreate'}{'requestcrs'} = $cancreate{'requestcrs'};
11056:     $save_usercreate{'id_rule'} = \@id_rule;
11057:     $save_usercreate{'username_rule'} = \@username_rule,
11058:     $save_usercreate{'authtypes'} = \%authhash;
11059: 
11060:     my %usercreation_hash =  (
11061:         usercreation     => \%save_usercreate,
11062:     );
11063: 
11064:     my $putresult = &Apache::lonnet::put_dom('configuration',\%usercreation_hash,
11065:                                              $dom);
11066: 
11067:     if ($putresult eq 'ok') {
11068:         if (keys(%changes) > 0) {
11069:             $resulttext = &mt('Changes made:').'<ul>';
11070:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
11071:                 my %lt = &usercreation_types();
11072:                 foreach my $type (@{$changes{'cancreate'}}) {
11073:                     my $chgtext = $lt{$type}.', ';
11074:                     if ($cancreate{$type} eq 'none') {
11075:                         $chgtext .= &mt('creation of new users is not permitted, except by a Domain Coordinator.');
11076:                     } elsif ($cancreate{$type} eq 'any') {
11077:                         $chgtext .= &mt('creation of new users is permitted for both institutional and non-institutional usernames.');
11078:                     } elsif ($cancreate{$type} eq 'official') {
11079:                         $chgtext .= &mt('creation of new users is only permitted for institutional usernames.');
11080:                     } elsif ($cancreate{$type} eq 'unofficial') {
11081:                         $chgtext .= &mt('creation of new users is only permitted for non-institutional usernames.');
11082:                     }
11083:                     $resulttext .= '<li>'.$chgtext.'</li>';
11084:                 }
11085:             }
11086:             if (ref($changes{'username_rule'}) eq 'ARRAY') {
11087:                 my ($rules,$ruleorder) = 
11088:                     &Apache::lonnet::inst_userrules($dom,'username');
11089:                 my $chgtext = '<ul>';
11090:                 foreach my $type (@username_rule) {
11091:                     if (ref($rules->{$type}) eq 'HASH') {
11092:                         $chgtext .= '<li>'.$rules->{$type}{'name'}.'</li>';
11093:                     }
11094:                 }
11095:                 $chgtext .= '</ul>';
11096:                 if (@username_rule > 0) {
11097:                     $resulttext .= '<li>'.&mt('Usernames with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';     
11098:                 } else {
11099:                     $resulttext .= '<li>'.&mt('There are now no username formats restricted to verified users in the institutional directory.').'</li>'; 
11100:                 }
11101:             }
11102:             if (ref($changes{'id_rule'}) eq 'ARRAY') {
11103:                 my ($idrules,$idruleorder) = 
11104:                     &Apache::lonnet::inst_userrules($dom,'id');
11105:                 my $chgtext = '<ul>';
11106:                 foreach my $type (@id_rule) {
11107:                     if (ref($idrules->{$type}) eq 'HASH') {
11108:                         $chgtext .= '<li>'.$idrules->{$type}{'name'}.'</li>';
11109:                     }
11110:                 }
11111:                 $chgtext .= '</ul>';
11112:                 if (@id_rule > 0) {
11113:                     $resulttext .= '<li>'.&mt('IDs with the following formats are restricted to verified users in the institutional directory: ').$chgtext.'</li>';
11114:                 } else {
11115:                     $resulttext .= '<li>'.&mt('There are now no ID formats restricted to verified users in the institutional directory.').'</li>';
11116:                 }
11117:             }
11118:             my %authname = &authtype_names();
11119:             my %context_title = &context_names();
11120:             if (ref($changes{'authtypes'}) eq 'ARRAY') {
11121:                 my $chgtext = '<ul>';
11122:                 foreach my $type (@{$changes{'authtypes'}}) {
11123:                     my @allowed;
11124:                     $chgtext .= '<li><span class="LC_cusr_emph">'.$context_title{$type}.'</span> - '.&mt('assignable authentication types: ');
11125:                     foreach my $auth (@authtypes) {
11126:                         if ($authhash{$type}{$auth}) {
11127:                             push(@allowed,$authname{$auth});
11128:                         }
11129:                     }
11130:                     if (@allowed > 0) {
11131:                         $chgtext .= join(', ',@allowed).'</li>';
11132:                     } else {
11133:                         $chgtext .= &mt('none').'</li>';
11134:                     }
11135:                 }
11136:                 $chgtext .= '</ul>';
11137:                 $resulttext .= '<li>'.&mt('Authentication types available for assignment to new users').'<br />'.$chgtext;
11138:                 $resulttext .= '</li>';
11139:             }
11140:             $resulttext .= '</ul>';
11141:         } else {
11142:             $resulttext = &mt('No changes made to user creation settings');
11143:         }
11144:     } else {
11145:         $resulttext = '<span class="LC_error">'.
11146:             &mt('An error occurred: [_1]',$putresult).'</span>';
11147:     }
11148:     if ($warningmsg ne '') {
11149:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
11150:     }
11151:     return $resulttext;
11152: }
11153: 
11154: sub modify_selfcreation {
11155:     my ($dom,%domconfig) = @_;
11156:     my ($resulttext,$warningmsg,%curr_usercreation,%curr_usermodify,%changes,%cancreate);
11157:     my (%save_usercreate,%save_usermodify);
11158:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
11159:     if (ref($types) eq 'ARRAY') {
11160:         $usertypes->{'default'} = $othertitle;
11161:         push(@{$types},'default');
11162:     }
11163: #
11164: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usercreation'}.
11165: #
11166:     if (ref($domconfig{'usercreation'}) eq 'HASH') {
11167:         foreach my $key (keys(%{$domconfig{'usercreation'}})) {
11168:             if ($key eq 'cancreate') {
11169:                 if (ref($domconfig{'usercreation'}{$key}) eq 'HASH') {
11170:                     foreach my $item (keys(%{$domconfig{'usercreation'}{$key}})) {
11171:                         if (($item eq 'selfcreate') || ($item eq 'statustocreate') ||
11172:                             ($item eq 'captcha') || ($item eq 'recaptchakeys') || 
11173:                             ($item eq 'recaptchaversion') ||
11174:                             ($item eq 'emailusername') || ($item eq 'notify') ||
11175:                             ($item eq 'selfcreateprocessing') || ($item eq 'shibenv')) {
11176:                             $curr_usercreation{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11177:                         } else {
11178:                             $save_usercreate{$key}{$item} = $domconfig{'usercreation'}{$key}{$item};
11179:                         }
11180:                     }
11181:                 }
11182:             } elsif ($key eq 'email_rule') {
11183:                 $curr_usercreation{$key} = $domconfig{'usercreation'}{$key};
11184:             } else {
11185:                 $save_usercreate{$key} = $domconfig{'usercreation'}{$key};
11186:             }
11187:         }
11188:     }
11189: #
11190: # Retrieve current domain configuration for self-creation of usernames from $domconfig{'usermodification'}.
11191: #
11192:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
11193:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
11194:             if ($key eq 'selfcreate') {
11195:                 $curr_usermodify{$key} = $domconfig{'usermodification'}{$key};
11196:             } else {
11197:                 $save_usermodify{$key} = $domconfig{'usermodification'}{$key};
11198:             }
11199:         }
11200:     }
11201: 
11202:     my @contexts = ('selfcreate');
11203:     @{$cancreate{'selfcreate'}} = ();
11204:     %{$cancreate{'emailusername'}} = ();
11205:     @{$cancreate{'statustocreate'}} = ();
11206:     %{$cancreate{'selfcreateprocessing'}} = ();
11207:     %{$cancreate{'shibenv'}} = ();
11208:     my %selfcreatetypes = (
11209:                              sso   => 'users authenticated by institutional single sign on',
11210:                              login => 'users authenticated by institutional log-in',
11211:                              email => 'users who provide a valid e-mail address for use as username',
11212:                           );
11213: #
11214: # Populate $cancreate{'selfcreate'} array reference with types of user, for which self-creation of user accounts
11215: # is permitted.
11216: #
11217: 
11218:     my @statuses;
11219:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
11220:         if (ref($domconfig{'inststatus'}{'inststatusguest'}) eq 'ARRAY') {
11221:             @statuses = @{$domconfig{'inststatus'}{'inststatusguest'}};
11222:         }
11223:     }
11224:     push(@statuses,'default');
11225: 
11226:     foreach my $item ('login','sso','email') {
11227:         if ($item eq 'email') {
11228:             if ($env{'form.cancreate_email'}) {
11229:                 push(@{$cancreate{'selfcreate'}},'email');
11230:                 push(@contexts,'selfcreateprocessing');
11231:                 foreach my $type (@statuses) {
11232:                     if ($type eq 'default') {
11233:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess'};
11234:                     } else { 
11235:                         $cancreate{'selfcreateprocessing'}{$type} = $env{'form.cancreate_emailprocess_'.$type};
11236:                     }
11237:                 }
11238:             }
11239:         } else {
11240:             if ($env{'form.cancreate_'.$item}) {
11241:                 push(@{$cancreate{'selfcreate'}},$item);
11242:             }
11243:         }
11244:     }
11245:     my (@email_rule,%userinfo,%savecaptcha);
11246:     my ($infofields,$infotitles) = &Apache::loncommon::emailusername_info();
11247: #
11248: # Populate $cancreate{'emailusername'}{$type} hash ref with information fields (if new user will provide data
11249: # value set to one), if self-creation with e-mail address permitted, where $type is user type: faculty, staff, student etc.
11250: #
11251: 
11252:     if ($env{'form.cancreate_email'}) {
11253:         push(@contexts,'emailusername');
11254:         if (ref($types) eq 'ARRAY') {
11255:             foreach my $type (@{$types}) {
11256:                 if (ref($infofields) eq 'ARRAY') {
11257:                     foreach my $field (@{$infofields}) {
11258:                         if ($env{'form.canmodify_emailusername_'.$type.'_'.$field} =~ /^(required|optional)$/) {
11259:                             $cancreate{'emailusername'}{$type}{$field} = $1;
11260:                         }
11261:                     }
11262:                 }
11263:             }
11264:         }
11265: #
11266: # Populate $cancreate{'notify'} hash ref with names of Domain Coordinators who are to be notified of
11267: # queued requests for self-creation of account using e-mail address as username
11268: #
11269: 
11270:         my @approvalnotify = &Apache::loncommon::get_env_multiple('form.selfcreationnotifyapproval');
11271:         @approvalnotify = sort(@approvalnotify);
11272:         $cancreate{'notify'}{'approval'} = join(',',@approvalnotify);
11273:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11274:             if (ref($curr_usercreation{'cancreate'}{'notify'}) eq 'HASH') {
11275:                 if ($curr_usercreation{'cancreate'}{'notify'}{'approval'} ne $cancreate{'notify'}{'approval'}) {
11276:                     push(@{$changes{'cancreate'}},'notify');
11277:                 }
11278:             } else {
11279:                 if ($cancreate{'notify'}{'approval'}) {
11280:                     push(@{$changes{'cancreate'}},'notify');
11281:                 }
11282:             }
11283:         } elsif ($cancreate{'notify'}{'approval'}) {
11284:             push(@{$changes{'cancreate'}},'notify');
11285:         }
11286: 
11287: #
11288: # Retrieve rules (if any) governing types of e-mail address which may be used as a username
11289: #
11290:         @email_rule = &Apache::loncommon::get_env_multiple('form.email_rule');
11291:         &process_captcha('cancreate',\%changes,\%savecaptcha,$curr_usercreation{'cancreate'});
11292:         if (ref($curr_usercreation{'email_rule'}) eq 'ARRAY') {
11293:             if (@{$curr_usercreation{'email_rule'}} > 0) {
11294:                 foreach my $type (@{$curr_usercreation{'email_rule'}}) {
11295:                     if (!grep(/^\Q$type\E$/,@email_rule)) {
11296:                         push(@{$changes{'email_rule'}},$type);
11297:                     }
11298:                 }
11299:             }
11300:             if (@email_rule > 0) {
11301:                 foreach my $type (@email_rule) {
11302:                     if (!grep(/^\Q$type\E$/,@{$curr_usercreation{'email_rule'}})) {
11303:                         push(@{$changes{'email_rule'}},$type);
11304:                     }
11305:                 }
11306:             }
11307:         } elsif (@email_rule > 0) {
11308:             push(@{$changes{'email_rule'}},@email_rule);
11309:         }
11310:     }
11311: #  
11312: # Check if domain default is set appropriately, if self-creation of accounts is to be available for
11313: # institutional log-in.
11314: #
11315:     if (grep(/^login$/,@{$cancreate{'selfcreate'}})) {
11316:         my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11317:         if (!((($domdefaults{'auth_def'} =~/^krb/) && ($domdefaults{'auth_arg_def'} ne '')) || 
11318:                ($domdefaults{'auth_def'} eq 'localauth'))) {
11319:             $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.').' '.
11320:                           &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.');
11321:         }
11322:     }
11323:     my @fields = ('lastname','firstname','middlename','generation',
11324:                   'permanentemail','id');
11325:     my @shibfields = (@fields,'inststatus');
11326:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11327: #
11328: # Where usernames may created for institutional log-in and/or institutional single sign on:
11329: # (a) populate $cancreate{'statustocreate'} array reference with institutional status types who
11330: # may self-create accounts 
11331: # (b) populate $save_usermodify{'selfcreate'} hash reference with status types, and information fields
11332: # which the user may supply, if institutional data is unavailable.
11333: #
11334:     if (($env{'form.cancreate_login'}) || ($env{'form.cancreate_sso'})) {
11335:         if (ref($types) eq 'ARRAY') {
11336:             if (@{$types} > 1) {
11337:                 @{$cancreate{'statustocreate'}} = &Apache::loncommon::get_env_multiple('form.statustocreate');
11338:                 push(@contexts,'statustocreate');
11339:             } else {
11340:                 undef($cancreate{'statustocreate'});
11341:             } 
11342:             foreach my $type (@{$types}) {
11343:                 my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$type);
11344:                 foreach my $field (@fields) {
11345:                     if (grep(/^\Q$field\E$/,@modifiable)) {
11346:                         $save_usermodify{'selfcreate'}{$type}{$field} = 1;
11347:                     } else {
11348:                         $save_usermodify{'selfcreate'}{$type}{$field} = 0;
11349:                     }
11350:                 }
11351:             }
11352:             if (ref($curr_usermodify{'selfcreate'}) eq 'HASH') {
11353:                 foreach my $type (@{$types}) {
11354:                     if (ref($curr_usermodify{'selfcreate'}{$type}) eq 'HASH') {
11355:                         foreach my $field (@fields) {
11356:                             if ($save_usermodify{'selfcreate'}{$type}{$field} ne
11357:                                 $curr_usermodify{'selfcreate'}{$type}{$field}) {
11358:                                 push(@{$changes{'selfcreate'}},$type);
11359:                                 last;
11360:                             }
11361:                         }
11362:                     }
11363:                 }
11364:             } else {
11365:                 foreach my $type (@{$types}) {
11366:                     push(@{$changes{'selfcreate'}},$type);
11367:                 }
11368:             }
11369:         }
11370:         foreach my $field (@shibfields) {
11371:             if ($env{'form.shibenv_'.$field} ne '') {
11372:                 $cancreate{'shibenv'}{$field} = $env{'form.shibenv_'.$field};
11373:             }
11374:         }
11375:         if (ref($curr_usercreation{'cancreate'}) eq 'HASH') {
11376:             if (ref($curr_usercreation{'cancreate'}{'shibenv'}) eq 'HASH') {
11377:                 foreach my $field (@shibfields) {
11378:                     if ($env{'form.shibenv_'.$field} ne $curr_usercreation{'cancreate'}{'shibenv'}{$field}) {
11379:                         push(@{$changes{'cancreate'}},'shibenv');
11380:                     }
11381:                 }
11382:             } else {
11383:                 foreach my $field (@shibfields) {
11384:                     if ($env{'form.shibenv_'.$field}) {
11385:                         push(@{$changes{'cancreate'}},'shibenv');
11386:                         last;
11387:                     }
11388:                 }
11389:             }
11390:         }
11391:     }
11392:     foreach my $item (@contexts) {
11393:         if (ref($curr_usercreation{'cancreate'}{$item}) eq 'ARRAY') {
11394:             foreach my $curr (@{$curr_usercreation{'cancreate'}{$item}}) {
11395:                 if (ref($cancreate{$item}) eq 'ARRAY') {
11396:                     if (!grep(/^$curr$/,@{$cancreate{$item}})) {
11397:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11398:                             push(@{$changes{'cancreate'}},$item);
11399:                         }
11400:                     }
11401:                 }
11402:             }
11403:             if (ref($cancreate{$item}) eq 'ARRAY') {
11404:                 foreach my $type (@{$cancreate{$item}}) {
11405:                     if (!grep(/^$type$/,@{$curr_usercreation{'cancreate'}{$item}})) {
11406:                         if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11407:                             push(@{$changes{'cancreate'}},$item);
11408:                         }
11409:                     }
11410:                 }
11411:             }
11412:         } elsif (ref($curr_usercreation{'cancreate'}{$item}) eq 'HASH') {
11413:             if (ref($cancreate{$item}) eq 'HASH') {
11414:                 foreach my $curr (keys(%{$curr_usercreation{'cancreate'}{$item}})) {
11415:                     if (ref($curr_usercreation{'cancreate'}{$item}{$curr}) eq 'HASH') {
11416:                         foreach my $field (keys(%{$curr_usercreation{'cancreate'}{$item}{$curr}})) {
11417:                             unless ($curr_usercreation{'cancreate'}{$item}{$curr}{$field} eq $cancreate{$item}{$curr}{$field}) {
11418:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11419:                                     push(@{$changes{'cancreate'}},$item);
11420:                                 }
11421:                             }
11422:                         }
11423:                     } elsif ($item eq 'selfcreateprocessing') {
11424:                         if ($cancreate{$item}{$curr} ne $curr_usercreation{'cancreate'}{$item}{$curr}) {
11425:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11426:                                 push(@{$changes{'cancreate'}},$item);
11427:                             }
11428:                         }
11429:                     } else {
11430:                         if (!$cancreate{$item}{$curr}) {
11431:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11432:                                 push(@{$changes{'cancreate'}},$item);
11433:                             }
11434:                         }
11435:                     }
11436:                 }
11437:                 foreach my $field (keys(%{$cancreate{$item}})) {
11438:                     if (ref($cancreate{$item}{$field}) eq 'HASH') {
11439:                         foreach my $inner (keys(%{$cancreate{$item}{$field}})) {
11440:                             if (ref($curr_usercreation{'cancreate'}{$item}{$field}) eq 'HASH') {
11441:                                 unless ($curr_usercreation{'cancreate'}{$item}{$field}{$inner} eq $cancreate{$item}{$field}{$inner}) {
11442:                                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11443:                                         push(@{$changes{'cancreate'}},$item);
11444:                                     }
11445:                                 }
11446:                             } else {
11447:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11448:                                     push(@{$changes{'cancreate'}},$item);
11449:                                 }
11450:                             }
11451:                         }
11452:                     } elsif ($item eq 'selfcreateprocessing') {
11453:                         if ($cancreate{$item}{$field} ne $curr_usercreation{'cancreate'}{$item}{$field}) {
11454:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11455:                                 push(@{$changes{'cancreate'}},$item);
11456:                             }
11457:                         }
11458:                     } else {
11459:                         if (!$curr_usercreation{'cancreate'}{$item}{$field}) {
11460:                             if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11461:                                 push(@{$changes{'cancreate'}},$item);
11462:                             }
11463:                         }
11464:                     }
11465:                 }
11466:             }
11467:         } elsif ($curr_usercreation{'cancreate'}{$item}) {
11468:             if (ref($cancreate{$item}) eq 'ARRAY') {
11469:                 if (!grep(/^\Q$curr_usercreation{'cancreate'}{$item}\E$/,@{$cancreate{$item}})) {
11470:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11471:                         push(@{$changes{'cancreate'}},$item);
11472:                     }
11473:                 }
11474:             } elsif (ref($cancreate{$item}) eq 'HASH') {
11475:                 if (!$cancreate{$item}{$curr_usercreation{'cancreate'}{$item}}) {
11476:                     if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11477:                         push(@{$changes{'cancreate'}},$item);
11478:                     }
11479:                 }
11480:             }
11481:         } elsif ($item eq 'emailusername') {
11482:             if (ref($cancreate{$item}) eq 'HASH') {
11483:                 foreach my $type (keys(%{$cancreate{$item}})) {
11484:                     if (ref($cancreate{$item}{$type}) eq 'HASH') {
11485:                         foreach my $field (keys(%{$cancreate{$item}{$type}})) {
11486:                             if ($cancreate{$item}{$type}{$field}) {
11487:                                 if (!grep(/^$item$/,@{$changes{'cancreate'}})) {
11488:                                     push(@{$changes{'cancreate'}},$item);
11489:                                 }
11490:                                 last;
11491:                             }
11492:                         }
11493:                     }
11494:                 }
11495:             }
11496:         }
11497:     }
11498: #
11499: # Populate %save_usercreate hash with updates to self-creation configuration.
11500: #
11501:     $save_usercreate{'cancreate'}{'captcha'} = $savecaptcha{'captcha'};
11502:     $save_usercreate{'cancreate'}{'recaptchakeys'} = $savecaptcha{'recaptchakeys'};
11503:     $save_usercreate{'cancreate'}{'recaptchaversion'} = $savecaptcha{'recaptchaversion'};
11504:     $save_usercreate{'cancreate'}{'selfcreate'} = $cancreate{'selfcreate'};
11505:     if (ref($cancreate{'notify'}) eq 'HASH') {
11506:         $save_usercreate{'cancreate'}{'notify'} = $cancreate{'notify'};
11507:     }
11508:     if (ref($cancreate{'selfcreateprocessing'}) eq 'HASH') {
11509:         $save_usercreate{'cancreate'}{'selfcreateprocessing'} = $cancreate{'selfcreateprocessing'};
11510:     }
11511:     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
11512:         $save_usercreate{'cancreate'}{'statustocreate'} = $cancreate{'statustocreate'};
11513:     }
11514:     if (ref($cancreate{'shibenv'}) eq 'HASH') {
11515:         $save_usercreate{'cancreate'}{'shibenv'} = $cancreate{'shibenv'};
11516:     }
11517:     $save_usercreate{'cancreate'}{'emailusername'} = $cancreate{'emailusername'};
11518:     $save_usercreate{'emailrule'} = \@email_rule;
11519: 
11520:     my %userconfig_hash = (
11521:             usercreation     => \%save_usercreate,
11522:             usermodification => \%save_usermodify,
11523:     );
11524:     my $putresult = &Apache::lonnet::put_dom('configuration',\%userconfig_hash,
11525:                                              $dom);
11526: #
11527: # Accumulate details of changes to domain cofiguration for self-creation of usernames in $resulttext
11528: #
11529:     if ($putresult eq 'ok') {
11530:         if (keys(%changes) > 0) {
11531:             $resulttext = &mt('Changes made:').'<ul>';
11532:             if (ref($changes{'cancreate'}) eq 'ARRAY') {
11533:                 my %lt = &selfcreation_types();
11534:                 foreach my $type (@{$changes{'cancreate'}}) {
11535:                     my $chgtext;
11536:                     if ($type eq 'selfcreate') {
11537:                         if (@{$cancreate{$type}} == 0) {
11538:                             $chgtext .= &mt('Self creation of a new user account is not permitted.');
11539:                         } else {
11540:                             $chgtext .= &mt('Self-creation of a new account is permitted for:').
11541:                                         '<ul>';
11542:                             foreach my $case (@{$cancreate{$type}}) {
11543:                                 $chgtext .= '<li>'.$selfcreatetypes{$case}.'</li>';
11544:                             }
11545:                             $chgtext .= '</ul>';
11546:                             if (ref($cancreate{$type}) eq 'ARRAY') {
11547:                                 if (grep(/^(login|sso)$/,@{$cancreate{$type}})) {
11548:                                     if (ref($cancreate{'statustocreate'}) eq 'ARRAY') {
11549:                                         if (@{$cancreate{'statustocreate'}} == 0) {
11550:                                             $chgtext .= '<br />'.
11551:                                                         '<span class="LC_warning">'.
11552:                                                         &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
11553:                                                         '</span>';
11554:                                         }
11555:                                     }
11556:                                 }
11557:                             }
11558:                         }
11559:                     } elsif ($type eq 'shibenv') {
11560:                         if (keys(%{$cancreate{$type}}) == 0) {
11561:                             $chgtext .= &mt('Shibboleth-autheticated user does not use environment variables to set user information'); 
11562:                         } else {
11563:                             $chgtext .= &mt('Shibboleth-autheticated user information set from environment variables, as follows:').
11564:                                         '<ul>';
11565:                             foreach my $field (@shibfields) {
11566:                                 next if ($cancreate{$type}{$field} eq '');
11567:                                 if ($field eq 'inststatus') {
11568:                                     $chgtext .= '<li>'.&mt('Institutional status').' -- '.$cancreate{$type}{$field}.'</li>';
11569:                                 } else {
11570:                                     $chgtext .= '<li>'.$fieldtitles{$field}.' -- '.$cancreate{$type}{$field}.'</li>';
11571:                                 }
11572:                             }
11573:                             $chgtext .= '</ul>';
11574:                         }  
11575:                     } elsif ($type eq 'statustocreate') {
11576:                         if ((ref($cancreate{'selfcreate'}) eq 'ARRAY') &&
11577:                             (ref($cancreate{'statustocreate'}) eq 'ARRAY')) {
11578:                             if (@{$cancreate{'selfcreate'}} > 0) {
11579:                                 if (@{$cancreate{'statustocreate'}} == 0) {
11580:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts set to 'None'.");
11581:                                     if (!grep(/^email$/,@{$cancreate{'selfcreate'}})) {
11582:                                         $chgtext .= '<br />'.
11583:                                                     '<span class="LC_warning">'.
11584:                                                     &mt("However, no institutional affiliations (including 'other') are currently permitted to create accounts.").
11585:                                                     '</span>';
11586:                                     }
11587:                                 } elsif (ref($usertypes) eq 'HASH') {
11588:                                     if (grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
11589:                                         $chgtext .= &mt('Creation of a new account for an institutional user is restricted to the following institutional affiliation(s):');
11590:                                     } else {
11591:                                         $chgtext .= &mt('Institutional affiliations permitted to create accounts with institutional authentication were set as follows:');
11592:                                     }
11593:                                     $chgtext .= '<ul>';
11594:                                     foreach my $case (@{$cancreate{$type}}) {
11595:                                         if ($case eq 'default') {
11596:                                             $chgtext .= '<li>'.$othertitle.'</li>';
11597:                                         } else {
11598:                                             $chgtext .= '<li>'.$usertypes->{$case}.'</li>';
11599:                                         }
11600:                                     }
11601:                                     $chgtext .= '</ul>';
11602:                                     if (!grep(/^(login|sso)$/,@{$cancreate{'selfcreate'}})) {
11603:                                         $chgtext .= '<br /><span class="LC_warning">'.
11604:                                                     &mt('However, users authenticated by institutional login/single sign on are not currently permitted to create accounts.').
11605:                                                     '</span>';
11606:                                     }
11607:                                 }
11608:                             } else {
11609:                                 if (@{$cancreate{$type}} == 0) {
11610:                                     $chgtext .= &mt("Institutional affiliations permitted to create accounts were set to 'none'.");
11611:                                 } else {
11612:                                     $chgtext .= &mt('Although institutional affiliations permitted to create accounts were changed, self creation of accounts is not currently permitted for any authentication types.');
11613:                                 }
11614:                             }
11615:                         }
11616:                     } elsif ($type eq 'selfcreateprocessing') {
11617:                         my %choices = &Apache::lonlocal::texthash (
11618:                                                                     automatic => 'Automatic approval',
11619:                                                                     approval  => 'Queued for approval',
11620:                                                                   );
11621:                         if (@statuses > 1) {
11622:                             $chgtext .= &mt('Processing of requests to create account with e-mail address as username set as follows:'). 
11623:                                         '<ul>';
11624:                            foreach my $type (@statuses) {
11625:                                if ($type eq 'default') {
11626:                                    $chgtext .= '<li>'.$othertitle.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
11627:                                } else {
11628:                                    $chgtext .= '<li>'.$usertypes->{$type}.' -- '.$choices{$cancreate{'selfcreateprocessing'}{$type}}.'</li>';
11629:                                }
11630:                            }
11631:                            $chgtext .= '</ul>';
11632:                         } else {
11633:                            $chgtext .= &mt('Processing of requests to create account with e-mail address as username set to: "[_1]"',
11634:                                          $choices{$cancreate{'selfcreateprocessing'}{'default'}});
11635:                         }
11636:                     } elsif ($type eq 'captcha') {
11637:                         if ($savecaptcha{$type} eq 'notused') {
11638:                             $chgtext .= &mt('No CAPTCHA validation in use for self-creation screen.');
11639:                         } else {
11640:                             my %captchas = &captcha_phrases();
11641:                             if ($captchas{$savecaptcha{$type}}) {
11642:                                 $chgtext .= &mt("Validation for self-creation screen set to $captchas{$savecaptcha{$type}}.");
11643:                             } else {
11644:                                 $chgtext .= &mt('Validation for self-creation screen set to unknown type.');
11645:                             }
11646:                         }
11647:                     } elsif ($type eq 'recaptchakeys') {
11648:                         my ($privkey,$pubkey);
11649:                         if (ref($savecaptcha{$type}) eq 'HASH') {
11650:                             $pubkey = $savecaptcha{$type}{'public'};
11651:                             $privkey = $savecaptcha{$type}{'private'};
11652:                         }
11653:                         $chgtext .= &mt('ReCAPTCHA keys changes').'<ul>';
11654:                         if (!$pubkey) {
11655:                             $chgtext .= '<li>'.&mt('Public key deleted').'</li>';
11656:                         } else {
11657:                             $chgtext .= '<li>'.&mt('Public key set to [_1]',$pubkey).'</li>';
11658:                         }
11659:                         if (!$privkey) {
11660:                             $chgtext .= '<li>'.&mt('Private key deleted').'</li>';
11661:                         } else {
11662:                             $chgtext .= '<li>'.&mt('Private key set to [_1]',$pubkey).'</li>';
11663:                         }
11664:                         $chgtext .= '</ul>';
11665:                     } elsif ($type eq 'recaptchaversion') {
11666:                         if ($savecaptcha{'captcha'} eq 'recaptcha') {
11667:                             $chgtext .= &mt('ReCAPTCHA set to version [_1]',$savecaptcha{$type});
11668:                         }
11669:                     } elsif ($type eq 'emailusername') {
11670:                         if (ref($cancreate{'emailusername'}) eq 'HASH') {
11671:                             if (ref($types) eq 'ARRAY') {
11672:                                 foreach my $type (@{$types}) {
11673:                                     if (ref($cancreate{'emailusername'}{$type}) eq 'HASH') {
11674:                                         if (keys(%{$cancreate{'emailusername'}{$type}}) > 0) {
11675:                                             $chgtext .= &mt('When self-creating account with e-mail as username, the following information will be provided by [_1]:',"'$usertypes->{$type}'").
11676:                                                     '<ul>';
11677:                                             foreach my $field (@{$infofields}) {
11678:                                                 if ($cancreate{'emailusername'}{$type}{$field}) {
11679:                                                     $chgtext .= '<li>'.$infotitles->{$field}.'</li>';
11680:                                                 }
11681:                                             }
11682:                                             $chgtext .= '</ul>';
11683:                                         } else {
11684:                                             $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 />';
11685:                                         }
11686:                                     } else {
11687:                                         $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 />';
11688:                                     }
11689:                                 }
11690:                             }
11691:                         }
11692:                     } elsif ($type eq 'notify') {
11693:                         $chgtext = &mt('No Domain Coordinators will receive notification of username requests requiring approval.');
11694:                         if (ref($changes{'cancreate'}) eq 'ARRAY') {
11695:                             if ((grep(/^notify$/,@{$changes{'cancreate'}})) && (ref($cancreate{'notify'}) eq 'HASH')) {
11696:                                 if ($cancreate{'notify'}{'approval'}) {
11697:                                     $chgtext = &mt('Notification of username requests requiring approval will be sent to: ').$cancreate{'notify'}{'approval'};
11698:                                 }
11699:                             }
11700:                         }
11701:                     }
11702:                     if ($chgtext) {
11703:                         $resulttext .= '<li>'.$chgtext.'</li>';
11704:                     }
11705:                 }
11706:             }
11707:             if (ref($changes{'email_rule'}) eq 'ARRAY') {
11708:                 my ($emailrules,$emailruleorder) =
11709:                     &Apache::lonnet::inst_userrules($dom,'email');
11710:                 my $chgtext = '<ul>';
11711:                 foreach my $type (@email_rule) {
11712:                     if (ref($emailrules->{$type}) eq 'HASH') {
11713:                         $chgtext .= '<li>'.$emailrules->{$type}{'name'}.'</li>';
11714:                     }
11715:                 }
11716:                 $chgtext .= '</ul>';
11717:                 if (@email_rule > 0) {
11718:                     $resulttext .= '<li>'.
11719:                                    &mt('Accounts may not be created by users self-enrolling with e-mail addresses of the following types: ').
11720:                                        $chgtext.
11721:                                    '</li>';
11722:                 } else {
11723:                     $resulttext .= '<li>'.
11724:                                    &mt('There are now no restrictions on e-mail addresses which may be used as a username when self-enrolling.').
11725:                                    '</li>';
11726:                 }
11727:             }
11728:             if (ref($changes{'selfcreate'}) eq 'ARRAY') {
11729:                 $resulttext .= '<li>'.&mt('When self-creating institutional account:').'<ul>';
11730:                 my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11731:                 foreach my $type (@{$changes{'selfcreate'}}) {
11732:                     my $typename = $type;
11733:                     if (ref($usertypes) eq 'HASH') {
11734:                         if ($usertypes->{$type} ne '') {
11735:                             $typename = $usertypes->{$type};
11736:                         }
11737:                     }
11738:                     my @modifiable;
11739:                     $resulttext .= '<li>'.
11740:                                     &mt('Self-creation of account by users with status: [_1]',
11741:                                         '<span class="LC_cusr_emph">'.$typename.'</span>').
11742:                                     ' - '.&mt('modifiable fields (if institutional data blank): ');
11743:                     foreach my $field (@fields) {
11744:                         if ($save_usermodify{'selfcreate'}{$type}{$field}) {
11745:                             push(@modifiable,'<b>'.$fieldtitles{$field}.'</b>');
11746:                         }
11747:                     }
11748:                     if (@modifiable > 0) {
11749:                         $resulttext .= join(', ',@modifiable);
11750:                     } else {
11751:                         $resulttext .= &mt('none');
11752:                     }
11753:                     $resulttext .= '</li>';
11754:                 }
11755:                 $resulttext .= '</ul></li>';
11756:             }
11757:             $resulttext .= '</ul>';
11758:         } else {
11759:             $resulttext = &mt('No changes made to self-creation settings');
11760:         }
11761:     } else {
11762:         $resulttext = '<span class="LC_error">'.
11763:             &mt('An error occurred: [_1]',$putresult).'</span>';
11764:     }
11765:     if ($warningmsg ne '') {
11766:         $resulttext .= '<br /><span class="LC_warning">'.$warningmsg.'</span><br />';
11767:     }
11768:     return $resulttext;
11769: }
11770: 
11771: sub process_captcha {
11772:     my ($container,$changes,$newsettings,$current) = @_;
11773:     return unless ((ref($changes) eq 'HASH') && (ref($newsettings) eq 'HASH') || (ref($current) eq 'HASH'));
11774:     $newsettings->{'captcha'} = $env{'form.'.$container.'_captcha'};
11775:     unless ($newsettings->{'captcha'} eq 'recaptcha' || $newsettings->{'captcha'} eq 'notused') {
11776:         $newsettings->{'captcha'} = 'original';
11777:     }
11778:     if ($current->{'captcha'} ne $newsettings->{'captcha'}) {
11779:         if ($container eq 'cancreate') {
11780:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11781:                 push(@{$changes->{'cancreate'}},'captcha');
11782:             } elsif (!defined($changes->{'cancreate'})) {
11783:                 $changes->{'cancreate'} = ['captcha'];
11784:             }
11785:         } else {
11786:             $changes->{'captcha'} = 1;
11787:         }
11788:     }
11789:     my ($newpub,$newpriv,$currpub,$currpriv,$newversion,$currversion);
11790:     if ($newsettings->{'captcha'} eq 'recaptcha') {
11791:         $newpub = $env{'form.'.$container.'_recaptchapub'};
11792:         $newpriv = $env{'form.'.$container.'_recaptchapriv'};
11793:         $newpub =~ s/[^\w\-]//g;
11794:         $newpriv =~ s/[^\w\-]//g;
11795:         $newsettings->{'recaptchakeys'} = {
11796:                                              public  => $newpub,
11797:                                              private => $newpriv,
11798:                                           };
11799:         $newversion = $env{'form.'.$container.'_recaptchaversion'};
11800:         $newversion =~ s/\D//g;
11801:         if ($newversion ne '2') {
11802:             $newversion = 1;
11803:         }
11804:         $newsettings->{'recaptchaversion'} = $newversion;
11805:     }
11806:     if (ref($current->{'recaptchakeys'}) eq 'HASH') {
11807:         $currpub = $current->{'recaptchakeys'}{'public'};
11808:         $currpriv = $current->{'recaptchakeys'}{'private'};
11809:         unless ($newsettings->{'captcha'} eq 'recaptcha') {
11810:             $newsettings->{'recaptchakeys'} = {
11811:                                                  public  => '',
11812:                                                  private => '',
11813:                                               }
11814:         }
11815:     }
11816:     if ($current->{'captcha'} eq 'recaptcha') {
11817:         $currversion = $current->{'recaptchaversion'};
11818:         if ($currversion ne '2') {
11819:             $currversion = 1;
11820:         }
11821:     }
11822:     if ($currversion ne $newversion) {
11823:         if ($container eq 'cancreate') {
11824:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11825:                 push(@{$changes->{'cancreate'}},'recaptchaversion');
11826:             } elsif (!defined($changes->{'cancreate'})) {
11827:                 $changes->{'cancreate'} = ['recaptchaversion'];
11828:             }
11829:         } else {
11830:             $changes->{'recaptchaversion'} = 1;
11831:         }
11832:     }
11833:     if (($newpub ne $currpub) || ($newpriv ne $currpriv)) {
11834:         if ($container eq 'cancreate') {
11835:             if (ref($changes->{'cancreate'}) eq 'ARRAY') {
11836:                 push(@{$changes->{'cancreate'}},'recaptchakeys');
11837:             } elsif (!defined($changes->{'cancreate'})) {
11838:                 $changes->{'cancreate'} = ['recaptchakeys'];
11839:             }
11840:         } else {
11841:             $changes->{'recaptchakeys'} = 1;
11842:         }
11843:     }
11844:     return;
11845: }
11846: 
11847: sub modify_usermodification {
11848:     my ($dom,%domconfig) = @_;
11849:     my ($resulttext,%curr_usermodification,%changes,%modifyhash);
11850:     if (ref($domconfig{'usermodification'}) eq 'HASH') {
11851:         foreach my $key (keys(%{$domconfig{'usermodification'}})) {
11852:             if ($key eq 'selfcreate') {
11853:                 $modifyhash{$key} = $domconfig{'usermodification'}{$key};
11854:             } else {  
11855:                 $curr_usermodification{$key} = $domconfig{'usermodification'}{$key};
11856:             }
11857:         }
11858:     }
11859:     my @contexts = ('author','course');
11860:     my %context_title = (
11861:                            author => 'In author context',
11862:                            course => 'In course context',
11863:                         );
11864:     my @fields = ('lastname','firstname','middlename','generation',
11865:                   'permanentemail','id');
11866:     my %roles = (
11867:                   author => ['ca','aa'],
11868:                   course => ['st','ep','ta','in','cr'],
11869:                 );
11870:     my %fieldtitles = &Apache::loncommon::personal_data_fieldtitles();
11871:     foreach my $context (@contexts) {
11872:         foreach my $role (@{$roles{$context}}) {
11873:             my @modifiable =  &Apache::loncommon::get_env_multiple('form.canmodify_'.$role);
11874:             foreach my $item (@fields) {
11875:                 if (grep(/^\Q$item\E$/,@modifiable)) {
11876:                     $modifyhash{$context}{$role}{$item} = 1;
11877:                 } else {
11878:                     $modifyhash{$context}{$role}{$item} = 0;
11879:                 }
11880:             }
11881:         }
11882:         if (ref($curr_usermodification{$context}) eq 'HASH') {
11883:             foreach my $role (@{$roles{$context}}) {
11884:                 if (ref($curr_usermodification{$context}{$role}) eq 'HASH') {
11885:                     foreach my $field (@fields) {
11886:                         if ($modifyhash{$context}{$role}{$field} ne 
11887:                                 $curr_usermodification{$context}{$role}{$field}) {
11888:                             push(@{$changes{$context}},$role);
11889:                             last;
11890:                         }
11891:                     }
11892:                 }
11893:             }
11894:         } else {
11895:             foreach my $context (@contexts) {
11896:                 foreach my $role (@{$roles{$context}}) {
11897:                     push(@{$changes{$context}},$role);
11898:                 }
11899:             }
11900:         }
11901:     }
11902:     my %usermodification_hash =  (
11903:                                    usermodification => \%modifyhash,
11904:                                  );
11905:     my $putresult = &Apache::lonnet::put_dom('configuration',
11906:                                              \%usermodification_hash,$dom);
11907:     if ($putresult eq 'ok') {
11908:         if (keys(%changes) > 0) {
11909:             $resulttext = &mt('Changes made: ').'<ul>';
11910:             foreach my $context (@contexts) {
11911:                 if (ref($changes{$context}) eq 'ARRAY') {
11912:                     $resulttext .= '<li>'.$context_title{$context}.':<ul>';
11913:                     if (ref($changes{$context}) eq 'ARRAY') {
11914:                         foreach my $role (@{$changes{$context}}) {
11915:                             my $rolename;
11916:                             if ($role eq 'cr') {
11917:                                 $rolename = &mt('Custom');
11918:                             } else {
11919:                                 $rolename = &Apache::lonnet::plaintext($role);
11920:                             }
11921:                             my @modifiable;
11922:                             $resulttext .= '<li><span class="LC_cusr_emph">'.&mt('Target user with [_1] role',$rolename).'</span> - '.&mt('modifiable fields: ');
11923:                             foreach my $field (@fields) {
11924:                                 if ($modifyhash{$context}{$role}{$field}) {
11925:                                     push(@modifiable,$fieldtitles{$field});
11926:                                 }
11927:                             }
11928:                             if (@modifiable > 0) {
11929:                                 $resulttext .= join(', ',@modifiable);
11930:                             } else {
11931:                                 $resulttext .= &mt('none'); 
11932:                             }
11933:                             $resulttext .= '</li>';
11934:                         }
11935:                         $resulttext .= '</ul></li>';
11936:                     }
11937:                 }
11938:             }
11939:             $resulttext .= '</ul>';
11940:         } else {
11941:             $resulttext = &mt('No changes made to user modification settings');
11942:         }
11943:     } else {
11944:         $resulttext = '<span class="LC_error">'.
11945:             &mt('An error occurred: [_1]',$putresult).'</span>';
11946:     }
11947:     return $resulttext;
11948: }
11949: 
11950: sub modify_defaults {
11951:     my ($dom,$lastactref,%domconfig) = @_;
11952:     my ($resulttext,$mailmsgtxt,%newvalues,%changes,@errors);
11953:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
11954:     my @items = ('auth_def','auth_arg_def','lang_def','timezone_def','datelocale_def',
11955:                  'portal_def','intauth_cost','intauth_check','intauth_switch');
11956:     my @authtypes = ('internal','krb4','krb5','localauth');
11957:     foreach my $item (@items) {
11958:         $newvalues{$item} = $env{'form.'.$item};
11959:         if ($item eq 'auth_def') {
11960:             if ($newvalues{$item} ne '') {
11961:                 if (!grep(/^\Q$newvalues{$item}\E$/,@authtypes)) {
11962:                     push(@errors,$item);
11963:                 }
11964:             }
11965:         } elsif ($item eq 'lang_def') {
11966:             if ($newvalues{$item} ne '') {
11967:                 if ($newvalues{$item} =~ /^(\w+)/) {
11968:                     my $langcode = $1;
11969:                     if ($langcode ne 'x_chef') {
11970:                         if (code2language($langcode) eq '') {
11971:                             push(@errors,$item);
11972:                         }
11973:                     }
11974:                 } else {
11975:                     push(@errors,$item);
11976:                 }
11977:             }
11978:         } elsif ($item eq 'timezone_def') {
11979:             if ($newvalues{$item} ne '') {
11980:                 if (!DateTime::TimeZone->is_valid_name($newvalues{$item})) {
11981:                     push(@errors,$item);   
11982:                 }
11983:             }
11984:         } elsif ($item eq 'datelocale_def') {
11985:             if ($newvalues{$item} ne '') {
11986:                 my @datelocale_ids = DateTime::Locale->ids();
11987:                 if (!grep(/^\Q$newvalues{$item}\E$/,@datelocale_ids)) {
11988:                     push(@errors,$item);
11989:                 }
11990:             }
11991:         } elsif ($item eq 'portal_def') {
11992:             if ($newvalues{$item} ne '') {
11993:                 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])\/?$/) {
11994:                     push(@errors,$item);
11995:                 }
11996:             }
11997:         } elsif ($item eq 'intauth_cost') {
11998:             if ($newvalues{$item} ne '') {
11999:                 if ($newvalues{$item} =~ /\D/) {
12000:                     push(@errors,$item);
12001:                 }
12002:             }
12003:         } elsif ($item eq 'intauth_check') {
12004:             if ($newvalues{$item} ne '') {
12005:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
12006:                     push(@errors,$item);
12007:                 }
12008:             }
12009:         } elsif ($item eq 'intauth_switch') {
12010:             if ($newvalues{$item} ne '') {
12011:                 unless ($newvalues{$item} =~ /^(0|1|2)$/) {
12012:                     push(@errors,$item);
12013:                 }
12014:             }
12015:         }
12016:         if (grep(/^\Q$item\E$/,@errors)) {
12017:             $newvalues{$item} = $domdefaults{$item};
12018:         } elsif ($domdefaults{$item} ne $newvalues{$item}) {
12019:             $changes{$item} = 1;
12020:         }
12021:         $domdefaults{$item} = $newvalues{$item};
12022:     }
12023:     my %defaults_hash = (
12024:                          defaults => \%newvalues,
12025:                         );
12026:     my $title = &defaults_titles();
12027: 
12028:     my $currinststatus;
12029:     if (ref($domconfig{'inststatus'}) eq 'HASH') {
12030:         $currinststatus = $domconfig{'inststatus'};
12031:     } else {
12032:         my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12033:         $currinststatus = {
12034:                              inststatustypes => $usertypes,
12035:                              inststatusorder => $types,
12036:                              inststatusguest => [],
12037:                           };
12038:     }
12039:     my @todelete = &Apache::loncommon::get_env_multiple('form.inststatus_delete');
12040:     my @allpos;
12041:     my %guests;
12042:     my %alltypes;
12043:     my ($currtitles,$currguests,$currorder);
12044:     if (ref($currinststatus) eq 'HASH') {
12045:         if (ref($currinststatus->{'inststatusorder'}) eq 'ARRAY') {
12046:             foreach my $type (@{$currinststatus->{'inststatusorder'}}) {
12047:                 if (ref($currinststatus->{inststatustypes}) eq 'HASH') {
12048:                     if ($currinststatus->{inststatustypes}->{$type} ne '') {
12049:                         $currtitles .= $currinststatus->{inststatustypes}->{$type}.',';
12050:                     }
12051:                 }
12052:                 unless (grep(/^\Q$type\E$/,@todelete)) { 
12053:                     my $position = $env{'form.inststatus_pos_'.$type};
12054:                     $position =~ s/\D+//g;
12055:                     $allpos[$position] = $type;
12056:                     $alltypes{$type} = $env{'form.inststatus_title_'.$type};
12057:                     $alltypes{$type} =~ s/`//g;
12058:                     if ($env{'form.inststatus_guest_'.$type}) {
12059:                         $guests{$type} = 1;
12060:                     }
12061:                 }
12062:             }
12063:             if (ref($currinststatus->{'inststatusguest'}) eq 'ARRAY') {
12064:                 $currguests = join(',',@{$currinststatus->{'inststatusguest'}});
12065:             }
12066:             $currorder = join(',',@{$currinststatus->{'inststatusorder'}});
12067:             $currtitles =~ s/,$//;
12068:         }
12069:     }
12070:     if ($env{'form.addinststatus'}) {
12071:         my $newtype = $env{'form.addinststatus'};
12072:         $newtype =~ s/\W//g;
12073:         unless (exists($alltypes{$newtype})) {
12074:             if ($env{'form.addinststatus_guest'}) {
12075:                 $guests{$newtype} = 1;
12076:             }
12077:             $alltypes{$newtype} = $env{'form.addinststatus_title'};
12078:             $alltypes{$newtype} =~ s/`//g; 
12079:             my $position = $env{'form.addinststatus_pos'};
12080:             $position =~ s/\D+//g;
12081:             if ($position ne '') {
12082:                 $allpos[$position] = $newtype;
12083:             }
12084:         }
12085:     }
12086:     my (@orderedstatus,@orderedguests);
12087:     foreach my $type (@allpos) {
12088:         unless (($type eq '') || (grep(/^\Q$type\E$/,@orderedstatus))) {
12089:             push(@orderedstatus,$type);
12090:             if ($guests{$type}) {
12091:                 push(@orderedguests,$type);
12092:             }
12093:         }
12094:     }
12095:     foreach my $type (keys(%alltypes)) {
12096:         unless (grep(/^\Q$type\E$/,@orderedstatus)) {
12097:             delete($alltypes{$type});
12098:         }
12099:     }
12100:     $defaults_hash{'inststatus'} = {
12101:                                      inststatustypes => \%alltypes,
12102:                                      inststatusorder => \@orderedstatus,
12103:                                      inststatusguest => \@orderedguests,
12104:                                    };
12105:     if (ref($defaults_hash{'inststatus'}) eq 'HASH') {
12106:         foreach my $item ('inststatustypes','inststatusorder','inststatusguest') {
12107:             $domdefaults{$item} = $defaults_hash{'inststatus'}{$item};
12108:         }
12109:     }
12110:     if ($currorder ne join(',',@orderedstatus)) {
12111:         $changes{'inststatus'}{'inststatusorder'} = 1;
12112:     }
12113:     if ($currguests ne join(',',@orderedguests)) {
12114:         $changes{'inststatus'}{'inststatusguest'} = 1;
12115:     }
12116:     my $newtitles;
12117:     foreach my $item (@orderedstatus) {
12118:         $newtitles .= $alltypes{$item}.',';
12119:     }
12120:     $newtitles =~ s/,$//;
12121:     if ($currtitles ne $newtitles) {
12122:         $changes{'inststatus'}{'inststatustypes'} = 1;
12123:     }
12124:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaults_hash,
12125:                                              $dom);
12126:     if ($putresult eq 'ok') {
12127:         if (keys(%changes) > 0) {
12128:             $resulttext = &mt('Changes made:').'<ul>';
12129:             my $version = &Apache::lonnet::get_server_loncaparev($dom);
12130:             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";
12131:             foreach my $item (sort(keys(%changes))) {
12132:                 if ($item eq 'inststatus') {
12133:                     if (ref($changes{'inststatus'}) eq 'HASH') {
12134:                         if (($changes{'inststatus'}{'inststatustypes'}) || $changes{'inststatus'}{'inststatusorder'}) {
12135:                             $resulttext .= '<li>'.&mt('Institutional user status types set to:').' ';
12136:                             foreach my $type (@orderedstatus) { 
12137:                                 $resulttext .= $alltypes{$type}.', ';
12138:                             }
12139:                             $resulttext =~ s/, $//;
12140:                             $resulttext .= '</li>';
12141:                         }
12142:                         if ($changes{'inststatus'}{'inststatusguest'}) {
12143:                             $resulttext .= '<li>'; 
12144:                             if (@orderedguests) {
12145:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to:').' ';
12146:                                 foreach my $type (@orderedguests) {
12147:                                     $resulttext .= $alltypes{$type}.', ';
12148:                                 }
12149:                                 $resulttext =~ s/, $//;
12150:                             } else {
12151:                                 $resulttext .= &mt('Types assignable to "non-institutional" usernames set to none.');
12152:                             }
12153:                             $resulttext .= '</li>';
12154:                         }
12155:                     }
12156:                 } else {
12157:                     my $value = $env{'form.'.$item};
12158:                     if ($value eq '') {
12159:                         $value = &mt('none');
12160:                     } elsif ($item eq 'auth_def') {
12161:                         my %authnames = &authtype_names();
12162:                         my %shortauth = (
12163:                                           internal   => 'int',
12164:                                           krb4       => 'krb4',
12165:                                           krb5       => 'krb5',
12166:                                           localauth  => 'loc',
12167:                         );
12168:                         $value = $authnames{$shortauth{$value}};
12169:                     } elsif ($item eq 'intauth_switch') {
12170:                         my %optiondesc = &Apache::lonlocal::texthash (
12171:                                             0 => 'No',
12172:                                             1 => 'Yes',
12173:                                             2 => 'Yes, and copy existing passwd file to passwd.bak file',
12174:                                          );
12175:                         if ($value =~ /^(0|1|2)$/) {
12176:                             $value = $optiondesc{$value};
12177:                         } else {
12178:                             $value = &mt('none -- defaults to No');
12179:                         }
12180:                     } elsif ($item eq 'intauth_check') {
12181:                         my %optiondesc = &Apache::lonlocal::texthash (
12182:                                              0 => 'No',
12183:                                              1 => 'Yes, allow login then update passwd file using default cost (if higher)',
12184:                                              2 => 'Yes, disallow login if stored cost is less than domain default',
12185:                                          );
12186:                         if ($value =~ /^(0|1|2)$/) {
12187:                             $value = $optiondesc{$value};
12188:                         } else {
12189:                             $value = &mt('none -- defaults to No');
12190:                         }
12191:                     }
12192:                     $resulttext .= '<li>'.&mt('[_1] set to "[_2]"',$title->{$item},$value).'</li>';
12193:                     $mailmsgtext .= "$title->{$item} set to $value\n";  
12194:                 }
12195:             }
12196:             $resulttext .= '</ul>';
12197:             $mailmsgtext .= "\n";
12198:             my $cachetime = 24*60*60;
12199:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12200:             if (ref($lastactref) eq 'HASH') {
12201:                 $lastactref->{'domdefaults'} = 1;
12202:             }
12203:             if ($changes{'auth_def'} || $changes{'auth_arg_def'} || $changes{'lang_def'} || $changes{'datelocale_def'}) {
12204:                 my $notify = 1;
12205:                 if (ref($domconfig{'contacts'}) eq 'HASH') {
12206:                     if ($domconfig{'contacts'}{'reportupdates'} == 0) {
12207:                         $notify = 0;
12208:                     }
12209:                 }
12210:                 if ($notify) {
12211:                     &Apache::lonmsg::sendemail('installrecord@loncapa.org',
12212:                                                "LON-CAPA Domain Settings Change - $dom",
12213:                                                $mailmsgtext);
12214:                 }
12215:             }
12216:         } else {
12217:             $resulttext = &mt('No changes made to default authentication/language/timezone settings');
12218:         }
12219:     } else {
12220:         $resulttext = '<span class="LC_error">'.
12221:             &mt('An error occurred: [_1]',$putresult).'</span>';
12222:     }
12223:     if (@errors > 0) {
12224:         $resulttext .= '<br />'.&mt('The following were left unchanged because the values entered were invalid:');
12225:         foreach my $item (@errors) {
12226:             $resulttext .= ' "'.$title->{$item}.'",';
12227:         }
12228:         $resulttext =~ s/,$//;
12229:     }
12230:     return $resulttext;
12231: }
12232: 
12233: sub modify_scantron {
12234:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12235:     my ($resulttext,%confhash,%changes,$errors);
12236:     my $custom = 'custom.tab';
12237:     my $default = 'default.tab';
12238:     my $servadm = $r->dir_config('lonAdmEMail');
12239:     my ($configuserok,$author_ok,$switchserver) = 
12240:         &config_check($dom,$confname,$servadm);
12241:     if ($env{'form.scantronformat.filename'} ne '') {
12242:         my $error;
12243:         if ($configuserok eq 'ok') {
12244:             if ($switchserver) {
12245:                 $error = &mt("Upload of bubblesheet format file is not permitted to this server: [_1]",$switchserver);
12246:             } else {
12247:                 if ($author_ok eq 'ok') {
12248:                     my ($result,$scantronurl) =
12249:                         &publishlogo($r,'upload','scantronformat',$dom,
12250:                                      $confname,'scantron','','',$custom);
12251:                     if ($result eq 'ok') {
12252:                         $confhash{'scantron'}{'scantronformat'} = $scantronurl;
12253:                         $changes{'scantronformat'} = 1;
12254:                     } else {
12255:                         $error = &mt("Upload of [_1] failed because an error occurred publishing the file in RES space. Error was: [_2].",$custom,$result);
12256:                     }
12257:                 } else {
12258:                     $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);
12259:                 }
12260:             }
12261:         } else {
12262:             $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);
12263:         }
12264:         if ($error) {
12265:             &Apache::lonnet::logthis($error);
12266:             $errors .= '<li><span class="LC_error">'.$error.'</span></li>';
12267:         }
12268:     }
12269:     if (ref($domconfig{'scantron'}) eq 'HASH') {
12270:         if ($domconfig{'scantron'}{'scantronformat'} ne '') {
12271:             if ($env{'form.scantronformat_del'}) {
12272:                 $confhash{'scantron'}{'scantronformat'} = '';
12273:                 $changes{'scantronformat'} = 1;
12274:             }
12275:         }
12276:     }
12277:     if (keys(%confhash) > 0) {
12278:         my $putresult = &Apache::lonnet::put_dom('configuration',\%confhash,
12279:                                                  $dom);
12280:         if ($putresult eq 'ok') {
12281:             if (keys(%changes) > 0) {
12282:                 if (ref($confhash{'scantron'}) eq 'HASH') {
12283:                     $resulttext = &mt('Changes made:').'<ul>';
12284:                     if ($confhash{'scantron'}{'scantronformat'} eq '') {
12285:                         $resulttext .= '<li>'.&mt('[_1] bubblesheet format file removed; [_2] file will be used for courses in this domain.',$custom,$default).'</li>';
12286:                     } else {
12287:                         $resulttext .= '<li>'.&mt('Custom bubblesheet format file ([_1]) uploaded for use with courses in this domain.',$custom).'</li>';
12288:                     }
12289:                     $resulttext .= '</ul>';
12290:                 } else {
12291:                     $resulttext = &mt('Changes made to bubblesheet format file.');
12292:                 }
12293:                 $resulttext .= '</ul>';
12294:                 &Apache::loncommon::devalidate_domconfig_cache($dom);
12295:                 if (ref($lastactref) eq 'HASH') {
12296:                     $lastactref->{'domainconfig'} = 1;
12297:                 }
12298:             } else {
12299:                 $resulttext = &mt('No changes made to bubblesheet format file');
12300:             }
12301:         } else {
12302:             $resulttext = '<span class="LC_error">'.
12303:                 &mt('An error occurred: [_1]',$putresult).'</span>';
12304:         }
12305:     } else {
12306:         $resulttext = &mt('No changes made to bubblesheet format file'); 
12307:     }
12308:     if ($errors) {
12309:         $resulttext .= &mt('The following errors occurred: ').'<ul>'.
12310:                        $errors.'</ul>';
12311:     }
12312:     return $resulttext;
12313: }
12314: 
12315: sub modify_coursecategories {
12316:     my ($dom,$lastactref,%domconfig) = @_;
12317:     my ($resulttext,%deletions,%reorderings,%needreordering,%adds,%changes,$errors,
12318:         $cathash);
12319:     my @deletecategory = &Apache::loncommon::get_env_multiple('form.deletecategory');
12320:     my @catitems = ('unauth','auth');
12321:     my @cattypes = ('std','domonly','codesrch','none');
12322:     if (ref($domconfig{'coursecategories'}) eq 'HASH') {
12323:         $cathash = $domconfig{'coursecategories'}{'cats'};
12324:         if ($domconfig{'coursecategories'}{'togglecats'} ne $env{'form.togglecats'}) {
12325:             $changes{'togglecats'} = 1;
12326:             $domconfig{'coursecategories'}{'togglecats'} = $env{'form.togglecats'};
12327:         }
12328:         if ($domconfig{'coursecategories'}{'categorize'} ne $env{'form.categorize'}) {
12329:             $changes{'categorize'} = 1;
12330:             $domconfig{'coursecategories'}{'categorize'} = $env{'form.categorize'};
12331:         }
12332:         if ($domconfig{'coursecategories'}{'togglecatscomm'} ne $env{'form.togglecatscomm'}) {
12333:             $changes{'togglecatscomm'} = 1;
12334:             $domconfig{'coursecategories'}{'togglecatscomm'} = $env{'form.togglecatscomm'};
12335:         }
12336:         if ($domconfig{'coursecategories'}{'categorizecomm'} ne $env{'form.categorizecomm'}) {
12337:             $changes{'categorizecomm'} = 1;
12338:             $domconfig{'coursecategories'}{'categorizecomm'} = $env{'form.categorizecomm'};
12339: 
12340:         }
12341:         if ($domconfig{'coursecategories'}{'togglecatsplace'} ne $env{'form.togglecatsplace'}) {
12342:             $changes{'togglecatsplace'} = 1;
12343:             $domconfig{'coursecategories'}{'togglecatsplace'} = $env{'form.togglecatsplace'};
12344:         }
12345:         if ($domconfig{'coursecategories'}{'categorizeplace'} ne $env{'form.categorizeplace'}) {
12346:             $changes{'categorizeplace'} = 1;
12347:             $domconfig{'coursecategories'}{'categorizeplace'} = $env{'form.categorizeplace'};
12348:         }
12349:         foreach my $item (@catitems) {
12350:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
12351:                 if ($domconfig{'coursecategories'}{$item} ne $env{'form.coursecat_'.$item}) {
12352:                     $changes{$item} = 1;
12353:                     $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
12354:                 }
12355:             }
12356:         }
12357:     } else {
12358:         $changes{'togglecats'} = 1;
12359:         $changes{'categorize'} = 1;
12360:         $changes{'togglecatscomm'} = 1;
12361:         $changes{'categorizecomm'} = 1;
12362:         $changes{'togglecatsplace'} = 1;
12363:         $changes{'categorizeplace'} = 1;
12364:         $domconfig{'coursecategories'} = {
12365:                                              togglecats => $env{'form.togglecats'},
12366:                                              categorize => $env{'form.categorize'},
12367:                                              togglecatscomm => $env{'form.togglecatscomm'},
12368:                                              categorizecomm => $env{'form.categorizecomm'},
12369:                                              togglecatsplace => $env{'form.togglecatsplace'},
12370:                                              categorizeplace => $env{'form.categorizeplace'},
12371:                                          };
12372:         foreach my $item (@catitems) {
12373:             if ($env{'form.coursecat_'.$item} ne 'std') {
12374:                 $changes{$item} = 1;
12375:             }
12376:             if (grep(/^\Q$env{'form.coursecat_'.$item}\E$/,@cattypes)) {
12377:                 $domconfig{'coursecategories'}{$item} = $env{'form.coursecat_'.$item};
12378:             }
12379:         }
12380:     }
12381:     if (ref($cathash) eq 'HASH') {
12382:         if (($domconfig{'coursecategories'}{'cats'}{'instcode::0'} ne '')  && ($env{'form.instcode'} == 0)) {
12383:             push (@deletecategory,'instcode::0');
12384:         }
12385:         if (($domconfig{'coursecategories'}{'cats'}{'communities::0'} ne '')  && ($env{'form.communities'} == 0)) {
12386:             push(@deletecategory,'communities::0');
12387:         }
12388:         if (($domconfig{'coursecategories'}{'cats'}{'placement::0'} ne '')  && ($env{'form.placement'} == 0)) {
12389:             push(@deletecategory,'placement::0');
12390:         }
12391:     }
12392:     my (@predelcats,@predeltrails,%predelallitems,%sort_by_deltrail);
12393:     if (ref($cathash) eq 'HASH') {
12394:         if (@deletecategory > 0) {
12395:             #FIXME Need to remove category from all courses using a deleted category 
12396:             &Apache::loncommon::extract_categories($cathash,\@predelcats,\@predeltrails,\%predelallitems);
12397:             foreach my $item (@deletecategory) {
12398:                 if ($domconfig{'coursecategories'}{'cats'}{$item} ne '') {
12399:                     delete($domconfig{'coursecategories'}{'cats'}{$item});
12400:                     $deletions{$item} = 1;
12401:                     &recurse_cat_deletes($item,$cathash,\%deletions);
12402:                 }
12403:             }
12404:         }
12405:         foreach my $item (keys(%{$cathash})) {
12406:             my ($cat,$container,$depth) = map { &unescape($_); } split(/:/,$item);
12407:             if ($cathash->{$item} ne $env{'form.'.$item}) {
12408:                 $reorderings{$item} = 1;
12409:                 $domconfig{'coursecategories'}{'cats'}{$item} = $env{'form.'.$item};
12410:             }
12411:             if ($env{'form.addcategory_name_'.$item} ne '') {
12412:                 my $newcat = $env{'form.addcategory_name_'.$item};
12413:                 my $newdepth = $depth+1;
12414:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
12415:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos_'.$item};
12416:                 $adds{$newitem} = 1; 
12417:             }
12418:             if ($env{'form.subcat_'.$item} ne '') {
12419:                 my $newcat = $env{'form.subcat_'.$item};
12420:                 my $newdepth = $depth+1;
12421:                 my $newitem = &escape($newcat).':'.&escape($cat).':'.$newdepth;
12422:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = 0;
12423:                 $adds{$newitem} = 1;
12424:             }
12425:         }
12426:     }
12427:     if ($env{'form.instcode'} eq '1') {
12428:         if (ref($cathash) eq 'HASH') {
12429:             my $newitem = 'instcode::0';
12430:             if ($cathash->{$newitem} eq '') {  
12431:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
12432:                 $adds{$newitem} = 1;
12433:             }
12434:         } else {
12435:             my $newitem = 'instcode::0';
12436:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.instcode_pos'};
12437:             $adds{$newitem} = 1;
12438:         }
12439:     }
12440:     if ($env{'form.communities'} eq '1') {
12441:         if (ref($cathash) eq 'HASH') {
12442:             my $newitem = 'communities::0';
12443:             if ($cathash->{$newitem} eq '') {
12444:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
12445:                 $adds{$newitem} = 1;
12446:             }
12447:         } else {
12448:             my $newitem = 'communities::0';
12449:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.communities_pos'};
12450:             $adds{$newitem} = 1;
12451:         }
12452:     }
12453:     if ($env{'form.placement'} eq '1') {
12454:         if (ref($cathash) eq 'HASH') {
12455:             my $newitem = 'placement::0';
12456:             if ($cathash->{$newitem} eq '') {
12457:                 $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
12458:                 $adds{$newitem} = 1;
12459:             }
12460:         } else {
12461:             my $newitem = 'placement::0';
12462:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.placement_pos'};
12463:             $adds{$newitem} = 1;
12464:         }
12465:     }
12466:     if ($env{'form.addcategory_name'} ne '') {
12467:         if (($env{'form.addcategory_name'} ne 'instcode') &&
12468:             ($env{'form.addcategory_name'} ne 'communities') &&
12469:             ($env{'form.addcategory_name'} ne 'placement')) {
12470:             my $newitem = &escape($env{'form.addcategory_name'}).'::0';
12471:             $domconfig{'coursecategories'}{'cats'}{$newitem} = $env{'form.addcategory_pos'};
12472:             $adds{$newitem} = 1;
12473:         }
12474:     }
12475:     my $putresult;
12476:     if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12477:         if (keys(%deletions) > 0) {
12478:             foreach my $key (keys(%deletions)) {
12479:                 if ($predelallitems{$key} ne '') {
12480:                     $sort_by_deltrail{$predelallitems{$key}} = $predeltrails[$predelallitems{$key}];
12481:                 }
12482:             }
12483:         }
12484:         my (@chkcats,@chktrails,%chkallitems);
12485:         &Apache::loncommon::extract_categories($domconfig{'coursecategories'}{'cats'},\@chkcats,\@chktrails,\%chkallitems);
12486:         if (ref($chkcats[0]) eq 'ARRAY') {
12487:             my $depth = 0;
12488:             my $chg = 0;
12489:             for (my $i=0; $i<@{$chkcats[0]}; $i++) {
12490:                 my $name = $chkcats[0][$i];
12491:                 my $item;
12492:                 if ($name eq '') {
12493:                     $chg ++;
12494:                 } else {
12495:                     $item = &escape($name).'::0';
12496:                     if ($chg) {
12497:                         $domconfig{'coursecategories'}{'cats'}{$item} -= $chg;
12498:                     }
12499:                     $depth ++; 
12500:                     &recurse_check(\@chkcats,$domconfig{'coursecategories'}{'cats'},$depth,$name);
12501:                     $depth --;
12502:                 }
12503:             }
12504:         }
12505:     }
12506:     if ((keys(%changes) > 0) || (keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12507:         $putresult = &Apache::lonnet::put_dom('configuration',\%domconfig,$dom);
12508:         if ($putresult eq 'ok') {
12509:             my %title = (
12510:                          togglecats     => 'Show/Hide a course in catalog',
12511:                          categorize     => 'Assign a category to a course',
12512:                          togglecatscomm => 'Show/Hide a community in catalog',
12513:                          categorizecomm => 'Assign a category to a community',
12514:                         );
12515:             my %level = (
12516:                          dom  => 'set in Domain ("Modify Course/Community")',
12517:                          crs  => 'set in Course ("Course Configuration")',
12518:                          comm => 'set in Community ("Community Configuration")',
12519:                          none     => 'No catalog',
12520:                          std      => 'Standard catalog',
12521:                          domonly  => 'Domain-only catalog',
12522:                          codesrch => 'Code search form',
12523:                         );
12524:             $resulttext = &mt('Changes made:').'<ul>';
12525:             if ($changes{'togglecats'}) {
12526:                 $resulttext .= '<li>'.&mt("$title{'togglecats'} $level{$env{'form.togglecats'}}").'</li>'; 
12527:             }
12528:             if ($changes{'categorize'}) {
12529:                 $resulttext .= '<li>'.&mt("$title{'categorize'} $level{$env{'form.categorize'}}").'</li>';
12530:             }
12531:             if ($changes{'togglecatscomm'}) {
12532:                 $resulttext .= '<li>'.&mt("$title{'togglecatscomm'} $level{$env{'form.togglecatscomm'}}").'</li>';
12533:             }
12534:             if ($changes{'categorizecomm'}) {
12535:                 $resulttext .= '<li>'.&mt("$title{'categorizecomm'} $level{$env{'form.categorizecomm'}}").'</li>';
12536:             }
12537:             if ($changes{'unauth'}) {
12538:                 $resulttext .= '<li>'.&mt('Catalog type for unauthenticated users set to "'.$level{$env{'form.coursecat_unauth'}}.'"').'</li>';
12539:             }
12540:             if ($changes{'auth'}) {
12541:                 $resulttext .= '<li>'.&mt('Catalog type for authenticated users set to "'.$level{$env{'form.coursecat_auth'}}.'"').'</li>';
12542:             }
12543:             if ((keys(%deletions) > 0) || (keys(%reorderings) > 0) || (keys(%adds) > 0)) {
12544:                 my $cathash;
12545:                 if (ref($domconfig{'coursecategories'}) eq 'HASH') {
12546:                     $cathash = $domconfig{'coursecategories'}{'cats'};
12547:                 } else {
12548:                     $cathash = {};
12549:                 } 
12550:                 my (@cats,@trails,%allitems);
12551:                     &Apache::loncommon::extract_categories($cathash,\@cats,\@trails,\%allitems);
12552:                 if (keys(%deletions) > 0) {
12553:                     $resulttext .= '<li>'.&mt('Deleted categories:').'<ul>';
12554:                     foreach my $predeltrail (sort {$a <=> $b } (keys(%sort_by_deltrail))) { 
12555:                         $resulttext .= '<li>'.$predeltrails[$predeltrail].'</li>';
12556:                     }
12557:                     $resulttext .= '</ul></li>';
12558:                 }
12559:                 if (keys(%reorderings) > 0) {
12560:                     my %sort_by_trail;
12561:                     $resulttext .= '<li>'.&mt('Reordered categories:').'<ul>';
12562:                     foreach my $key (keys(%reorderings)) {
12563:                         if ($allitems{$key} ne '') {
12564:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
12565:                         }
12566:                     }
12567:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
12568:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
12569:                     }
12570:                     $resulttext .= '</ul></li>';
12571:                 }
12572:                 if (keys(%adds) > 0) {
12573:                     my %sort_by_trail;
12574:                     $resulttext .= '<li>'.&mt('Added categories:').'<ul>';
12575:                     foreach my $key (keys(%adds)) {
12576:                         if ($allitems{$key} ne '') {
12577:                             $sort_by_trail{$allitems{$key}} = $trails[$allitems{$key}];
12578:                         }
12579:                     }
12580:                     foreach my $trail (sort {$a <=> $b } (keys(%sort_by_trail))) {
12581:                         $resulttext .= '<li>'.$trails[$trail].'</li>';
12582:                     }
12583:                     $resulttext .= '</ul></li>';
12584:                 }
12585:             }
12586:             $resulttext .= '</ul>';
12587:             if ($changes{'unauth'} || $changes{'auth'}) {
12588:                 my %domdefaults = &Apache::lonnet::get_domain_defaults($dom);
12589:                 if ($changes{'auth'}) {
12590:                     $domdefaults{'catauth'} = $domconfig{'coursecategories'}{'auth'};
12591:                 }
12592:                 if ($changes{'unauth'}) {
12593:                     $domdefaults{'catunauth'} = $domconfig{'coursecategories'}{'unauth'};
12594:                 }
12595:                 my $cachetime = 24*60*60;
12596:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12597:                 if (ref($lastactref) eq 'HASH') {
12598:                     $lastactref->{'domdefaults'} = 1;
12599:                 }
12600:             }
12601:         } else {
12602:             $resulttext = '<span class="LC_error">'.
12603:                           &mt('An error occurred: [_1]',$putresult).'</span>';
12604:         }
12605:     } else {
12606:         $resulttext = &mt('No changes made to course and community categories');
12607:     }
12608:     return $resulttext;
12609: }
12610: 
12611: sub modify_serverstatuses {
12612:     my ($dom,%domconfig) = @_;
12613:     my ($resulttext,%changes,%currserverstatus,%newserverstatus);
12614:     if (ref($domconfig{'serverstatuses'}) eq 'HASH') {
12615:         %currserverstatus = %{$domconfig{'serverstatuses'}};
12616:     }
12617:     my @pages = &serverstatus_pages();
12618:     foreach my $type (@pages) {
12619:         $newserverstatus{$type}{'namedusers'} = '';
12620:         $newserverstatus{$type}{'machines'} = '';
12621:         if (defined($env{'form.'.$type.'_namedusers'})) {
12622:             my @users = split(/,/,$env{'form.'.$type.'_namedusers'});
12623:             my @okusers;
12624:             foreach my $user (@users) {
12625:                 my ($uname,$udom) = split(/:/,$user);
12626:                 if (($udom =~ /^$match_domain$/) &&   
12627:                     (&Apache::lonnet::domain($udom)) &&
12628:                     ($uname =~ /^$match_username$/)) {
12629:                     if (!grep(/^\Q$user\E/,@okusers)) {
12630:                         push(@okusers,$user);
12631:                     }
12632:                 }
12633:             }
12634:             if (@okusers > 0) {
12635:                  @okusers = sort(@okusers);
12636:                  $newserverstatus{$type}{'namedusers'} = join(',',@okusers);
12637:             }
12638:         }
12639:         if (defined($env{'form.'.$type.'_machines'})) {
12640:             my @machines = split(/,/,$env{'form.'.$type.'_machines'});
12641:             my @okmachines;
12642:             foreach my $ip (@machines) {
12643:                 my @parts = split(/\./,$ip);
12644:                 next if (@parts < 4);
12645:                 my $badip = 0;
12646:                 for (my $i=0; $i<4; $i++) {
12647:                     if (!(($parts[$i] >= 0) && ($parts[$i] <= 255))) {
12648:                         $badip = 1;
12649:                         last;
12650:                     }
12651:                 }
12652:                 if (!$badip) {
12653:                     push(@okmachines,$ip);     
12654:                 }
12655:             }
12656:             @okmachines = sort(@okmachines);
12657:             $newserverstatus{$type}{'machines'} = join(',',@okmachines);
12658:         }
12659:     }
12660:     my %serverstatushash =  (
12661:                                 serverstatuses => \%newserverstatus,
12662:                             );
12663:     foreach my $type (@pages) {
12664:         foreach my $setting ('namedusers','machines') {
12665:             my (@current,@new);
12666:             if (ref($currserverstatus{$type}) eq 'HASH') {
12667:                 if ($currserverstatus{$type}{$setting} ne '') { 
12668:                     @current = split(/,/,$currserverstatus{$type}{$setting});
12669:                 }
12670:             }
12671:             if ($newserverstatus{$type}{$setting} ne '') {
12672:                 @new = split(/,/,$newserverstatus{$type}{$setting});
12673:             }
12674:             if (@current > 0) {
12675:                 if (@new > 0) {
12676:                     foreach my $item (@current) {
12677:                         if (!grep(/^\Q$item\E$/,@new)) {
12678:                             $changes{$type}{$setting} = 1;
12679:                             last;
12680:                         }
12681:                     }
12682:                     foreach my $item (@new) {
12683:                         if (!grep(/^\Q$item\E$/,@current)) {
12684:                             $changes{$type}{$setting} = 1;
12685:                             last;
12686:                         }
12687:                     }
12688:                 } else {
12689:                     $changes{$type}{$setting} = 1;
12690:                 }
12691:             } elsif (@new > 0) {
12692:                 $changes{$type}{$setting} = 1;
12693:             }
12694:         }
12695:     }
12696:     if (keys(%changes) > 0) {
12697:         my $titles= &LONCAPA::lonauthcgi::serverstatus_titles();
12698:         my $putresult = &Apache::lonnet::put_dom('configuration',
12699:                                                  \%serverstatushash,$dom);
12700:         if ($putresult eq 'ok') {
12701:             $resulttext .= &mt('Changes made:').'<ul>';
12702:             foreach my $type (@pages) {
12703:                 if (ref($changes{$type}) eq 'HASH') {
12704:                     $resulttext .= '<li>'.$titles->{$type}.'<ul>';
12705:                     if ($changes{$type}{'namedusers'}) {
12706:                         if ($newserverstatus{$type}{'namedusers'} eq '') {
12707:                             $resulttext .= '<li>'.&mt("Access terminated for all specific (named) users").'</li>'."\n";
12708:                         } else {
12709:                             $resulttext .= '<li>'.&mt("Access available for the following specified users: ").$newserverstatus{$type}{'namedusers'}.'</li>'."\n";
12710:                         }
12711:                     }
12712:                     if ($changes{$type}{'machines'}) {
12713:                         if ($newserverstatus{$type}{'machines'} eq '') {
12714:                             $resulttext .= '<li>'.&mt("Access terminated for all specific IP addresses").'</li>'."\n";
12715:                         } else {
12716:                             $resulttext .= '<li>'.&mt("Access available for the following specified IP addresses: ").$newserverstatus{$type}{'machines'}.'</li>'."\n";
12717:                         }
12718: 
12719:                     }
12720:                     $resulttext .= '</ul></li>';
12721:                 }
12722:             }
12723:             $resulttext .= '</ul>';
12724:         } else {
12725:             $resulttext = '<span class="LC_error">'.
12726:                           &mt('An error occurred saving access settings for server status pages: [_1].',$putresult).'</span>';
12727: 
12728:         }
12729:     } else {
12730:         $resulttext = &mt('No changes made to access to server status pages');
12731:     }
12732:     return $resulttext;
12733: }
12734: 
12735: sub modify_helpsettings {
12736:     my ($r,$dom,$confname,$lastactref,%domconfig) = @_;
12737:     my ($resulttext,$errors,%changes,%helphash);
12738:     my %defaultchecked = ('submitbugs' => 'on');
12739:     my @offon = ('off','on');
12740:     my @toggles = ('submitbugs');
12741:     my %current = ('submitbugs' => '',
12742:                    'adhoc'      => {},
12743:                   );
12744:     if (ref($domconfig{'helpsettings'}) eq 'HASH') {
12745:         %current = %{$domconfig{'helpsettings'}};
12746:     }
12747:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
12748:     foreach my $item (@toggles) {
12749:         if ($defaultchecked{$item} eq 'on') { 
12750:             if ($current{$item} eq '') {
12751:                 if ($env{'form.'.$item} eq '0') {
12752:                     $changes{$item} = 1;
12753:                 }
12754:             } elsif ($current{$item} ne $env{'form.'.$item}) {
12755:                 $changes{$item} = 1;
12756:             }
12757:         } elsif ($defaultchecked{$item} eq 'off') {
12758:             if ($current{$item} eq '') {
12759:                 if ($env{'form.'.$item} eq '1') {
12760:                     $changes{$item} = 1;
12761:                 }
12762:             } elsif ($current{$item} ne $env{'form.'.$item}) {
12763:                 $changes{$item} = 1;
12764:             }
12765:         }
12766:         if (($env{'form.'.$item} eq '0') || ($env{'form.'.$item} eq '1')) {
12767:             $helphash{'helpsettings'}{$item} = $env{'form.'.$item};
12768:         }
12769:     }
12770:     my $maxnum = $env{'form.helproles_maxnum'};
12771:     my $confname = $dom.'-domainconfig';
12772:     my %existing=&Apache::lonnet::dump('roles',$dom,$confname,'rolesdef_');
12773:     my (@allpos,%newsettings,%changedprivs,$newrole);
12774:     my ($othertitle,$usertypes,$types) = &Apache::loncommon::sorted_inst_types($dom);
12775:     my @accesstypes = ('all','dh','da','none','status','inc','exc');
12776:     my %domhelpdesk = &Apache::lonnet::get_active_domroles($dom,['dh','da']);
12777:     my %lt = &Apache::lonlocal::texthash(
12778:                     s      => 'system',
12779:                     d      => 'domain',
12780:                     order  => 'Display order',
12781:                     access => 'Role usage',
12782:                     all    => 'All with domain helpdesk or helpdesk assistant role',
12783:                     dh     => 'All with domain helpdesk role',
12784:                     da     => 'All with domain helpdesk assistant role',
12785:                     none   => 'None',
12786:                     status => 'Determined based on institutional status',
12787:                     inc    => 'Include all, but exclude specific personnel',
12788:                     exc    => 'Exclude all, but include specific personnel',
12789:     );
12790:     for (my $num=0; $num<=$maxnum; $num++) {
12791:         my ($prefix,$identifier,$rolename,%curr);
12792:         if ($num == $maxnum) {
12793:             next unless ($env{'form.newcusthelp'} == $maxnum);
12794:             $identifier = 'custhelp'.$num;
12795:             $prefix = 'helproles_'.$num;
12796:             $rolename = $env{'form.custhelpname'.$num};
12797:             $rolename=~s/[^A-Za-z0-9]//gs;
12798:             next if ($rolename eq '');
12799:             next if (exists($existing{'rolesdef_'.$rolename}));
12800:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
12801:             my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
12802:                                                      $newprivs{'c'},$confname,$dom);
12803:             if ($result ne 'ok') {
12804:                 $errors .= '<li><span class="LC_error">'.
12805:                            &mt('An error occurred storing the new custom role: [_1]',
12806:                            $result).'</span></li>';
12807:                 next;
12808:             } else {
12809:                 $changedprivs{$rolename} = \%newprivs;
12810:                 $newrole = $rolename;
12811:             }
12812:         } else {
12813:             $prefix = 'helproles_'.$num;
12814:             $rolename = $env{'form.'.$prefix};
12815:             next if ($rolename eq '');
12816:             next unless (exists($existing{'rolesdef_'.$rolename}));
12817:             $identifier = 'custhelp'.$num;
12818:             my %newprivs = &Apache::lonuserutils::custom_role_update($rolename,$identifier);
12819:             my %currprivs;
12820:             ($currprivs{'s'},$currprivs{'d'},$currprivs{'c'}) =
12821:                 split(/\_/,$existing{'rolesdef_'.$rolename});
12822:             foreach my $level ('c','d','s') {
12823:                 if ($newprivs{$level} ne $currprivs{$level}) {
12824:                     my $result = &Apache::lonnet::definerole($rolename,$newprivs{'s'},$newprivs{'d'},
12825:                                                              $newprivs{'c'},$confname,$dom);
12826:                     if ($result ne 'ok') {
12827:                         $errors .= '<li><span class="LC_error">'.
12828:                                    &mt('An error occurred storing privileges for existing role [_1]: [_2]',
12829:                                        $rolename,$result).'</span></li>';
12830:                     } else {
12831:                         $changedprivs{$rolename} = \%newprivs;
12832:                     }
12833:                     last;
12834:                 }
12835:             }
12836:             if (ref($current{'adhoc'}) eq 'HASH') {
12837:                 if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
12838:                     %curr = %{$current{'adhoc'}{$rolename}};
12839:                 }
12840:             }
12841:         }
12842:         my $newpos = $env{'form.'.$prefix.'_pos'};
12843:         $newpos =~ s/\D+//g;
12844:         $allpos[$newpos] = $rolename;
12845:         my $newdesc = $env{'form.'.$prefix.'_desc'};
12846:         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'desc'} = $newdesc;
12847:         if ($curr{'desc'}) {
12848:             if ($curr{'desc'} ne $newdesc) {
12849:                 $changes{'customrole'}{$rolename}{'desc'} = 1;
12850:                 $newsettings{$rolename}{'desc'} = $newdesc;
12851:             }
12852:         } elsif ($newdesc ne '') {
12853:             $changes{'customrole'}{$rolename}{'desc'} = 1;
12854:             $newsettings{$rolename}{'desc'} = $newdesc;
12855:         }
12856:         my $access = $env{'form.'.$prefix.'_access'};
12857:         if (grep(/^\Q$access\E$/,@accesstypes)) {
12858:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = $access;
12859:             if ($access eq 'status') {
12860:                 my @statuses = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_status');
12861:                 if (scalar(@statuses) == 0) {
12862:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'none';
12863:                 } else {
12864:                     my (@shownstatus,$numtypes);
12865:                     $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
12866:                     if (ref($types) eq 'ARRAY') {
12867:                         $numtypes = scalar(@{$types});
12868:                         foreach my $type (sort(@statuses)) {
12869:                             if ($type eq 'default') {
12870:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
12871:                             } elsif (grep(/^\Q$type\E$/,@{$types})) {
12872:                                 push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$type);
12873:                                 push(@shownstatus,$usertypes->{$type});
12874:                             }
12875:                         }
12876:                     }
12877:                     if (grep(/^default$/,@statuses)) {
12878:                         push(@shownstatus,$othertitle);
12879:                     }
12880:                     if (scalar(@shownstatus) == 1+$numtypes) {
12881:                         $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'} = 'all';
12882:                         delete($helphash{'helpsettings'}{'adhoc'}{$rolename}{'status'});
12883:                     } else {
12884:                         $newsettings{$rolename}{'status'} = join(' '.&mt('or').' ',@shownstatus);
12885:                         if (ref($curr{'status'}) eq 'ARRAY') {
12886:                             my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
12887:                             if (@diffs) {
12888:                                 $changes{'customrole'}{$rolename}{$access} = 1;
12889:                             }
12890:                         } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12891:                             $changes{'customrole'}{$rolename}{$access} = 1;
12892:                         }
12893:                     }
12894:                 }
12895:             } elsif (($access eq 'inc') || ($access eq 'exc')) {
12896:                 my @personnel = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_staff_'.$access);
12897:                 my @newspecstaff;
12898:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{$access} = [];
12899:                 foreach my $person (sort(@personnel)) {
12900:                     if ($domhelpdesk{$person}) {
12901:                         push(@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}},$person);
12902:                     }
12903:                 }
12904:                 if (ref($curr{$access}) eq 'ARRAY') {
12905:                     my @diffs = &Apache::loncommon::compare_arrays($helphash{'helpsettings'}{'adhoc'}{$rolename}{$access},$curr{$access});
12906:                     if (@diffs) {
12907:                         $changes{'customrole'}{$rolename}{$access} = 1;
12908:                     }
12909:                 } elsif (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12910:                     $changes{'customrole'}{$rolename}{$access} = 1;
12911:                 }
12912:                 foreach my $person (@{$helphash{'helpsettings'}{'adhoc'}{$rolename}{$access}}) {
12913:                     my ($uname,$udom) = split(/:/,$person);
12914:                         push(@newspecstaff,&Apache::loncommon::aboutmewrapper(&Apache::loncommon::plainname($uname,$udom,'lastname'),$uname,$udom));
12915:                 }
12916:                 $newsettings{$rolename}{$access} = join(', ',sort(@newspecstaff));
12917:             }
12918:         } else {
12919:             $helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}= 'all';
12920:         }
12921:         unless ($curr{'access'} eq $access) {
12922:             $changes{'customrole'}{$rolename}{'access'} = 1;
12923:             $newsettings{$rolename}{'access'} = $lt{$helphash{'helpsettings'}{'adhoc'}{$rolename}{'access'}};
12924:         }
12925:     }
12926:     if (@allpos > 0) {
12927:         my $idx = 0;
12928:         foreach my $rolename (@allpos) {
12929:             if ($rolename ne '') {
12930:                 $helphash{'helpsettings'}{'adhoc'}{$rolename}{'order'} = $idx;
12931:                 if (ref($current{'adhoc'}) eq 'HASH') {
12932:                     if (ref($current{'adhoc'}{$rolename}) eq 'HASH') {
12933:                         if ($current{'adhoc'}{$rolename}{'order'} ne $idx) {
12934:                             $changes{'customrole'}{$rolename}{'order'} = 1;
12935:                             $newsettings{$rolename}{'order'} = $idx+1;
12936:                         }
12937:                     }
12938:                 }
12939:                 $idx ++;
12940:             }
12941:         }
12942:     }
12943:     my $putresult;
12944:     if (keys(%changes) > 0) {
12945:         $putresult = &Apache::lonnet::put_dom('configuration',\%helphash,$dom);
12946:         if ($putresult eq 'ok') {
12947:             if (ref($helphash{'helpsettings'}) eq 'HASH') {
12948:                 $domdefaults{'submitbugs'} = $helphash{'helpsettings'}{'submitbugs'};
12949:                 if (ref($helphash{'helpsettings'}{'adhoc'}) eq 'HASH') {
12950:                     $domdefaults{'adhocroles'} = $helphash{'helpsettings'}{'adhoc'};
12951:                 }
12952:             }
12953:             my $cachetime = 24*60*60;
12954:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
12955:             if (ref($lastactref) eq 'HASH') {
12956:                 $lastactref->{'domdefaults'} = 1;
12957:             }
12958:         } else {
12959:             $errors .= '<li><span class="LC_error">'.
12960:                        &mt('An error occurred storing the settings: [_1]',
12961:                            $putresult).'</span></li>';
12962:         }
12963:     }
12964:     if ((keys(%changes) && ($putresult eq 'ok')) || (keys(%changedprivs))) {
12965:         $resulttext = &mt('Changes made:').'<ul>';
12966:         my (%shownprivs,@levelorder);
12967:         @levelorder = ('c','d','s');
12968:         if ((keys(%changes)) && ($putresult eq 'ok')) {
12969:             foreach my $item (sort(keys(%changes))) {
12970:                 if ($item eq 'submitbugs') {
12971:                     $resulttext .= '<li>'.&mt('Display link to: [_1] set to "'.$offon[$env{'form.'.$item}].'".',
12972:                                               &Apache::loncommon::modal_link('http://bugs.loncapa.org',
12973:                                               &mt('LON-CAPA bug tracker'),600,500)).'</li>';
12974:                 } elsif ($item eq 'customrole') {
12975:                     if (ref($changes{'customrole'}) eq 'HASH') {
12976:                         my @keyorder = ('order','desc','access','status','exc','inc');
12977:                         my %keytext = &Apache::lonlocal::texthash(
12978:                                                                    order  => 'Order',
12979:                                                                    desc   => 'Role description',
12980:                                                                    access => 'Role usage',
12981:                                                                    status => 'Allowed instituional types',
12982:                                                                    exc    => 'Allowed personnel',
12983:                                                                    inc    => 'Disallowed personnel',
12984:                         );
12985:                         foreach my $role (sort(keys(%{$changes{'customrole'}}))) {
12986:                             if (ref($changes{'customrole'}{$role}) eq 'HASH') {
12987:                                 if ($role eq $newrole) {
12988:                                     $resulttext .= '<li>'.&mt('New custom role added: [_1]',
12989:                                                               $role).'<ul>';
12990:                                 } else {
12991:                                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
12992:                                                               $role).'<ul>';
12993:                                 }
12994:                                 foreach my $key (@keyorder) {
12995:                                     if ($changes{'customrole'}{$role}{$key}) {
12996:                                         $resulttext .= '<li>'.&mt("[_1] set to: [_2]",
12997:                                                                   $keytext{$key},$newsettings{$role}{$key}).
12998:                                                        '</li>';
12999:                                     }
13000:                                 }
13001:                                 if (ref($changedprivs{$role}) eq 'HASH') {
13002:                                     $shownprivs{$role} = 1;
13003:                                     $resulttext .= '<li>'.&mt('Privileges set to :').'<ul>';
13004:                                     foreach my $level (@levelorder) {
13005:                                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
13006:                                             next if ($item eq '');
13007:                                             my ($priv) = split(/\&/,$item,2);
13008:                                             if (&Apache::lonnet::plaintext($priv)) {
13009:                                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
13010:                                                 unless ($level eq 'c') {
13011:                                                     $resulttext .= ' ('.$lt{$level}.')';
13012:                                                 }
13013:                                                 $resulttext .= '</li>';
13014:                                             }
13015:                                         }
13016:                                     }
13017:                                     $resulttext .= '</ul>';
13018:                                 }
13019:                                 $resulttext .= '</ul></li>';
13020:                             }
13021:                         }
13022:                     }
13023:                 }
13024:             }
13025:         }
13026:         if (keys(%changedprivs)) {
13027:             foreach my $role (sort(keys(%changedprivs))) {
13028:                 unless ($shownprivs{$role}) {
13029:                     $resulttext .= '<li>'.&mt('Existing custom role modified: [_1]',
13030:                                               $role).'<ul>'.
13031:                                    '<li>'.&mt('Privileges set to :').'<ul>';
13032:                     foreach my $level (@levelorder) {
13033:                         foreach my $item (split(/\:/,$changedprivs{$role}{$level})) {
13034:                             next if ($item eq '');
13035:                             my ($priv) = split(/\&/,$item,2);
13036:                             if (&Apache::lonnet::plaintext($priv)) {
13037:                                 $resulttext .= '<li>'.&Apache::lonnet::plaintext($priv);
13038:                                 unless ($level eq 'c') {
13039:                                     $resulttext .= ' ('.$lt{$level}.')';
13040:                                 }
13041:                                 $resulttext .= '</li>';
13042:                             }
13043:                         }
13044:                     }
13045:                     $resulttext .= '</ul></li></ul></li>';
13046:                 }
13047:             }
13048:         }
13049:         $resulttext .= '</ul>';
13050:     } else {
13051:         $resulttext = &mt('No changes made to help settings');
13052:     }
13053:     if ($errors) {
13054:         $resulttext .= '<br />'.&mt('The following errors occurred: ').'<ul>'.
13055:                                     $errors.'</ul>';
13056:     }
13057:     return $resulttext;
13058: }
13059: 
13060: sub modify_coursedefaults {
13061:     my ($dom,$lastactref,%domconfig) = @_;
13062:     my ($resulttext,$errors,%changes,%defaultshash);
13063:     my %defaultchecked = (
13064:                            'canuse_pdfforms' => 'off',
13065:                            'uselcmath'       => 'on',
13066:                            'usejsme'         => 'on'
13067:                          );
13068:     my @toggles = ('canuse_pdfforms','uselcmath','usejsme');
13069:     my @numbers = ('anonsurvey_threshold','uploadquota_official','uploadquota_unofficial',
13070:                    'uploadquota_community','uploadquota_textbook','uploadquota_placement',
13071:                    'mysqltables_official','mysqltables_unofficial','mysqltables_community',
13072:                    'mysqltables_textbook','mysqltables_placement');
13073:     my @types = ('official','unofficial','community','textbook','placement');
13074:     my %staticdefaults = (
13075:                            anonsurvey_threshold => 10,
13076:                            uploadquota          => 500,
13077:                            postsubmit           => 60,
13078:                            mysqltables          => 172800,
13079:                          );
13080: 
13081:     $defaultshash{'coursedefaults'} = {};
13082: 
13083:     if (ref($domconfig{'coursedefaults'}) ne 'HASH') {
13084:         if ($domconfig{'coursedefaults'} eq '') {
13085:             $domconfig{'coursedefaults'} = {};
13086:         }
13087:     }
13088: 
13089:     if (ref($domconfig{'coursedefaults'}) eq 'HASH') {
13090:         foreach my $item (@toggles) {
13091:             if ($defaultchecked{$item} eq 'on') {
13092:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
13093:                     ($env{'form.'.$item} eq '0')) {
13094:                     $changes{$item} = 1;
13095:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
13096:                     $changes{$item} = 1;
13097:                 }
13098:             } elsif ($defaultchecked{$item} eq 'off') {
13099:                 if (($domconfig{'coursedefaults'}{$item} eq '') &&
13100:                     ($env{'form.'.$item} eq '1')) {
13101:                     $changes{$item} = 1;
13102:                 } elsif ($domconfig{'coursedefaults'}{$item} ne $env{'form.'.$item}) {
13103:                     $changes{$item} = 1;
13104:                 }
13105:             }
13106:             $defaultshash{'coursedefaults'}{$item} = $env{'form.'.$item};
13107:         }
13108:         foreach my $item (@numbers) {
13109:             my ($currdef,$newdef);
13110:             $newdef = $env{'form.'.$item};
13111:             if ($item eq 'anonsurvey_threshold') {
13112:                 $currdef = $domconfig{'coursedefaults'}{$item};
13113:                 $newdef =~ s/\D//g;
13114:                 if ($newdef eq '' || $newdef < 1) {
13115:                     $newdef = 1;
13116:                 }
13117:                 $defaultshash{'coursedefaults'}{$item} = $newdef;
13118:             } else {
13119:                 my ($setting,$type) = ($item =~ /^(uploadquota|mysqltables)_(\w+)$/);
13120:                 if (ref($domconfig{'coursedefaults'}{$setting}) eq 'HASH') {
13121:                     $currdef = $domconfig{'coursedefaults'}{$setting}{$type};
13122:                 }
13123:                 $newdef =~ s/[^\w.\-]//g;
13124:                 $defaultshash{'coursedefaults'}{$setting}{$type} = $newdef;
13125:             }
13126:             if ($currdef ne $newdef) {
13127:                 my $staticdef;
13128:                 if ($item eq 'anonsurvey_threshold') {
13129:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$item})) {
13130:                         $changes{$item} = 1;
13131:                     }
13132:                 } elsif ($item =~ /^(uploadquota|mysqltables)_/) {
13133:                     my $setting = $1;
13134:                     unless (($currdef eq '') && ($newdef == $staticdefaults{$setting})) {
13135:                         $changes{$setting} = 1;
13136:                     }
13137:                 }
13138:             }
13139:         }
13140:         my $currclone = $domconfig{'coursedefaults'}{'canclone'};
13141:         my @currclonecode;
13142:         if (ref($currclone) eq 'HASH') {
13143:             if (ref($currclone->{'instcode'}) eq 'ARRAY') {
13144:                 @currclonecode = @{$currclone->{'instcode'}};
13145:             }
13146:         }
13147:         my $newclone;
13148:         if ($env{'form.canclone'} =~ /^(none|domain|instcode)$/) {
13149:             $newclone = $env{'form.canclone'};
13150:         }
13151:         if ($newclone eq 'instcode') {
13152:             my @newcodes = &Apache::loncommon::get_env_multiple('form.clonecode');
13153:             my (%codedefaults,@code_order,@clonecode);
13154:             &Apache::lonnet::auto_instcode_defaults($dom,\%codedefaults,
13155:                                                     \@code_order);
13156:             foreach my $item (@code_order) {
13157:                 if (grep(/^\Q$item\E$/,@newcodes)) {
13158:                     push(@clonecode,$item);
13159:                 }
13160:             }
13161:             if (@clonecode) {
13162:                 $defaultshash{'coursedefaults'}{'canclone'} = { $newclone => \@clonecode };
13163:                 my @diffs = &Apache::loncommon::compare_arrays(\@currclonecode,\@clonecode);
13164:                 if (@diffs) {
13165:                     $changes{'canclone'} = 1;
13166:                 }
13167:             } else {
13168:                 $newclone eq '';
13169:             }
13170:         } elsif ($newclone ne '') {
13171:             $defaultshash{'coursedefaults'}{'canclone'} = $newclone;
13172:         }
13173:         if ($newclone ne $currclone) {
13174:             $changes{'canclone'} = 1;
13175:         }
13176:         my %credits;
13177:         foreach my $type (@types) {
13178:             unless ($type eq 'community') {
13179:                 $credits{$type} = $env{'form.'.$type.'_credits'};
13180:                 $credits{$type} =~ s/[^\d.]+//g;
13181:             }
13182:         }
13183:         if ((ref($domconfig{'coursedefaults'}{'coursecredits'}) ne 'HASH') &&
13184:             ($env{'form.coursecredits'} eq '1')) {
13185:             $changes{'coursecredits'} = 1;
13186:             foreach my $type (keys(%credits)) {
13187:                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
13188:             }
13189:         } else {
13190:             if ($env{'form.coursecredits'} eq '1') {
13191:                 foreach my $type (@types) {
13192:                     unless ($type eq 'community') {
13193:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type} ne $credits{$type}) {
13194:                             $changes{'coursecredits'} = 1;
13195:                         }
13196:                         $defaultshash{'coursedefaults'}{'coursecredits'}{$type} = $credits{$type};
13197:                     }
13198:                 }
13199:             } elsif (ref($domconfig{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13200:                 foreach my $type (@types) {
13201:                     unless ($type eq 'community') {
13202:                         if ($domconfig{'coursedefaults'}{'coursecredits'}{$type}) {
13203:                             $changes{'coursecredits'} = 1;
13204:                             last;
13205:                         }
13206:                     }
13207:                 }
13208:             }
13209:         }
13210:         if ($env{'form.postsubmit'} eq '1') {
13211:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'on';
13212:             my %currtimeout;
13213:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13214:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'off') {
13215:                     $changes{'postsubmit'} = 1;
13216:                 }
13217:                 if (ref($domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13218:                     %currtimeout = %{$domconfig{'coursedefaults'}{'postsubmit'}{'timeout'}};
13219:                 }
13220:             } else {
13221:                 $changes{'postsubmit'} = 1;
13222:             }
13223:             foreach my $type (@types) {
13224:                 my $timeout = $env{'form.'.$type.'_timeout'};
13225:                 $timeout =~ s/\D//g;
13226:                 if ($timeout == $staticdefaults{'postsubmit'}) {
13227:                     $timeout = '';
13228:                 } elsif (($timeout eq '') || ($timeout =~ /^0+$/)) {
13229:                     $timeout = '0';
13230:                 }
13231:                 unless ($timeout eq '') {
13232:                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type} = $timeout;
13233:                 }
13234:                 if (exists($currtimeout{$type})) {
13235:                     if ($timeout ne $currtimeout{$type}) {
13236:                         $changes{'postsubmit'} = 1;
13237:                     }
13238:                 } elsif ($timeout ne '') {
13239:                     $changes{'postsubmit'} = 1;
13240:                 }
13241:             }
13242:         } else {
13243:             $defaultshash{'coursedefaults'}{'postsubmit'}{'client'} = 'off';
13244:             if (ref($domconfig{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13245:                 if ($domconfig{'coursedefaults'}{'postsubmit'}{'client'} eq 'on') {
13246:                     $changes{'postsubmit'} = 1;
13247:                 }
13248:             } else {
13249:                 $changes{'postsubmit'} = 1;
13250:             }
13251:         }
13252:     }
13253:     my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13254:                                              $dom);
13255:     if ($putresult eq 'ok') {
13256:         if (keys(%changes) > 0) {
13257:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13258:             if (($changes{'canuse_pdfforms'}) || ($changes{'uploadquota'}) || ($changes{'postsubmit'}) ||
13259:                 ($changes{'coursecredits'}) || ($changes{'uselcmath'}) || ($changes{'usejsme'}) ||
13260:                 ($changes{'canclone'}) || ($changes{'mysqltables'})) {
13261:                 foreach my $item ('canuse_pdfforms','uselcmath','usejsme') { 
13262:                     if ($changes{$item}) {
13263:                         $domdefaults{$item}=$defaultshash{'coursedefaults'}{$item};
13264:                     }
13265:                 }
13266:                 if ($changes{'coursecredits'}) {
13267:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13268:                         foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'coursecredits'}})) {
13269:                             $domdefaults{$type.'credits'} =
13270:                                 $defaultshash{'coursedefaults'}{'coursecredits'}{$type};
13271:                         }
13272:                     }
13273:                 }
13274:                 if ($changes{'postsubmit'}) {
13275:                     if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13276:                         $domdefaults{'postsubmit'} = $defaultshash{'coursedefaults'}{'postsubmit'}{'client'};
13277:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13278:                             foreach my $type (keys(%{$defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}})) {
13279:                                 $domdefaults{$type.'postsubtimeout'} =
13280:                                     $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
13281:                             }
13282:                         }
13283:                     }
13284:                 }
13285:                 if ($changes{'uploadquota'}) {
13286:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
13287:                         foreach my $type (@types) {
13288:                             $domdefaults{$type.'quota'}=$defaultshash{'coursedefaults'}{'uploadquota'}{$type};
13289:                         }
13290:                     }
13291:                 }
13292:                 if ($changes{'canclone'}) {
13293:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
13294:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
13295:                             my @clonecodes = @{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}};
13296:                             if (@clonecodes) {
13297:                                 $domdefaults{'canclone'} = join('+',@clonecodes);
13298:                             }
13299:                         }
13300:                     } else {
13301:                         $domdefaults{'canclone'}=$defaultshash{'coursedefaults'}{'canclone'};
13302:                     }
13303:                 }
13304:                 my $cachetime = 24*60*60;
13305:                 &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13306:                 if (ref($lastactref) eq 'HASH') {
13307:                     $lastactref->{'domdefaults'} = 1;
13308:                 }
13309:             }
13310:             $resulttext = &mt('Changes made:').'<ul>';
13311:             foreach my $item (sort(keys(%changes))) {
13312:                 if ($item eq 'canuse_pdfforms') {
13313:                     if ($env{'form.'.$item} eq '1') {
13314:                         $resulttext .= '<li>'.&mt("Course/Community users can create/upload PDF forms set to 'on'").'</li>';
13315:                     } else {
13316:                         $resulttext .= '<li>'.&mt('Course/Community users can create/upload PDF forms set to "off"').'</li>';
13317:                     }
13318:                 } elsif ($item eq 'uselcmath') {
13319:                     if ($env{'form.'.$item} eq '1') {
13320:                         $resulttext .= '<li>'.&mt('Math preview uses LON-CAPA previewer (javascript), if supported by browser.').'</li>';
13321:                     } else {
13322:                         $resulttext .= '<li>'.&mt('Math preview uses DragMath (Java), if supported by client OS.').'</li>';
13323:                     }
13324:                 } elsif ($item eq 'usejsme') {
13325:                     if ($env{'form.'.$item} eq '1') {
13326:                         $resulttext .= '<li>'.&mt('Molecule editor uses JSME (HTML5), if supported by browser.').'</li>';
13327:                     } else {
13328:                         $resulttext .= '<li>'.&mt('Molecule editor uses JME (Java), if supported by client OS.').'</li>';
13329:                     }
13330:                 } elsif ($item eq 'anonsurvey_threshold') {
13331:                     $resulttext .= '<li>'.&mt('Responder count required for display of anonymous survey submissions set to [_1].',$defaultshash{'coursedefaults'}{'anonsurvey_threshold'}).'</li>';
13332:                 } elsif ($item eq 'uploadquota') {
13333:                     if (ref($defaultshash{'coursedefaults'}{'uploadquota'}) eq 'HASH') {
13334:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded to a course/community via Course Editor set as follows:').'<ul>'.
13335:                                        '<li>'.&mt('Official courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'official'}.'</b>').'</li>'.
13336:                                        '<li>'.&mt('Unofficial courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'unofficial'}.'</b>').'</li>'.
13337:                                        '<li>'.&mt('Textbook courses: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'textbook'}.'</b>').'</li>'.
13338:                                        '<li>'.&mt('Placement tests: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'placement'}.'</b>').'</li>'. 
13339:                                        '<li>'.&mt('Communities: [_1] MB','<b>'.$defaultshash{'coursedefaults'}{'uploadquota'}{'community'}.'</b>').'</li>'.
13340:                                        '</ul>'.
13341:                                        '</li>';
13342:                     } else {
13343:                         $resulttext .= '<li>'.&mt('Default quota for content uploaded via Course Editor remains default: [_1] MB',$staticdefaults{'uploadquota'}).'</li>';
13344:                     }
13345:                 } elsif ($item eq 'mysqltables') {
13346:                     if (ref($defaultshash{'coursedefaults'}{'mysqltables'}) eq 'HASH') {
13347:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver').'<ul>'.
13348:                                        '<li>'.&mt('Official courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'official'}.'</b>').'</li>'.
13349:                                        '<li>'.&mt('Unofficial courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'unofficial'}.'</b>').'</li>'.
13350:                                        '<li>'.&mt('Textbook courses: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'textbook'}.'</b>').'</li>'.
13351:                                        '<li>'.&mt('Placement tests: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'placement'}.'</b>').'</li>'.
13352:                                        '<li>'.&mt('Communities: [_1] s','<b>'.$defaultshash{'coursedefaults'}{'mysqltables'}{'community'}.'</b>').'</li>'.
13353:                                        '</ul>'.
13354:                                        '</li>';
13355:                     } else {
13356:                         $resulttext .= '<li>'.&mt('Lifetime of "Temporary" MySQL tables (student performance data) on homeserver remains default: [_1] s',$staticdefaults{'uploadquota'}).'</li>';
13357:                     }
13358:                 } elsif ($item eq 'postsubmit') {
13359:                     if ($domdefaults{'postsubmit'} eq 'off') {
13360:                         $resulttext .= '<li>'.&mt('Submit button(s) remain enabled on page after student makes submission.');
13361:                     } else {
13362:                         $resulttext .= '<li>'.&mt('Submit button(s) disabled on page after student makes submission').'; ';
13363:                         if (ref($defaultshash{'coursedefaults'}{'postsubmit'}) eq 'HASH') {
13364:                             $resulttext .= &mt('durations:').'<ul>';
13365:                             foreach my $type (@types) {
13366:                                 $resulttext .= '<li>';
13367:                                 my $timeout;
13368:                                 if (ref($defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}) eq 'HASH') {
13369:                                     $timeout = $defaultshash{'coursedefaults'}{'postsubmit'}{'timeout'}{$type};
13370:                                 }
13371:                                 my $display;
13372:                                 if ($timeout eq '0') {
13373:                                     $display = &mt('unlimited');
13374:                                 } elsif ($timeout eq '') {
13375:                                     $display = &mt('[quant,_1,second] (default)',$staticdefaults{'postsubmit'});
13376:                                 } else {
13377:                                     $display = &mt('[quant,_1,second]',$timeout);
13378:                                 }
13379:                                 if ($type eq 'community') {
13380:                                     $resulttext .= &mt('Communities');
13381:                                 } elsif ($type eq 'official') {
13382:                                     $resulttext .= &mt('Official courses');
13383:                                 } elsif ($type eq 'unofficial') {
13384:                                     $resulttext .= &mt('Unofficial courses');
13385:                                 } elsif ($type eq 'textbook') {
13386:                                     $resulttext .= &mt('Textbook courses');
13387:                                 } elsif ($type eq 'placement') {
13388:                                     $resulttext .= &mt('Placement tests');
13389:                                 }
13390:                                 $resulttext .= ' -- '.$display.'</li>';
13391:                             }
13392:                             $resulttext .= '</ul>';
13393:                         }
13394:                         $resulttext .= '</li>';
13395:                     }
13396:                 } elsif ($item eq 'coursecredits') {
13397:                     if (ref($defaultshash{'coursedefaults'}{'coursecredits'}) eq 'HASH') {
13398:                         if (($domdefaults{'officialcredits'} eq '') &&
13399:                             ($domdefaults{'unofficialcredits'} eq '') &&
13400:                             ($domdefaults{'textbookcredits'} eq '')) {
13401:                             $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
13402:                         } else {
13403:                             $resulttext .= '<li>'.&mt('Student credits can be set per course by a Domain Coordinator, with the following defaults applying:').'<ul>'.
13404:                                            '<li>'.&mt('Official courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'official'}).'</li>'.
13405:                                            '<li>'.&mt('Unofficial courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'unofficial'}).'</li>'.
13406:                                            '<li>'.&mt('Textbook courses: [_1]',$defaultshash{'coursedefaults'}{'coursecredits'}{'textbook'}).'</li>'.
13407:                                            '</ul>'.
13408:                                            '</li>';
13409:                         }
13410:                     } else {
13411:                         $resulttext .= '<li>'.&mt('Student credits not in use for courses in this domain').'</li>';
13412:                     }
13413:                 } elsif ($item eq 'canclone') {
13414:                     if (ref($defaultshash{'coursedefaults'}{'canclone'}) eq 'HASH') {
13415:                         if (ref($defaultshash{'coursedefaults'}{'canclone'}{'instcode'}) eq 'ARRAY') {
13416:                             my $clonecodes = join(' '.&mt('and').' ',@{$defaultshash{'coursedefaults'}{'canclone'}{'instcode'}});
13417:                             $resulttext .= '<li>'.&mt('By default, official courses can be cloned from existing courses with the same: [_1]','<b>'.$clonecodes.'</b>').'</li>';
13418:                         }
13419:                     } elsif ($defaultshash{'coursedefaults'}{'canclone'} eq 'domain') {
13420:                         $resulttext .= '<li>'.&mt('By default, a course requester can clone any course from his/her domain.').'</li>';
13421:                     } else {
13422:                         $resulttext .= '<li>'.&mt('By default, only course owner and coordinators may clone a course.').'</li>';
13423:                     }
13424:                 }
13425:             }
13426:             $resulttext .= '</ul>';
13427:         } else {
13428:             $resulttext = &mt('No changes made to course defaults');
13429:         }
13430:     } else {
13431:         $resulttext = '<span class="LC_error">'.
13432:             &mt('An error occurred: [_1]',$putresult).'</span>';
13433:     }
13434:     return $resulttext;
13435: }
13436: 
13437: sub modify_selfenrollment {
13438:     my ($dom,$lastactref,%domconfig) = @_;
13439:     my ($resulttext,$errors,%changes,%selfenrollhash,%ordered);
13440:     my @types = ('official','unofficial','community','textbook','placement');
13441:     my %titles = &tool_titles();
13442:     my %descs = &Apache::lonuserutils::selfenroll_default_descs();
13443:     ($ordered{'admin'},my $titlesref) = &Apache::lonuserutils::get_selfenroll_titles();
13444:     $ordered{'default'} = ['types','registered','approval','limit'];
13445: 
13446:     my (%roles,%shown,%toplevel);
13447:     $roles{'0'} = &Apache::lonnet::plaintext('dc');
13448: 
13449:     if (ref($domconfig{'selfenrollment'}) ne 'HASH') {
13450:         if ($domconfig{'selfenrollment'} eq '') {
13451:             $domconfig{'selfenrollment'} = {};
13452:         }
13453:     }
13454:     %toplevel = (
13455:                   admin      => 'Configuration Rights',
13456:                   default    => 'Default settings',
13457:                   validation => 'Validation of self-enrollment requests',
13458:                 );
13459:     my ($itemsref,$namesref,$fieldsref) = &Apache::lonuserutils::selfenroll_validation_types();
13460: 
13461:     if (ref($ordered{'admin'}) eq 'ARRAY') {
13462:         foreach my $item (@{$ordered{'admin'}}) {
13463:             foreach my $type (@types) {
13464:                 if ($env{'form.selfenrolladmin_'.$item.'_'.$type}) {
13465:                     $selfenrollhash{'admin'}{$type}{$item} = 1;
13466:                 } else {
13467:                     $selfenrollhash{'admin'}{$type}{$item} = 0;
13468:                 }
13469:                 if (ref($domconfig{'selfenrollment'}{'admin'}) eq 'HASH') {
13470:                     if (ref($domconfig{'selfenrollment'}{'admin'}{$type}) eq 'HASH') {
13471:                         if ($selfenrollhash{'admin'}{$type}{$item} ne
13472:                             $domconfig{'selfenrollment'}{'admin'}{$type}{$item})  {
13473:                             push(@{$changes{'admin'}{$type}},$item);
13474:                         }
13475:                     } else {
13476:                         if (!$selfenrollhash{'admin'}{$type}{$item}) {
13477:                             push(@{$changes{'admin'}{$type}},$item);
13478:                         }
13479:                     }
13480:                 } elsif (!$selfenrollhash{'admin'}{$type}{$item}) {
13481:                     push(@{$changes{'admin'}{$type}},$item);
13482:                 }
13483:             }
13484:         }
13485:     }
13486: 
13487:     foreach my $item (@{$ordered{'default'}}) {
13488:         foreach my $type (@types) {
13489:             my $value = $env{'form.selfenrolldefault_'.$item.'_'.$type};
13490:             if ($item eq 'types') {
13491:                 unless (($value eq 'all') || ($value eq 'dom')) {
13492:                     $value = '';
13493:                 }
13494:             } elsif ($item eq 'registered') {
13495:                 unless ($value eq '1') {
13496:                     $value = 0;
13497:                 }
13498:             } elsif ($item eq 'approval') {
13499:                 unless ($value =~ /^[012]$/) {
13500:                     $value = 0;
13501:                 }
13502:             } else {
13503:                 unless (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
13504:                     $value = 'none';
13505:                 }
13506:             }
13507:             $selfenrollhash{'default'}{$type}{$item} = $value;
13508:             if (ref($domconfig{'selfenrollment'}{'default'}) eq 'HASH') {
13509:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
13510:                     if ($selfenrollhash{'default'}{$type}{$item} ne
13511:                          $domconfig{'selfenrollment'}{'default'}{$type}{$item})  {
13512:                          push(@{$changes{'default'}{$type}},$item);
13513:                     }
13514:                 } else {
13515:                     push(@{$changes{'default'}{$type}},$item);
13516:                 }
13517:             } else {
13518:                 push(@{$changes{'default'}{$type}},$item);
13519:             }
13520:             if ($item eq 'limit') {
13521:                 if (($value eq 'allstudents') || ($value eq 'selfenrolled')) {
13522:                     $env{'form.selfenrolldefault_cap_'.$type} =~ s/\D//g;
13523:                     if ($env{'form.selfenrolldefault_cap_'.$type} ne '') {
13524:                         $selfenrollhash{'default'}{$type}{'cap'} = $env{'form.selfenrolldefault_cap_'.$type};
13525:                     }
13526:                 } else {
13527:                     $selfenrollhash{'default'}{$type}{'cap'} = '';
13528:                 }
13529:                 if (ref($domconfig{'selfenrollment'}{'default'}{$type}) eq 'HASH') {
13530:                     if ($selfenrollhash{'default'}{$type}{'cap'} ne
13531:                          $domconfig{'selfenrollment'}{'admin'}{$type}{'cap'})  {
13532:                          push(@{$changes{'default'}{$type}},'cap');
13533:                     }
13534:                 } elsif ($selfenrollhash{'default'}{$type}{'cap'} ne '') {
13535:                     push(@{$changes{'default'}{$type}},'cap');
13536:                 }
13537:             }
13538:         }
13539:     }
13540: 
13541:     foreach my $item (@{$itemsref}) {
13542:         if ($item eq 'fields') {
13543:             my @changed;
13544:             @{$selfenrollhash{'validation'}{$item}} = &Apache::loncommon::get_env_multiple('form.selfenroll_validation_'.$item);
13545:             if (@{$selfenrollhash{'validation'}{$item}} > 0) {
13546:                 @{$selfenrollhash{'validation'}{$item}} = sort(@{$selfenrollhash{'validation'}{$item}});
13547:             }
13548:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
13549:                 if (ref($domconfig{'selfenrollment'}{'validation'}{$item}) eq 'ARRAY') {
13550:                     @changed = &Apache::loncommon::compare_arrays($selfenrollhash{'validation'}{$item},
13551:                                                                   $domconfig{'selfenrollment'}{'validation'}{$item});
13552:                 } else {
13553:                     @changed = @{$selfenrollhash{'validation'}{$item}};
13554:                 }
13555:             } else {
13556:                 @changed = @{$selfenrollhash{'validation'}{$item}};
13557:             }
13558:             if (@changed) {
13559:                 if ($selfenrollhash{'validation'}{$item}) { 
13560:                     $changes{'validation'}{$item} = join(', ',@{$selfenrollhash{'validation'}{$item}});
13561:                 } else {
13562:                     $changes{'validation'}{$item} = &mt('None');
13563:                 }
13564:             }
13565:         } else {
13566:             $selfenrollhash{'validation'}{$item} = $env{'form.selfenroll_validation_'.$item};
13567:             if ($item eq 'markup') {
13568:                if ($env{'form.selfenroll_validation_'.$item}) {
13569:                    $env{'form.selfenroll_validation_'.$item} =~ s/[\n\r\f]+/\s/gs;
13570:                }
13571:             }
13572:             if (ref($domconfig{'selfenrollment'}{'validation'}) eq 'HASH') {
13573:                 if ($domconfig{'selfenrollment'}{'validation'}{$item} ne $selfenrollhash{'validation'}{$item}) {
13574:                     $changes{'validation'}{$item} = $selfenrollhash{'validation'}{$item};
13575:                 }
13576:             }
13577:         }
13578:     }
13579: 
13580:     my $putresult = &Apache::lonnet::put_dom('configuration',{'selfenrollment' => \%selfenrollhash},
13581:                                              $dom);
13582:     if ($putresult eq 'ok') {
13583:         if (keys(%changes) > 0) {
13584:             my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13585:             $resulttext = &mt('Changes made:').'<ul>';
13586:             foreach my $key ('admin','default','validation') {
13587:                 if (ref($changes{$key}) eq 'HASH') {
13588:                     $resulttext .= '<li>'.$toplevel{$key}.'<ul>';
13589:                     if ($key eq 'validation') {
13590:                         foreach my $item (@{$itemsref}) {
13591:                             if (exists($changes{$key}{$item})) {
13592:                                 if ($item eq 'markup') {
13593:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
13594:                                                               '<br /><pre>'.$changes{$key}{$item}.'</pre>').'</li>';
13595:                                 } else {  
13596:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2]',$namesref->{$item},
13597:                                                               '<b>'.$changes{$key}{$item}.'</b>').'</li>';
13598:                                 }
13599:                             }
13600:                         }
13601:                     } else {
13602:                         foreach my $type (@types) {
13603:                             if ($type eq 'community') {
13604:                                 $roles{'1'} = &mt('Community personnel');
13605:                             } else {
13606:                                 $roles{'1'} = &mt('Course personnel');
13607:                             }
13608:                             if (ref($changes{$key}{$type}) eq 'ARRAY') {
13609:                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') {
13610:                                     if ($key eq 'admin') {
13611:                                         my @mgrdc = ();
13612:                                         if (ref($ordered{$key}) eq 'ARRAY') {
13613:                                             foreach my $item (@{$ordered{'admin'}}) {
13614:                                                 if (ref($selfenrollhash{$key}{$type}) eq 'HASH') { 
13615:                                                     if ($selfenrollhash{$key}{$type}{$item} eq '0') {
13616:                                                         push(@mgrdc,$item);
13617:                                                     }
13618:                                                 }
13619:                                             }
13620:                                             if (@mgrdc) {
13621:                                                 $domdefaults{$type.'selfenrolladmdc'} = join(',',@mgrdc);
13622:                                             } else {
13623:                                                 delete($domdefaults{$type.'selfenrolladmdc'});
13624:                                             }
13625:                                         }
13626:                                     } else {
13627:                                         if (ref($ordered{$key}) eq 'ARRAY') {
13628:                                             foreach my $item (@{$ordered{$key}}) {
13629:                                                 if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
13630:                                                     $domdefaults{$type.'selfenroll'.$item} =
13631:                                                         $selfenrollhash{$key}{$type}{$item};
13632:                                                 }
13633:                                             }
13634:                                         }
13635:                                     }
13636:                                 }
13637:                                 $resulttext .= '<li>'.$titles{$type}.'<ul>';
13638:                                 foreach my $item (@{$ordered{$key}}) {
13639:                                     if (grep(/^\Q$item\E$/,@{$changes{$key}{$type}})) {
13640:                                         $resulttext .= '<li>';
13641:                                         if ($key eq 'admin') {
13642:                                             $resulttext .= &mt('[_1] -- management by: [_2]',$titlesref->{$item},
13643:                                                                '<b>'.$roles{$selfenrollhash{'admin'}{$type}{$item}}.'</b>');
13644:                                         } else {
13645:                                             $resulttext .= &mt('[_1] set to: [_2]',$titlesref->{$item},
13646:                                                                '<b>'.$descs{$item}{$selfenrollhash{'default'}{$type}{$item}}.'</b>');
13647:                                         }
13648:                                         $resulttext .= '</li>';
13649:                                     }
13650:                                 }
13651:                                 $resulttext .= '</ul></li>';
13652:                             }
13653:                         }
13654:                         $resulttext .= '</ul></li>'; 
13655:                     }
13656:                 }
13657:                 if ((exists($changes{'admin'})) || (exists($changes{'default'}))) {
13658:                     my $cachetime = 24*60*60;
13659:                     &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13660:                     if (ref($lastactref) eq 'HASH') {
13661:                         $lastactref->{'domdefaults'} = 1;
13662:                     }
13663:                 }
13664:             }
13665:             $resulttext .= '</ul>';
13666:         } else {
13667:             $resulttext = &mt('No changes made to self-enrollment settings');
13668:         }
13669:     } else {
13670:         $resulttext = '<span class="LC_error">'.
13671:             &mt('An error occurred: [_1]',$putresult).'</span>';
13672:     }
13673:     return $resulttext;
13674: }
13675: 
13676: sub modify_usersessions {
13677:     my ($dom,$lastactref,%domconfig) = @_;
13678:     my @hostingtypes = ('version','excludedomain','includedomain');
13679:     my @offloadtypes = ('primary','default');
13680:     my %types = (
13681:                   remote => \@hostingtypes,
13682:                   hosted => \@hostingtypes,
13683:                   spares => \@offloadtypes,
13684:                 );
13685:     my @prefixes = ('remote','hosted','spares');
13686:     my @lcversions = &Apache::lonnet::all_loncaparevs();
13687:     my (%by_ip,%by_location,@intdoms,@instdoms);
13688:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
13689:     my @locations = sort(keys(%by_location));
13690:     my (%defaultshash,%changes);
13691:     foreach my $prefix (@prefixes) {
13692:         $defaultshash{'usersessions'}{$prefix} = {};
13693:     }
13694:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
13695:     my $resulttext;
13696:     my %iphost = &Apache::lonnet::get_iphost();
13697:     foreach my $prefix (@prefixes) {
13698:         next if ($prefix eq 'spares');
13699:         foreach my $type (@{$types{$prefix}}) {
13700:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
13701:             if ($type eq 'version') {
13702:                 my $value = $env{'form.'.$prefix.'_'.$type};
13703:                 my $okvalue;
13704:                 if ($value ne '') {
13705:                     if (grep(/^\Q$value\E$/,@lcversions)) {
13706:                         $okvalue = $value;
13707:                     }
13708:                 }
13709:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
13710:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
13711:                         if ($domconfig{'usersessions'}{$prefix}{$type} ne '') {
13712:                             if ($inuse == 0) {
13713:                                 $changes{$prefix}{$type} = 1;
13714:                             } else {
13715:                                 if ($okvalue ne $domconfig{'usersessions'}{$prefix}{$type}) {
13716:                                     $changes{$prefix}{$type} = 1;
13717:                                 }
13718:                                 if ($okvalue ne '') {
13719:                                     $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13720:                                 } 
13721:                             }
13722:                         } else {
13723:                             if (($inuse == 1) && ($okvalue ne '')) {
13724:                                 $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13725:                                 $changes{$prefix}{$type} = 1;
13726:                             }
13727:                         }
13728:                     } else {
13729:                         if (($inuse == 1) && ($okvalue ne '')) {
13730:                             $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13731:                             $changes{$prefix}{$type} = 1;
13732:                         }
13733:                     }
13734:                 } else {
13735:                     if (($inuse == 1) && ($okvalue ne '')) {
13736:                         $defaultshash{'usersessions'}{$prefix}{$type} = $okvalue;
13737:                         $changes{$prefix}{$type} = 1;
13738:                     }
13739:                 }
13740:             } else {
13741:                 my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
13742:                 my @okvals;
13743:                 foreach my $val (@vals) {
13744:                     if ($val =~ /:/) {
13745:                         my @items = split(/:/,$val);
13746:                         foreach my $item (@items) {
13747:                             if (ref($by_location{$item}) eq 'ARRAY') {
13748:                                 push(@okvals,$item);
13749:                             }
13750:                         }
13751:                     } else {
13752:                         if (ref($by_location{$val}) eq 'ARRAY') {
13753:                             push(@okvals,$val);
13754:                         }
13755:                     }
13756:                 }
13757:                 @okvals = sort(@okvals);
13758:                 if (ref($domconfig{'usersessions'}) eq 'HASH') {
13759:                     if (ref($domconfig{'usersessions'}{$prefix}) eq 'HASH') {
13760:                         if (ref($domconfig{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
13761:                             if ($inuse == 0) {
13762:                                 $changes{$prefix}{$type} = 1; 
13763:                             } else {
13764:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13765:                                 my @changed = &Apache::loncommon::compare_arrays($domconfig{'usersessions'}{$prefix}{$type},$defaultshash{'usersessions'}{$prefix}{$type});
13766:                                 if (@changed > 0) {
13767:                                     $changes{$prefix}{$type} = 1;
13768:                                 }
13769:                             }
13770:                         } else {
13771:                             if ($inuse == 1) {
13772:                                 $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13773:                                 $changes{$prefix}{$type} = 1;
13774:                             }
13775:                         } 
13776:                     } else {
13777:                         if ($inuse == 1) {
13778:                             $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13779:                             $changes{$prefix}{$type} = 1;
13780:                         }
13781:                     }
13782:                 } else {
13783:                     if ($inuse == 1) {
13784:                         $defaultshash{'usersessions'}{$prefix}{$type} = \@okvals;
13785:                         $changes{$prefix}{$type} = 1;
13786:                     }
13787:                 }
13788:             }
13789:         }
13790:     }
13791: 
13792:     my @alldoms = &Apache::lonnet::all_domains();
13793:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
13794:     my %spareid = &current_offloads_to($dom,$domconfig{'usersessions'},\%servers);
13795:     my $savespares;
13796: 
13797:     foreach my $lonhost (sort(keys(%servers))) {
13798:         my $serverhomeID =
13799:             &Apache::lonnet::get_server_homeID($servers{$lonhost});
13800:         my $serverhostname = &Apache::lonnet::hostname($lonhost);
13801:         $defaultshash{'usersessions'}{'spares'}{$lonhost} = {};
13802:         my %spareschg;
13803:         foreach my $type (@{$types{'spares'}}) {
13804:             my @okspares;
13805:             my @checked = &Apache::loncommon::get_env_multiple('form.spare_'.$type.'_'.$lonhost);
13806:             foreach my $server (@checked) {
13807:                 if (&Apache::lonnet::hostname($server) ne '') {
13808:                     unless (&Apache::lonnet::hostname($server) eq $serverhostname) {
13809:                         unless (grep(/^\Q$server\E$/,@okspares)) {
13810:                             push(@okspares,$server);
13811:                         }
13812:                     }
13813:                 }
13814:             }
13815:             my $new = $env{'form.newspare_'.$type.'_'.$lonhost};
13816:             my $newspare;
13817:             if (($new ne '') && (&Apache::lonnet::hostname($new))) {
13818:                 unless (&Apache::lonnet::hostname($new) eq $serverhostname) {
13819:                     $newspare = $new;
13820:                 }
13821:             }
13822:             my @spares;
13823:             if (($newspare ne '') && (!grep(/^\Q$newspare\E$/,@okspares))) {
13824:                 @spares = sort(@okspares,$newspare);
13825:             } else {
13826:                 @spares = sort(@okspares);
13827:             }
13828:             $defaultshash{'usersessions'}{'spares'}{$lonhost}{$type} = \@spares;
13829:             if (ref($spareid{$lonhost}) eq 'HASH') {
13830:                 if (ref($spareid{$lonhost}{$type}) eq 'ARRAY') {
13831:                     my @diffs = &Apache::loncommon::compare_arrays($spareid{$lonhost}{$type},\@spares);
13832:                     if (@diffs > 0) {
13833:                         $spareschg{$type} = 1;
13834:                     }
13835:                 }
13836:             }
13837:         }
13838:         if (keys(%spareschg) > 0) {
13839:             $changes{'spares'}{$lonhost} = \%spareschg;
13840:         }
13841:     }
13842:     $defaultshash{'usersessions'}{'offloadnow'} = {};
13843:     my @offloadnow = &Apache::loncommon::get_env_multiple('form.offloadnow');
13844:     my @okoffload;
13845:     if (@offloadnow) {
13846:         foreach my $server (@offloadnow) {
13847:             if (&Apache::lonnet::hostname($server) ne '') {
13848:                 unless (grep(/^\Q$server\E$/,@okoffload)) {
13849:                     push(@okoffload,$server);
13850:                 }
13851:             }
13852:         }
13853:         if (@okoffload) {
13854:             foreach my $lonhost (@okoffload) {
13855:                 $defaultshash{'usersessions'}{'offloadnow'}{$lonhost} = 1;
13856:             }
13857:         }
13858:     }
13859:     if (ref($domconfig{'usersessions'}) eq 'HASH') {
13860:         if (ref($domconfig{'usersessions'}{'spares'}) eq 'HASH') {
13861:             if (ref($changes{'spares'}) eq 'HASH') {
13862:                 if (keys(%{$changes{'spares'}}) > 0) {
13863:                     $savespares = 1;
13864:                 }
13865:             }
13866:         } else {
13867:             $savespares = 1;
13868:         }
13869:         if (ref($domconfig{'usersessions'}{'offloadnow'}) eq 'HASH') {
13870:             foreach my $lonhost (keys(%{$domconfig{'usersessions'}{'offloadnow'}})) {
13871:                 unless ($defaultshash{'usersessions'}{'offloadnow'}{$lonhost}) {
13872:                     $changes{'offloadnow'} = 1;
13873:                     last;
13874:                 }
13875:             }
13876:             unless ($changes{'offloadnow'}) {
13877:                 foreach my $lonhost (keys(%{$defaultshash{'usersessions'}{'offloadnow'}})) {
13878:                     unless ($domconfig{'usersessions'}{'offloadnow'}{$lonhost}) {
13879:                         $changes{'offloadnow'} = 1;
13880:                         last;
13881:                     }
13882:                 }
13883:             }
13884:         } elsif (@okoffload) {
13885:             $changes{'offloadnow'} = 1;
13886:         }
13887:     } elsif (@okoffload) {
13888:         $changes{'offloadnow'} = 1;
13889:     }
13890:     my $nochgmsg = &mt('No changes made to settings for user session hosting/offloading.');
13891:     if ((keys(%changes) > 0) || ($savespares)) {
13892:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
13893:                                                  $dom);
13894:         if ($putresult eq 'ok') {
13895:             if (ref($defaultshash{'usersessions'}) eq 'HASH') {
13896:                 if (ref($defaultshash{'usersessions'}{'remote'}) eq 'HASH') {
13897:                     $domdefaults{'remotesessions'} = $defaultshash{'usersessions'}{'remote'};
13898:                 }
13899:                 if (ref($defaultshash{'usersessions'}{'hosted'}) eq 'HASH') {
13900:                     $domdefaults{'hostedsessions'} = $defaultshash{'usersessions'}{'hosted'};
13901:                 }
13902:                 if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
13903:                     $domdefaults{'offloadnow'} = $defaultshash{'usersessions'}{'offloadnow'};
13904:                 }
13905:             }
13906:             my $cachetime = 24*60*60;
13907:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
13908:             &Apache::lonnet::do_cache_new('usersessions',$dom,$defaultshash{'usersessions'},3600);
13909:             if (ref($lastactref) eq 'HASH') {
13910:                 $lastactref->{'domdefaults'} = 1;
13911:                 $lastactref->{'usersessions'} = 1;
13912:             }
13913:             if (keys(%changes) > 0) {
13914:                 my %lt = &usersession_titles();
13915:                 $resulttext = &mt('Changes made:').'<ul>';
13916:                 foreach my $prefix (@prefixes) {
13917:                     if (ref($changes{$prefix}) eq 'HASH') {
13918:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
13919:                         if ($prefix eq 'spares') {
13920:                             if (ref($changes{$prefix}) eq 'HASH') {
13921:                                 foreach my $lonhost (sort(keys(%{$changes{$prefix}}))) {
13922:                                     $resulttext .= '<li><b>'.$lonhost.'</b> ';
13923:                                     my $lonhostdom = &Apache::lonnet::host_domain($lonhost);
13924:                                     my $cachekey = &escape('spares').':'.&escape($lonhostdom);
13925:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
13926:                                     if (ref($changes{$prefix}{$lonhost}) eq 'HASH') {
13927:                                         foreach my $type (@{$types{$prefix}}) {
13928:                                             if ($changes{$prefix}{$lonhost}{$type}) {
13929:                                                 my $offloadto = &mt('None');
13930:                                                 if (ref($defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}) eq 'ARRAY') {
13931:                                                     if (@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}} > 0) {   
13932:                                                         $offloadto = join(', ',@{$defaultshash{'usersessions'}{'spares'}{$lonhost}{$type}});
13933:                                                     }
13934:                                                 }
13935:                                                 $resulttext .= &mt('[_1] set to: [_2].','<i>'.$lt{$type}.'</i>',$offloadto).('&nbsp;'x3);
13936:                                             }
13937:                                         }
13938:                                     }
13939:                                     $resulttext .= '</li>';
13940:                                 }
13941:                             }
13942:                         } else {
13943:                             foreach my $type (@{$types{$prefix}}) {
13944:                                 if (defined($changes{$prefix}{$type})) {
13945:                                     my $newvalue;
13946:                                     if (ref($defaultshash{'usersessions'}) eq 'HASH') {
13947:                                         if (ref($defaultshash{'usersessions'}{$prefix})) {
13948:                                             if ($type eq 'version') {
13949:                                                 $newvalue = $defaultshash{'usersessions'}{$prefix}{$type};
13950:                                             } elsif (ref($defaultshash{'usersessions'}{$prefix}{$type}) eq 'ARRAY') {
13951:                                                 if (@{$defaultshash{'usersessions'}{$prefix}{$type}} > 0) {
13952:                                                     $newvalue = join(', ',@{$defaultshash{'usersessions'}{$prefix}{$type}});
13953:                                                 }
13954:                                             }
13955:                                         }
13956:                                     }
13957:                                     if ($newvalue eq '') {
13958:                                         if ($type eq 'version') {
13959:                                             $resulttext .= '<li>'.&mt('[_1] set to: off',$lt{$type}).'</li>';
13960:                                         } else {
13961:                                             $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
13962:                                         }
13963:                                     } else {
13964:                                         if ($type eq 'version') {
13965:                                             $newvalue .= ' '.&mt('(or later)'); 
13966:                                         }
13967:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
13968:                                     }
13969:                                 }
13970:                             }
13971:                         }
13972:                         $resulttext .= '</ul>';
13973:                     }
13974:                 }
13975:                 if ($changes{'offloadnow'}) {
13976:                     if (ref($defaultshash{'usersessions'}{'offloadnow'}) eq 'HASH') {
13977:                         if (keys(%{$defaultshash{'usersessions'}{'offloadnow'}}) > 0) {
13978:                             $resulttext .= '<li>'.&mt('Switch active users on next access, for server(s):').'<ul>';
13979:                             foreach my $lonhost (sort(keys(%{$defaultshash{'usersessions'}{'offloadnow'}}))) {
13980:                                 $resulttext .= '<li>'.$lonhost.'</li>';
13981:                             }
13982:                             $resulttext .= '</ul>';
13983:                         } else {
13984:                             $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.');
13985:                         }
13986:                     } else {
13987:                         $resulttext .= '<li>'.&mt('No servers now set to switch active users on next access.').'</li>';
13988:                     }
13989:                 }
13990:                 $resulttext .= '</ul>';
13991:             } else {
13992:                 $resulttext = $nochgmsg;
13993:             }
13994:         } else {
13995:             $resulttext = '<span class="LC_error">'.
13996:                           &mt('An error occurred: [_1]',$putresult).'</span>';
13997:         }
13998:     } else {
13999:         $resulttext = $nochgmsg;
14000:     }
14001:     return $resulttext;
14002: }
14003: 
14004: sub modify_ssl {
14005:     my ($dom,$lastactref,%domconfig) = @_;
14006:     my (%by_ip,%by_location,@intdoms,@instdoms);
14007:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
14008:     my @locations = sort(keys(%by_location));
14009:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14010:     my (%defaultshash,%changes);
14011:     my $action = 'ssl';
14012:     my @prefixes = ('connto','connfrom','replication');
14013:     foreach my $prefix (@prefixes) {
14014:         $defaultshash{$action}{$prefix} = {};
14015:     }
14016:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14017:     my $resulttext;
14018:     my %iphost = &Apache::lonnet::get_iphost();
14019:     my @reptypes = ('certreq','nocertreq');
14020:     my @connecttypes = ('dom','intdom','other');
14021:     my %types = (
14022:                   connto      => \@connecttypes,
14023:                   connfrom    => \@connecttypes,
14024:                   replication => \@reptypes,
14025:                 );
14026:     foreach my $prefix (sort(keys(%types))) {
14027:         foreach my $type (@{$types{$prefix}}) {
14028:             if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
14029:                 my $value = 'yes';
14030:                 if ($env{'form.'.$prefix.'_'.$type} =~ /^(no|req)$/) {
14031:                     $value = $env{'form.'.$prefix.'_'.$type};
14032:                 }
14033:                 if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
14034:                     if ($domconfig{$action}{$prefix}{$type} ne '') {
14035:                         if ($value ne $domconfig{$action}{$prefix}{$type}) {
14036:                             $changes{$prefix}{$type} = 1;
14037:                         }
14038:                         $defaultshash{$action}{$prefix}{$type} = $value;
14039:                     } else {
14040:                         $defaultshash{$action}{$prefix}{$type} = $value;
14041:                         $changes{$prefix}{$type} = 1;
14042:                     }
14043:                 } else {
14044:                     $defaultshash{$action}{$prefix}{$type} = $value;
14045:                     $changes{$prefix}{$type} = 1;
14046:                 }
14047:                 if (($type eq 'dom') && (keys(%servers) == 1)) {
14048:                     delete($changes{$prefix}{$type});
14049:                 } elsif (($type eq 'intdom') && (@instdoms == 1)) {
14050:                     delete($changes{$prefix}{$type});
14051:                 } elsif (($type eq 'other') && (keys(%by_location) == 0)) { 
14052:                     delete($changes{$prefix}{$type});
14053:                 }
14054:             } elsif ($prefix eq 'replication') {
14055:                 if (@locations > 0) {
14056:                     my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
14057:                     my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
14058:                     my @okvals;
14059:                     foreach my $val (@vals) {
14060:                         if ($val =~ /:/) {
14061:                             my @items = split(/:/,$val);
14062:                             foreach my $item (@items) {
14063:                                 if (ref($by_location{$item}) eq 'ARRAY') {
14064:                                     push(@okvals,$item);
14065:                                 }
14066:                             }
14067:                         } else {
14068:                             if (ref($by_location{$val}) eq 'ARRAY') {
14069:                                 push(@okvals,$val);
14070:                             }
14071:                         }
14072:                     }
14073:                     @okvals = sort(@okvals);
14074:                     if (ref($domconfig{$action}) eq 'HASH') {
14075:                         if (ref($domconfig{$action}{$prefix}) eq 'HASH') {
14076:                             if (ref($domconfig{$action}{$prefix}{$type}) eq 'ARRAY') {
14077:                                 if ($inuse == 0) {
14078:                                     $changes{$prefix}{$type} = 1;
14079:                                 } else {
14080:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
14081:                                     my @changed = &Apache::loncommon::compare_arrays($domconfig{$action}{$prefix}{$type},$defaultshash{$action}{$prefix}{$type});
14082:                                     if (@changed > 0) {
14083:                                         $changes{$prefix}{$type} = 1;
14084:                                     }
14085:                                 }
14086:                             } else {
14087:                                 if ($inuse == 1) {
14088:                                     $defaultshash{$action}{$prefix}{$type} = \@okvals;
14089:                                     $changes{$prefix}{$type} = 1;
14090:                                 }
14091:                             }
14092:                         } else {
14093:                             if ($inuse == 1) {
14094:                                 $defaultshash{$action}{$prefix}{$type} = \@okvals;
14095:                                 $changes{$prefix}{$type} = 1;
14096:                             }
14097:                         }
14098:                     } else {
14099:                         if ($inuse == 1) {
14100:                             $defaultshash{$action}{$prefix}{$type} = \@okvals;
14101:                             $changes{$prefix}{$type} = 1;
14102:                         }
14103:                     }
14104:                 }
14105:             }
14106:         }
14107:     }
14108:     my $nochgmsg = &mt('No changes made to LON-CAPA SSL settings');
14109:     if (keys(%changes) > 0) {
14110:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
14111:                                                  $dom);
14112:         if ($putresult eq 'ok') {
14113:             if (ref($defaultshash{$action}) eq 'HASH') {
14114:                 if (ref($defaultshash{$action}{'replication'}) eq 'HASH') {
14115:                     $domdefaults{'replication'} = $defaultshash{$action}{'replication'};
14116:                 }
14117:                 if (ref($defaultshash{$action}{'connto'}) eq 'HASH') {
14118:                     $domdefaults{'connto'} = $domconfig{$action}{'connto'};
14119:                 }
14120:                 if (ref($defaultshash{$action}{'connfrom'}) eq 'HASH') {
14121:                     $domdefaults{'connfrom'} = $domconfig{$action}{'connfrom'};
14122:                 }
14123:             }
14124:             my $cachetime = 24*60*60;
14125:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14126:             if (ref($lastactref) eq 'HASH') {
14127:                 $lastactref->{'domdefaults'} = 1;
14128:             }
14129:             if (keys(%changes) > 0) {
14130:                 my %titles = &ssl_titles();
14131:                 $resulttext = &mt('Changes made:').'<ul>';
14132:                 foreach my $prefix (@prefixes) {
14133:                     if (ref($changes{$prefix}) eq 'HASH') {
14134:                         $resulttext .= '<li>'.$titles{$prefix}.'<ul>';
14135:                         foreach my $type (@{$types{$prefix}}) {
14136:                             if (defined($changes{$prefix}{$type})) {
14137:                                 my $newvalue;
14138:                                 if (ref($defaultshash{$action}) eq 'HASH') {
14139:                                     if (ref($defaultshash{$action}{$prefix})) {
14140:                                         if (($prefix eq 'connto') || ($prefix eq 'connfrom')) {
14141:                                             $newvalue = $titles{$defaultshash{$action}{$prefix}{$type}};
14142:                                         } elsif (ref($defaultshash{$action}{$prefix}{$type}) eq 'ARRAY') {
14143:                                             if (@{$defaultshash{$action}{$prefix}{$type}} > 0) {
14144:                                                 $newvalue = join(', ',@{$defaultshash{$action}{$prefix}{$type}});
14145:                                             }
14146:                                         }
14147:                                     }
14148:                                     if ($newvalue eq '') {
14149:                                         $resulttext .= '<li>'.&mt('[_1] set to: none',$titles{$type}).'</li>';
14150:                                     } else {
14151:                                         $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$titles{$type},$newvalue).'</li>';
14152:                                     }
14153:                                 }
14154:                             }
14155:                         }
14156:                         $resulttext .= '</ul>';
14157:                     }
14158:                 }
14159:             } else {
14160:                 $resulttext = $nochgmsg;
14161:             }
14162:         } else {
14163:             $resulttext = '<span class="LC_error">'.
14164:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14165:         }
14166:     } else {
14167:         $resulttext = $nochgmsg;
14168:     }
14169:     return $resulttext;
14170: }
14171: 
14172: sub modify_trust {
14173:     my ($dom,$lastactref,%domconfig) = @_;
14174:     my (%by_ip,%by_location,@intdoms,@instdoms);
14175:     &build_location_hashes(\@intdoms,\%by_ip,\%by_location,\@instdoms);
14176:     my @locations = sort(keys(%by_location));
14177:     my @prefixes = qw(content shared enroll othcoau coaurem domroles catalog reqcrs msg);
14178:     my @types = ('exc','inc');
14179:     my (%defaultshash,%changes);
14180:     foreach my $prefix (@prefixes) {
14181:         $defaultshash{'trust'}{$prefix} = {};
14182:     }
14183:     my %domdefaults = &Apache::lonnet::get_domain_defaults($dom,1);
14184:     my $resulttext;
14185:     foreach my $prefix (@prefixes) {
14186:         foreach my $type (@types) {
14187:             my $inuse = $env{'form.'.$prefix.'_'.$type.'_inuse'};
14188:             my @vals = &Apache::loncommon::get_env_multiple('form.'.$prefix.'_'.$type);
14189:             my @okvals;
14190:             foreach my $val (@vals) {
14191:                 if ($val =~ /:/) {
14192:                     my @items = split(/:/,$val);
14193:                     foreach my $item (@items) {
14194:                         if (ref($by_location{$item}) eq 'ARRAY') {
14195:                             push(@okvals,$item);
14196:                         }
14197:                     }
14198:                 } else {
14199:                     if (ref($by_location{$val}) eq 'ARRAY') {
14200:                         push(@okvals,$val);
14201:                     }
14202:                 }
14203:             }
14204:             @okvals = sort(@okvals);
14205:             if (ref($domconfig{'trust'}) eq 'HASH') {
14206:                 if (ref($domconfig{'trust'}{$prefix}) eq 'HASH') {
14207:                     if (ref($domconfig{'trust'}{$prefix}{$type}) eq 'ARRAY') {
14208:                         if ($inuse == 0) {
14209:                             $changes{$prefix}{$type} = 1;
14210:                         } else {
14211:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
14212:                             my @changed = &Apache::loncommon::compare_arrays($domconfig{'trust'}{$prefix}{$type},$defaultshash{'trust'}{$prefix}{$type});
14213:                             if (@changed > 0) {
14214:                                 $changes{$prefix}{$type} = 1;
14215:                             }
14216:                         }
14217:                     } else {
14218:                         if ($inuse == 1) {
14219:                             $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
14220:                             $changes{$prefix}{$type} = 1;
14221:                         }
14222:                     }
14223:                 } else {
14224:                     if ($inuse == 1) {
14225:                         $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
14226:                         $changes{$prefix}{$type} = 1;
14227:                     }
14228:                 }
14229:             } else {
14230:                 if ($inuse == 1) {
14231:                     $defaultshash{'trust'}{$prefix}{$type} = \@okvals;
14232:                     $changes{$prefix}{$type} = 1;
14233:                 }
14234:             }
14235:         }
14236:     }
14237:     my $nochgmsg = &mt('No changes made to trust settings.');
14238:     if (keys(%changes) > 0) {
14239:         my $putresult = &Apache::lonnet::put_dom('configuration',\%defaultshash,
14240:                                                  $dom);
14241:         if ($putresult eq 'ok') {
14242:             if (ref($defaultshash{'trust'}) eq 'HASH') {
14243:                 foreach my $prefix (@prefixes) {
14244:                     if (ref($defaultshash{'trust'}{$prefix}) eq 'HASH') {
14245:                         $domdefaults{'trust'.$prefix} = $defaultshash{'trust'}{$prefix};
14246:                     }
14247:                 }
14248:             }
14249:             my $cachetime = 24*60*60;
14250:             &Apache::lonnet::do_cache_new('domdefaults',$dom,\%domdefaults,$cachetime);
14251:             if (ref($lastactref) eq 'HASH') {
14252:                 $lastactref->{'domdefaults'} = 1;
14253:             }
14254:             if (keys(%changes) > 0) {
14255:                 my %lt = &trust_titles();
14256:                 $resulttext = &mt('Changes made:').'<ul>';
14257:                 foreach my $prefix (@prefixes) {
14258:                     if (ref($changes{$prefix}) eq 'HASH') {
14259:                         $resulttext .= '<li>'.$lt{$prefix}.'<ul>';
14260:                         foreach my $type (@types) {
14261:                             if (defined($changes{$prefix}{$type})) {
14262:                                 my $newvalue;
14263:                                 if (ref($defaultshash{'trust'}) eq 'HASH') {
14264:                                     if (ref($defaultshash{'trust'}{$prefix})) {
14265:                                         if (ref($defaultshash{'trust'}{$prefix}{$type}) eq 'ARRAY') {
14266:                                             if (@{$defaultshash{'trust'}{$prefix}{$type}} > 0) {
14267:                                                 $newvalue = join(', ',@{$defaultshash{'trust'}{$prefix}{$type}});
14268:                                             }
14269:                                         }
14270:                                     }
14271:                                 }
14272:                                 if ($newvalue eq '') {
14273:                                     $resulttext .= '<li>'.&mt('[_1] set to: none',$lt{$type}).'</li>';
14274:                                 } else {
14275:                                     $resulttext .= '<li>'.&mt('[_1] set to: [_2].',$lt{$type},$newvalue).'</li>';
14276:                                 }
14277:                             }
14278:                         }
14279:                         $resulttext .= '</ul>';
14280:                     }
14281:                 }
14282:                 $resulttext .= '</ul>';
14283:             } else {
14284:                 $resulttext = $nochgmsg;
14285:             }
14286:         } else {
14287:             $resulttext = '<span class="LC_error">'.
14288:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14289:         }
14290:     } else {
14291:         $resulttext = $nochgmsg;
14292:     }
14293:     return $resulttext;
14294: }
14295: 
14296: sub modify_loadbalancing {
14297:     my ($dom,%domconfig) = @_;
14298:     my $primary_id = &Apache::lonnet::domain($dom,'primary');
14299:     my $intdom = &Apache::lonnet::internet_dom($primary_id);
14300:     my ($othertitle,$usertypes,$types) =
14301:         &Apache::loncommon::sorted_inst_types($dom);
14302:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
14303:     my %libraryservers = &Apache::lonnet::get_servers($dom,'library');
14304:     my @sparestypes = ('primary','default');
14305:     my %typetitles = &sparestype_titles();
14306:     my $resulttext;
14307:     my (%currbalancer,%currtargets,%currrules,%existing);
14308:     if (ref($domconfig{'loadbalancing'}) eq 'HASH') {
14309:         %existing = %{$domconfig{'loadbalancing'}};
14310:     }
14311:     &get_loadbalancers_config(\%servers,\%existing,\%currbalancer,
14312:                               \%currtargets,\%currrules);
14313:     my ($saveloadbalancing,%defaultshash,%changes);
14314:     my ($alltypes,$othertypes,$titles) =
14315:         &loadbalancing_titles($dom,$intdom,$usertypes,$types);
14316:     my %ruletitles = &offloadtype_text();
14317:     my @deletions = &Apache::loncommon::get_env_multiple('form.loadbalancing_delete');
14318:     for (my $i=0; $i<$env{'form.loadbalancing_total'}; $i++) {
14319:         my $balancer = $env{'form.loadbalancing_lonhost_'.$i};
14320:         if ($balancer eq '') {
14321:             next;
14322:         }
14323:         if (!exists($servers{$balancer})) {
14324:             if (exists($currbalancer{$balancer})) {
14325:                 push(@{$changes{'delete'}},$balancer);
14326:             }
14327:             next;
14328:         }
14329:         if ((@deletions > 0) && (grep(/^\Q$i\E$/,@deletions))) {
14330:             push(@{$changes{'delete'}},$balancer);
14331:             next;
14332:         }
14333:         if (!exists($currbalancer{$balancer})) {
14334:             push(@{$changes{'add'}},$balancer);
14335:         }
14336:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'} = [];
14337:         $defaultshash{'loadbalancing'}{$balancer}{'targets'}{'default'} = [];
14338:         $defaultshash{'loadbalancing'}{$balancer}{'rules'} = {};
14339:         unless (ref($domconfig{'loadbalancing'}) eq 'HASH') {
14340:             $saveloadbalancing = 1;
14341:         }
14342:         foreach my $sparetype (@sparestypes) {
14343:             my @targets = &Apache::loncommon::get_env_multiple('form.loadbalancing_target_'.$i.'_'.$sparetype);
14344:             my @offloadto;
14345:             foreach my $target (@targets) {
14346:                 if (($servers{$target}) && ($target ne $balancer)) {
14347:                     if ($sparetype eq 'default') {
14348:                         if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}) eq 'ARRAY') {
14349:                             next if (grep(/^\Q$target\E$/,@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{'primary'}}));
14350:                         }
14351:                     }
14352:                     unless(grep(/^\Q$target\E$/,@offloadto)) {
14353:                         push(@offloadto,$target);
14354:                     }
14355:                 }
14356:             }
14357:             if ($env{'form.loadbalancing_target_'.$i.'_hosthere'} eq $sparetype) {
14358:                 unless(grep(/^\Q$balancer\E$/,@offloadto)) {
14359:                     push(@offloadto,$balancer);
14360:                 }
14361:             }
14362:             $defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype} = \@offloadto;
14363:         }
14364:         if (ref($currtargets{$balancer}) eq 'HASH') {
14365:             foreach my $sparetype (@sparestypes) {
14366:                 if (ref($currtargets{$balancer}{$sparetype}) eq 'ARRAY') {
14367:                     my @targetdiffs = &Apache::loncommon::compare_arrays($currtargets{$balancer}{$sparetype},$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype});
14368:                     if (@targetdiffs > 0) {
14369:                         $changes{'curr'}{$balancer}{'targets'} = 1;
14370:                     }
14371:                 } elsif (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14372:                     if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14373:                         $changes{'curr'}{$balancer}{'targets'} = 1;
14374:                     }
14375:                 }
14376:             }
14377:         } else {
14378:             if (ref($defaultshash{'loadbalancing'}{$balancer}) eq 'HASH') {
14379:                 foreach my $sparetype (@sparestypes) {
14380:                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14381:                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14382:                             $changes{'curr'}{$balancer}{'targets'} = 1;
14383:                         }
14384:                     }
14385:                 }
14386:             }
14387:         }
14388:         my $ishomedom;
14389:         if (&Apache::lonnet::host_domain($balancer) eq $dom) {
14390:             $ishomedom = 1;
14391:         }
14392:         if (ref($alltypes) eq 'ARRAY') {
14393:             foreach my $type (@{$alltypes}) {
14394:                 my $rule;
14395:                 unless ((($type eq '_LC_external') || ($type eq '_LC_internetdom')) &&
14396:                          (!$ishomedom)) {
14397:                     $rule = $env{'form.loadbalancing_rules_'.$i.'_'.$type};
14398:                 }
14399:                 if ($rule eq 'specific') {
14400:                     my $specifiedhost = $env{'form.loadbalancing_singleserver_'.$i.'_'.$type};
14401:                     if (exists($servers{$specifiedhost})) {
14402:                         $rule = $specifiedhost;
14403:                     }
14404:                 }
14405:                 $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type} = $rule;
14406:                 if (ref($currrules{$balancer}) eq 'HASH') {
14407:                     if ($rule ne $currrules{$balancer}{$type}) {
14408:                         $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
14409:                     }
14410:                 } elsif ($rule ne '') {
14411:                     $changes{'curr'}{$balancer}{'rules'}{$type} = 1;
14412:                 }
14413:             }
14414:         }
14415:     }
14416:     my $nochgmsg = &mt('No changes made to Load Balancer settings.');
14417:     if ((keys(%changes) > 0) || ($saveloadbalancing)) {
14418:         unless (ref($defaultshash{'loadbalancing'}) eq 'HASH') {
14419:             $defaultshash{'loadbalancing'} = {};
14420:         }
14421:         my $putresult = &Apache::lonnet::put_dom('configuration',
14422:                                                  \%defaultshash,$dom);
14423:         if ($putresult eq 'ok') {
14424:             if (keys(%changes) > 0) {
14425:                 my %toupdate;
14426:                 if (ref($changes{'delete'}) eq 'ARRAY') {
14427:                     foreach my $balancer (sort(@{$changes{'delete'}})) {
14428:                         $resulttext .= '<li>'.&mt('Load Balancing discontinued for: [_1]',$balancer).'</li>';
14429:                         $toupdate{$balancer} = 1;
14430:                     }
14431:                 }
14432:                 if (ref($changes{'add'}) eq 'ARRAY') {
14433:                     foreach my $balancer (sort(@{$changes{'add'}})) {
14434:                         $resulttext .= '<li>'.&mt('Load Balancing enabled for: [_1]',$balancer);
14435:                         $toupdate{$balancer} = 1;
14436:                     }
14437:                 }
14438:                 if (ref($changes{'curr'}) eq 'HASH') {
14439:                     foreach my $balancer (sort(keys(%{$changes{'curr'}}))) {
14440:                         $toupdate{$balancer} = 1;
14441:                         if (ref($changes{'curr'}{$balancer}) eq 'HASH') {
14442:                             if ($changes{'curr'}{$balancer}{'targets'}) {
14443:                                 my %offloadstr;
14444:                                 foreach my $sparetype (@sparestypes) {
14445:                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14446:                                         if (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}} > 0) {
14447:                                             $offloadstr{$sparetype} = join(', ',@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
14448:                                         }
14449:                                     }
14450:                                 }
14451:                                 if (keys(%offloadstr) == 0) {
14452:                                     $resulttext .= '<li>'.&mt("Servers to which Load Balance server offloads set to 'None', by default").'</li>';
14453:                                 } else {
14454:                                     my $showoffload;
14455:                                     foreach my $sparetype (@sparestypes) {
14456:                                         $showoffload .= '<i>'.$typetitles{$sparetype}.'</i>:&nbsp;';
14457:                                         if (defined($offloadstr{$sparetype})) {
14458:                                             $showoffload .= $offloadstr{$sparetype};
14459:                                         } else {
14460:                                             $showoffload .= &mt('None');
14461:                                         }
14462:                                         $showoffload .= ('&nbsp;'x3);
14463:                                     }
14464:                                     $resulttext .= '<li>'.&mt('By default, Load Balancer: [_1] set to offload to - [_2]',$balancer,$showoffload).'</li>';
14465:                                 }
14466:                             }
14467:                         }
14468:                         if (ref($changes{'curr'}{$balancer}{'rules'}) eq 'HASH') {
14469:                             if ((ref($alltypes) eq 'ARRAY') && (ref($titles) eq 'HASH')) {
14470:                                 foreach my $type (@{$alltypes}) {
14471:                                     if ($changes{'curr'}{$balancer}{'rules'}{$type}) {
14472:                                         my $rule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
14473:                                         my $balancetext;
14474:                                         if ($rule eq '') {
14475:                                             $balancetext =  $ruletitles{'default'};
14476:                                         } elsif (($rule eq 'homeserver') || ($rule eq 'externalbalancer') ||
14477:                                                  ($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
14478:                                             if (($type eq '_LC_ipchange') || ($type eq '_LC_ipchangesso')) {
14479:                                                 foreach my $sparetype (@sparestypes) {
14480:                                                     if (ref($defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}) eq 'ARRAY') {
14481:                                                         map { $toupdate{$_} = 1; } (@{$defaultshash{'loadbalancing'}{$balancer}{'targets'}{$sparetype}});
14482:                                                     }
14483:                                                 }
14484:                                                 foreach my $item (@{$alltypes}) {
14485:                                                     next if ($item =~  /^_LC_ipchange/);
14486:                                                     my $hasrule = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$item};
14487:                                                     if ($hasrule eq 'homeserver') {
14488:                                                         map { $toupdate{$_} = 1; } (keys(%libraryservers));
14489:                                                     } else {
14490:                                                         unless (($hasrule eq 'default') || ($hasrule eq 'none') || ($hasrule eq 'externalbalancer')) {
14491:                                                             if ($servers{$hasrule}) {
14492:                                                                 $toupdate{$hasrule} = 1;
14493:                                                             }
14494:                                                         }
14495:                                                     }
14496:                                                 }
14497:                                                 if (($rule eq 'balancer') || ($rule eq 'offloadedto')) {
14498:                                                     $balancetext =  $ruletitles{$rule};
14499:                                                 } else {
14500:                                                     my $receiver = $defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type};
14501:                                                     $balancetext = $ruletitles{'particular'}.' '.$receiver;
14502:                                                     if ($receiver) {
14503:                                                         $toupdate{$receiver};
14504:                                                     }
14505:                                                 }
14506:                                             } else {
14507:                                                 $balancetext =  $ruletitles{$rule};
14508:                                             }
14509:                                         } else {
14510:                                             $balancetext = &mt('offload to [_1]',$defaultshash{'loadbalancing'}{$balancer}{'rules'}{$type});
14511:                                         }
14512:                                         $resulttext .= '<li>'.&mt('Load Balancer: [_1] -- balancing for [_2] set to - "[_3]"',$balancer,$titles->{$type},$balancetext).'</li>';
14513:                                     }
14514:                                 }
14515:                             }
14516:                         }
14517:                         if (keys(%toupdate)) {
14518:                             my %thismachine;
14519:                             my $updatedhere;
14520:                             my $cachetime = 60*60*24;
14521:                             map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
14522:                             foreach my $lonhost (keys(%toupdate)) {
14523:                                 if ($thismachine{$lonhost}) {
14524:                                     unless ($updatedhere) {
14525:                                         &Apache::lonnet::do_cache_new('loadbalancing',$dom,
14526:                                                                       $defaultshash{'loadbalancing'},
14527:                                                                       $cachetime);
14528:                                         $updatedhere = 1;
14529:                                     }
14530:                                 } else {
14531:                                     my $cachekey = &escape('loadbalancing').':'.&escape($dom);
14532:                                     &Apache::lonnet::remote_devalidate_cache($lonhost,[$cachekey]);
14533:                                 }
14534:                             }
14535:                         }
14536:                     }
14537:                 }
14538:                 if ($resulttext ne '') {
14539:                     $resulttext = &mt('Changes made:').'<ul>'.$resulttext.'</ul>';
14540:                 } else {
14541:                     $resulttext = $nochgmsg;
14542:                 }
14543:             } else {
14544:                 $resulttext = $nochgmsg;
14545:             }
14546:         } else {
14547:             $resulttext = '<span class="LC_error">'.
14548:                           &mt('An error occurred: [_1]',$putresult).'</span>';
14549:         }
14550:     } else {
14551:         $resulttext = $nochgmsg;
14552:     }
14553:     return $resulttext;
14554: }
14555: 
14556: sub recurse_check {
14557:     my ($chkcats,$categories,$depth,$name) = @_;
14558:     if (ref($chkcats->[$depth]{$name}) eq 'ARRAY') {
14559:         my $chg = 0;
14560:         for (my $j=0; $j<@{$chkcats->[$depth]{$name}}; $j++) {
14561:             my $category = $chkcats->[$depth]{$name}[$j];
14562:             my $item;
14563:             if ($category eq '') {
14564:                 $chg ++;
14565:             } else {
14566:                 my $deeper = $depth + 1;
14567:                 $item = &escape($category).':'.&escape($name).':'.$depth;
14568:                 if ($chg) {
14569:                     $categories->{$item} -= $chg;
14570:                 }
14571:                 &recurse_check($chkcats,$categories,$deeper,$category);
14572:                 $deeper --;
14573:             }
14574:         }
14575:     }
14576:     return;
14577: }
14578: 
14579: sub recurse_cat_deletes {
14580:     my ($item,$coursecategories,$deletions) = @_;
14581:     my ($deleted,$container,$depth) = map { &unescape($_); } split(/:/,$item);
14582:     my $subdepth = $depth + 1;
14583:     if (ref($coursecategories) eq 'HASH') {
14584:         foreach my $subitem (keys(%{$coursecategories})) {
14585:             my ($child,$parent,$itemdepth) = map { &unescape($_); } split(/:/,$subitem);
14586:             if (($parent eq $deleted) && ($itemdepth == $subdepth)) {
14587:                 delete($coursecategories->{$subitem});
14588:                 $deletions->{$subitem} = 1;
14589:                 &recurse_cat_deletes($subitem,$coursecategories,$deletions);
14590:             }
14591:         }
14592:     }
14593:     return;
14594: }
14595: 
14596: sub active_dc_picker {
14597:     my ($dom,$numinrow,$inputtype,$name,%currhash) = @_;
14598:     my %domcoords = &Apache::lonnet::get_active_domroles($dom,['dc']);
14599:     my @domcoord = keys(%domcoords);
14600:     if (keys(%currhash)) {
14601:         foreach my $dc (keys(%currhash)) {
14602:             unless (exists($domcoords{$dc})) {
14603:                 push(@domcoord,$dc);
14604:             }
14605:         }
14606:     }
14607:     @domcoord = sort(@domcoord);
14608:     my $numdcs = scalar(@domcoord);
14609:     my $rows = 0;
14610:     my $table;
14611:     if ($numdcs > 1) {
14612:         $table = '<table>';
14613:         for (my $i=0; $i<@domcoord; $i++) {
14614:             my $rem = $i%($numinrow);
14615:             if ($rem == 0) {
14616:                 if ($i > 0) {
14617:                     $table .= '</tr>';
14618:                 }
14619:                 $table .= '<tr>';
14620:                 $rows ++;
14621:             }
14622:             my $check = '';
14623:             if ($inputtype eq 'radio') {
14624:                 if (keys(%currhash) == 0) {
14625:                     if (!$i) {
14626:                         $check = ' checked="checked"';
14627:                     }
14628:                 } elsif (exists($currhash{$domcoord[$i]})) {
14629:                     $check = ' checked="checked"';
14630:                 }
14631:             } else {
14632:                 if (exists($currhash{$domcoord[$i]})) {
14633:                     $check = ' checked="checked"';
14634:                 }
14635:             }
14636:             if ($i == @domcoord - 1) {
14637:                 my $colsleft = $numinrow - $rem;
14638:                 if ($colsleft > 1) {
14639:                     $table .= '<td class="LC_left_item" colspan="'.$colsleft.'">';
14640:                 } else {
14641:                     $table .= '<td class="LC_left_item">';
14642:                 }
14643:             } else {
14644:                 $table .= '<td class="LC_left_item">';
14645:             }
14646:             my ($dcname,$dcdom) = split(':',$domcoord[$i]);
14647:             my $user = &Apache::loncommon::plainname($dcname,$dcdom);
14648:             $table .= '<span class="LC_nobreak"><label>'.
14649:                       '<input type="'.$inputtype.'" name="'.$name.'"'.
14650:                       ' value="'.$domcoord[$i].'"'.$check.' />'.$user;
14651:             if ($user ne $dcname.':'.$dcdom) {
14652:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14653:             }
14654:             $table .= '</label></span></td>';
14655:         }
14656:         $table .= '</tr></table>';
14657:     } elsif ($numdcs == 1) {
14658:         my ($dcname,$dcdom) = split(':',$domcoord[0]);
14659:         my $user = &Apache::loncommon::plainname($dcname,$dcdom);
14660:         if ($inputtype eq 'radio') {
14661:             $table = '<input type="hidden" name="'.$name.'" value="'.$domcoord[0].'" />'.$user;
14662:             if ($user ne $dcname.':'.$dcdom) {
14663:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14664:             }
14665:         } else {
14666:             my $check;
14667:             if (exists($currhash{$domcoord[0]})) {
14668:                 $check = ' checked="checked"';
14669:             }
14670:             $table = '<span class="LC_nobreak"><label>'.
14671:                      '<input type="checkbox" name="'.$name.'" '.
14672:                      'value="'.$domcoord[0].'"'.$check.' />'.$user;
14673:             if ($user ne $dcname.':'.$dcdom) {
14674:                 $table .=  ' ('.$dcname.':'.$dcdom.')';
14675:             }
14676:             $table .= '</label></span>';
14677:             $rows ++;
14678:         }
14679:     }
14680:     return ($numdcs,$table,$rows);
14681: }
14682: 
14683: sub usersession_titles {
14684:     return &Apache::lonlocal::texthash(
14685:                hosted => 'Hosting of sessions for users from other domains on servers in this domain',
14686:                remote => 'Hosting of sessions for users in this domain on servers in other domains',
14687:                spares => 'Servers offloaded to, when busy',
14688:                version => 'LON-CAPA version requirement',
14689:                excludedomain => 'Allow all, but exclude specific domains',
14690:                includedomain => 'Deny all, but include specific domains',
14691:                primary => 'Primary (checked first)',
14692:                default => 'Default',
14693:            );
14694: }
14695: 
14696: sub id_for_thisdom {
14697:     my (%servers) = @_;
14698:     my %altids;
14699:     foreach my $server (keys(%servers)) {
14700:         my $serverhome = &Apache::lonnet::get_server_homeID($servers{$server});
14701:         if ($serverhome ne $server) {
14702:             $altids{$serverhome} = $server;
14703:         }
14704:     }
14705:     return %altids;
14706: }
14707: 
14708: sub count_servers {
14709:     my ($currbalancer,%servers) = @_;
14710:     my (@spares,$numspares);
14711:     foreach my $lonhost (sort(keys(%servers))) {
14712:         next if ($currbalancer eq $lonhost);
14713:         push(@spares,$lonhost);
14714:     }
14715:     if ($currbalancer) {
14716:         $numspares = scalar(@spares);
14717:     } else {
14718:         $numspares = scalar(@spares) - 1;
14719:     }
14720:     return ($numspares,@spares);
14721: }
14722: 
14723: sub lonbalance_targets_js {
14724:     my ($dom,$types,$servers,$settings) = @_;
14725:     my $select = &mt('Select');
14726:     my ($alltargets,$allishome,$allinsttypes,@alltypes);
14727:     if (ref($servers) eq 'HASH') {
14728:         $alltargets = join("','",sort(keys(%{$servers})));
14729:         my @homedoms;
14730:         foreach my $server (sort(keys(%{$servers}))) {
14731:             if (&Apache::lonnet::host_domain($server) eq $dom) {
14732:                 push(@homedoms,'1');
14733:             } else {
14734:                 push(@homedoms,'0');
14735:             }
14736:         }
14737:         $allishome = join("','",@homedoms);
14738:     }
14739:     if (ref($types) eq 'ARRAY') {
14740:         if (@{$types} > 0) {
14741:             @alltypes = @{$types};
14742:         }
14743:     }
14744:     push(@alltypes,'default','_LC_adv','_LC_author','_LC_internetdom','_LC_external');
14745:     $allinsttypes = join("','",@alltypes);
14746:     my (%currbalancer,%currtargets,%currrules,%existing);
14747:     if (ref($settings) eq 'HASH') {
14748:         %existing = %{$settings};
14749:     }
14750:     &get_loadbalancers_config($servers,\%existing,\%currbalancer,
14751:                               \%currtargets,\%currrules);
14752:     my $balancers = join("','",sort(keys(%currbalancer)));
14753:     return <<"END";
14754: 
14755: <script type="text/javascript">
14756: // <![CDATA[
14757: 
14758: currBalancers = new Array('$balancers');
14759: 
14760: function toggleTargets(balnum) {
14761:     var lonhostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
14762:     var prevhostitem = document.getElementById('loadbalancing_prevlonhost_'+balnum);
14763:     var balancer = lonhostitem.options[lonhostitem.selectedIndex].value;
14764:     var prevbalancer = prevhostitem.value;
14765:     var baltotal = document.getElementById('loadbalancing_total').value;
14766:     prevhostitem.value = balancer;
14767:     if (prevbalancer != '') {
14768:         var prevIdx = currBalancers.indexOf(prevbalancer);
14769:         if (prevIdx != -1) {
14770:             currBalancers.splice(prevIdx,1);
14771:         }
14772:     }
14773:     if (balancer == '') {
14774:         hideSpares(balnum);
14775:     } else {
14776:         var currIdx = currBalancers.indexOf(balancer);
14777:         if (currIdx == -1) {
14778:             currBalancers.push(balancer);
14779:         }
14780:         var homedoms = new Array('$allishome');
14781:         var ishomedom = homedoms[lonhostitem.selectedIndex];
14782:         showSpares(balancer,ishomedom,balnum);
14783:     }
14784:     balancerChange(balnum,baltotal,'change',prevbalancer,balancer);
14785:     return;
14786: }
14787: 
14788: function showSpares(balancer,ishomedom,balnum) {
14789:     var alltargets = new Array('$alltargets');
14790:     var insttypes = new Array('$allinsttypes');
14791:     var offloadtypes = new Array('primary','default');
14792: 
14793:     document.getElementById('loadbalancing_targets_'+balnum).style.display='block';
14794:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='none';
14795:  
14796:     for (var i=0; i<offloadtypes.length; i++) {
14797:         var count = 0;
14798:         for (var j=0; j<alltargets.length; j++) {
14799:             if (alltargets[j] != balancer) {
14800:                 var item = document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+count);
14801:                 item.value = alltargets[j];
14802:                 item.style.textAlign='left';
14803:                 item.style.textFace='normal';
14804:                 document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+count).innerHTML = alltargets[j];
14805:                 if (currBalancers.indexOf(alltargets[j]) == -1) {
14806:                     item.disabled = '';
14807:                 } else {
14808:                     item.disabled = 'disabled';
14809:                     item.checked = false;
14810:                 }
14811:                 count ++;
14812:             }
14813:         }
14814:     }
14815:     for (var k=0; k<insttypes.length; k++) {
14816:         if ((insttypes[k] == '_LC_external') || (insttypes[k] == '_LC_internetdom')) {
14817:             if (ishomedom == 1) {
14818:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
14819:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
14820:             } else {
14821:                 document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
14822:                 document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
14823:             }
14824:         } else {
14825:             document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='block';
14826:             document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='block';
14827:         }
14828:         if ((insttypes[k] != '_LC_external') && 
14829:             ((insttypes[k] != '_LC_internetdom') ||
14830:              ((insttypes[k] == '_LC_internetdom') && (ishomedom == 1)))) {
14831:             var item = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]);
14832:             item.options.length = 0;
14833:             item.options[0] = new Option("","",true,true);
14834:             var idx = 0;
14835:             for (var m=0; m<alltargets.length; m++) {
14836:                 if ((currBalancers.indexOf(alltargets[m]) == -1) && (alltargets[m] != balancer)) {
14837:                     idx ++;
14838:                     item.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
14839:                 }
14840:             }
14841:         }
14842:     }
14843:     return;
14844: }
14845: 
14846: function hideSpares(balnum) {
14847:     var alltargets = new Array('$alltargets');
14848:     var insttypes = new Array('$allinsttypes');
14849:     var offloadtypes = new Array('primary','default');
14850: 
14851:     document.getElementById('loadbalancing_targets_'+balnum).style.display='none';
14852:     document.getElementById('loadbalancing_disabled_'+balnum).style.display='block';
14853: 
14854:     var total = alltargets.length - 1;
14855:     for (var i=0; i<offloadtypes; i++) {
14856:         for (var j=0; j<total; j++) {
14857:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).checked = false;
14858:            document.getElementById('loadbalancing_target_'+balnum+'_'+offloadtypes[i]+'_'+j).value = '';
14859:            document.getElementById('loadbalancing_targettxt_'+balnum+'_'+offloadtypes[i]+'_'+j).innerHTML = '';
14860:         }
14861:     }
14862:     for (var k=0; k<insttypes.length; k++) {
14863:         document.getElementById('balanceruletitle_'+balnum+'_'+insttypes[k]).style.display='none';
14864:         document.getElementById('balancerule_'+balnum+'_'+insttypes[k]).style.display='none';
14865:         if (insttypes[k] != '_LC_external') {
14866:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).length = 0;
14867:             document.getElementById('loadbalancing_singleserver_'+balnum+'_'+insttypes[k]).options[0] = new Option("","",true,true);
14868:         }
14869:     }
14870:     return;
14871: }
14872: 
14873: function checkOffloads(item,balnum,type) {
14874:     var alltargets = new Array('$alltargets');
14875:     var offloadtypes = new Array('primary','default');
14876:     if (item.checked) {
14877:         var total = alltargets.length - 1;
14878:         var other;
14879:         if (type == offloadtypes[0]) {
14880:             other = offloadtypes[1];
14881:         } else {
14882:             other = offloadtypes[0];
14883:         }
14884:         for (var i=0; i<total; i++) {
14885:             var server = document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).value;
14886:             if (server == item.value) {
14887:                 if (document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked) {
14888:                     document.getElementById('loadbalancing_target_'+balnum+'_'+other+'_'+i).checked = false;
14889:                 }
14890:             }
14891:         }
14892:     }
14893:     return;
14894: }
14895: 
14896: function singleServerToggle(balnum,type) {
14897:     var offloadtoSelIdx = document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex;
14898:     if (offloadtoSelIdx == 0) {
14899:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_0').checked = true;
14900:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
14901: 
14902:     } else {
14903:         document.getElementById('loadbalancing_rules_'+balnum+'_'+type+'_2').checked = true;
14904:         document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
14905:     }
14906:     return;
14907: }
14908: 
14909: function balanceruleChange(formname,balnum,type) {
14910:     if (type == '_LC_external') {
14911:         return;
14912:     }
14913:     var typesRules = getIndicesByName(formname,'loadbalancing_rules_'+balnum+'_'+type);
14914:     for (var i=0; i<typesRules.length; i++) {
14915:         if (formname.elements[typesRules[i]].checked) {
14916:             if (formname.elements[typesRules[i]].value != 'specific') {
14917:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).selectedIndex = 0;
14918:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '';
14919:             } else {
14920:                 document.getElementById('loadbalancing_singleserver_'+balnum+'_'+type).options[0].text = '$select';
14921:             }
14922:         }
14923:     }
14924:     return;
14925: }
14926: 
14927: function balancerDeleteChange(balnum) {
14928:     var hostitem = document.getElementById('loadbalancing_lonhost_'+balnum);
14929:     var baltotal = document.getElementById('loadbalancing_total').value;
14930:     var addtarget;
14931:     var removetarget;
14932:     var action = 'delete';
14933:     if (document.getElementById('loadbalancing_delete_'+balnum)) {
14934:         var lonhost = hostitem.value;
14935:         var currIdx = currBalancers.indexOf(lonhost);
14936:         if (document.getElementById('loadbalancing_delete_'+balnum).checked) {
14937:             if (currIdx != -1) {
14938:                 currBalancers.splice(currIdx,1);
14939:             }
14940:             addtarget = lonhost;
14941:         } else {
14942:             if (currIdx == -1) {
14943:                 currBalancers.push(lonhost);
14944:             }
14945:             removetarget = lonhost;
14946:             action = 'undelete';
14947:         }
14948:         balancerChange(balnum,baltotal,action,addtarget,removetarget);
14949:     }
14950:     return;
14951: }
14952: 
14953: function balancerChange(balnum,baltotal,action,addtarget,removetarget) {
14954:     if (baltotal > 1) {
14955:         var offloadtypes = new Array('primary','default');
14956:         var alltargets = new Array('$alltargets');
14957:         var insttypes = new Array('$allinsttypes');
14958:         for (var i=0; i<baltotal; i++) {
14959:             if (i != balnum) {
14960:                 for (var j=0; j<offloadtypes.length; j++) {
14961:                     var total = alltargets.length - 1;
14962:                     for (var k=0; k<total; k++) {
14963:                         var serveritem = document.getElementById('loadbalancing_target_'+i+'_'+offloadtypes[j]+'_'+k);
14964:                         var server = serveritem.value;
14965:                         if ((action == 'delete') || (action == 'change' && addtarget != ''))  {
14966:                             if (server == addtarget) {
14967:                                 serveritem.disabled = '';
14968:                             }
14969:                         }
14970:                         if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
14971:                             if (server == removetarget) {
14972:                                 serveritem.disabled = 'disabled';
14973:                                 serveritem.checked = false;
14974:                             }
14975:                         }
14976:                     }
14977:                 }
14978:                 for (var j=0; j<insttypes.length; j++) {
14979:                     if (insttypes[j] != '_LC_external') {
14980:                         if (document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j])) {
14981:                             var singleserver = document.getElementById('loadbalancing_singleserver_'+i+'_'+insttypes[j]);
14982:                             var currSel = singleserver.selectedIndex;
14983:                             var currVal = singleserver.options[currSel].value;
14984:                             if ((action == 'delete') || (action == 'change' && addtarget != '')) {
14985:                                 var numoptions = singleserver.options.length;
14986:                                 var needsnew = 1;
14987:                                 for (var k=0; k<numoptions; k++) {
14988:                                     if (singleserver.options[k] == addtarget) {
14989:                                         needsnew = 0;
14990:                                         break;
14991:                                     }
14992:                                 }
14993:                                 if (needsnew == 1) {
14994:                                     singleserver.options[numoptions] = new Option(addtarget,addtarget,false,false);
14995:                                 }
14996:                             }
14997:                             if ((action == 'undelete') || (action == 'change' && removetarget != '')) {
14998:                                 singleserver.options.length = 0;
14999:                                 if ((currVal) && (currVal != removetarget)) {
15000:                                     singleserver.options[0] = new Option("","",false,false);
15001:                                 } else {
15002:                                     singleserver.options[0] = new Option("","",true,true);
15003:                                 }
15004:                                 var idx = 0;
15005:                                 for (var m=0; m<alltargets.length; m++) {
15006:                                     if (currBalancers.indexOf(alltargets[m]) == -1) {
15007:                                         idx ++;
15008:                                         if (currVal == alltargets[m]) {
15009:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],true,true);
15010:                                         } else {
15011:                                             singleserver.options[idx] = new Option(alltargets[m],alltargets[m],false,false);
15012:                                         }
15013:                                     }
15014:                                 }
15015:                             }
15016:                         }
15017:                     }
15018:                 }
15019:             }
15020:         }
15021:     }
15022:     return;
15023: }
15024: 
15025: // ]]>
15026: </script>
15027: 
15028: END
15029: }
15030: 
15031: sub new_spares_js {
15032:     my @sparestypes = ('primary','default');
15033:     my $types = join("','",@sparestypes);
15034:     my $select = &mt('Select');
15035:     return <<"END";
15036: 
15037: <script type="text/javascript">
15038: // <![CDATA[
15039: 
15040: function updateNewSpares(formname,lonhost) {
15041:     var types = new Array('$types');
15042:     var include = new Array();
15043:     var exclude = new Array();
15044:     for (var i=0; i<types.length; i++) {
15045:         var spareboxes = getIndicesByName(formname,'spare_'+types[i]+'_'+lonhost);
15046:         for (var j=0; j<spareboxes.length; j++) {
15047:             if (formname.elements[spareboxes[j]].checked) {
15048:                 exclude.push(formname.elements[spareboxes[j]].value);
15049:             } else {
15050:                 include.push(formname.elements[spareboxes[j]].value);
15051:             }
15052:         }
15053:     }
15054:     for (var i=0; i<types.length; i++) {
15055:         var newSpare = document.getElementById('newspare_'+types[i]+'_'+lonhost);
15056:         var selIdx = newSpare.selectedIndex;
15057:         var currnew = newSpare.options[selIdx].value;
15058:         var okSpares = new Array();
15059:         for (var j=0; j<newSpare.options.length; j++) {
15060:             var possible = newSpare.options[j].value;
15061:             if (possible != '') {
15062:                 if (exclude.indexOf(possible) == -1) {
15063:                     okSpares.push(possible);
15064:                 } else {
15065:                     if (currnew == possible) {
15066:                         selIdx = 0;
15067:                     }
15068:                 }
15069:             }
15070:         }
15071:         for (var k=0; k<include.length; k++) {
15072:             if (okSpares.indexOf(include[k]) == -1) {
15073:                 okSpares.push(include[k]);
15074:             }
15075:         }
15076:         okSpares.sort();
15077:         newSpare.options.length = 0;
15078:         if (selIdx == 0) {
15079:             newSpare.options[0] = new Option("$select","",true,true);
15080:         } else {
15081:             newSpare.options[0] = new Option("$select","",false,false);
15082:         }
15083:         for (var m=0; m<okSpares.length; m++) {
15084:             var idx = m+1;
15085:             var selThis = 0;
15086:             if (selIdx != 0) {
15087:                 if (okSpares[m] == currnew) {
15088:                     selThis = 1;
15089:                 }
15090:             }
15091:             if (selThis == 1) {
15092:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],true,true);
15093:             } else {
15094:                 newSpare.options[idx] = new Option(okSpares[m],okSpares[m],false,false);
15095:             }
15096:         }
15097:     }
15098:     return;
15099: }
15100: 
15101: function checkNewSpares(lonhost,type) {
15102:     var newSpare = document.getElementById('newspare_'+type+'_'+lonhost);
15103:     var chosen =  newSpare.options[newSpare.selectedIndex].value;
15104:     if (chosen != '') { 
15105:         var othertype;
15106:         var othernewSpare;
15107:         if (type == 'primary') {
15108:             othernewSpare = document.getElementById('newspare_default_'+lonhost);
15109:         }
15110:         if (type == 'default') {
15111:             othernewSpare = document.getElementById('newspare_primary_'+lonhost);
15112:         }
15113:         if (othernewSpare.options[othernewSpare.selectedIndex].value == chosen) {
15114:             othernewSpare.selectedIndex = 0;
15115:         }
15116:     }
15117:     return;
15118: }
15119: 
15120: // ]]>
15121: </script>
15122: 
15123: END
15124: 
15125: }
15126: 
15127: sub common_domprefs_js {
15128:     return <<"END";
15129: 
15130: <script type="text/javascript">
15131: // <![CDATA[
15132: 
15133: function getIndicesByName(formname,item) {
15134:     var group = new Array();
15135:     for (var i=0;i<formname.elements.length;i++) {
15136:         if (formname.elements[i].name == item) {
15137:             group.push(formname.elements[i].id);
15138:         }
15139:     }
15140:     return group;
15141: }
15142: 
15143: // ]]>
15144: </script>
15145: 
15146: END
15147: 
15148: }
15149: 
15150: sub recaptcha_js {
15151:     my %lt = &captcha_phrases();
15152:     return <<"END";
15153: 
15154: <script type="text/javascript">
15155: // <![CDATA[
15156: 
15157: function updateCaptcha(caller,context) {
15158:     var privitem;
15159:     var pubitem;
15160:     var privtext;
15161:     var pubtext;
15162:     var versionitem;
15163:     var versiontext;
15164:     if (document.getElementById(context+'_recaptchapub')) {
15165:         pubitem = document.getElementById(context+'_recaptchapub');
15166:     } else {
15167:         return;
15168:     }
15169:     if (document.getElementById(context+'_recaptchapriv')) {
15170:         privitem = document.getElementById(context+'_recaptchapriv');
15171:     } else {
15172:         return;
15173:     }
15174:     if (document.getElementById(context+'_recaptchapubtxt')) {
15175:         pubtext = document.getElementById(context+'_recaptchapubtxt');
15176:     } else {
15177:         return;
15178:     }
15179:     if (document.getElementById(context+'_recaptchaprivtxt')) {
15180:         privtext = document.getElementById(context+'_recaptchaprivtxt');
15181:     } else {
15182:         return;
15183:     }
15184:     if (document.getElementById(context+'_recaptchaversion')) {
15185:         versionitem = document.getElementById(context+'_recaptchaversion');
15186:     } else {
15187:         return;
15188:     }
15189:     if (document.getElementById(context+'_recaptchavertxt')) {
15190:         versiontext = document.getElementById(context+'_recaptchavertxt');
15191:     } else {
15192:         return;
15193:     }
15194:     if (caller.checked) {
15195:         if (caller.value == 'recaptcha') {
15196:             pubitem.type = 'text';
15197:             privitem.type = 'text';
15198:             pubitem.size = '40';
15199:             privitem.size = '40';
15200:             pubtext.innerHTML = "$lt{'pub'}";
15201:             privtext.innerHTML = "$lt{'priv'}";
15202:             versionitem.type = 'text';
15203:             versionitem.size = '3';
15204:             versiontext.innerHTML = "$lt{'ver'}";
15205:         } else {
15206:             pubitem.type = 'hidden';
15207:             privitem.type = 'hidden';
15208:             versionitem.type = 'hidden';
15209:             pubtext.innerHTML = '';
15210:             privtext.innerHTML = '';
15211:             versiontext.innerHTML = '';
15212:         }
15213:     }
15214:     return;
15215: }
15216: 
15217: // ]]>
15218: </script>
15219: 
15220: END
15221: 
15222: }
15223: 
15224: sub toggle_display_js {
15225:     return <<"END";
15226: 
15227: <script type="text/javascript">
15228: // <![CDATA[
15229: 
15230: function toggleDisplay(domForm,caller) {
15231:     if (document.getElementById(caller)) {
15232:         var divitem = document.getElementById(caller);
15233:         var optionsElement = domForm.coursecredits;
15234:         var checkval = 1;
15235:         var dispval = 'block';
15236:         if (caller == 'emailoptions') {
15237:             optionsElement = domForm.cancreate_email; 
15238:         }
15239:         if (caller == 'studentsubmission') {
15240:             optionsElement = domForm.postsubmit;
15241:         }
15242:         if (caller == 'cloneinstcode') {
15243:             optionsElement = domForm.canclone;
15244:             checkval = 'instcode';
15245:         }
15246:         if (optionsElement.length) {
15247:             var currval;
15248:             for (var i=0; i<optionsElement.length; i++) {
15249:                 if (optionsElement[i].checked) {
15250:                    currval = optionsElement[i].value;
15251:                 }
15252:             }
15253:             if (currval == checkval) {
15254:                 divitem.style.display = dispval;
15255:             } else {
15256:                 divitem.style.display = 'none';
15257:             }
15258:         }
15259:     }
15260:     return;
15261: }
15262: 
15263: // ]]>
15264: </script>
15265: 
15266: END
15267: 
15268: }
15269: 
15270: sub captcha_phrases {
15271:     return &Apache::lonlocal::texthash (
15272:                  priv => 'Private key',
15273:                  pub  => 'Public key',
15274:                  original  => 'original (CAPTCHA)',
15275:                  recaptcha => 'successor (ReCAPTCHA)',
15276:                  notused   => 'unused',
15277:                  ver => 'ReCAPTCHA version (1 or 2)',
15278:     );
15279: }
15280: 
15281: sub devalidate_remote_domconfs {
15282:     my ($dom,$cachekeys) = @_;
15283:     return unless (ref($cachekeys) eq 'HASH');
15284:     my %servers = &Apache::lonnet::internet_dom_servers($dom);
15285:     my %thismachine;
15286:     map { $thismachine{$_} = 1; } &Apache::lonnet::current_machine_ids();
15287:     my @posscached = ('domainconfig','domdefaults','ltitools','usersessions','directorysrch');
15288:     if (keys(%servers)) {
15289:         foreach my $server (keys(%servers)) {
15290:             next if ($thismachine{$server});
15291:             my @cached;
15292:             foreach my $name (@posscached) {
15293:                 if ($cachekeys->{$name}) {
15294:                     push(@cached,&escape($name).':'.&escape($dom));
15295:                 }
15296:             }
15297:             if (@cached) {
15298:                 &Apache::lonnet::remote_devalidate_cache($server,\@cached);
15299:             }
15300:         }
15301:     }
15302:     return;
15303: }
15304: 
15305: 1;

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